-
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.
P4ADEV-24P4ADEV-248-creation-api-get-token-selfcare tests
- Loading branch information
1 parent
3173ff3
commit bf89164
Showing
4 changed files
with
160 additions
and
67 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
65 changes: 65 additions & 0 deletions
65
src/test/java/it/gov/pagopa/payhub/auth/utils/JWTValidatorTest.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,65 @@ | ||
package it.gov.pagopa.payhub.auth.utils; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import it.gov.pagopa.payhub.auth.exception.InvalidTokenException; | ||
import it.gov.pagopa.payhub.auth.exception.TokenExpiredException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
class JWTValidatorTest { | ||
|
||
private JWTValidator jwtValidator; | ||
private WireMockServer wireMockServer; | ||
private JWTValidatorUtils utils; | ||
|
||
@BeforeEach | ||
void setup(){ | ||
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); | ||
wireMockServer.start(); | ||
utils = new JWTValidatorUtils(wireMockServer); | ||
jwtValidator = new JWTValidator(); | ||
} | ||
|
||
@AfterEach | ||
void clean(){ | ||
wireMockServer.stop(); | ||
} | ||
|
||
@Test | ||
void validateToken() throws Exception { | ||
String token = utils.generateJWK(new Date(System.currentTimeMillis() + 3600000)); | ||
|
||
String urlJwkProvider = utils.getUrlJwkProvider(); | ||
|
||
Map<String, String> claimsMap = jwtValidator.validate(token, urlJwkProvider); | ||
|
||
assertNotNull(claimsMap); | ||
} | ||
|
||
@Test | ||
void validate_ExpiredToken_ThrowsTokenExpiredException() throws Exception { | ||
String expiredToken = utils.generateJWK(new Date(System.currentTimeMillis() - 3600000)); | ||
String urlJwkProvider = utils.getUrlJwkProvider(); | ||
|
||
assertThrows(TokenExpiredException.class, () -> jwtValidator.validate(expiredToken, urlJwkProvider)); | ||
} | ||
|
||
@Test | ||
void validate_InvalidToken_ThrowsInvalidTokenException() { | ||
String invalidToken = "your_invalid_token_here"; | ||
String urlJwkProvider = "your_jwk_provider_url_here"; | ||
|
||
assertThrows(InvalidTokenException.class, () -> jwtValidator.validate(invalidToken, urlJwkProvider)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/it/gov/pagopa/payhub/auth/utils/JWTValidatorUtils.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,72 @@ | ||
package it.gov.pagopa.payhub.auth.utils; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.nimbusds.jose.jwk.JWK; | ||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.nimbusds.jose.jwk.RSAKey; | ||
import org.json.JSONObject; | ||
|
||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.interfaces.RSAPrivateKey; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.util.Date; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
|
||
public class JWTValidatorUtils { | ||
|
||
private final WireMockServer wireMockServer; | ||
|
||
private static final String AUD = "AUD"; | ||
private static final String ISS = "ISS"; | ||
|
||
public JWTValidatorUtils(WireMockServer wireMockServer) { | ||
this.wireMockServer = wireMockServer; | ||
} | ||
|
||
public String generateJWK(Date expiresAt) throws Exception { | ||
KeyPair keyPair = generateKeyPair(); | ||
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); | ||
|
||
String token = generateToken(keyPair, expiresAt); | ||
|
||
JWK jwk = new RSAKey.Builder(rsaPublicKey) | ||
.keyID("my-key-id") | ||
.build(); | ||
JWKSet jwkSet = new JWKSet(jwk); | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("keys", jwkSet.toJSONObject().get("keys")); | ||
|
||
WireMock.configureFor("localhost", wireMockServer.port()); | ||
stubFor(get(urlEqualTo("/jwks/.well-known/jwks.json")) | ||
.willReturn(aResponse() | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(String.valueOf(jwkSet)))); | ||
|
||
return token; | ||
} | ||
|
||
public static String generateToken(KeyPair keyPair, Date expiresAt) { | ||
return JWT.create() | ||
.withIssuer(ISS) | ||
.withAudience(AUD) | ||
.withKeyId("my-key-id") | ||
.withJWTId("my-jwt-id") | ||
.withExpiresAt(expiresAt) | ||
.sign(Algorithm.RSA256((RSAPrivateKey) keyPair.getPrivate())); | ||
} | ||
|
||
public String getUrlJwkProvider() { | ||
return "http://localhost:" + wireMockServer.port() + "/jwks"; | ||
} | ||
|
||
private static KeyPair generateKeyPair() throws Exception { | ||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); | ||
keyPairGenerator.initialize(2048); | ||
return keyPairGenerator.generateKeyPair(); | ||
} | ||
} |