use of org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration in project cxf by apache.
the class CryptoUtilsTest method testEncryptDecryptToken.
@Test
public void testEncryptDecryptToken() throws Exception {
AccessTokenRegistration atr = prepareTokenRegistration();
// encrypt
ServerAccessToken token = p.createAccessToken(atr);
ServerAccessToken token2 = p.getAccessToken(token.getTokenKey());
// compare tokens
compareAccessTokens(token, token2);
}
use of org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration in project cxf by apache.
the class CryptoUtilsTest method testBearerTokenCertAndSecretKey.
@Test
public void testBearerTokenCertAndSecretKey() throws Exception {
AccessTokenRegistration atr = prepareTokenRegistration();
BearerAccessToken token = p.createAccessTokenInternal(atr);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = kpg.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
SecretKey secretKey = CryptoUtils.getSecretKey("AES");
String encryptedSecretKey = CryptoUtils.encryptSecretKey(secretKey, publicKey);
String encryptedToken = ModelEncryptionSupport.encryptAccessToken(token, secretKey);
token.setTokenKey(encryptedToken);
SecretKey decryptedSecretKey = CryptoUtils.decryptSecretKey(encryptedSecretKey, privateKey);
ServerAccessToken token2 = ModelEncryptionSupport.decryptAccessToken(p, encryptedToken, decryptedSecretKey);
// compare tokens
compareAccessTokens(token, token2);
}
use of org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration in project cxf by apache.
the class CryptoUtilsTest method testBearerTokenJSONCertificate.
@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<BearerAccessToken>();
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);
}
use of org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration in project cxf by apache.
the class EncryptingDataProvider method createAccessTokenInternal.
BearerAccessToken createAccessTokenInternal(AccessTokenRegistration accessTokenReg) {
BearerAccessToken token = new BearerAccessToken(accessTokenReg.getClient(), 3600L);
token.setSubject(accessTokenReg.getSubject());
createRefreshToken(token);
token.setGrantType(accessTokenReg.getGrantType());
token.setAudiences(accessTokenReg.getAudiences());
token.setParameters(Collections.singletonMap("param", "value"));
token.setScopes(Collections.singletonList(new OAuthPermission("read", "read permission")));
return token;
}
use of org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration in project cxf by apache.
the class EncryptingDataProvider method createAccessToken.
@Override
public ServerAccessToken createAccessToken(AccessTokenRegistration accessTokenReg) throws OAuthServiceException {
ServerAccessToken token = createAccessTokenInternal(accessTokenReg);
encryptAccessToken(token);
return token;
}
Aggregations