Java Code Examples for org.apache.cxf.rt.security.crypto.CryptoUtils#decryptSequence()

The following examples show how to use org.apache.cxf.rt.security.crypto.CryptoUtils#decryptSequence() . 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: CryptoUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testBearerTokenJSON() throws Exception {
    AccessTokenRegistration atr = prepareTokenRegistration();

    BearerAccessToken token = p.createAccessTokenInternal(atr);
    JSONProvider<BearerAccessToken> jsonp = new JSONProvider<>();
    jsonp.setMarshallAsJaxbElement(true);
    jsonp.setUnmarshallAsJaxbElement(true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    jsonp.writeTo(token, BearerAccessToken.class, new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                  new MetadataMap<String, Object>(), bos);

    String encrypted = CryptoUtils.encryptSequence(bos.toString(), p.key);
    String decrypted = CryptoUtils.decryptSequence(encrypted, p.key);
    ServerAccessToken token2 = jsonp.readFrom(BearerAccessToken.class, BearerAccessToken.class,
                                              new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                                              new MetadataMap<String, String>(),
                                              new ByteArrayInputStream(decrypted.getBytes()));

    // compare tokens
    compareAccessTokens(token, token2);
}
 
Example 2
Source File: CryptoUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientJSON() throws Exception {
    Client c = new Client("client", "secret", true);
    c.setSubject(new UserSubject("subject", "id"));
    JSONProvider<Client> jsonp = new JSONProvider<>();
    jsonp.setMarshallAsJaxbElement(true);
    jsonp.setUnmarshallAsJaxbElement(true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    jsonp.writeTo(c, Client.class, new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                  new MetadataMap<String, Object>(), bos);
    String encrypted = CryptoUtils.encryptSequence(bos.toString(), p.key);
    String decrypted = CryptoUtils.decryptSequence(encrypted, p.key);
    Client c2 = jsonp.readFrom(Client.class, Client.class,
                                              new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                                              new MetadataMap<String, String>(),
                                              new ByteArrayInputStream(decrypted.getBytes()));

    assertEquals(c.getClientId(), c2.getClientId());
    assertEquals(c.getClientSecret(), c2.getClientSecret());
    assertTrue(c2.isConfidential());
    assertEquals("subject", c2.getSubject().getLogin());
    assertEquals("id", c2.getSubject().getId());
}
 
Example 3
Source File: CryptoUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGrantJSON() throws Exception {
    Client c = new Client("client", "secret", true);
    ServerAuthorizationCodeGrant grant = new ServerAuthorizationCodeGrant(c, "code", 1, 2);
    JSONProvider<ServerAuthorizationCodeGrant> jsonp = new JSONProvider<>();
    jsonp.setMarshallAsJaxbElement(true);
    jsonp.setUnmarshallAsJaxbElement(true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    jsonp.writeTo(grant, ServerAuthorizationCodeGrant.class, new Annotation[]{},
                  MediaType.APPLICATION_JSON_TYPE,
                  new MetadataMap<String, Object>(), bos);

    String encrypted = CryptoUtils.encryptSequence(bos.toString(), p.key);
    String decrypted = CryptoUtils.decryptSequence(encrypted, p.key);
    ServerAuthorizationCodeGrant grant2 = jsonp.readFrom(ServerAuthorizationCodeGrant.class,
                                                         Client.class,
                                              new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                                              new MetadataMap<String, String>(),
                                              new ByteArrayInputStream(decrypted.getBytes()));
    assertEquals("code", grant2.getCode());
    assertEquals(1, grant2.getExpiresIn());
    assertEquals(2, grant2.getIssuedAt());
}
 
Example 4
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static ServerAccessToken decryptAccessToken(OAuthDataProvider provider,
                                             String encodedData,
                                             Key secretKey,
                                             KeyProperties props) throws SecurityException {
    String decryptedSequence = CryptoUtils.decryptSequence(encodedData, secretKey, props);
    return recreateAccessToken(provider, encodedData, decryptedSequence);
}
 
Example 5
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static RefreshToken decryptRefreshToken(OAuthDataProvider provider,
                                               String encodedData,
                                               Key key,
                                               KeyProperties props) throws SecurityException {
    String decryptedSequence = CryptoUtils.decryptSequence(encodedData, key, props);
    return recreateRefreshToken(provider, encodedData, decryptedSequence);
}
 
Example 6
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static ServerAuthorizationCodeGrant decryptCodeGrant(OAuthDataProvider provider,
                                               String encodedData,
                                               Key key,
                                               KeyProperties props) throws SecurityException {
    String decryptedSequence = CryptoUtils.decryptSequence(encodedData, key, props);
    return recreateCodeGrant(provider, decryptedSequence);
}
 
Example 7
Source File: CryptoUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBearerTokenJSONCertificate() throws Exception {
    if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
        return;
    }
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    KeyPair keyPair = kpg.generateKeyPair();
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    AccessTokenRegistration atr = prepareTokenRegistration();

    BearerAccessToken token = p.createAccessTokenInternal(atr);
    JSONProvider<BearerAccessToken> jsonp = new JSONProvider<>();
    jsonp.setMarshallAsJaxbElement(true);
    jsonp.setUnmarshallAsJaxbElement(true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    jsonp.writeTo(token, BearerAccessToken.class, new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                  new MetadataMap<String, Object>(), bos);

    KeyProperties props1 = new KeyProperties(publicKey.getAlgorithm());
    String encrypted = CryptoUtils.encryptSequence(bos.toString(), publicKey, props1);
    KeyProperties props2 = new KeyProperties(privateKey.getAlgorithm());
    String decrypted = CryptoUtils.decryptSequence(encrypted, privateKey, props2);
    ServerAccessToken token2 = jsonp.readFrom(BearerAccessToken.class, BearerAccessToken.class,
                                              new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
                                              new MetadataMap<String, String>(),
                                              new ByteArrayInputStream(decrypted.getBytes()));

    // compare tokens
    compareAccessTokens(token, token2);
}
 
Example 8
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static Client decryptClient(String encodedData, Key secretKey,
                                   KeyProperties props) throws SecurityException {
    String decryptedSequence = CryptoUtils.decryptSequence(encodedData, secretKey, props);
    return recreateClient(decryptedSequence);
}