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-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.builders;
018
019import java.security.Key;
020
021import org.forgerock.json.jose.common.JwtReconstruction;
022import org.forgerock.json.jose.jws.handlers.NOPSigningHandler;
023import org.forgerock.json.jose.jws.handlers.SigningHandler;
024import org.forgerock.json.jose.jwt.Jwt;
025
026/**
027 * A factory for getting builders for plaintext, signed and encrypted JWTs and reconstructing JWT strings back into
028 * their relevant JWT objects.
029 *
030 * @since 2.0.0
031 */
032public class JwtBuilderFactory {
033
034    /**
035     * Creates a builder for building a plaintext JWT into base64url UTF-8 encoded JWT string.
036     *
037     * @return The JwtBuilder instance that will build the plaintext JWT.
038     */
039    public SignedJwtBuilderImpl jwt() {
040        return new SignedJwtBuilderImpl(new NOPSigningHandler());
041    }
042
043    /**
044     * Creates a builder for building a signed JWT into a base64url UTF-8 encoded JWT string.
045     *
046     * @param signingHandler The SigningHandler instance used to sign the JWS.
047     * @return The JwtBuilder instance that will build the signed JWT.
048     */
049    public SignedJwtBuilderImpl jws(SigningHandler signingHandler) {
050        return new SignedJwtBuilderImpl(signingHandler);
051    }
052
053    /**
054     * Creates a builder for building an encrypted JWT into a base64url UTF-8 encoded JWT string.
055     *
056     * @param publicKey The public key that will be used to encrypted the JWT.
057     * @return The JwtBuilder instance that will build the encrypted JWT.
058     */
059    public EncryptedJwtBuilder jwe(Key publicKey) {
060        return new EncryptedJwtBuilder(publicKey);
061    }
062
063    /**
064     * Creates a builder for building a JWT Claims Set to be used in the building of JWTs.
065     *
066     * @return The JwtClaimsSetBuilder instance that will build the claims set.
067     */
068    public JwtClaimsSetBuilder claims() {
069        return new JwtClaimsSetBuilder();
070    }
071
072    /**
073     * Reconstructs the given JWT string into a JWT object of the specified type.
074     *
075     * @param jwtString The JWT string.
076     * @param jwtClass The JWT class to reconstruct the JWT string to.
077     * @param <T> The type of JWT the JWT string represents.
078     * @return The reconstructed JWT object.
079     */
080    public <T extends Jwt> T reconstruct(String jwtString, Class<T> jwtClass) {
081        return new JwtReconstruction().reconstructJwt(jwtString, jwtClass);
082    }
083}