forked from opensearch-project/security
-
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 tests and relocate KeyPaddingUtil
Signed-off-by: Ryan Liang <[email protected]>
- Loading branch information
Showing
3 changed files
with
76 additions
and
15 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
33 changes: 33 additions & 0 deletions
33
src/main/java/org/opensearch/security/authtoken/jwt/KeyPaddingUtil.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 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.authtoken.jwt; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jose.util.ByteUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import static com.nimbusds.jose.crypto.MACSigner.getMinRequiredSecretLength; | ||
|
||
public class KeyPaddingUtil { | ||
public static String padSecret(String signingKey, JWSAlgorithm jwsAlgorithm) { | ||
int requiredSecretLength; | ||
try { | ||
requiredSecretLength = getMinRequiredSecretLength(jwsAlgorithm); | ||
} catch (JOSEException e) { | ||
throw new RuntimeException(e); | ||
} | ||
int requiredByteLength = ByteUtils.byteLength(requiredSecretLength); | ||
// padding the signing key with 0s to meet the minimum required length | ||
return StringUtils.rightPad(signingKey, requiredByteLength, "\0"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/opensearch/security/authtoken/jwt/KeyPaddingUtilTest.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,42 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.authtoken.jwt; | ||
|
||
import com.nimbusds.jose.JWSAlgorithm; | ||
import org.junit.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class KeyPaddingUtilTest { | ||
|
||
private String signingKey = "testKey"; | ||
|
||
@Test | ||
public void testPadSecretForHS256() { | ||
JWSAlgorithm jwsAlgorithm = JWSAlgorithm.HS256; | ||
String paddedKey = KeyPaddingUtil.padSecret(signingKey, jwsAlgorithm); | ||
|
||
// For HS256, HMAC using SHA-256, typical key length is 256 bits or 32 bytes | ||
int expectedLength = 32; | ||
assertEquals(expectedLength, paddedKey.length()); | ||
} | ||
|
||
@Test | ||
public void testPadSecretForHS384() { | ||
JWSAlgorithm jwsAlgorithm = JWSAlgorithm.HS384; | ||
String paddedKey = KeyPaddingUtil.padSecret(signingKey, jwsAlgorithm); | ||
|
||
// For HS384, HMAC using SHA-384, typical key length is 384 bits or 48 bytes | ||
int expectedLength = 48; | ||
assertEquals(expectedLength, paddedKey.length()); | ||
} | ||
} |