Java Code Examples for org.jose4j.jwk.JsonWebKey#getKey()

The following examples show how to use org.jose4j.jwk.JsonWebKey#getKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultCipherExecutor.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * Prepare json web token key.
 *
 * @param secret the secret
 * @return the key
 */
private Key prepareJsonWebTokenKey(final String secret) {
    try {
        final Map<String, Object> keys = new HashMap<>(2);
        keys.put("kty", "oct");
        keys.put("k", secret);
        final JsonWebKey jwk = JsonWebKey.Factory.newJwk(keys);
        return jwk.getKey();
    } catch (final Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}
 
Example 2
Source File: EcdhKeyAgreementAlgorithm.java    From Jose4j with Apache License 2.0 5 votes vote down vote up
public Key manageForDecrypt(Key managementKey, byte[] encryptedKey, ContentEncryptionKeyDescriptor cekDesc, Headers headers,  ProviderContext providerContext) throws JoseException
{
    String keyFactoryProvider = providerContext.getGeneralProviderContext().getKeyFactoryProvider();
    JsonWebKey ephemeralJwk = headers.getPublicJwkHeaderValue(HeaderParameterNames.EPHEMERAL_PUBLIC_KEY, keyFactoryProvider);
    ephemeralJwk.getKey();
    byte[] z = generateEcdhSecret((PrivateKey) managementKey, (PublicKey)ephemeralJwk.getKey(), providerContext);
    byte[] derivedKey = kdf(cekDesc, headers, z, providerContext);
    String cekAlg = cekDesc.getContentEncryptionKeyAlgorithm();
    return new SecretKeySpec(derivedKey, cekAlg);
}
 
Example 3
Source File: DefaultCipherExecutor.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Prepare json web token key.
 *
 * @param secret the secret
 * @return the key
 */
private Key prepareJsonWebTokenKey(final String secret) {
    try {
        final Map<String, Object> keys = new HashMap<>(2);
        keys.put("kty", "oct");
        keys.put("k", secret);
        final JsonWebKey jwk = JsonWebKey.Factory.newJwk(keys);
        return jwk.getKey();
    } catch (final Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}
 
Example 4
Source File: JwtSignTest.java    From smallrye-jwt with Apache License 2.0 4 votes vote down vote up
private static SecretKey createSecretKey() throws Exception {
    String jwkJson = "{\"kty\":\"oct\",\"k\":\"Fdh9u8rINxfivbrianbbVT1u232VQBZYKx1HGAGPt2I\"}";
    JsonWebKey jwk = JsonWebKey.Factory.newJwk(jwkJson);
    return (SecretKey) jwk.getKey();
}
 
Example 5
Source File: JwtEncryptTest.java    From smallrye-jwt with Apache License 2.0 4 votes vote down vote up
private static SecretKey createSecretKey() throws Exception {
    String jwkJson = "{\"kty\":\"oct\",\"k\":\"Fdh9u8rINxfivbrianbbVT1u232VQBZYKx1HGAGPt2I\"}";
    JsonWebKey jwk = JsonWebKey.Factory.newJwk(jwkJson);
    return (SecretKey) jwk.getKey();
}
 
Example 6
Source File: JwsUnencodedPayloadOptionTest.java    From Jose4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testExamplesFromDraftEvenWithoutDirectSupportForTheHeader() throws Exception
{
    // a test of sorts to verify the examples from
    // http://tools.ietf.org/html/draft-ietf-jose-jws-signing-input-options-09#section-4
    // at Mike's request

    String jwkJson =
            "{" +
            "  \"kty\":\"oct\"," +
            "  \"k\":\"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75" +
            "      aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow\"" +
            "}";
    final JsonWebKey jsonWebKey = JsonWebKey.Factory.newJwk(jwkJson);
    final Key key = jsonWebKey.getKey();

    String payload = "$.02";

    final String encodedPayload = Base64Url.encode(payload, StringUtil.US_ASCII);
    assertThat(encodedPayload, equalTo("JC4wMg"));

    String jwscsWithB64 = "eyJhbGciOiJIUzI1NiJ9.JC4wMg.5mvfOroL-g7HyqJoozehmsaqmvTYGEq5jTI1gVvoEoQ";

    JsonWebSignature jws = new JsonWebSignature();
    jws.setCompactSerialization(jwscsWithB64);
    jws.setKey(key);
    assertThat(jws.getPayload(), equalTo(payload));
    assertTrue(jws.verifySignature());

    jws = new JsonWebSignature();
    jws.setPayload(payload);
    jws.setKey(key);
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
    assertThat(jws.getCompactSerialization(), equalTo(jwscsWithB64));

    String jwscsWithoutB64andDetachedPaylod = "eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19.." +
            "A5dxf2s96_n5FLueVuW1Z_vh161FwXZC4YLPff6dmDY";

    jws = new JsonWebSignature();
    jws.setCompactSerialization(jwscsWithoutB64andDetachedPaylod);
    assertThat(jws.getHeaders().getFullHeaderAsJsonString(), equalTo("{\"alg\":\"HS256\",\"b64\":false,\"crit\":[\"b64\"]}"));

    HmacUsingShaAlgorithm.HmacSha256 hmacSha256 = new HmacUsingShaAlgorithm.HmacSha256();

    final String signingInputString = jws.getHeaders().getEncodedHeader() + "." + payload;

    final byte[] signatureBytes = Base64Url.decode(jws.getEncodedSignature());
    final byte[] securedInputBytes = StringUtil.getBytesAscii(signingInputString);
    final ProviderContext providerContext = new ProviderContext();
    boolean okay = hmacSha256.verifySignature(signatureBytes, key, securedInputBytes, providerContext);
    assertTrue(okay);

    final byte[] signed = hmacSha256.sign(key, securedInputBytes, providerContext);
    assertThat(Base64Url.encode(signed), equalTo(jws.getEncodedSignature()));
}