use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.
the class JwtService method generate.
public String generate(String id, String subject, Map<String, String> customJwtClaimsMap) {
try {
JWTClaimsSet claimsSet = prepareClaims(id, subject, customJwtClaimsMap);
SignedJWT signedJWT = sign(claimsSet);
String token;
if (configuration.isEncryptionEnabled()) {
JWEObject jweObject = encrypt(signedJWT);
token = jweObject.serialize();
} else {
token = signedJWT.serialize();
}
return removeHeader(token);
} catch (Exception e) {
throw new JwtException(e);
}
}
use of com.nimbusds.jose.JWEObject in project fitpay-android-sdk by fitpay.
the class StringUtils method getDecryptedString.
/**
* Get decrypted string
*
* @param type key type
* @param encryptedString encrypted string
* @return decrypted string
*/
public static String getDecryptedString(@KeysManager.KeyType int type, String encryptedString) {
KeysManager keysManager = KeysManager.getInstance();
JWEObject jweObject;
try {
jweObject = JWEObject.parse(encryptedString);
JWEHeader jweHeader = jweObject.getHeader();
if (jweHeader.getKeyID() == null || jweHeader.getKeyID().equals(keysManager.getKeyId(type))) {
jweObject.decrypt(new AESDecrypter(keysManager.getSecretKey(type)));
if ("JWT".equals(jweObject.getHeader().getContentType())) {
SignedJWT signedJwt = jweObject.getPayload().toSignedJWT();
ECCKeyPair keyPair = keysManager.getPairForType(type);
ECPublicKey key = null;
if ("https://fit-pay.com".equals(signedJwt.getJWTClaimsSet().getIssuer())) {
key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getServerPublicKey()));
} else {
key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getPublicKey()));
}
JWSVerifier verifier = new ECDSAVerifier(key);
if (!signedJwt.verify(verifier)) {
throw new IllegalArgumentException("jwt did not pass signature validation");
}
return signedJwt.getJWTClaimsSet().getStringClaim("data");
} else {
return jweObject.getPayload().toString();
}
}
} catch (Exception e) {
FPLog.e(e);
}
return null;
}
use of com.nimbusds.jose.JWEObject in project fitpay-android-sdk by fitpay.
the class StringUtils method getEncryptedString.
/**
* Get encrypted string
*
* @param type key type
* @param decryptedString decrypted string
* @return encrypted string
*/
public static String getEncryptedString(@KeysManager.KeyType int type, String decryptedString) {
JWEAlgorithm alg = JWEAlgorithm.A256GCMKW;
EncryptionMethod enc = EncryptionMethod.A256GCM;
ECCKeyPair keyPair = KeysManager.getInstance().getPairForType(type);
if (null == keyPair) {
throw new IllegalStateException("No key pair available for type (type = " + type + ")");
}
JWEHeader.Builder jweHeaderBuilder = new JWEHeader.Builder(alg, enc).contentType("application/json").keyID(keyPair.getKeyId());
JWEHeader header = jweHeaderBuilder.build();
Payload payload = new Payload(decryptedString);
JWEObject jweObject = new JWEObject(header, payload);
try {
JWEEncrypter encrypter = new AESEncrypter(KeysManager.getInstance().getSecretKey(type));
jweObject.encrypt(encrypter);
} catch (JOSEException e) {
FPLog.e(e);
}
return jweObject.serialize();
}
use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.
the class JwtService method decrypt.
private SignedJWT decrypt(String token) throws JwtException {
try {
SignedJWT signedJWT;
JWEObject jweObject = JWEObject.parse(token);
jweObject.decrypt(new DirectDecrypter(keyProvider.getEncryptingKey().getEncoded()));
signedJWT = jweObject.getPayload().toSignedJWT();
return signedJWT;
} catch (Exception e) {
throw new JwtException(e);
}
}
use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.
the class JwtService method encrypt.
private JWEObject encrypt(SignedJWT signedJWT) throws JwtException {
try {
JWEObject jweObject = new JWEObject(jweHeader(), new Payload(signedJWT));
jweObject.encrypt(new DirectEncrypter(keyProvider.getEncryptingKey().getEncoded()));
return jweObject;
} catch (Exception e) {
throw new JwtException(e);
}
}
Aggregations