forked from vilelaricardo/mandacarubroker
-
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.
- Loading branch information
1 parent
08cd60e
commit 90909c2
Showing
13 changed files
with
234 additions
and
34 deletions.
There are no files selected for viewing
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
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
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
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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mandacarubroker/domain/auth/ResponseAuthUserDTO.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,8 @@ | ||
package com.mandacarubroker.domain.auth; | ||
|
||
public record ResponseAuthUserDTO( | ||
String token, | ||
int expiresIn, | ||
String tokenType | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mandacarubroker/security/MissingSecuritySecretException.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,7 @@ | ||
package com.mandacarubroker.security; | ||
|
||
public class MissingSecuritySecretException extends RuntimeException { | ||
public MissingSecuritySecretException(final String message) { | ||
super("Missing security secret: " + message); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/mandacarubroker/security/SecuritySecrets.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,16 @@ | ||
package com.mandacarubroker.security; | ||
|
||
public final class SecuritySecrets { | ||
private SecuritySecrets() { | ||
} | ||
|
||
public static String getJWTSecret() { | ||
final String secret = System.getenv("MANDACARU_JWT_SECRET"); | ||
|
||
if (secret == null) { | ||
throw new MissingSecuritySecretException("JWT secret not found"); | ||
} | ||
|
||
return secret; | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/com/mandacarubroker/service/TokenService.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,63 @@ | ||
package com.mandacarubroker.service; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.JWTCreationException; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.mandacarubroker.domain.auth.ResponseAuthUserDTO; | ||
import com.mandacarubroker.security.SecuritySecrets; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
|
||
@Service | ||
public class TokenService { | ||
private static final String TOKEN_ISSUER = "mandacaru-broker"; | ||
private static final int EXPIRATION_TIME = 864 * 1000 * 1000; | ||
private static final int EXPIRATION_TIME_IN_SECONDS = EXPIRATION_TIME / 1000; | ||
private static final String TOKEN_TYPE = "Bearer"; | ||
private final Algorithm algorithm; | ||
|
||
public TokenService() { | ||
String secret = SecuritySecrets.getJWTSecret(); | ||
algorithm = Algorithm.HMAC512(secret.getBytes()); | ||
} | ||
|
||
public ResponseAuthUserDTO encodeToken(final String subject) { | ||
try { | ||
return tryToEncodeToken(subject); | ||
} catch (JWTCreationException exception) { | ||
return null; | ||
} | ||
} | ||
|
||
private ResponseAuthUserDTO tryToEncodeToken(final String subject) { | ||
Date expirationDate = new Date(System.currentTimeMillis() + EXPIRATION_TIME); | ||
|
||
String generatedToken = JWT.create() | ||
.withSubject(subject) | ||
.withIssuer(TOKEN_ISSUER) | ||
.withExpiresAt(expirationDate) | ||
.sign(algorithm); | ||
|
||
String tokenWithPrefix = TOKEN_TYPE + " " + generatedToken; | ||
return new ResponseAuthUserDTO(tokenWithPrefix, EXPIRATION_TIME_IN_SECONDS, TOKEN_TYPE); | ||
} | ||
|
||
public String getTokenSubject(final String token) { | ||
DecodedJWT decodedToken = decodeUserToken(token); | ||
return decodedToken.getSubject(); | ||
} | ||
|
||
public DecodedJWT decodeUserToken(final String token) { | ||
try { | ||
return tryToDecodeUserToken(token); | ||
} catch (Exception exception) { | ||
return null; | ||
} | ||
} | ||
|
||
private DecodedJWT tryToDecodeUserToken(final String token) { | ||
return JWT.require(algorithm).build().verify(token); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package com.mandacarubroker.controller; | ||
|
||
import com.mandacarubroker.domain.auth.RequestAuthUserDTO; | ||
import com.mandacarubroker.domain.auth.ResponseAuthUserDTO; | ||
import com.mandacarubroker.domain.user.RequestUserDTO; | ||
import com.mandacarubroker.domain.user.User; | ||
import com.mandacarubroker.security.SecuritySecretsMock; | ||
import com.mandacarubroker.service.AuthService; | ||
import com.mandacarubroker.service.PasswordHashingService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -22,6 +23,8 @@ class AuthControllerTest { | |
private AuthService authService; | ||
private AuthController authController; | ||
|
||
private static final String TOKEN_TYPE = "Bearer"; | ||
|
||
private final PasswordHashingService passwordHashingService = new PasswordHashingService(); | ||
|
||
private final String validEmail = "[email protected]"; | ||
|
@@ -50,21 +53,31 @@ class AuthControllerTest { | |
validPassword | ||
); | ||
|
||
private final ResponseAuthUserDTO validResponseAuthUserDTO = new ResponseAuthUserDTO( | ||
"Bearer token", | ||
86400, | ||
"Bearer" | ||
); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
SecuritySecretsMock.mockStatic(); | ||
|
||
authService = Mockito.mock(AuthService.class); | ||
User validUser = new User(validRequestUserDTO); | ||
Optional<User> optionalValidUser = Optional.of(validUser); | ||
Mockito.when(authService.login(validRequestAuthUserDTO)).thenReturn(optionalValidUser); | ||
Mockito.when(authService.login(validRequestAuthUserDTO)).thenReturn(Optional.of(validResponseAuthUserDTO)); | ||
Mockito.when(authService.login(new RequestAuthUserDTO(invalidUsername, validPassword))).thenReturn(Optional.empty()); | ||
Mockito.when(authService.login(new RequestAuthUserDTO(validUsername, invalidPassword))).thenReturn(Optional.empty()); | ||
authController = new AuthController(authService); | ||
} | ||
|
||
@Test | ||
void itShouldBeAbleToLoginWithValidUser() { | ||
ResponseEntity<String> response = authController.login(validRequestAuthUserDTO); | ||
assertEquals(ResponseEntity.ok("User logged in successfully"), response); | ||
ResponseEntity<ResponseAuthUserDTO> response = authController.login(validRequestAuthUserDTO); | ||
ResponseAuthUserDTO responseAuthUserDTO = response.getBody(); | ||
|
||
assertEquals(ResponseEntity.ok().build().getStatusCode(), response.getStatusCode()); | ||
assertEquals(ResponseAuthUserDTO.class, responseAuthUserDTO.getClass()); | ||
assertEquals(TOKEN_TYPE, responseAuthUserDTO.tokenType()); | ||
} | ||
|
||
@Test | ||
|
@@ -74,7 +87,7 @@ void itShouldNotBeAbleToLoginWithInvalidUser() { | |
validPassword | ||
); | ||
|
||
ResponseEntity<String> response = authController.login(invalidRequestAuthUserDTO); | ||
ResponseEntity<ResponseAuthUserDTO> response = authController.login(invalidRequestAuthUserDTO); | ||
assertEquals(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(), response); | ||
} | ||
|
||
|
@@ -85,7 +98,7 @@ void itShouldNotBeAbleToLoginWithInvalidPassword() { | |
invalidPassword | ||
); | ||
|
||
ResponseEntity<String> response = authController.login(invalidRequestAuthUserDTO); | ||
ResponseEntity<ResponseAuthUserDTO> response = authController.login(invalidRequestAuthUserDTO); | ||
assertEquals(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(), response); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/mandacarubroker/security/SecuritySecretsMock.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,20 @@ | ||
package com.mandacarubroker.security; | ||
|
||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
public final class SecuritySecretsMock { | ||
private static MockedStatic<SecuritySecrets> securitySecretsMockedStatic = null; | ||
|
||
private SecuritySecretsMock() { | ||
} | ||
|
||
public static void mockStatic() { | ||
if (securitySecretsMockedStatic != null) { | ||
return; | ||
} | ||
|
||
securitySecretsMockedStatic = Mockito.mockStatic(SecuritySecrets.class); | ||
securitySecretsMockedStatic.when(SecuritySecrets::getJWTSecret).thenReturn("secret"); | ||
} | ||
} |
Oops, something went wrong.