com.google.api.auth.DefaultJwksSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of endpoints-management-auth Show documentation
Show all versions of endpoints-management-auth Show documentation
Enables authentication by multiple authentication providers
The newest version!
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.auth;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.util.Preconditions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jose4j.jwk.EllipticCurveJsonWebKey;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwk.JsonWebKeySet;
import org.jose4j.jwk.RsaJsonWebKey;
import org.jose4j.keys.X509Util;
import org.jose4j.lang.JoseException;
import java.io.IOException;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;
import java.util.Map.Entry;
/**
* The default implementation of {@link JwksSupplier}.
*
* @author [email protected]
*
*/
public class DefaultJwksSupplier implements JwksSupplier {
@VisibleForTesting
static final String X509_CERT_PREFIX = "-----BEGIN CERTIFICATE-----";
@VisibleForTesting
static final String X509_CERT_SUFFIX = "-----END CERTIFICATE-----";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final HttpRequestFactory httpRequestFactory;
private final KeyUriSupplier keyUriSupplier;
public DefaultJwksSupplier(HttpRequestFactory httpRequestFactory, KeyUriSupplier keyUriSupplier) {
this.httpRequestFactory = Preconditions.checkNotNull(httpRequestFactory);
this.keyUriSupplier = Preconditions.checkNotNull(keyUriSupplier);
}
@Override
public JsonWebKeySet supply(String issuer) {
Preconditions.checkNotNull(issuer);
Optional jwksUri = this.keyUriSupplier.supply(issuer);
if (!jwksUri.isPresent()) {
String message = String.format("Cannot find the jwks_uri for issuer %s: "
+ "either the issuer is unknown or the OpenID discovery failed", issuer);
throw new UnauthenticatedException(message);
}
String rawJson = retrieveJwksJson(jwksUri.get());
Map rawMap = parse(rawJson, new TypeReference