001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2015 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.provider;
015
016import java.net.URI;
017import java.net.URL;
018import java.net.http.HttpClient;
019import java.util.Optional;
020import java.util.ServiceLoader;
021
022import edu.umd.cs.findbugs.annotations.Nullable;
023import org.shredzone.acme4j.Login;
024import org.shredzone.acme4j.Session;
025import org.shredzone.acme4j.challenge.Challenge;
026import org.shredzone.acme4j.connector.Connection;
027import org.shredzone.acme4j.connector.NetworkSettings;
028import org.shredzone.acme4j.exception.AcmeException;
029import org.shredzone.acme4j.toolbox.JSON;
030
031/**
032 * An {@link AcmeProvider} provides methods to be used for communicating with the ACME
033 * server. Implementations handle individual features of each ACME server.
034 * <p>
035 * Provider implementations must be registered with Java's {@link ServiceLoader}.
036 */
037public interface AcmeProvider {
038
039    /**
040     * Checks if this provider accepts the given server URI.
041     *
042     * @param serverUri
043     *            Server URI to test
044     * @return {@code true} if this provider accepts the server URI, {@code false}
045     *         otherwise
046     */
047    boolean accepts(URI serverUri);
048
049    /**
050     * Resolves the server URI and returns the matching directory URL.
051     *
052     * @param serverUri
053     *            Server {@link URI}
054     * @return Resolved directory {@link URL}
055     * @throws IllegalArgumentException
056     *             if the server {@link URI} is not accepted
057     */
058    URL resolve(URI serverUri);
059
060    /**
061     * Creates an {@link HttpClient} instance configured with the given network settings.
062     * <p>
063     * The default implementation creates a standard HttpClient with the network settings.
064     * Subclasses can override this method to create a customized HttpClient, for example
065     * to configure SSL context or other provider-specific requirements.
066     *
067     * @param networkSettings The network settings to use
068     * @return {@link HttpClient} instance
069     * @since 4.0.0
070     */
071    default HttpClient createHttpClient(NetworkSettings networkSettings) {
072        var builder = HttpClient.newBuilder()
073                .followRedirects(HttpClient.Redirect.NORMAL)
074                .connectTimeout(networkSettings.getTimeout())
075                .proxy(networkSettings.getProxySelector());
076
077        if (networkSettings.getAuthenticator() != null) {
078            builder.authenticator(networkSettings.getAuthenticator());
079        }
080
081        return builder.build();
082    }
083
084    /**
085     * Creates a {@link Connection} for communication with the ACME server.
086     *
087     * @param serverUri
088     *         Server {@link URI}
089     * @param networkSettings
090     *         {@link NetworkSettings} to be used for the connection
091     * @param httpClient
092     *         {@link HttpClient} to be used for HTTP requests
093     * @return {@link Connection} that was generated
094     * @since 4.0.0
095     */
096    Connection connect(URI serverUri, NetworkSettings networkSettings, HttpClient httpClient);
097
098    /**
099     * Returns the provider's directory. The structure must contain resource URLs, and may
100     * optionally contain metadata.
101     * <p>
102     * The default implementation resolves the server URI and fetches the directory via
103     * HTTP request. Subclasses may override this method, e.g. if the directory is static.
104     *
105     * @param session
106     *            {@link Session} to be used
107     * @param serverUri
108     *            Server {@link URI}
109     * @return Directory data, as JSON object, or {@code null} if the directory has not
110     * been changed since the last request.
111     */
112    @Nullable
113    JSON directory(Session session, URI serverUri) throws AcmeException;
114
115    /**
116     * Creates a {@link Challenge} instance for the given challenge data.
117     *
118     * @param login
119     *            {@link Login} to bind the challenge to
120     * @param data
121     *            Challenge {@link JSON} data
122     * @return {@link Challenge} instance, or {@code null} if this provider is unable to
123     *         generate a matching {@link Challenge} instance.
124     */
125    @Nullable
126    Challenge createChallenge(Login login, JSON data);
127
128    /**
129     * Returns a proposal for the EAB MAC algorithm to be used. Only set if the CA
130     * requires External Account Binding and the MAC algorithm cannot be correctly derived
131     * from the MAC key. Empty otherwise.
132     *
133     * @return Proposed MAC algorithm to be used for EAB, or empty for the default
134     * behavior.
135     * @since 3.5.0
136     */
137    default Optional<String> getProposedEabMacAlgorithm() {
138        return Optional.empty();
139    }
140
141}