-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created AuthTokenHandler and PasswordHasher, and used both of them to…
… create FreelancerRegister command. Signed-off-by: Akram-Fahim <akramadel2001@gmail.com>
- Loading branch information
1 parent
97e6a6a
commit 70183aa
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
services/users/src/main/java/com/workup/users/commands/FreelancerRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
services/users/src/main/java/com/workup/users/commands/utils/AuthTokenHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
services/users/src/main/java/com/workup/users/commands/utils/PasswordHasher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters