generated from cuioss/cui-java-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
178 additions
and
16 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
59 changes: 59 additions & 0 deletions
59
...uthentication-token/src/main/java/de/cuioss/portal/authentication/token/TokenFactory.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,59 @@ | ||
package de.cuioss.portal.authentication.token; | ||
|
||
import io.smallrye.jwt.auth.principal.JWTParser; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* Factory for creating different types of tokens. | ||
*/ | ||
@ApplicationScoped | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE, onConstructor_ = @Inject) | ||
public class TokenFactory { | ||
|
||
private final JWTParser tokenParser; | ||
|
||
/** | ||
* Creates a new token factory using the given parser. | ||
* | ||
* @param tokenParser The parser to use for token validation, must not be null | ||
* @return A new TokenFactory instance | ||
*/ | ||
public static TokenFactory of(@NonNull JWTParser tokenParser) { | ||
return new TokenFactory(tokenParser); | ||
} | ||
|
||
/** | ||
* Creates an access token from the given token string. | ||
* | ||
* @param tokenString The token string to parse, must not be null | ||
* @return The parsed access token | ||
*/ | ||
public ParsedAccessToken createAccessToken(@NonNull String tokenString) { | ||
return ParsedAccessToken.fromTokenString(tokenString, tokenParser); | ||
} | ||
|
||
/** | ||
* Creates an ID token from the given token string. | ||
* | ||
* @param tokenString The token string to parse, must not be null | ||
* @return The parsed ID token | ||
*/ | ||
public ParsedIdToken createIdToken(@NonNull String tokenString) { | ||
return ParsedIdToken.fromTokenString(tokenString, tokenParser); | ||
} | ||
|
||
/** | ||
* Creates a refresh token from the given token string. | ||
* | ||
* @param tokenString The token string to parse, must not be null | ||
* @return The parsed refresh token | ||
*/ | ||
public ParsedRefreshToken createRefreshToken(@NonNull String tokenString) { | ||
return ParsedRefreshToken.fromTokenString(tokenString); | ||
} | ||
|
||
} |
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
78 changes: 78 additions & 0 deletions
78
...ntication-token/src/test/java/de/cuioss/portal/authentication/token/TokenFactoryTest.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,78 @@ | ||
package de.cuioss.portal.authentication.token; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class TokenFactoryTest { | ||
|
||
private TokenFactory tokenFactory; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
tokenFactory = TokenFactory.of(TestTokenProducer.DEFAULT_TOKEN_PARSER); | ||
} | ||
|
||
@Test | ||
void shouldCreateAccessToken() { | ||
var token = TestTokenProducer.validSignedJWTWithClaims(TestTokenProducer.SOME_SCOPES); | ||
var parsedToken = tokenFactory.createAccessToken(token); | ||
|
||
assertNotNull(parsedToken); | ||
assertFalse(parsedToken.getScopes().isEmpty()); | ||
assertNotNull(parsedToken.getSubject()); | ||
assertNotNull(parsedToken.getIssuer()); | ||
} | ||
|
||
@Test | ||
void shouldCreateIdToken() { | ||
var token = TestTokenProducer.validSignedJWTWithClaims(TestTokenProducer.SOME_ID_TOKEN); | ||
var parsedToken = tokenFactory.createIdToken(token); | ||
|
||
assertNotNull(parsedToken); | ||
assertNotNull(parsedToken.getSubject()); | ||
assertNotNull(parsedToken.getIssuer()); | ||
} | ||
|
||
@Test | ||
void shouldCreateRefreshToken() { | ||
var token = TestTokenProducer.validSignedJWTWithClaims(TestTokenProducer.REFRESH_TOKEN); | ||
var parsedToken = tokenFactory.createRefreshToken(token); | ||
|
||
assertNotNull(parsedToken); | ||
} | ||
|
||
@Test | ||
void shouldHandleExpiredToken() { | ||
var expiredToken = TestTokenProducer.validSignedJWTExpireAt( | ||
Instant.now().minus(1, ChronoUnit.HOURS)); | ||
var token = tokenFactory.createAccessToken(expiredToken); | ||
|
||
assertNotNull(token); | ||
assertTrue(token.isEmpty()); | ||
} | ||
|
||
@Test | ||
void shouldHandleInvalidIssuer() { | ||
var wrongIssuerTokenFactory = TokenFactory.of(TestTokenProducer.WRONG_ISSUER_TOKEN_PARSER); | ||
var token = TestTokenProducer.validSignedJWTWithClaims(TestTokenProducer.SOME_SCOPES); | ||
var parsedToken = wrongIssuerTokenFactory.createAccessToken(token); | ||
assertNotNull(parsedToken); | ||
assertTrue(parsedToken.isEmpty()); | ||
} | ||
|
||
@Test | ||
void shouldHandleInvalidSignature() { | ||
var wrongSignatureTokenFactory = TokenFactory.of(TestTokenProducer.WRONG_SIGNATURE_TOKEN_PARSER); | ||
var token = TestTokenProducer.validSignedJWTWithClaims(TestTokenProducer.SOME_SCOPES); | ||
var parsedToken = wrongSignatureTokenFactory.createAccessToken(token); | ||
assertNotNull(parsedToken); | ||
assertTrue(parsedToken.isEmpty()); | ||
} | ||
} |