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