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.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 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 @SuppressWarnings("unchecked") 052 public JweHeaderBuilder<? extends EncryptedJwtBuilder> headers() { 053 setJwtHeaderBuilder(new JweHeaderBuilder<>(this)); 054 return (JweHeaderBuilder<? extends EncryptedJwtBuilder>) getHeaderBuilder(); 055 } 056 057 /** 058 * Sets the JwtClaimsSet for this JwtBuilder. 059 * 060 * @param claimsSet {@inheritDoc} 061 * @return This EncryptedJwtBuilder. 062 */ 063 @Override 064 public EncryptedJwtBuilder claims(JwtClaimsSet claimsSet) { 065 return (EncryptedJwtBuilder) super.claims(claimsSet); 066 } 067 068 /** 069 * Returns a SignedEncryptedJwtBuilder that will build a signed JWT with this builder's encrypted JWT as its 070 * payload. 071 * 072 * @param signingHandler The SigningHandler instance used to sign the JWS. 073 * @param jwsAlgorithm The JwsAlgorithm to use when signing the JWT. 074 * @return The SignedEncryptedJwtBuilder instance. 075 * @deprecated Use {@link #signedWith(SigningHandler, JwsAlgorithm)} instead. 076 */ 077 @Deprecated 078 public SignedEncryptedJwtBuilder sign(SigningHandler signingHandler, JwsAlgorithm jwsAlgorithm) { 079 return new SignedEncryptedJwtBuilder(this, signingHandler, jwsAlgorithm); 080 } 081 082 /** 083 * Returns an {@link EncryptedThenSignedJwtBuilder} that will build a signed JWT with this builder's encrypted JWT 084 * as its payload. 085 * 086 * @param signingHandler The SigningHandler instance used to sign the JWS. 087 * @param jwsAlgorithm The JwsAlgorithm to use when signing the JWT. 088 * @return The EncryptedThenSignedJwtBuilder instance. 089 */ 090 public EncryptedThenSignedJwtBuilder signedWith(SigningHandler signingHandler, JwsAlgorithm jwsAlgorithm) { 091 return new EncryptedThenSignedJwtBuilder(this, signingHandler, jwsAlgorithm); 092 } 093 094 @Override 095 public EncryptedJwt asJwt() { 096 JwtHeaderBuilder<?, ?> headerBuilder = getHeaderBuilder(); 097 JweHeader header; 098 if (headerBuilder == null) { 099 header = new JweHeader(); 100 } else { 101 header = (JweHeader) getHeaderBuilder().build(); 102 } 103 JwtClaimsSet claimsSet = getClaimsSet(); 104 if (claimsSet == null) { 105 claimsSet = new JwtClaimsSet(); 106 } 107 return new EncryptedJwt(header, claimsSet, publicKey); 108 } 109 110 /** 111 * Builds the JWE into a <code>String</code> by calling the <tt>build</tt> method on the JWE object. 112 * <p> 113 * @see org.forgerock.json.jose.jwe.EncryptedJwt#build() 114 * 115 * @return The base64url encoded UTF-8 parts of the JWE. 116 */ 117 @Override 118 public String build() { 119 return asJwt().build(); 120 } 121}