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.net.URI; 020import java.util.Date; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.forgerock.json.jose.jwt.JwtClaimsSet; 026import org.forgerock.json.jose.jwt.JwtType; 027 028/** 029 * An implementation of a JWT Claims Set builder that provides a fluent builder pattern to creating JWT Claims Sets. 030 * <p> 031 * See {@link org.forgerock.json.jose.jwt.JwtClaimsSet} for information on the JwtClaimsSet object that this builder 032 * creates. 033 * 034 * @since 2.0.0 035 */ 036public class JwtClaimsSetBuilder { 037 038 private final Map<String, Object> claims = new HashMap<>(); 039 040 /** 041 * Adds a custom claim to the JWT Claims Set. 042 * <p> 043 * @see JwtClaimsSet#setClaim(String, Object) 044 * 045 * @param key The claim name. 046 * @param claim The claim value. 047 * @return This JwtClaimsSetBuilder. 048 */ 049 public JwtClaimsSetBuilder claim(String key, Object claim) { 050 claims.put(key, claim); 051 return this; 052 } 053 054 /** 055 * Sets all of the claims the JWT Claims Set with the values contained in the specified map. 056 * <p> 057 * @see JwtClaimsSet#setClaims(java.util.Map) 058 * 059 * @param claims The Map to use to set the claims. 060 * @return This JwtClaimsSetBuilder. 061 */ 062 public JwtClaimsSetBuilder claims(Map<String, Object> claims) { 063 this.claims.putAll(claims); 064 return this; 065 } 066 067 /** 068 * Sets the type of the contents of the Claims Set. 069 * <p> 070 * @see JwtClaimsSet#getType() 071 * 072 * @param typ The Claims Set content type. 073 * @return This JwtClaimsSetBuilder. 074 */ 075 public JwtClaimsSetBuilder typ(JwtType typ) { 076 return claim("typ", typ); 077 } 078 079 /** 080 * Sets the unique ID of the JWT. 081 * <p> 082 * @see JwtClaimsSet#setJwtId(String) 083 * 084 * @param jti The JWT's ID. 085 * @return This JwtClaimsSetBuilder. 086 */ 087 public JwtClaimsSetBuilder jti(String jti) { 088 return claim("jti", jti); 089 } 090 091 /** 092 * Sets the issuer this JWT was issued by. 093 * <p> 094 * @see JwtClaimsSet#setIssuer(String) 095 * 096 * @param iss The JWT's issuer. 097 * @return This JwtClaimsSetBuilder. 098 */ 099 public JwtClaimsSetBuilder iss(String iss) { 100 return claim("iss", iss); 101 } 102 103 /** 104 * Sets the issuer this JWT was issued by. 105 * <p> 106 * @see JwtClaimsSet#setIssuer(java.net.URI) 107 * 108 * @param iss The JWT's issuer. 109 * @return This JwtClaimsSetBuilder. 110 */ 111 public JwtClaimsSetBuilder iss(URI iss) { 112 return claim("iss", iss); 113 } 114 115 /** 116 * Sets the subject this JWT is issued to. 117 * <p> 118 * @see JwtClaimsSet#setSubject(String) 119 * 120 * @param sub The JWT's subject. 121 * @return This JwtClaimsSetBuilder. 122 */ 123 public JwtClaimsSetBuilder sub(String sub) { 124 return claim("sub", sub); 125 } 126 127 /** 128 * Sets the subject this JWT is issued to. 129 * <p> 130 * @see JwtClaimsSet#setSubject(java.net.URI) 131 * 132 * @param sub The JWT's subject. 133 * @return This JwtClaimsSetBuilder. 134 */ 135 public JwtClaimsSetBuilder sub(URI sub) { 136 return claim("sub", sub); 137 } 138 139 /** 140 * Sets the JWT's intended audience list in the Claims Set. 141 * <p> 142 * @see JwtClaimsSet#addAudience(String) 143 * 144 * @param aud The JWT's audience. 145 * @return This JwtClaimsSetBuilder. 146 */ 147 public JwtClaimsSetBuilder aud(List<String> aud) { 148 return claim("aud", aud); 149 } 150 151 /** 152 * Sets the time the JWT was issued at, in the Claims Set. 153 * <p> 154 * @see JwtClaimsSet#setIssuedAtTime(java.util.Date) 155 * 156 * @param iat The JWT's issued at time. 157 * @return This JwtClaimsSetBuilder. 158 */ 159 public JwtClaimsSetBuilder iat(Date iat) { 160 return claim("iat", iat); 161 } 162 163 /** 164 * Sets the time the JWT is not allowed to be processed before, in the Claims Set. 165 * <p> 166 * @see JwtClaimsSet#setNotBeforeTime(java.util.Date) 167 * 168 * @param nbf The JWT's not before time. 169 * @return This JwtClaimsSetBuilder. 170 */ 171 public JwtClaimsSetBuilder nbf(Date nbf) { 172 return claim("nbf", nbf); 173 } 174 175 /** 176 * Sets the expiration time of the JWT in the Claims Set. 177 * <p> 178 * @see JwtClaimsSet#setExpirationTime(java.util.Date) 179 * 180 * @param exp The JWT's expiration time. 181 * @return This JwtClaimsSetBuilder. 182 */ 183 public JwtClaimsSetBuilder exp(Date exp) { 184 return claim("exp", exp); 185 } 186 187 /** 188 * Creates a JwtClaimsSet instance from the claims set in this builder. 189 * 190 * @return A JwtClaimsSet instance. 191 */ 192 public JwtClaimsSet build() { 193 return new JwtClaimsSet(claims); 194 } 195}