|
| 1 | +package com.auth0.jwt.creators; |
| 2 | + |
| 3 | +import com.auth0.jwt.algorithms.Algorithm; |
| 4 | +import com.auth0.jwt.exceptions.JWTCreationException; |
| 5 | +import com.auth0.jwt.impl.PublicClaims; |
| 6 | +import com.auth0.jwt.jwts.JWT; |
| 7 | + |
| 8 | +import java.util.Date; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +/** |
| 14 | + * The AccessJwtCreator class holds the sign method to generate a complete Access JWT (with Signature) from a given Header and Payload content. |
| 15 | + */ |
| 16 | +public class AccessJwtCreator { |
| 17 | + |
| 18 | + protected JWTCreator.Builder jwt; |
| 19 | + protected HashMap<String, Boolean> addedClaims; |
| 20 | + protected Set<String> publicClaims; |
| 21 | + |
| 22 | + public AccessJwtCreator() { |
| 23 | + jwt = JWT.create(); |
| 24 | + addedClaims = new HashMap<String, Boolean>() {{ |
| 25 | + put("Issuer", false); |
| 26 | + put("Subject", false); |
| 27 | + put("Iat", false); |
| 28 | + }}; |
| 29 | + publicClaims = new HashSet<String>() {{ |
| 30 | + add(PublicClaims.ISSUER); |
| 31 | + add(PublicClaims.SUBJECT); |
| 32 | + add(PublicClaims.EXPIRES_AT); |
| 33 | + add(PublicClaims.NOT_BEFORE); |
| 34 | + add(PublicClaims.ISSUED_AT); |
| 35 | + add(PublicClaims.JWT_ID); |
| 36 | + add(PublicClaims.AUDIENCE); |
| 37 | + }}; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add a specific Issuer ("issuer") claim to the Payload. |
| 42 | + * Allows for multiple issuers |
| 43 | + * |
| 44 | + * @param issuer the Issuer value. |
| 45 | + * @return this same Builder instance. |
| 46 | + */ |
| 47 | + public AccessJwtCreator withIssuer(String... issuer) { |
| 48 | + jwt.withIssuer(issuer); |
| 49 | + addedClaims.put("Issuer", true); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Add a specific Subject ("subject") claim to the Payload. |
| 55 | + * Allows for multiple subjects |
| 56 | + * |
| 57 | + * @param subject the Subject value. |
| 58 | + * @return this same Builder instance. |
| 59 | + */ |
| 60 | + public AccessJwtCreator withSubject(String... subject) { |
| 61 | + jwt.withSubject(subject); |
| 62 | + addedClaims.put("Subject", true); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Add a specific Audience ("audience") claim to the Payload. |
| 68 | + * Allows for multiple audience |
| 69 | + * |
| 70 | + * @param audience the Audience value. |
| 71 | + * @return this same Builder instance. |
| 72 | + */ |
| 73 | + public AccessJwtCreator withAudience(String... audience) { |
| 74 | + jwt.withAudience(audience); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Add a specific Issued At ("iat") claim to the Payload. |
| 80 | + * |
| 81 | + * @param iat the Issued At value. |
| 82 | + * @return this same Builder instance. |
| 83 | + */ |
| 84 | + public AccessJwtCreator withIat(Date iat) { |
| 85 | + jwt.withIssuedAt(iat); |
| 86 | + addedClaims.put("Iat", true); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Add a specific Expires At ("exp") claim to the Payload. |
| 92 | + * |
| 93 | + * @param exp the Expires At value. |
| 94 | + * @return this same Builder instance. |
| 95 | + */ |
| 96 | + public AccessJwtCreator withExp(Date exp) { |
| 97 | + jwt.withExpiresAt(exp); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Require a specific Claim value. |
| 103 | + * |
| 104 | + * @param name the Claim's name. |
| 105 | + * @param value the Claim's value. |
| 106 | + * @return this same Verification instance. |
| 107 | + * @throws IllegalArgumentException if the name is null. |
| 108 | + */ |
| 109 | + public AccessJwtCreator withNonStandardClaim(String name, String value) { |
| 110 | + jwt.withNonStandardClaim(name, value); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Add a custom Claim value. |
| 116 | + * |
| 117 | + * @param name the Claim's name. |
| 118 | + * @param value the Claim's value. |
| 119 | + * @return this same Builder instance. |
| 120 | + * @throws IllegalArgumentException if the name is null. |
| 121 | + */ |
| 122 | + public AccessJwtCreator withNonStandardClaim(String name, Boolean value) throws IllegalArgumentException { |
| 123 | + jwt.withNonStandardClaim(name, value); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Add a custom Claim value. |
| 129 | + * |
| 130 | + * @param name the Claim's name. |
| 131 | + * @param value the Claim's value. |
| 132 | + * @return this same Builder instance. |
| 133 | + * @throws IllegalArgumentException if the name is null. |
| 134 | + */ |
| 135 | + public AccessJwtCreator withNonStandardClaim(String name, Integer value) throws IllegalArgumentException { |
| 136 | + jwt.withNonStandardClaim(name, value); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Add a custom Claim value. |
| 142 | + * |
| 143 | + * @param name the Claim's name. |
| 144 | + * @param value the Claim's value. |
| 145 | + * @return this same Builder instance. |
| 146 | + * @throws IllegalArgumentException if the name is null. |
| 147 | + */ |
| 148 | + public AccessJwtCreator withNonStandardClaim(String name, Long value) throws IllegalArgumentException { |
| 149 | + jwt.withNonStandardClaim(name, value); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Add a custom Claim value. |
| 155 | + * |
| 156 | + * @param name the Claim's name. |
| 157 | + * @param value the Claim's value. |
| 158 | + * @return this same Builder instance. |
| 159 | + * @throws IllegalArgumentException if the name is null. |
| 160 | + */ |
| 161 | + public AccessJwtCreator withNonStandardClaim(String name, Double value) throws IllegalArgumentException { |
| 162 | + jwt.withNonStandardClaim(name, value); |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Add a custom Claim value. |
| 168 | + * |
| 169 | + * @param name the Claim's name. |
| 170 | + * @param value the Claim's value. |
| 171 | + * @return this same Builder instance. |
| 172 | + * @throws IllegalArgumentException if the name is null. |
| 173 | + */ |
| 174 | + public AccessJwtCreator withNonStandardClaim(String name, Date value) throws IllegalArgumentException { |
| 175 | + jwt.withNonStandardClaim(name, value); |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Require a specific Array Claim to contain at least the given items. |
| 181 | + * |
| 182 | + * @param name the Claim's name. |
| 183 | + * @param items the items the Claim must contain. |
| 184 | + * @return this same Verification instance. |
| 185 | + * @throws IllegalArgumentException if the name is null. |
| 186 | + */ |
| 187 | + public AccessJwtCreator withArrayClaim(String name, String... items) throws IllegalArgumentException { |
| 188 | + jwt.withArrayClaim(name, items); |
| 189 | + if(publicClaims.contains(name)) |
| 190 | + addedClaims.put(name, true); |
| 191 | + return this; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Developer explicitly specifies whether they want to accept |
| 196 | + * NONE algorithms or not. |
| 197 | + * |
| 198 | + * @param isNoneAlgorithmAllowed |
| 199 | + * @return |
| 200 | + */ |
| 201 | + public AccessJwtCreator setIsNoneAlgorithmAllowed(boolean isNoneAlgorithmAllowed) { |
| 202 | + jwt.setIsNoneAlgorithmAllowed(isNoneAlgorithmAllowed); |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Creates a new JWT and signs it with the given algorithm. |
| 208 | + * |
| 209 | + * @param algorithm used to sign the JWT |
| 210 | + * @return a new JWT token |
| 211 | + * @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in |
| 212 | + * @throws IllegalArgumentException if the provided algorithm is null. |
| 213 | + * @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key. |
| 214 | + */ |
| 215 | + public String sign(Algorithm algorithm) throws Exception { |
| 216 | + if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) { |
| 217 | + throw new IllegalAccessException("None algorithm isn't allowed"); |
| 218 | + } |
| 219 | + String JWS = jwt.sign(algorithm); |
| 220 | + verifyClaims(); |
| 221 | + return JWS; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Verifies that all the standard claims were provided |
| 226 | + * @throws Exception if all the standard claims weren't provided |
| 227 | + */ |
| 228 | + private void verifyClaims() throws Exception { |
| 229 | + for(String claim : addedClaims.keySet()) |
| 230 | + if(!addedClaims.get(claim)) |
| 231 | + throw new Exception("Standard claim: " + claim + " has not been set"); |
| 232 | + } |
| 233 | + |
| 234 | + public static AccessJwtCreator build() { |
| 235 | + return new AccessJwtCreator(); |
| 236 | + } |
| 237 | +} |
0 commit comments