Search in sources :

Example 1 with TimeStamp

use of com.microsoft.azure.oidc.common.timestamp.TimeStamp in project azure-tools-for-java by Microsoft.

the class SimpeTokenParser method getToken.

@Override
public Token getToken(String value) {
    final String[] parts = value.split("\\.");
    if (parts.length != 3) {
        throw new IllegalStateException(String.format("Incorrect number of parts: Expected 3 got %s", parts.length));
    }
    final JsonNode header = parsePart(decodePart(parts[0]));
    final JsonNode body = parsePart(decodePart(parts[1]));
    final Name keyName = getKeyName(header);
    final Algorithm algorithm = getAlgorithm(header);
    final TimeStamp issuedAt = getIssuedAt(body);
    final TimeStamp notBefore = getNotBefore(body);
    final TimeStamp expiration = getExpiration(body);
    final Issuer issuer = getIssuer(body);
    final ID audience = getAudience(body);
    final ID userID = getUserID(body);
    final List<Email> userEmails = getEmails(body);
    final Payload payload = getPayload(parts[0], parts[1]);
    final Signature signature = getSignature(parts[2]);
    return tokenFactory.createToken(keyName, algorithm, issuedAt, notBefore, expiration, userID, userEmails, issuer, audience, payload, signature);
}
Also used : Email(com.microsoft.azure.oidc.token.email.Email) Issuer(com.microsoft.azure.oidc.common.issuer.Issuer) JsonNode(com.fasterxml.jackson.databind.JsonNode) Algorithm(com.microsoft.azure.oidc.common.algorithm.Algorithm) TimeStamp(com.microsoft.azure.oidc.common.timestamp.TimeStamp) Name(com.microsoft.azure.oidc.common.name.Name) Signature(com.microsoft.azure.oidc.token.signature.Signature) Payload(com.microsoft.azure.oidc.token.payload.Payload) ID(com.microsoft.azure.oidc.common.id.ID)

Example 2 with TimeStamp

use of com.microsoft.azure.oidc.common.timestamp.TimeStamp in project azure-tools-for-java by Microsoft.

the class SimpleKeyStoreParser method getKeys.

@Override
public Map<Name, Key> getKeys(final JsonNode node) {
    if (node == null) {
        throw new PreconditionException("Required parameter is null");
    }
    final Map<Name, Key> keys = new HashMap<Name, Key>();
    for (final JsonNode n : node.get("keys")) {
        final TimeStamp notBefore = timeStampFactory.createTimeStamp(n.has("nbf") ? n.get("nbf").asLong() : 0L);
        final Name keyName = nameFactory.createKeyName(n.get("kid").asText());
        final Modulus modulus = modulusFactory.createKeyValue(n.get("n").asText());
        final Exponent exponent = exponentFactory.createKeyExponent(n.get("e").asText());
        final Key key = keyFactory.createKey(notBefore, modulus, exponent);
        keys.put(keyName, key);
    }
    return keys;
}
Also used : Exponent(com.microsoft.azure.oidc.configuration.key.exponent.Exponent) HashMap(java.util.HashMap) Modulus(com.microsoft.azure.oidc.configuration.key.modulus.Modulus) JsonNode(com.fasterxml.jackson.databind.JsonNode) Key(com.microsoft.azure.oidc.configuration.key.Key) TimeStamp(com.microsoft.azure.oidc.common.timestamp.TimeStamp) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException) Name(com.microsoft.azure.oidc.common.name.Name)

Example 3 with TimeStamp

use of com.microsoft.azure.oidc.common.timestamp.TimeStamp in project azure-tools-for-java by Microsoft.

the class SimpleTokenValidator method validateSignature.

@Override
public Boolean validateSignature(final Token token) {
    if (token == null) {
        throw new PreconditionException("Required parameter is null");
    }
    if (algorithmConfigurationService.get().getAlgorithmClassMap().get(token.getAlgorithm().getName()).equals("HMAC")) {
        return Boolean.FALSE;
    }
    final Configuration configuration = configurationCache.load();
    if (configuration == null) {
        throw new GeneralException("Error loading configuration");
    }
    try {
        final TimeStamp now = timeStampFactory.createTimeStamp(System.currentTimeMillis() / 1000);
        if (configuration.getKey(token.getKeyName()).getNotBefore().compareTo(now) > 0) {
            return Boolean.FALSE;
        }
        final Base64 decoder = new Base64();
        final BigInteger exponent = new BigInteger(1, decoder.decode(configuration.getKey(token.getKeyName()).getExponent().getValue()));
        final BigInteger modulus = new BigInteger(1, decoder.decode(configuration.getKey(token.getKeyName()).getSecret().getValue()));
        final RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, exponent);
        final KeyFactory keyFactory = KeyFactory.getInstance(algorithmConfigurationService.get().getAlgorithmClassMap().get(token.getAlgorithm().getName()));
        final PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        final Signature sig = Signature.getInstance(algorithmConfigurationService.get().getAlgorithmMap().get(token.getAlgorithm().getName()));
        sig.initVerify(pubKey);
        sig.update(token.getPayload().getValue().getBytes());
        return sig.verify(decoder.decode(token.getSignature().getValue()));
    } catch (NoSuchAlgorithmException | InvalidKeySpecException | SignatureException | InvalidKeyException e) {
        LOGGER.error(e.getMessage(), e);
        return Boolean.FALSE;
    }
}
Also used : GeneralException(com.microsoft.azure.oidc.exception.GeneralException) Base64(org.apache.commons.codec.binary.Base64) Configuration(com.microsoft.azure.oidc.configuration.Configuration) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) TimeStamp(com.microsoft.azure.oidc.common.timestamp.TimeStamp) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException) Signature(java.security.Signature) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

TimeStamp (com.microsoft.azure.oidc.common.timestamp.TimeStamp)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Name (com.microsoft.azure.oidc.common.name.Name)2 PreconditionException (com.microsoft.azure.oidc.exception.PreconditionException)2 Algorithm (com.microsoft.azure.oidc.common.algorithm.Algorithm)1 ID (com.microsoft.azure.oidc.common.id.ID)1 Issuer (com.microsoft.azure.oidc.common.issuer.Issuer)1 Configuration (com.microsoft.azure.oidc.configuration.Configuration)1 Key (com.microsoft.azure.oidc.configuration.key.Key)1 Exponent (com.microsoft.azure.oidc.configuration.key.exponent.Exponent)1 Modulus (com.microsoft.azure.oidc.configuration.key.modulus.Modulus)1 GeneralException (com.microsoft.azure.oidc.exception.GeneralException)1 Email (com.microsoft.azure.oidc.token.email.Email)1 Payload (com.microsoft.azure.oidc.token.payload.Payload)1 Signature (com.microsoft.azure.oidc.token.signature.Signature)1 BigInteger (java.math.BigInteger)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PublicKey (java.security.PublicKey)1