Skip to content

Commit

Permalink
Created AuthTokenHandler and PasswordHasher, and used both of them to…
Browse files Browse the repository at this point in the history
… create FreelancerRegister command.

Signed-off-by: Akram-Fahim <akramadel2001@gmail.com>
  • Loading branch information
Akram-Fahim committed May 9, 2024
1 parent 97e6a6a commit 70183aa
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions services/users/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@
<version>1.19.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FreelancerRegisterRequest, SignUpAndInResponse> {

@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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Claims> claimsJws = parser.parseClaimsJws(token);
Claims body = claimsJws.getBody();
return body.getSubject();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions services/users/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 70183aa

Please sign in to comment.