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