001package com.nimbusds.jwt; 002 003 004import java.text.ParseException; 005import java.util.*; 006 007import net.minidev.json.JSONArray; 008import net.minidev.json.JSONObject; 009 010import com.nimbusds.jose.util.JSONObjectUtils; 011 012 013/** 014 * JSON Web Token (JWT) claims set. 015 * 016 * <p>Supports all {@link #getRegisteredNames()} registered claims} of the JWT 017 * specification: 018 * 019 * <ul> 020 * <li>iss - Issuer 021 * <li>sub - Subject 022 * <li>aud - Audience 023 * <li>exp - Expiration Time 024 * <li>nbf - Not Before 025 * <li>iat - Issued At 026 * <li>jti - JWT ID 027 * <li>typ - Type 028 * </ul> 029 * 030 * <p>The set may also contain {@link #setCustomClaims custom claims}; these 031 * will be serialised and parsed along the registered ones. 032 * 033 * @author Vladimir Dzhuvinov 034 * @author Justin Richer 035 * @version $version$ (2014-08-16) 036 */ 037public class JWTClaimsSet implements ReadOnlyJWTClaimsSet { 038 039 040 private static final String TYPE_CLAIM = "typ"; 041 private static final String JWT_ID_CLAIM = "jti"; 042 private static final String ISSUED_AT_CLAIM = "iat"; 043 private static final String NOT_BEFORE_CLAIM = "nbf"; 044 private static final String EXPIRATION_TIME_CLAIM = "exp"; 045 private static final String AUDIENCE_CLAIM = "aud"; 046 private static final String SUBJECT_CLAIM = "sub"; 047 private static final String ISSUER_CLAIM = "iss"; 048 049 050 /** 051 * The registered claim names. 052 */ 053 private static final Set<String> REGISTERED_CLAIM_NAMES; 054 055 056 /** 057 * Initialises the registered claim name set. 058 */ 059 static { 060 Set<String> n = new HashSet<>(); 061 062 n.add(ISSUER_CLAIM); 063 n.add(SUBJECT_CLAIM); 064 n.add(AUDIENCE_CLAIM); 065 n.add(EXPIRATION_TIME_CLAIM); 066 n.add(NOT_BEFORE_CLAIM); 067 n.add(ISSUED_AT_CLAIM); 068 n.add(JWT_ID_CLAIM); 069 n.add(TYPE_CLAIM); 070 071 REGISTERED_CLAIM_NAMES = Collections.unmodifiableSet(n); 072 } 073 074 075 /** 076 * The issuer claim. 077 */ 078 private String iss = null; 079 080 081 /** 082 * The subject claim. 083 */ 084 private String sub = null; 085 086 087 /** 088 * The audience claim. 089 */ 090 private List<String> aud = null; 091 092 093 /** 094 * The expiration time claim. 095 */ 096 private Date exp = null; 097 098 099 /** 100 * The not-before claim. 101 */ 102 private Date nbf = null; 103 104 105 /** 106 * The issued-at claim. 107 */ 108 private Date iat = null; 109 110 111 /** 112 * The JWT ID claim. 113 */ 114 private String jti = null; 115 116 117 /** 118 * The type claim. 119 */ 120 private String typ = null; 121 122 123 /** 124 * Custom claims. 125 */ 126 private Map<String,Object> customClaims = new HashMap<>(); 127 128 129 /** 130 * Creates a new empty JWT claims set. 131 */ 132 public JWTClaimsSet() { 133 134 // Nothing to do 135 } 136 137 138 /** 139 * Creates a copy of the specified JWT claims set. 140 * 141 * @param old The JWT claims set to copy. Must not be {@code null}. 142 */ 143 public JWTClaimsSet(final ReadOnlyJWTClaimsSet old) { 144 145 super(); 146 setAllClaims(old.getAllClaims()); 147 } 148 149 150 /** 151 * Gets the registered JWT claim names. 152 * 153 * @return The registered claim names, as a unmodifiable set. 154 */ 155 public static Set<String> getRegisteredNames() { 156 157 return REGISTERED_CLAIM_NAMES; 158 } 159 160 161 @Override 162 public String getIssuer() { 163 164 return iss; 165 } 166 167 168 /** 169 * Sets the issuer ({@code iss}) claim. 170 * 171 * @param iss The issuer claim, {@code null} if not specified. 172 */ 173 public void setIssuer(final String iss) { 174 175 this.iss = iss; 176 } 177 178 179 @Override 180 public String getSubject() { 181 182 return sub; 183 } 184 185 186 /** 187 * Sets the subject ({@code sub}) claim. 188 * 189 * @param sub The subject claim, {@code null} if not specified. 190 */ 191 public void setSubject(final String sub) { 192 193 this.sub = sub; 194 } 195 196 197 @Override 198 public List<String> getAudience() { 199 200 return aud; 201 } 202 203 204 /** 205 * Sets the audience ({@code aud}) claim. 206 * 207 * @param aud The audience claim, {@code null} if not specified. 208 */ 209 public void setAudience(final List<String> aud) { 210 211 this.aud = aud; 212 } 213 214 215 /** 216 * Sets a single-valued audience ({@code aud}) claim. 217 * 218 * @param aud The audience claim, {@code null} if not specified. 219 */ 220 public void setAudience(final String aud) { 221 222 if (aud == null) { 223 this.aud = null; 224 } else { 225 this.aud = Arrays.asList(aud); 226 } 227 } 228 229 230 @Override 231 public Date getExpirationTime() { 232 233 return exp; 234 } 235 236 237 /** 238 * Sets the expiration time ({@code exp}) claim. 239 * 240 * @param exp The expiration time, {@code null} if not specified. 241 */ 242 public void setExpirationTime(final Date exp) { 243 244 this.exp = exp; 245 } 246 247 248 @Override 249 public Date getNotBeforeTime() { 250 251 return nbf; 252 } 253 254 255 /** 256 * Sets the not-before ({@code nbf}) claim. 257 * 258 * @param nbf The not-before claim, {@code null} if not specified. 259 */ 260 public void setNotBeforeTime(final Date nbf) { 261 262 this.nbf = nbf; 263 } 264 265 266 @Override 267 public Date getIssueTime() { 268 269 return iat; 270 } 271 272 273 /** 274 * Sets the issued-at ({@code iat}) claim. 275 * 276 * @param iat The issued-at claim, {@code null} if not specified. 277 */ 278 public void setIssueTime(final Date iat) { 279 280 this.iat = iat; 281 } 282 283 284 @Override 285 public String getJWTID() { 286 287 return jti; 288 } 289 290 291 /** 292 * Sets the JWT ID ({@code jti}) claim. 293 * 294 * @param jti The JWT ID claim, {@code null} if not specified. 295 */ 296 public void setJWTID(final String jti) { 297 298 this.jti = jti; 299 } 300 301 302 @Override 303 public String getType() { 304 305 return typ; 306 } 307 308 309 /** 310 * Sets the type ({@code typ}) claim. 311 * 312 * @param typ The type claim, {@code null} if not specified. 313 */ 314 public void setType(final String typ) { 315 316 this.typ = typ; 317 } 318 319 320 @Override 321 public Object getCustomClaim(final String name) { 322 323 return customClaims.get(name); 324 } 325 326 327 /** 328 * Sets a custom (non-registered) claim. 329 * 330 * @param name The name of the custom claim. Must not be {@code null}. 331 * @param value The value of the custom claim, should map to a valid 332 * JSON entity, {@code null} if not specified. 333 * 334 * @throws IllegalArgumentException If the specified custom claim name 335 * matches a registered claim name. 336 */ 337 public void setCustomClaim(final String name, final Object value) { 338 339 if (getRegisteredNames().contains(name)) { 340 341 throw new IllegalArgumentException("The claim name \"" + name + "\" matches a registered name"); 342 } 343 344 customClaims.put(name, value); 345 } 346 347 348 @Override 349 public Map<String,Object> getCustomClaims() { 350 351 return Collections.unmodifiableMap(customClaims); 352 } 353 354 355 /** 356 * Sets the custom (non-registered) claims. If a claim value doesn't 357 * map to a JSON entity it will be ignored during serialisation. 358 * 359 * @param customClaims The custom claims, empty map or {@code null} if 360 * none. 361 */ 362 public void setCustomClaims(final Map<String,Object> customClaims) { 363 364 if (customClaims == null) { 365 this.customClaims.clear(); 366 } else { 367 this.customClaims = customClaims; 368 } 369 } 370 371 372 @Override 373 public Object getClaim(final String name) { 374 375 switch (name) { 376 case ISSUER_CLAIM: 377 return getIssuer(); 378 case SUBJECT_CLAIM: 379 return getSubject(); 380 case AUDIENCE_CLAIM: 381 return getAudience(); 382 case EXPIRATION_TIME_CLAIM: 383 return getExpirationTime(); 384 case NOT_BEFORE_CLAIM: 385 return getNotBeforeTime(); 386 case ISSUED_AT_CLAIM: 387 return getIssueTime(); 388 case JWT_ID_CLAIM: 389 return getJWTID(); 390 case TYPE_CLAIM: 391 return getType(); 392 default: 393 return getCustomClaim(name); 394 } 395 } 396 397 398 @Override 399 public String getStringClaim(final String name) 400 throws ParseException { 401 402 Object value = getClaim(name); 403 404 if (value == null || value instanceof String) { 405 return (String)value; 406 } else { 407 throw new ParseException("The \"" + name + "\" claim is not a String", 0); 408 } 409 } 410 411 412 @Override 413 public Boolean getBooleanClaim(final String name) 414 throws ParseException { 415 416 Object value = getClaim(name); 417 418 if (value == null || value instanceof Boolean) { 419 return (Boolean)value; 420 } else { 421 throw new ParseException("The \"" + name + "\" claim is not a Boolean", 0); 422 } 423 } 424 425 426 @Override 427 public Integer getIntegerClaim(final String name) 428 throws ParseException { 429 430 Object value = getClaim(name); 431 432 if (value == null) { 433 return null; 434 } else if (value instanceof Number) { 435 return ((Number)value).intValue(); 436 } else { 437 throw new ParseException("The \"" + name + "\" claim is not an Integer", 0); 438 } 439 } 440 441 442 @Override 443 public Long getLongClaim(final String name) 444 throws ParseException { 445 446 Object value = getClaim(name); 447 448 if (value == null) { 449 return null; 450 } else if (value instanceof Number) { 451 return ((Number)value).longValue(); 452 } else { 453 throw new ParseException("The \"" + name + "\" claim is not a Number", 0); 454 } 455 } 456 457 458 @Override 459 public Float getFloatClaim(final String name) 460 throws ParseException { 461 462 Object value = getClaim(name); 463 464 if (value == null) { 465 return null; 466 } else if (value instanceof Number) { 467 return ((Number)value).floatValue(); 468 } else { 469 throw new ParseException("The \"" + name + "\" claim is not a Float", 0); 470 } 471 } 472 473 474 @Override 475 public Double getDoubleClaim(final String name) 476 throws ParseException { 477 478 Object value = getClaim(name); 479 480 if (value == null) { 481 return null; 482 } else if (value instanceof Number) { 483 return ((Number)value).doubleValue(); 484 } else { 485 throw new ParseException("The \"" + name + "\" claim is not a Double", 0); 486 } 487 } 488 489 490 /** 491 * Sets the specified claim, whether registered or custom. 492 * 493 * @param name The name of the claim to set. Must not be {@code null}. 494 * @param value The value of the claim to set, {@code null} if not 495 * specified. 496 * 497 * @throws IllegalArgumentException If the claim is registered and its 498 * value is not of the expected type. 499 */ 500 public void setClaim(final String name, final Object value) { 501 502 if (ISSUER_CLAIM.equals(name)) { 503 if (value == null || value instanceof String) { 504 setIssuer((String) value); 505 } else { 506 throw new IllegalArgumentException("Issuer claim must be a String"); 507 } 508 } else if (SUBJECT_CLAIM.equals(name)) { 509 if (value == null || value instanceof String) { 510 setSubject((String) value); 511 } else { 512 throw new IllegalArgumentException("Subject claim must be a String"); 513 } 514 } else if (AUDIENCE_CLAIM.equals(name)) { 515 if (value == null || value instanceof List<?>) { 516 setAudience((List<String>) value); 517 } else { 518 throw new IllegalArgumentException("Audience claim must be a List<String>"); 519 } 520 } else if (EXPIRATION_TIME_CLAIM.equals(name)) { 521 if (value == null || value instanceof Date) { 522 setExpirationTime((Date) value); 523 } else { 524 throw new IllegalArgumentException("Expiration claim must be a Date"); 525 } 526 } else if (NOT_BEFORE_CLAIM.equals(name)) { 527 if (value == null || value instanceof Date) { 528 setNotBeforeTime((Date) value); 529 } else { 530 throw new IllegalArgumentException("Not-before claim must be a Date"); 531 } 532 } else if (ISSUED_AT_CLAIM.equals(name)) { 533 if (value == null || value instanceof Date) { 534 setIssueTime((Date) value); 535 } else { 536 throw new IllegalArgumentException("Issued-at claim must be a Date"); 537 } 538 } else if (JWT_ID_CLAIM.equals(name)) { 539 if (value == null || value instanceof String) { 540 setJWTID((String) value); 541 } else { 542 throw new IllegalArgumentException("JWT-ID claim must be a String"); 543 } 544 } else if (TYPE_CLAIM.equals(name)) { 545 if (value == null || value instanceof String) { 546 setType((String) value); 547 } else { 548 throw new IllegalArgumentException("Type claim must be a String"); 549 } 550 } else { 551 setCustomClaim(name, value); 552 } 553 } 554 555 556 @Override 557 public Map<String, Object> getAllClaims() { 558 559 Map<String, Object> allClaims = new HashMap<>(); 560 561 allClaims.putAll(customClaims); 562 563 for (String registeredClaim : REGISTERED_CLAIM_NAMES) { 564 565 allClaims.put(registeredClaim, getClaim(registeredClaim)); 566 } 567 568 return Collections.unmodifiableMap(allClaims); 569 } 570 571 572 /** 573 * Sets the claims of this JWT claims set, replacing any existing ones. 574 * 575 * @param newClaims The JWT claims. Must not be {@code null}. 576 */ 577 public void setAllClaims(final Map<String, Object> newClaims) { 578 579 for (String name : newClaims.keySet()) { 580 setClaim(name, newClaims.get(name)); 581 } 582 } 583 584 585 @Override 586 public JSONObject toJSONObject() { 587 588 JSONObject o = new JSONObject(customClaims); 589 590 if (iss != null) { 591 o.put(ISSUER_CLAIM, iss); 592 } 593 594 if (sub != null) { 595 o.put(SUBJECT_CLAIM, sub); 596 } 597 598 if (aud != null && ! aud.isEmpty()) { 599 600 if (aud.size() == 1) { 601 o.put(AUDIENCE_CLAIM, aud.get(0)); 602 } else { 603 JSONArray audArray = new JSONArray(); 604 audArray.addAll(aud); 605 o.put(AUDIENCE_CLAIM, audArray); 606 } 607 } 608 609 if (exp != null) { 610 o.put(EXPIRATION_TIME_CLAIM, exp.getTime() / 1000); 611 } 612 613 if (nbf != null) { 614 o.put(NOT_BEFORE_CLAIM, nbf.getTime() / 1000); 615 } 616 617 if (iat != null) { 618 o.put(ISSUED_AT_CLAIM, iat.getTime() / 1000); 619 } 620 621 if (jti != null) { 622 o.put(JWT_ID_CLAIM, jti); 623 } 624 625 if (typ != null) { 626 o.put(TYPE_CLAIM, typ); 627 } 628 629 return o; 630 } 631 632 633 /** 634 * Parses a JSON Web Token (JWT) claims set from the specified JSON 635 * object representation. 636 * 637 * @param json The JSON object to parse. Must not be {@code null}. 638 * 639 * @return The JWT claims set. 640 * 641 * @throws ParseException If the specified JSON object doesn't 642 * represent a valid JWT claims set. 643 */ 644 public static JWTClaimsSet parse(final JSONObject json) 645 throws ParseException { 646 647 JWTClaimsSet cs = new JWTClaimsSet(); 648 649 // Parse registered + custom params 650 for (final String name: json.keySet()) { 651 652 if (name.equals(ISSUER_CLAIM)) { 653 654 cs.setIssuer(JSONObjectUtils.getString(json, ISSUER_CLAIM)); 655 656 } else if (name.equals(SUBJECT_CLAIM)) { 657 658 cs.setSubject(JSONObjectUtils.getString(json, SUBJECT_CLAIM)); 659 660 } else if (name.equals(AUDIENCE_CLAIM)) { 661 662 Object audValue = json.get(AUDIENCE_CLAIM); 663 664 if (audValue instanceof String) { 665 List<String> singleAud = new ArrayList<>(); 666 singleAud.add(JSONObjectUtils.getString(json, AUDIENCE_CLAIM)); 667 cs.setAudience(singleAud); 668 } else if (audValue instanceof List) { 669 cs.setAudience(JSONObjectUtils.getStringList(json, AUDIENCE_CLAIM)); 670 } 671 672 } else if (name.equals(EXPIRATION_TIME_CLAIM)) { 673 674 cs.setExpirationTime(new Date(JSONObjectUtils.getLong(json, EXPIRATION_TIME_CLAIM) * 1000)); 675 676 } else if (name.equals(NOT_BEFORE_CLAIM)) { 677 678 cs.setNotBeforeTime(new Date(JSONObjectUtils.getLong(json, NOT_BEFORE_CLAIM) * 1000)); 679 680 } else if (name.equals(ISSUED_AT_CLAIM)) { 681 682 cs.setIssueTime(new Date(JSONObjectUtils.getLong(json, ISSUED_AT_CLAIM) * 1000)); 683 684 } else if (name.equals(JWT_ID_CLAIM)) { 685 686 cs.setJWTID(JSONObjectUtils.getString(json, JWT_ID_CLAIM)); 687 688 } else if (name.equals(TYPE_CLAIM)) { 689 690 cs.setType(JSONObjectUtils.getString(json, TYPE_CLAIM)); 691 692 } else { 693 cs.setCustomClaim(name, json.get(name)); 694 } 695 } 696 697 return cs; 698 } 699 700 701 /** 702 * Parses a JSON Web Token (JWT) claims set from the specified JSON 703 * object string representation. 704 * 705 * @param s The JSON object string to parse. Must not be {@code null}. 706 * 707 * @return The JWT claims set. 708 * 709 * @throws ParseException If the specified JSON object string doesn't 710 * represent a valid JWT claims set. 711 */ 712 public static JWTClaimsSet parse(final String s) 713 throws ParseException { 714 715 return parse(JSONObjectUtils.parseJSONObject(s)); 716 } 717 718 @Override 719 public String toString() { 720 721 return "JWTClaimsSet [iss=" + iss + ", sub=" + sub + ", aud=" + aud + ", exp=" + exp + ", nbf=" + nbf + ", iat=" + iat + ", jti=" + jti + ", typ=" + typ + ", customClaims=" + customClaims + "]"; 722 } 723}