001/*
002* The contents of this file are subject to the terms of the Common Development and
003* Distribution License (the License). You may not use this file except in compliance with the
004* License.
005*
006* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007* specific language governing permission and limitations under the License.
008*
009* When distributing Covered Software, include this CDDL Header Notice in each file and include
010* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011* Header, with the fields enclosed by brackets [] replaced by your own identifying
012* information: "Portions copyright [year] [name of copyright owner]".
013*
014* Copyright 2014 ForgeRock AS.
015*/
016package org.forgerock.jaspi.modules.openid.resolvers;
017
018import java.net.URL;
019import java.security.PublicKey;
020import org.forgerock.jaspi.modules.openid.exceptions.FailedToLoadJWKException;
021
022/**
023 * For producing OpenId Resolvers.
024 */
025public class OpenIdResolverFactory {
026
027    private WellKnownOpenIdConfigurationFactory openIdConfigurationFactory;
028
029    /**
030     * For tests.
031     *
032     * @param openIdConfigurationFactory used to produce JWKResolvers from supplied well-known open ID config URLs
033     */
034    OpenIdResolverFactory(final WellKnownOpenIdConfigurationFactory openIdConfigurationFactory) {
035        this.openIdConfigurationFactory = openIdConfigurationFactory;
036    }
037
038    /**
039     * For generating an OpenIDResolverFactory with the supplied timeouts which will
040     * be used for all HTTP communication originating form this factory.
041     *
042     * @param readTimeout HTTP read timeout for produced resolvers
043     * @param connTimeout HTTP connection timeout for produced resolvers
044     */
045    public OpenIdResolverFactory(final int readTimeout, final int connTimeout) {
046        openIdConfigurationFactory = new WellKnownOpenIdConfigurationFactory(readTimeout, connTimeout);
047    }
048
049    /**
050     * Creates a public key resolver for the supplied issuer.
051     *
052     * @param issuer The issuer's reference name
053     * @param key Key to use for this issuer
054     * @return a configured and usable PublicKeyOpenIdResolverImpl
055     */
056    public OpenIdResolver createPublicKeyResolver(String issuer, PublicKey key) {
057        return new PublicKeyOpenIdResolverImpl(issuer, key);
058    }
059
060    /**
061     * Creates a shared secret (HMAC) key resolver for the supplied issuer.
062     *
063     * @param issuer The issuer's reference name
064     * @param sharedSecret SharedSecret for which to use with HMAC
065     * @return a configured and usable SharedSecretOpenIdResolverImpl
066     */
067    public OpenIdResolver createSharedSecretResolver(String issuer, String sharedSecret) {
068        return new SharedSecretOpenIdResolverImpl(issuer, sharedSecret);
069    }
070
071    /**
072     * Creates a public key resolver for the supplied issuer using
073     * keys supplied at the JWK Set URL.
074     *
075     * @param issuer The issuer's reference name
076     * @param jwkUrl From which to read the JWK Set
077     * @param readTimeout read timeout setting for HTTP connections
078     * @param connTimeout connection timeout setting for HTTP connections
079     * @return a configured and usable JWKOpenIdResolverImpl
080     * @throws FailedToLoadJWKException If there were problems reading or configuring data from the URL
081     */
082    public OpenIdResolver createJWKResolver(String issuer, URL jwkUrl, int readTimeout, int connTimeout)
083            throws FailedToLoadJWKException {
084        return new JWKOpenIdResolverImpl(issuer, jwkUrl, readTimeout, connTimeout);
085    }
086
087    /**
088     * Creates a public key resolver for the supplied issuer using keys supplied
089     * at the .well-known open ID configuration URL.
090     *
091     * @param configUrl Location of the .well-known Open ID Connect config
092     * @return a configured and usable JWKOpenIdResolverImpl
093     * @throws FailedToLoadJWKException If there were problems reading or configuring data from the URL
094     */
095    public OpenIdResolver createFromOpenIDConfigUrl(URL configUrl) throws FailedToLoadJWKException {
096        return openIdConfigurationFactory.build(configUrl);
097    }
098}