-
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
dd63e04
commit 0542c77
Showing
5 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/it/pagopa/swclient/mil/auth/util/SignedJWTDeserializer.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 @@ | ||
/* | ||
* SignedJWTDeserializer.java | ||
* | ||
* 8 jan 2025 | ||
*/ | ||
package it.pagopa.swclient.mil.auth.util; | ||
|
||
import java.io.IOException; | ||
import java.text.ParseException; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
import io.quarkus.logging.Log; | ||
import it.pagopa.swclient.mil.auth.AuthErrorCode; | ||
import it.pagopa.swclient.mil.bean.Errors; | ||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.Response.Status; | ||
|
||
/** | ||
* <p> | ||
* Deserializes strings in signed JWT. | ||
* </p> | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
public class SignedJWTDeserializer extends JsonDeserializer<SignedJWT> { | ||
/** | ||
* <p> | ||
* Default constructor. | ||
* </p> | ||
*/ | ||
public SignedJWTDeserializer() { | ||
super(); | ||
} | ||
|
||
/** | ||
* | ||
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(JsonParser, | ||
* DeserializationContext) JsonDeserializer#deserialize(JsonParser, DeserializationContext) | ||
*/ | ||
@Override | ||
public SignedJWT deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
Log.trace("deserialize"); | ||
try { | ||
return SignedJWT.parse(p.getText()); | ||
} catch (ParseException e) { | ||
String message = String.format("[%s] Error parsing token", AuthErrorCode.ERROR_PARSING_TOKEN); | ||
Log.errorf(e, message); | ||
Response error = Response.status(Status.BAD_REQUEST) | ||
.entity(new Errors(AuthErrorCode.ERROR_PARSING_TOKEN, message)) | ||
.build(); | ||
throw new BadRequestException(error); | ||
} | ||
} | ||
} |
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: 37 additions & 0 deletions
37
src/test/java/it/pagopa/swclient/mil/auth/util/Sample.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,37 @@ | ||
/* | ||
* Sample.java | ||
* | ||
* 8 jan 2025 | ||
*/ | ||
package it.pagopa.swclient.mil.auth.util; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.Accessors; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
@Getter | ||
@Setter | ||
@Accessors(chain = true) | ||
public class Sample { | ||
/** | ||
* JSON keys. | ||
*/ | ||
public static final String JWT_JK = "jwt"; | ||
|
||
/** | ||
* | ||
*/ | ||
@JsonProperty(JWT_JK) | ||
@JsonSerialize(using = SignedJWTSerializer.class) | ||
@JsonDeserialize(using = SignedJWTDeserializer.class) | ||
private SignedJWT jwt; | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/it/pagopa/swclient/mil/auth/util/SignedJWTDeserializerTest.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,69 @@ | ||
/* | ||
* SignedJWTDeserializerTest.java | ||
* | ||
* 8 gen 2025 | ||
*/ | ||
package it.pagopa.swclient.mil.auth.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.text.ParseException; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
@QuarkusTest | ||
class SignedJWTDeserializerTest { | ||
/** | ||
* | ||
* @param testInfo | ||
*/ | ||
@BeforeEach | ||
void init(TestInfo testInfo) { | ||
String frame = "*".repeat(testInfo.getDisplayName().length() + 11); | ||
System.out.println(frame); | ||
System.out.printf("* %s: START *%n", testInfo.getDisplayName()); | ||
System.out.println(frame); | ||
} | ||
|
||
/** | ||
* Test method for | ||
* {@link it.pagopa.swclient.mil.auth.util.SignedJWTDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)}. | ||
* | ||
* @throws ParseException | ||
* @throws JsonProcessingException | ||
*/ | ||
@Test | ||
void testDeserializeJsonParserDeserializationContext() throws ParseException, JsonProcessingException { | ||
SignedJWT expected = SignedJWT.parse("eyJraWQiOiJrZXlfbmFtZS9rZXlfdmVyc2lvbiIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjbGllbnRfaWQiLCJjbGllbnRJZCI6ImNsaWVudF9pZCIsImNoYW5uZWwiOiJjaGFubmVsIiwiaXNzIjoiaHR0cHM6Ly9taWwtYXV0aCIsImdyb3VwcyI6InJvbGUiLCJ0ZXJtaW5hbElkIjoidGVybWluYWxfaWQiLCJhdWQiOiJodHRwczovL21pbCIsIm1lcmNoYW50SWQiOiJtZXJjaGFudF9pZCIsInNjb3BlIjoic2NvcGUiLCJmaXNjYWxDb2RlIjoiZW5jX2Zpc2NhbF9jb2RlIiwiZXhwIjoxNzE3NjUyLCJhY3F1aXJlcklkIjoiYWNxdWlyZXJfaWQiLCJpYXQiOjE3MTc1OTJ9.AA"); | ||
|
||
String json = "{\"jwt\":\"eyJraWQiOiJrZXlfbmFtZS9rZXlfdmVyc2lvbiIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjbGllbnRfaWQiLCJjbGllbnRJZCI6ImNsaWVudF9pZCIsImNoYW5uZWwiOiJjaGFubmVsIiwiaXNzIjoiaHR0cHM6Ly9taWwtYXV0aCIsImdyb3VwcyI6InJvbGUiLCJ0ZXJtaW5hbElkIjoidGVybWluYWxfaWQiLCJhdWQiOiJodHRwczovL21pbCIsIm1lcmNoYW50SWQiOiJtZXJjaGFudF9pZCIsInNjb3BlIjoic2NvcGUiLCJmaXNjYWxDb2RlIjoiZW5jX2Zpc2NhbF9jb2RlIiwiZXhwIjoxNzE3NjUyLCJhY3F1aXJlcklkIjoiYWNxdWlyZXJfaWQiLCJpYXQiOjE3MTc1OTJ9.AA\"}"; | ||
|
||
Sample sample = new ObjectMapper().readValue(json, Sample.class); | ||
|
||
SignedJWT actual = sample.getJwt(); | ||
|
||
assertEquals(expected.serialize(), actual.serialize()); | ||
} | ||
|
||
/** | ||
* Test method for | ||
* {@link it.pagopa.swclient.mil.auth.util.SignedJWTDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)}. | ||
*/ | ||
@Test | ||
void testDeserializeJsonParserDeserializationContextWithInvalidJwt() { | ||
String json = "{\"jwt\":\"@.@.@\"}"; | ||
assertThrows(JsonProcessingException.class, () -> new ObjectMapper().readValue(json, Sample.class)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/it/pagopa/swclient/mil/auth/util/SignedJWTParamConverterTest.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,35 @@ | ||
/* | ||
* SignedJWTParamConverterTest.java | ||
* | ||
* 8 gen 2025 | ||
*/ | ||
package it.pagopa.swclient.mil.auth.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.text.ParseException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
@QuarkusTest | ||
class SignedJWTParamConverterTest { | ||
/** | ||
* Test method for | ||
* {@link it.pagopa.swclient.mil.auth.util.SignedJWTParamConverter#toString(com.nimbusds.jwt.SignedJWT)}. | ||
* | ||
* @throws ParseException | ||
*/ | ||
@Test | ||
void testToStringSignedJWT() throws ParseException { | ||
SignedJWT jwt = SignedJWT.parse("eyJraWQiOiJrZXlfbmFtZS9rZXlfdmVyc2lvbiIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjbGllbnRfaWQiLCJjbGllbnRJZCI6ImNsaWVudF9pZCIsImNoYW5uZWwiOiJjaGFubmVsIiwiaXNzIjoiaHR0cHM6Ly9taWwtYXV0aCIsImdyb3VwcyI6InJvbGUiLCJ0ZXJtaW5hbElkIjoidGVybWluYWxfaWQiLCJhdWQiOiJodHRwczovL21pbCIsIm1lcmNoYW50SWQiOiJtZXJjaGFudF9pZCIsInNjb3BlIjoic2NvcGUiLCJmaXNjYWxDb2RlIjoiZW5jX2Zpc2NhbF9jb2RlIiwiZXhwIjoxNzE3NjUyLCJhY3F1aXJlcklkIjoiYWNxdWlyZXJfaWQiLCJpYXQiOjE3MTc1OTJ9.AA"); | ||
assertEquals(jwt.serialize(), new SignedJWTParamConverter().toString(jwt)); | ||
} | ||
} |