Skip to content

Commit

Permalink
Added SignedJWT deserializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniotarricone committed Jan 8, 2025
1 parent dd63e04 commit 0542c77
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 6 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public SignedJWT fromString(String value) {
*/
@Override
public String toString(SignedJWT value) {
// For now this method is not useful, so to avoid coverage checking,
// I prefer to return an exception!
// The real implementation should be:
// Log.trace("toString"); // NOSONAR
// return value.serialize(); // NOSONAR
throw new UnsupportedOperationException();
Log.trace("toString");
return value.serialize();
}
}
37 changes: 37 additions & 0 deletions src/test/java/it/pagopa/swclient/mil/auth/util/Sample.java
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;
}
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));
}
}
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));
}
}

0 comments on commit 0542c77

Please sign in to comment.