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