Skip to content

Commit

Permalink
use ObjectMapper from Spring
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuism committed Nov 25, 2024
1 parent dd98fb3 commit e6bf419
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 34 deletions.
16 changes: 13 additions & 3 deletions src/main/java/nl/ictu/Token.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package nl.ictu;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Date;


@Getter
@Setter
public final class Token {

@Getter
@Setter
public final class Identifier {
private String type;
private String value;
}

private final String version = "v1";
private String recipientOIN;
private String identifierType;
private String identifierValue;
private Identifier identifier = new Identifier();

@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private Date creationDate;
}
9 changes: 6 additions & 3 deletions src/main/java/nl/ictu/controller/v1/ExchangeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nl.ictu.psuedoniemenservice.generated.server.model.WsIdentifier;
import nl.ictu.psuedoniemenservice.generated.server.model.WsIdentifierTypes;
import nl.ictu.service.Cryptographer;
import nl.ictu.service.TokenConverter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -20,6 +21,8 @@ public class ExchangeToken implements ExchangeTokenApi, VersionOneController {

private final Cryptographer cryptographer;

private final TokenConverter tokenConverter;

@Override
@SneakyThrows
public ResponseEntity<WsExchangeTokenForIdentifier200Response> exchangeTokenForIdentifier(final String callerOIN, final WsExchangeTokenForIdentifierRequest wsExchangeTokenForIdentifierRequest) {
Expand All @@ -28,7 +31,7 @@ public ResponseEntity<WsExchangeTokenForIdentifier200Response> exchangeTokenForI

log.info("Received token: " + encodedToken);

final Token token = TokenHelper.decode(encodedToken);
final Token token = tokenConverter.decode(encodedToken);

if (!callerOIN.equals(token.getRecipientOIN())) {
throw new RuntimeException("Sink OIN not the same");
Expand All @@ -38,8 +41,8 @@ public ResponseEntity<WsExchangeTokenForIdentifier200Response> exchangeTokenForI

final WsIdentifier wsIdentifier = new WsIdentifier();

wsIdentifier.setIdentifierType(WsIdentifierTypes.fromValue(token.getIdentifierType()));
wsIdentifier.setIdentifierValue(token.getIdentifierValue());
wsIdentifier.setIdentifierType(WsIdentifierTypes.fromValue(token.getIdentifier().getType()));
wsIdentifier.setIdentifierValue(token.getIdentifier().getValue());

wsExchangeTokenForIdentifier200Response.setIdentifier(wsIdentifier);

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/nl/ictu/controller/v1/GetToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import nl.ictu.psuedoniemenservice.generated.server.model.WsGetToken200Response;
import nl.ictu.psuedoniemenservice.generated.server.model.WsGetTokenRequest;
import nl.ictu.service.Cryptographer;
import nl.ictu.service.TokenConverter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -18,6 +19,8 @@ public class GetToken implements GetTokenApi, VersionOneController {

private final Cryptographer cryptographer;

private final TokenConverter tokenConverter;

@SneakyThrows
@Override
public ResponseEntity<WsGetToken200Response> getToken(final String callerOIN, WsGetTokenRequest wsGetTokenRequest) {
Expand All @@ -30,10 +33,10 @@ public ResponseEntity<WsGetToken200Response> getToken(final String callerOIN, Ws

token.setCreationDate(new Date(System.currentTimeMillis()));
token.setRecipientOIN(wsGetTokenRequest.getRecipientOIN());
token.setIdentifierType(wsGetTokenRequest.getIdentifier().getIdentifierType().name());
token.setIdentifierValue(wsGetTokenRequest.getIdentifier().getIdentifierValue());
token.getIdentifier().setType(wsGetTokenRequest.getIdentifier().getIdentifierType().name());
token.getIdentifier().setValue(wsGetTokenRequest.getIdentifier().getIdentifierValue());

final String plainTextToken = TokenHelper.encode(token);
final String plainTextToken = tokenConverter.encode(token);

wsGetToken200Response.token(cryptographer.encrypt(plainTextToken));

Expand Down
25 changes: 0 additions & 25 deletions src/main/java/nl/ictu/controller/v1/TokenHelper.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/nl/ictu/service/TokenConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nl.ictu.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import nl.ictu.Token;

import java.io.IOException;

public interface TokenConverter {
String encode(Token token) throws IOException;

Token decode(String encodedToken) throws JsonProcessingException;
}
30 changes: 30 additions & 0 deletions src/main/java/nl/ictu/service/TokenConverterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nl.ictu.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import nl.ictu.Token;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;

@Service
@RequiredArgsConstructor
public class TokenConverterImpl implements TokenConverter {

private final ObjectMapper objectMapper;

@Override
public String encode(final Token token) throws IOException {
final StringWriter stringWriter = new StringWriter();
objectMapper.writeValue(stringWriter, token);
return stringWriter.toString();
}

@Override
public Token decode(final String encodedToken) throws JsonProcessingException {
return objectMapper.readValue(encodedToken, Token.class);
}

}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ logging:
spring:
application:
name: Pseudoniemen Service
jackson:
time-zone: Europe/Amsterdam

management:
server:
port: 9080
Expand Down

0 comments on commit e6bf419

Please sign in to comment.