diff --git a/.idea/compiler.xml b/.idea/compiler.xml index a0c6be67..c2c4090a 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,7 +2,9 @@ - + + + diff --git a/services/users/pom.xml b/services/users/pom.xml index ff215d4a..59fb0d78 100644 --- a/services/users/pom.xml +++ b/services/users/pom.xml @@ -103,6 +103,32 @@ 1.19.7 test + + + org.mindrot + jbcrypt + 0.4 + + + + io.jsonwebtoken + jjwt-api + 0.11.2 + + + + io.jsonwebtoken + jjwt-impl + 0.11.2 + runtime + + + + io.jsonwebtoken + jjwt-jackson + 0.11.2 + runtime + diff --git a/services/users/src/main/java/com/workup/users/commands/FreelancerRegister.java b/services/users/src/main/java/com/workup/users/commands/FreelancerRegister.java new file mode 100644 index 00000000..fa2450a3 --- /dev/null +++ b/services/users/src/main/java/com/workup/users/commands/FreelancerRegister.java @@ -0,0 +1,36 @@ +package com.workup.users.commands; + +import static com.workup.users.commands.utils.AuthTokenHandler.generateToken; +import static com.workup.users.commands.utils.PasswordHasher.hashPassword; + +import com.workup.shared.commands.users.requests.FreelancerRegisterRequest; +import com.workup.shared.commands.users.responses.SignUpAndInResponse; +import com.workup.shared.enums.HttpStatusCode; +import com.workup.users.db.Freelancer; +import java.util.Objects; + +public class FreelancerRegister + extends UserCommand { + + @Override + public SignUpAndInResponse Run(FreelancerRegisterRequest request) { + if (Objects.isNull(request.getEmail()) + || Objects.isNull(request.getPassword()) + || Objects.isNull(request.getFullName())) { + return SignUpAndInResponse.builder().withStatusCode(HttpStatusCode.BAD_REQUEST).build(); + } + Freelancer freelancer = + Freelancer.builder() + .withEmail(request.getEmail()) + .withPassword_hash(hashPassword(request.getPassword())) + .withFull_name(request.getFullName()) + .withJob_title(request.getJobTitle()) + .withCity(request.getCity()) + .build(); + + return SignUpAndInResponse.builder() + .withAuthToken(generateToken(freelancer.getEmail())) + .withStatusCode(HttpStatusCode.OK) + .build(); + } +} diff --git a/services/users/src/main/java/com/workup/users/commands/utils/AuthTokenHandler.java b/services/users/src/main/java/com/workup/users/commands/utils/AuthTokenHandler.java new file mode 100644 index 00000000..475546ea --- /dev/null +++ b/services/users/src/main/java/com/workup/users/commands/utils/AuthTokenHandler.java @@ -0,0 +1,27 @@ +package com.workup.users.commands.utils; + +import io.jsonwebtoken.*; +import java.util.Date; + +public class AuthTokenHandler { + private static final String SECRET_KEY = System.getenv("SECRET_KEY"); + private static final JwtParser parser = Jwts.parserBuilder().setSigningKey(SECRET_KEY).build(); + + public static String generateToken(String email) { + Date now = new Date(); + Date expiryDate = new Date(now.getTime() + 3600000); // Token expires in 1 hour + + return Jwts.builder() + .setSubject(email) + .setIssuedAt(now) + .setExpiration(expiryDate) + .signWith(SignatureAlgorithm.HS512, SECRET_KEY) + .compact(); + } + + public static String decodeToken(String token) { + Jws claimsJws = parser.parseClaimsJws(token); + Claims body = claimsJws.getBody(); + return body.getSubject(); + } +} diff --git a/services/users/src/main/java/com/workup/users/commands/utils/PasswordHasher.java b/services/users/src/main/java/com/workup/users/commands/utils/PasswordHasher.java new file mode 100644 index 00000000..87bebe26 --- /dev/null +++ b/services/users/src/main/java/com/workup/users/commands/utils/PasswordHasher.java @@ -0,0 +1,13 @@ +package com.workup.users.commands.utils; + +import org.mindrot.jbcrypt.BCrypt; + +public class PasswordHasher { + public static String hashPassword(String password) { + return BCrypt.hashpw(password, BCrypt.gensalt()); + } + + public static boolean checkPassword(String inputPassword, String hashedPassword) { + return BCrypt.checkpw(inputPassword, hashedPassword); + } +} diff --git a/services/users/src/main/resources/application.properties b/services/users/src/main/resources/application.properties index cbbffada..0a6f3603 100644 --- a/services/users/src/main/resources/application.properties +++ b/services/users/src/main/resources/application.properties @@ -8,3 +8,4 @@ spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest +SECRET_KEY=j4#BbFGfoc^2k*Bz