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 2013-2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.builders;
018
019import java.net.URL;
020import java.util.List;
021
022import org.forgerock.json.jose.jwe.CompressionAlgorithm;
023import org.forgerock.json.jose.jwk.JWK;
024import org.forgerock.json.jose.jws.JwtSecureHeader;
025
026/**
027 * A base implementation of a JWT header builder, for the common security header parameters shared by the JWS and JWE
028 * headers, that provides a fluent builder pattern to creating JWT headers.
029 * <p>
030 * See {@link org.forgerock.json.jose.jws.JwtSecureHeader} for information on the JwtSecureHeader object that this
031 * builder creates.
032 *
033 * @param <T> the type of JwtBuilder that parents this JwtHeaderBuilder.
034 * @param <B> the type of this JwtHeaderBuilder
035 *
036 * @since 2.0.0
037 */
038public abstract class JwtSecureHeaderBuilder<T extends JwtBuilder, B extends JwtSecureHeaderBuilder<T, B>>
039        extends JwtHeaderBuilder<T, B> {
040
041    /**
042     * Constructs a new JwtSecureHeaderBuilder, parented by the given JwtBuilder.
043     *
044     * @param jwtBuilder The JwtBuilder instance that this JwtSecureHeaderBuilder is a child of.
045     */
046    public JwtSecureHeaderBuilder(T jwtBuilder) {
047        super(jwtBuilder);
048    }
049
050    /**
051     * Sets the JWK Set URL header parameter for this JWS.
052     * <p>
053     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setJwkSetUrl(java.net.URL)
054     *
055     * @param jku The JWK Set URL.
056     * @return This JwtSecureHeaderBuilder.
057     */
058    @SuppressWarnings("unchecked")
059    public B jku(URL jku) {
060        header("jku", jku);
061        return (B) this;
062    }
063
064    /**
065     * Sets the JSON Web Key header parameter for this JWS.
066     * <p>
067     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setJsonWebKey(org.forgerock.json.jose.jwk.JWK)
068     *
069     * @param jwk The JSON Web Key.
070     * @return This JwtSecureHeaderBuilder.
071     */
072    @SuppressWarnings("unchecked")
073    public B jwk(JWK jwk) {
074        header("jwk", jwk);
075        return (B) this;
076    }
077
078    /**
079     * Sets the X.509 URL header parameter for this JWS.
080     * <p>
081     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509Url(java.net.URL)
082     *
083     * @param x5u THe X.509 URL.
084     * @return This JwtSecureHeaderBuilder.
085     */
086    @SuppressWarnings("unchecked")
087    public B x5u(URL x5u) {
088        header("x5u", x5u);
089        return (B) this;
090    }
091
092    /**
093     * Sets the X.509 Certificate Thumbprint header parameter for this JWS.
094     * <p>
095     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509CertificateThumbprint(String)
096     *
097     * @param x5t The X.509 Certificate Thumbprint.
098     * @return This JwtSecureHeaderBuilder.
099     */
100    @SuppressWarnings("unchecked")
101    public B x5t(String x5t) {
102        header("x5t", x5t);
103        return (B) this;
104    }
105
106    /**
107     * Sets the X.509 Certificate Chain header parameter for this JWS.
108     * <p>
109     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509CertificateChain(java.util.List)
110     *
111     * @param x5c The X.509 Certificate Chain.
112     * @return This JwtSecureHeaderBuilder.
113     */
114    @SuppressWarnings("unchecked")
115    public B x5c(List<String> x5c) {
116        header("x5c", x5c);
117        return (B) this;
118    }
119
120    /**
121     * Sets the Key ID header parameter for this JWS.
122     * <p>
123     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setKeyId(String)
124     *
125     * @param kid The Key ID.
126     * @return This JwtSecureHeaderBuilder.
127     */
128    @SuppressWarnings("unchecked")
129    public B kid(String kid) {
130        header("kid", kid);
131        return (B) this;
132    }
133
134    /**
135     * Sets the content type header parameter for this JWS.
136     * <p>
137     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setContentType(String)
138     *
139     * @param cty The content type of the JWS payload.
140     * @return This JwtSecureHeaderBuilder.
141     */
142    @SuppressWarnings("unchecked")
143    public B cty(String cty) {
144        header("cty", cty);
145        return (B) this;
146    }
147
148    /**
149     * Sets the critical header parameters for this JWS.
150     * <p>
151     * @see org.forgerock.json.jose.jws.JwtSecureHeader#setCriticalHeaders(java.util.List)
152     *
153     * @param crit A List of the JWS critical parameters.
154     * @return This JwtSecureHeaderBuilder.
155     */
156    @SuppressWarnings("unchecked")
157    public B crit(List<String> crit) {
158        header("crit", crit);
159        return (B) this;
160    }
161
162    /**
163     * Sets the Compression Algorithm header parameter for this JWE.
164     * <p>
165     * @see JwtSecureHeader#setCompressionAlgorithm(CompressionAlgorithm)
166     *
167     * @param zip The Compression Algorithm.
168     * @return This JweHeaderBuilder.
169     */
170    @SuppressWarnings("unchecked")
171    public B zip(CompressionAlgorithm zip) {
172        header("zip", zip.toString());
173        return (B) this;
174    }
175}