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.connector;
015
016import java.net.URL;
017import java.security.KeyPair;
018import java.security.cert.X509Certificate;
019import java.time.Instant;
020import java.time.ZonedDateTime;
021import java.util.Collection;
022import java.util.List;
023import java.util.Optional;
024
025import edu.umd.cs.findbugs.annotations.Nullable;
026import org.shredzone.acme4j.Login;
027import org.shredzone.acme4j.Session;
028import org.shredzone.acme4j.exception.AcmeException;
029import org.shredzone.acme4j.toolbox.JSON;
030import org.shredzone.acme4j.toolbox.JSONBuilder;
031
032/**
033 * Connects to the ACME server and offers different methods for invoking the API.
034 * <p>
035 * The actual way of communicating with the ACME server is intentionally left open.
036 * Implementations could use other means than HTTP, or could mock the communication for
037 * unit testing.
038 */
039public interface Connection extends AutoCloseable {
040
041    /**
042     * Resets the session nonce, by fetching a new one.
043     *
044     * @param session
045     *            {@link Session} instance to fetch a nonce for
046     */
047    void resetNonce(Session session) throws AcmeException;
048
049    /**
050     * Sends a simple GET request.
051     * <p>
052     * If the response code was not HTTP status 200, an {@link AcmeException} matching
053     * the error is raised.
054     *
055     * @param url
056     *            {@link URL} to send the request to.
057     * @param session
058     *            {@link Session} instance to be used for tracking
059     * @param ifModifiedSince
060     *            {@link ZonedDateTime} to be sent as "If-Modified-Since" header, or
061     *            {@code null} if this header is not to be used
062     * @return HTTP status that was returned
063     */
064    int sendRequest(URL url, Session session, @Nullable ZonedDateTime ifModifiedSince)
065            throws AcmeException;
066
067    /**
068     * Sends a signed POST-as-GET request for a certificate resource. Requires a
069     * {@link Login} for the session and {@link KeyPair}. The {@link Login} account
070     * location is sent in a "kid" protected header.
071     * <p>
072     * If the server does not return a 200 class status code, an {@link AcmeException} is
073     * raised matching the error.
074     *
075     * @param url
076     *            {@link URL} to send the request to.
077     * @param login
078     *            {@link Login} instance to be used for signing and tracking.
079     * @return HTTP 200 class status that was returned
080     */
081    int sendCertificateRequest(URL url, Login login) throws AcmeException;
082
083    /**
084     * Sends a signed POST-as-GET request. Requires a {@link Login} for the session and
085     * {@link KeyPair}. The {@link Login} account location is sent in a "kid" protected
086     * header.
087     * <p>
088     * If the server does not return a 200 class status code, an {@link AcmeException} is
089     * raised matching the error.
090     *
091     * @param url
092     *            {@link URL} to send the request to.
093     * @param login
094     *            {@link Login} instance to be used for signing and tracking.
095     * @return HTTP 200 class status that was returned
096     */
097    int sendSignedPostAsGetRequest(URL url, Login login) throws AcmeException;
098
099    /**
100     * Sends a signed POST request. Requires a {@link Login} for the session and
101     * {@link KeyPair}. The {@link Login} account location is sent in a "kid" protected
102     * header.
103     * <p>
104     * If the server does not return a 200 class status code, an {@link AcmeException} is
105     * raised matching the error.
106     *
107     * @param url
108     *            {@link URL} to send the request to.
109     * @param claims
110     *            {@link JSONBuilder} containing claims.
111     * @param login
112     *            {@link Login} instance to be used for signing and tracking.
113     * @return HTTP 200 class status that was returned
114     */
115    int sendSignedRequest(URL url, JSONBuilder claims, Login login) throws AcmeException;
116
117    /**
118     * Sends a signed POST request. Only requires a {@link Session}. The {@link KeyPair}
119     * is sent in a "jwk" protected header field.
120     * <p>
121     * If the server does not return a 200 class status code, an {@link AcmeException} is
122     * raised matching the error.
123     *
124     * @param url
125     *            {@link URL} to send the request to.
126     * @param claims
127     *            {@link JSONBuilder} containing claims.
128     * @param session
129     *            {@link Session} instance to be used for tracking.
130     * @param keypair
131     *            {@link KeyPair} to be used for signing.
132     * @return HTTP 200 class status that was returned
133     */
134    int sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair)
135                throws AcmeException;
136
137    /**
138     * Reads a server response as JSON object.
139     *
140     * @return The JSON response.
141     */
142    JSON readJsonResponse() throws AcmeException;
143
144    /**
145     * Reads a certificate and its chain of issuers.
146     *
147     * @return List of X.509 certificate and chain that was read.
148     */
149    List<X509Certificate> readCertificates() throws AcmeException;
150
151    /**
152     * Returns the Retry-After header if present.
153     *
154     * @since 3.0.0
155     */
156    Optional<Instant> getRetryAfter();
157
158    /**
159     * Gets the nonce from the nonce header.
160     *
161     * @return Base64 encoded nonce, or empty if no nonce header was set
162     */
163    Optional<String> getNonce();
164
165    /**
166     * Gets a location from the {@code Location} header.
167     * <p>
168     * Relative links are resolved against the last request's URL.
169     *
170     * @return Location {@link URL}
171     * @throws org.shredzone.acme4j.exception.AcmeProtocolException if the location
172     * header is missing
173     */
174    URL getLocation();
175
176    /**
177     * Returns the content of the last-modified header, if present.
178     *
179     * @return Date in the Last-Modified header, or empty if the server did not provide
180     * this information.
181     * @since 2.10
182     */
183    Optional<ZonedDateTime> getLastModified();
184
185    /**
186     * Returns the expiration date of the resource, if present.
187     *
188     * @return Expiration date, either from the Cache-Control or Expires header. If empty,
189     * the server did not provide an expiration date, or forbid caching.
190     * @since 2.10
191     */
192    Optional<ZonedDateTime> getExpiration();
193
194    /**
195     * Gets one or more relation links from the header. The result is expected to be a
196     * URL.
197     * <p>
198     * Relative links are resolved against the last request's URL.
199     *
200     * @param relation
201     *         Link relation
202     * @return Collection of links. Empty if there was no such relation.
203     */
204    Collection<URL> getLinks(String relation);
205
206    /**
207     * Closes the {@link Connection}, releasing all resources.
208     */
209    @Override
210    void close();
211
212}