-
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.
remove playground controller + add jwt test utils
- Loading branch information
1 parent
c7a2ee9
commit 658f8e3
Showing
5 changed files
with
73 additions
and
37 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
37 changes: 0 additions & 37 deletions
37
src/main/java/it/gov/pagopa/rtp/activator/PlaygroundController.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/test/java/it/gov/pagopa/rtp/activator/JwtTestUtils.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,50 @@ | ||
package it.gov.pagopa.rtp.activator; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jose.JWSHeader; | ||
import com.nimbusds.jose.JWSSigner; | ||
import com.nimbusds.jose.crypto.MACSigner; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
import java.util.Date; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public final class JwtTestUtils { | ||
|
||
public static String generateToken(String subject, String... roles) throws JOSEException { | ||
return generateToken(subject, new Date(new Date().getTime() + 60 * 60 * 1000), roles); // 1 hour | ||
} | ||
|
||
public static String generateExpiredToken(String subject, String... roles) throws JOSEException { | ||
return generateToken(subject, new Date(new Date().getTime() - 60 * 60 * 1000), roles); // 1 hour ago | ||
} | ||
|
||
private static String generateToken(String subject, Date expirationTime, String... roles) throws JOSEException { | ||
// Create HMAC signer | ||
JWSSigner signer = new MACSigner( | ||
IntStream.range(0, 256).mapToObj(Integer::toString).collect(Collectors.joining()) | ||
); | ||
|
||
// Prepare JWT with claims set | ||
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() | ||
.subject(subject) | ||
.claim("groups", roles) | ||
.issuer("pagopa.it") | ||
.expirationTime(expirationTime) // 1 hour expiration | ||
.build(); | ||
|
||
SignedJWT signedJWT = new SignedJWT( | ||
new JWSHeader(JWSAlgorithm.HS256), | ||
claimsSet); | ||
|
||
// Apply the HMAC signature | ||
signedJWT.sign(signer); | ||
|
||
// Serialize to compact form | ||
return signedJWT.serialize(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/it/gov/pagopa/rtp/activator/configuration/NoSignatureJwtDecoderTest.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,21 @@ | ||
package it.gov.pagopa.rtp.activator.configuration; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import it.gov.pagopa.rtp.activator.JwtTestUtils; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class NoSignatureJwtDecoderTest { | ||
|
||
|
||
@Test | ||
void givenSignedTokenMustDecodeWithoutVerifySignature() throws JOSEException { | ||
final var decoder = new NoSignatureJwtDecoder(); | ||
final var token = JwtTestUtils.generateToken("me", "none"); | ||
assertThat(decoder.decode(token), Matchers.notNullValue()); | ||
} | ||
|
||
} | ||
|
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 @@ | ||
logging.level.org.springframework.security=DEBUG |