forked from smallrye/smallrye-jwt
-
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.
Add methods for secret key generation (smallrye#342)
- Loading branch information
Showing
7 changed files
with
196 additions
and
1 deletion.
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
43 changes: 43 additions & 0 deletions
43
...on/common/src/test/java/io/smallrye/jwt/util/GenerateSecretFromKeyEncryptionAlgoTest.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,43 @@ | ||
package io.smallrye.jwt.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.security.InvalidAlgorithmParameterException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm; | ||
|
||
@RunWith(Parameterized.class) | ||
public class GenerateSecretFromKeyEncryptionAlgoTest { | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> data() { | ||
Collection<Object[]> args = new ArrayList<>(); | ||
for (Map.Entry<KeyEncryptionAlgorithm, Integer> e : KeyUtils.KEY_ENCRYPTION_BITS.entrySet()) { | ||
args.add(new Object[] { e.getKey().getAlgorithm(), e.getValue() }); | ||
} | ||
return args; | ||
} | ||
|
||
@Parameterized.Parameter | ||
public String algoName; | ||
|
||
@Parameterized.Parameter(1) | ||
public Integer keySizeInBits; | ||
|
||
@Test | ||
public void givenSymmetricAlgo_thenReturnSecretKey() throws InvalidAlgorithmParameterException { | ||
KeyEncryptionAlgorithm algo = KeyEncryptionAlgorithm.fromAlgorithm(algoName); | ||
SecretKey keySpec = KeyUtils.generateSecretKey(algo); | ||
assertEquals("AES", keySpec.getAlgorithm()); | ||
assertEquals(keySizeInBits / 8, keySpec.getEncoded().length); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...tation/common/src/test/java/io/smallrye/jwt/util/GenerateSecretFromSignatureAlgoTest.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,44 @@ | ||
package io.smallrye.jwt.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.security.InvalidAlgorithmParameterException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import io.smallrye.jwt.algorithm.SignatureAlgorithm; | ||
|
||
@RunWith(Parameterized.class) | ||
public class GenerateSecretFromSignatureAlgoTest { | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> data() { | ||
Collection<Object[]> args = new ArrayList<>(); | ||
for (Map.Entry<SignatureAlgorithm, Integer> e : KeyUtils.SIGNATURE_ALGORITHM_BITS.entrySet()) { | ||
args.add(new Object[] { e.getKey().getAlgorithm(), e.getValue() }); | ||
} | ||
return args; | ||
} | ||
|
||
@Parameterized.Parameter | ||
public String algoName; | ||
|
||
@Parameterized.Parameter(1) | ||
public Integer keySizeInBits; | ||
|
||
@Test | ||
public void givenSymmetricAlgo_thenReturnSecretKey() throws InvalidAlgorithmParameterException { | ||
SignatureAlgorithm algo = SignatureAlgorithm.fromAlgorithm(algoName); | ||
SecretKey keySpec = KeyUtils.generateSecretKey(algo); | ||
assertEquals("HMAC", keySpec.getAlgorithm()); | ||
assertEquals(keySizeInBits / 8, keySpec.getEncoded().length); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
implementation/common/src/test/java/io/smallrye/jwt/util/KeyUtilsTest.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,24 @@ | ||
package io.smallrye.jwt.util; | ||
|
||
import java.security.InvalidAlgorithmParameterException; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.junit.Test; | ||
|
||
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm; | ||
import io.smallrye.jwt.algorithm.SignatureAlgorithm; | ||
|
||
public class KeyUtilsTest { | ||
|
||
@Test(expected = InvalidAlgorithmParameterException.class) | ||
public void givenAsymmetricKeyEncryptionAlgo_thenThrowsInvalidAlgorithmException() | ||
throws InvalidAlgorithmParameterException { | ||
SecretKey keySpec = KeyUtils.generateSecretKey(KeyEncryptionAlgorithm.RSA_OAEP); | ||
} | ||
|
||
@Test(expected = InvalidAlgorithmParameterException.class) | ||
public void givenAsymmetricSignatureAlgo_thenThrowsInvalidAlgorithmException() throws InvalidAlgorithmParameterException { | ||
SecretKey keySpec = KeyUtils.generateSecretKey(SignatureAlgorithm.RS256); | ||
} | ||
} |