Search in sources :

Example 1 with ConsistencyException

use of org.owasp.oag.exception.ConsistencyException in project Application-Gateway by gianlucafrei.

the class JwtSigner method createSignedJwt.

/**
 * Creates the JWT and adds the passed in claims and signs it.
 * this is a template method.
 *
 * @param claimsSet the claims to add to the JWT
 * @return The signed JWT in Base64 encoded format.
 */
public String createSignedJwt(JWTClaimsSet claimsSet) {
    JWSHeader jwsHeader = createJwsHeader();
    SignedJWT signedJWT = new SignedJWT(jwsHeader, claimsSet);
    try {
        signedJWT.sign(getJwtSigner());
    } catch (JOSEException e) {
        throw new ConsistencyException("Could not sign jwt", e);
    }
    String jwt = signedJWT.serialize();
    return jwt;
}
Also used : ConsistencyException(org.owasp.oag.exception.ConsistencyException) SignedJWT(com.nimbusds.jwt.SignedJWT)

Example 2 with ConsistencyException

use of org.owasp.oag.exception.ConsistencyException in project Application-Gateway by gianlucafrei.

the class JweEncrypter method loadFromFileOrCreateAndStoreNewKey.

public static JweEncrypter loadFromFileOrCreateAndStoreNewKey(String filename) throws IOException {
    if (filename == null)
        throw new ApplicationException("Filename must not be null", null);
    File keyFile = new File(filename);
    byte[] keyBytes;
    if (keyFile.exists()) {
        // Read key from file
        keyBytes = Files.toByteArray(keyFile);
    } else {
        // Create new secret key and store it in file
        KeyGenerator keyGen;
        try {
            keyGen = KeyGenerator.getInstance("AES");
            // for example
            keyGen.init(128);
            SecretKey secretKey = keyGen.generateKey();
            // Store key om file
            keyBytes = secretKey.getEncoded();
            Files.write(keyBytes, keyFile);
        } catch (NoSuchAlgorithmException e) {
            throw new ConsistencyException("Cloud not create AES key", e);
        }
    }
    return new JweEncrypter(keyBytes);
}
Also used : SecretKey(javax.crypto.SecretKey) ApplicationException(org.owasp.oag.exception.ApplicationException) ConsistencyException(org.owasp.oag.exception.ConsistencyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) File(java.io.File) KeyGenerator(javax.crypto.KeyGenerator)

Example 3 with ConsistencyException

use of org.owasp.oag.exception.ConsistencyException in project Application-Gateway by gianlucafrei.

the class JweEncrypter method encryptObject.

@Override
public String encryptObject(Object payload) {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String payloadString = objectMapper.writeValueAsString(payload);
        return encrypt(payloadString);
    } catch (JsonProcessingException e) {
        throw new ConsistencyException("Could not encode Json", e);
    }
}
Also used : ConsistencyException(org.owasp.oag.exception.ConsistencyException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ConsistencyException (org.owasp.oag.exception.ConsistencyException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 File (java.io.File)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1 ApplicationException (org.owasp.oag.exception.ApplicationException)1