-
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.
Merge pull request #2 from youabledev/v0.2.0
V0.2.0
- Loading branch information
Showing
9 changed files
with
338 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.youable.safehttp.cipher; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.util.Base64; | ||
|
||
public class AESCipher implements HttpCipher { | ||
private final String privateKey; | ||
private final String ALGORITHM = "AES"; | ||
private final int BEGIN_INDEX = 0; | ||
private final int END_INDEX = 16; | ||
private final String TRANSFORMATION = "AES/CBC/PKCS5Padding"; | ||
|
||
@Override | ||
public String encrypt(String data) throws Exception { | ||
SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(privateKey), ALGORITHM); | ||
IvParameterSpec IV = new IvParameterSpec(privateKey.substring(BEGIN_INDEX, END_INDEX).getBytes()); | ||
Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV); | ||
byte[] encryptByte = cipher.doFinal(data.getBytes("UTF-8")); | ||
return Base64.getEncoder().encodeToString(encryptByte); | ||
} | ||
|
||
@Override | ||
public String decrypt(String data) throws Exception { | ||
SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(privateKey), ALGORITHM); | ||
IvParameterSpec IV = new IvParameterSpec(privateKey.substring(BEGIN_INDEX, END_INDEX).getBytes()); | ||
Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, IV); | ||
byte[] decodeByte = cipher.doFinal(Base64.getDecoder().decode(data)); | ||
return new String(decodeByte); | ||
} | ||
|
||
private AESCipher(AESCipherBuilder builder) { | ||
this.privateKey = builder.privateKey; | ||
} | ||
|
||
public static class AESCipherBuilder { | ||
private String privateKey; | ||
|
||
public AESCipherBuilder(String PRIVATE_KEY) { | ||
this.privateKey = PRIVATE_KEY; | ||
} | ||
|
||
public AESCipher build() { | ||
return new AESCipher(this); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.youable.safehttp.cipher; | ||
|
||
import javax.crypto.Cipher; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.util.Base64; | ||
|
||
public class RSACipher implements HttpCipher { | ||
private PublicKey publicKey; | ||
private PrivateKey privateKey; | ||
|
||
@Override | ||
public String encrypt(String data) throws Exception { | ||
Cipher cipher = Cipher.getInstance("RSA"); | ||
cipher.init(Cipher.ENCRYPT_MODE, publicKey); | ||
byte[] encryptedBytes = cipher.doFinal(data.getBytes()); | ||
return Base64.getEncoder().encodeToString(encryptedBytes); | ||
} | ||
|
||
@Override | ||
public String decrypt(String data) throws Exception { | ||
Cipher cipher = Cipher.getInstance("RSA"); | ||
cipher.init(Cipher.DECRYPT_MODE, privateKey); | ||
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(data)); | ||
return new String(decryptedBytes); | ||
} | ||
|
||
private RSACipher(RSACipherBuilder builder) { | ||
this.publicKey = builder.publicKey; | ||
this.privateKey = builder.privateKey; | ||
} | ||
|
||
public static class RSACipherBuilder { | ||
private PublicKey publicKey; | ||
private PrivateKey privateKey; | ||
|
||
public RSACipherBuilder(PublicKey publicKey, PrivateKey privateKey) { | ||
this.publicKey = publicKey; | ||
this.privateKey = privateKey; | ||
} | ||
|
||
public RSACipher build() { | ||
return new RSACipher(this); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/java/com/youable/safehttp/advice/DecryptRequestBodyAdviceTest.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,79 @@ | ||
package com.youable.safehttp.advice; | ||
|
||
import com.youable.safehttp.annotation.DecryptRequest; | ||
import com.youable.safehttp.cipher.HttpCipher; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpInputMessage; | ||
|
||
import java.io.ByteArrayInputStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
class DecryptRequestBodyAdviceTest { | ||
@DisplayName("DecryptRequestBodyAdvice should support methods annotated with @DecryptRequest") | ||
@Test | ||
void testDecryptRequestAnnotation() { | ||
// given | ||
HttpCipher mockHttpCiper = mock(HttpCipher.class); | ||
DecryptRequestBodyAdvice advice = new DecryptRequestBodyAdvice(mockHttpCiper); | ||
MethodParameter methodParameter = mock(MethodParameter.class); | ||
|
||
// when | ||
when(methodParameter.hasMethodAnnotation(DecryptRequest.class)).thenReturn(true); | ||
boolean supports = advice.supports(methodParameter, String.class, null); | ||
|
||
// then | ||
assert(supports); | ||
} | ||
|
||
@DisplayName("Decrypted request body should match expected JSON") | ||
@Test | ||
void testBeforeBodyRead() throws Exception { | ||
// given | ||
String encryptedBody = "SXuMtzsL8X05TCRuyOHEGA=="; | ||
String decryptedBody = "{\"key\":\"value\"}"; | ||
HttpCipher mockHttpCipher = mock(HttpCipher.class); | ||
when(mockHttpCipher.decrypt(encryptedBody)).thenReturn(decryptedBody); | ||
|
||
DecryptRequestBodyAdvice advice = new DecryptRequestBodyAdvice(mockHttpCipher); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
DecryptedHttpInputMessage inputMessage = new DecryptedHttpInputMessage(encryptedBody, headers); | ||
|
||
MethodParameter parameter = mock(MethodParameter.class); | ||
|
||
// when | ||
HttpInputMessage result = advice.beforeBodyRead(inputMessage, parameter, String.class, null); | ||
|
||
// then | ||
assertNotNull(result); | ||
assertEquals(decryptedBody, new String(result.getBody().readAllBytes())); | ||
} | ||
|
||
@DisplayName("Decryption failure should throw RuntimeException") | ||
@Test | ||
void testDecryptFailure() throws Exception { | ||
// given | ||
String encryptedBody = "SXuMtzsL8X05TCRuyOHEGA=="; | ||
HttpCipher mockHttpCipher = mock(HttpCipher.class); | ||
when(mockHttpCipher.decrypt(encryptedBody)).thenThrow(new RuntimeException("Decryption failed")); | ||
|
||
DecryptRequestBodyAdvice advice = new DecryptRequestBodyAdvice(mockHttpCipher); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
DecryptedHttpInputMessage inputMessage = new DecryptedHttpInputMessage(encryptedBody, headers); | ||
|
||
MethodParameter parameter = mock(MethodParameter.class); | ||
|
||
// when & then | ||
RuntimeException exception = assertThrows(RuntimeException.class, () -> { | ||
advice.beforeBodyRead(inputMessage, parameter, String.class, null); | ||
}); | ||
|
||
assertEquals("Decryption failed", exception.getCause().getMessage()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/com/youable/safehttp/aop/EncryptResponseAspectTest.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,53 @@ | ||
package com.youable.safehttp.aop; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.youable.safehttp.cipher.HttpCipher; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
class EncryptResponseAspectTest { | ||
@DisplayName("EncryptResponseAspect should match expected Response Body") | ||
@Test | ||
void testEncryptResponse() throws Throwable { | ||
// given | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
HttpCipher mockHttpCipher = mock(HttpCipher.class); | ||
EncryptResponseAspect aspect = new EncryptResponseAspect(mockHttpCipher, objectMapper); | ||
|
||
ProceedingJoinPoint mockJoinPoint = mock(ProceedingJoinPoint.class); | ||
|
||
Person response = new Person("hongkildong", 12); | ||
String responseJson = objectMapper.writeValueAsString(response); | ||
String encryptedData = "o3Y8Yiuffa+LNkTftpgSGmBasze4poDDbuQ3GVIwmiY="; | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-Type", "application/json"); | ||
ResponseEntity<Person> mockResponseEntity = | ||
new ResponseEntity<>(response, headers, HttpStatus.OK); | ||
|
||
// when | ||
when(mockJoinPoint.proceed()).thenReturn(mockResponseEntity); | ||
when(mockHttpCipher.encrypt(responseJson)).thenReturn(encryptedData); | ||
|
||
// Act | ||
Object result = aspect.encryptResponse(mockJoinPoint, null); | ||
|
||
// then | ||
assertTrue(result instanceof ResponseEntity<?>); | ||
ResponseEntity<?> responseEntity = (ResponseEntity<?>) result; | ||
assertEquals(encryptedData, responseEntity.getBody()); | ||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(headers, responseEntity.getHeaders()); | ||
|
||
verify(mockJoinPoint, times(1)).proceed(); | ||
verify(mockHttpCipher, times(1)).encrypt(responseJson); | ||
} | ||
|
||
private record Person(String name, Integer age) {} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/youable/safehttp/cipher/AESCipherTest.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,34 @@ | ||
package com.youable.safehttp.cipher; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class AESCipherTest { | ||
@DisplayName("AES encrypt/decrypt") | ||
@Test | ||
void testEncryptAndDecrypt() throws Exception { | ||
// given | ||
byte[] key = new byte[32]; | ||
new SecureRandom().nextBytes(key); | ||
String privateKey = Base64.getEncoder().encodeToString(key); | ||
|
||
HttpCipher cipher = new AESCipher.AESCipherBuilder(privateKey).build(); | ||
|
||
String plainText = "{\"key\":\"value\"}"; | ||
|
||
// when | ||
String encryptedText = cipher.encrypt(plainText); | ||
String decryptedText = cipher.decrypt(encryptedText); | ||
|
||
// then | ||
assertNotNull(encryptedText); | ||
assertNotEquals(plainText, encryptedText); | ||
assertEquals(plainText, decryptedText); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/com/youable/safehttp/cipher/RSACipherTest.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,33 @@ | ||
package com.youable.safehttp.cipher; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.SecureRandom; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RSACipherTest { | ||
@DisplayName("RSA encrypt/decrypt") | ||
@Test | ||
void test() throws Exception { | ||
// given | ||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); | ||
keyPairGenerator.initialize(2048, new SecureRandom()); | ||
KeyPair keyPair = keyPairGenerator.generateKeyPair(); | ||
|
||
HttpCipher cipher = new RSACipher.RSACipherBuilder(keyPair.getPublic(), keyPair.getPrivate()).build(); | ||
String plainText = "{\"key\":\"value\"}"; | ||
|
||
// when | ||
String encryptedText = cipher.encrypt(plainText); | ||
String decryptedText = cipher.decrypt(encryptedText); | ||
|
||
// then | ||
assertNotNull(encryptedText); | ||
assertNotEquals(plainText, encryptedText); | ||
assertEquals(plainText, decryptedText); | ||
} | ||
} |