-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
18 changed files
with
11,887 additions
and
89 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
package nl.ictu.controller.v1; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import nl.ictu.psuedoniemenservice.generated.server.api.GetTokenApi; | ||
import nl.ictu.psuedoniemenservice.generated.server.model.WsGetToken200Response; | ||
import nl.ictu.psuedoniemenservice.generated.server.model.WsGetTokenRequest; | ||
import nl.ictu.service.Cryptographer; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class GetToken implements GetTokenApi, VersionOneController { | ||
|
||
private final Cryptographer cryptographer; | ||
|
||
@SneakyThrows | ||
@Override | ||
public ResponseEntity<WsGetToken200Response> getToken(final WsGetTokenRequest wsGetTokenRequest) { | ||
|
||
final WsGetToken200Response wsGetToken200Response = new WsGetToken200Response(); | ||
|
||
wsGetToken200Response.token(UUID.randomUUID().toString()); | ||
final String plainTextToken = TokenHelper.encode(wsGetTokenRequest); | ||
|
||
wsGetToken200Response.token(cryptographer.encrypt(plainTextToken)); | ||
|
||
return ResponseEntity.ok(wsGetToken200Response); | ||
} | ||
|
||
} |
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,28 @@ | ||
package nl.ictu.controller.v1; | ||
|
||
import nl.ictu.psuedoniemenservice.generated.server.model.WsIdentifier; | ||
import nl.ictu.psuedoniemenservice.generated.server.model.WsIdentifierTypes; | ||
|
||
public final class IdentifierHelper { | ||
|
||
private final static String DELIMITER = ":"; | ||
|
||
public static String encode(final WsIdentifier wsIdentifier) { | ||
return wsIdentifier.getIdentifierType().name() + DELIMITER + wsIdentifier.getIdentifierValue(); | ||
} | ||
|
||
public static WsIdentifier decode(final String encoded) { | ||
|
||
final String[] parts = encoded.split(DELIMITER); | ||
|
||
final WsIdentifier wsIdentifier = new WsIdentifier(); | ||
|
||
wsIdentifier.identifierType(WsIdentifierTypes.fromValue(parts[0])); | ||
|
||
wsIdentifier.identifierValue(parts[1]); | ||
|
||
return wsIdentifier; | ||
|
||
} | ||
|
||
} |
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,38 @@ | ||
package nl.ictu.controller.v1; | ||
|
||
import nl.ictu.psuedoniemenservice.generated.server.model.WsGetTokenRequest; | ||
|
||
import java.util.StringJoiner; | ||
|
||
public final class TokenHelper { | ||
|
||
private static final String DELIMITER = "_"; | ||
|
||
public static String encode(final WsGetTokenRequest wsGetTokenRequest) { | ||
|
||
final StringJoiner joiner = new StringJoiner(DELIMITER); | ||
|
||
joiner.add(wsGetTokenRequest.getReceiverOin()); | ||
joiner.add(wsGetTokenRequest.getIdentifier().getIdentifierType() + wsGetTokenRequest.getIdentifier().getIdentifierValue()); | ||
|
||
final String encodedToken = wsGetTokenRequest.getRequesterOin() + DELIMITER + IdentifierHelper.encode(wsGetTokenRequest.getIdentifier()) + DELIMITER + wsGetTokenRequest.getReceiverOin(); | ||
|
||
return encodedToken; | ||
|
||
} | ||
|
||
public static WsGetTokenRequest decode(final String encodedToken) { | ||
|
||
final String[] parts = encodedToken.split(DELIMITER); | ||
|
||
final WsGetTokenRequest wsGetTokenRequest = new WsGetTokenRequest(); | ||
|
||
wsGetTokenRequest.setRequesterOin(parts[0]); | ||
wsGetTokenRequest.setIdentifier(IdentifierHelper.decode(parts[1])); | ||
wsGetTokenRequest.setReceiverOin(parts[2]); | ||
|
||
return wsGetTokenRequest; | ||
|
||
} | ||
|
||
} |
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,49 @@ | ||
package nl.ictu.service; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
public final class AESHelper { | ||
|
||
final private static int KEY_LENGTH = 256; | ||
|
||
final public static int IV_LENGTH = 12; | ||
|
||
final private static int TAG_LENGTH = 128; | ||
|
||
final private static String CIPHER = "AES/GCM/NoPadding"; | ||
|
||
final private static SecureRandom secureRandom = new SecureRandom(); | ||
|
||
// Method to generate a random AES key | ||
public static SecretKey generateKey() throws NoSuchAlgorithmException { | ||
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); | ||
keyGenerator.init(KEY_LENGTH); // 128-bit AES encryption | ||
return keyGenerator.generateKey(); | ||
} | ||
|
||
// Method to generate a random Initialization Vector (IV) | ||
public static GCMParameterSpec generateIV() { | ||
byte[] iv = new byte[IV_LENGTH]; // AES block size is 16 bytes | ||
secureRandom.nextBytes(iv); | ||
|
||
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH, iv); | ||
|
||
return gcmParameterSpec; | ||
} | ||
|
||
public static GCMParameterSpec createIVfromValues(byte[] iv) { | ||
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH, iv); | ||
return gcmParameterSpec; | ||
} | ||
|
||
public static Cipher createCipher() throws NoSuchPaddingException, NoSuchAlgorithmException { | ||
return Cipher.getInstance(CIPHER); | ||
} | ||
|
||
} |
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,15 @@ | ||
package nl.ictu.service; | ||
|
||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import javax.crypto.NoSuchPaddingException; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public interface Cryptographer { | ||
|
||
String encrypt(String plaintext) throws IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException; | ||
|
||
String decrypt(String ciphertext) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException; | ||
} |
Oops, something went wrong.