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

The following examples show how to use org.apache.cxf.rt.security.crypto.CryptoUtils#encryptSequence() . 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: 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 5
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static String encryptClient(Client client, Key secretKey,
                                   KeyProperties props) throws SecurityException {
    String tokenSequence = tokenizeClient(client);
    return CryptoUtils.encryptSequence(tokenSequence, secretKey, props);
}
 
Example 6
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static String encryptAccessToken(ServerAccessToken token, Key secretKey,
                                        KeyProperties props) throws SecurityException {
    String tokenSequence = tokenizeServerToken(token);
    return CryptoUtils.encryptSequence(tokenSequence, secretKey, props);
}
 
Example 7
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static String encryptRefreshToken(RefreshToken token, Key secretKey,
                                         KeyProperties props) throws SecurityException {
    String tokenSequence = tokenizeRefreshToken(token);

    return CryptoUtils.encryptSequence(tokenSequence, secretKey, props);
}
 
Example 8
Source File: ModelEncryptionSupport.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static String encryptCodeGrant(ServerAuthorizationCodeGrant grant, Key secretKey,
                                      KeyProperties props) throws SecurityException {
    String tokenSequence = tokenizeCodeGrant(grant);

    return CryptoUtils.encryptSequence(tokenSequence, secretKey, props);
}