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.security.Key;
020
021import org.forgerock.json.jose.jws.JwsHeader;
022import org.forgerock.json.jose.jws.SignedJwt;
023import org.forgerock.json.jose.jws.handlers.SigningHandler;
024import org.forgerock.json.jose.jwt.JwtClaimsSet;
025
026/**
027 * An implementation of a JwtBuilder that can build a JWT and sign it, resulting in a SignedJwt object.
028 *
029 * @since 2.0.0
030 */
031public class SignedJwtBuilderImpl extends AbstractJwtBuilder implements SignedJwtBuilder {
032
033    private final SigningHandler signingHandler;
034
035    /**
036     * Constructs a new SignedJwtBuilderImpl that will use the given private key to sign the JWT.
037     *
038     * @param signingHandler The SigningHandler instance used to sign the JWS.
039     */
040    public SignedJwtBuilderImpl(SigningHandler signingHandler) {
041        this.signingHandler = signingHandler;
042    }
043
044    /**
045     * Gets the JwsHeaderBuilder that this JwtBuilder will use to build the JWS' header parameters.
046     *
047     * @return The JwsHeaderBuilder instance.
048     */
049    @Override
050    public JwsHeaderBuilder headers() {
051        setJwtHeaderBuilder(new JwsHeaderBuilder(this));
052        return (JwsHeaderBuilder) getHeaderBuilder();
053    }
054
055    /**
056     * Sets the JwtClaimsSet for this JwtBuilder.
057     *
058     * @param claimsSet {@inheritDoc}
059     * @return This SignedJwtBuilderImpl.
060     */
061    @Override
062    public SignedJwtBuilderImpl claims(JwtClaimsSet claimsSet) {
063        return (SignedJwtBuilderImpl) super.claims(claimsSet);
064    }
065
066    /**
067     * Wraps the signed JWT in an outer encrypted JWE envelope.
068     *
069     * @param encryptionKey the key to use for encryption. This should either be a symmetric secret key or a public key.
070     * @return the nested encrypted signed JWT builder.
071     */
072    public SignedThenEncryptedJwtBuilder encrypt(Key encryptionKey) {
073        return new SignedThenEncryptedJwtBuilder(this, encryptionKey);
074    }
075
076    @Override
077    public SignedJwt asJwt() {
078        JwtHeaderBuilder<?, ?> headerBuilder = getHeaderBuilder();
079        JwsHeader header;
080        if (headerBuilder == null) {
081            header = new JwsHeader();
082        } else {
083            header = (JwsHeader) getHeaderBuilder().build();
084        }
085        JwtClaimsSet claimsSet = getClaimsSet();
086        if (claimsSet == null) {
087            claimsSet = new JwtClaimsSet();
088        }
089        return new SignedJwt(header, claimsSet, signingHandler);
090    }
091
092    /**
093     * Builds the JWS into a <code>String</code> by calling the <tt>build</tt> method on the JWS object.
094     * <p>
095     * @see org.forgerock.json.jose.jws.SignedJwt#build()
096     *
097     * @return The base64url encoded UTF-8 parts of the JWS.
098     */
099    @Override
100    public String build() {
101        return asJwt().build();
102    }
103}