View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2013-2016 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.jose.jwt;
18  
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  /**
25   * An Enum for the JWT Claims Set names.
26   * <p>
27   * As described in the JWT specification, this Enum class represents all the reserved JWT Claim Names, any other Claim
28   * name is deemed as a "custom" Claim name.
29   * <p>
30   * @see <a href="http://tools.ietf.org/html/draft-jones-json-web-token-10#section-4.1">Reserved Claim Names</a>
31   *
32   * @since 2.0.0
33   */
34  public enum JwtClaimsSetKey {
35  
36      /**
37       * Type Claim.
38       * <p>
39       * Used to declare a type for the contents of this JWT Claims Set.
40       * <p>
41       * The values used for the "typ" claim SHOULD come from the same value space as the "typ" header parameter, with
42       * the same rules applying.
43       */
44      TYP,
45      /**
46       * JWT ID Claim.
47       * <p>
48       * Provides a unique identifier for the JWT.
49       */
50      JTI,
51      /**
52       * Issuer Claim.
53       * <p>
54       * Identifies the principal that issued the JWT.
55       */
56      ISS,
57      /**
58       * Subject Claim.
59       * <p>
60       * Identifies the subject of the JWT.
61       */
62      SUB,
63      /**
64       * Audience Claim.
65       * <p>
66       * Identifies the audience that the JWT is intended for.
67       */
68      AUD,
69      /**
70       * Issued At Claim.
71       * <p>
72       * Identifies the time at which the JWT was issued. This claim can be used to determine the age of the token.
73       */
74      IAT,
75      /**
76       * Not Before Claim.
77       * <p>
78       * Identifies the time before which the token MUST NOT be accepted for processing.
79       */
80      NBF,
81      /**
82       * Expiration Time Claim.
83       * <p>
84       * Identifies the expiration time on or after which the token MUST NOT be accepted for processing.
85       */
86      EXP,
87      /**
88       * Custom (private) Claim.
89       * <p>
90       * Represents any claim not registered in the JWT spec.
91       */
92      CUSTOM;
93  
94      /**
95       * Read-only {@code Map} of {@code JwtClaimsSetKey} values as lower-case {@code String}s, for fast lookup.
96       */
97      private static final Map<String, JwtClaimsSetKey> NAME_MAP;
98  
99      static {
100         final Map<String, JwtClaimsSetKey> temp = new HashMap<>();
101         for (final JwtClaimsSetKey key : values()) {
102             temp.put(key.lowerCaseName, key);
103         }
104         NAME_MAP = Collections.unmodifiableMap(temp);
105     }
106 
107     private final String lowerCaseName;
108 
109     /**
110      * Creates a {@code JwtClaimsSetKey} with pre-allocated lower-case {@code String} representation, as a
111      * performance optimization, because this {@code enum} is often converted to a {@code String}.
112      */
113     JwtClaimsSetKey() {
114         this.lowerCaseName = name().toLowerCase(Locale.ROOT);
115     }
116 
117     /**
118      * Returns a lowercase String of the {@code JwtClaimsSetKey} constant.
119      *
120      * @return Lowercase String representation of the constant.
121      * @see #toString()
122      */
123     public String value() {
124         return toString();
125     }
126 
127     /**
128      * Gets the {@code JwtClaimsSetKey} constant that matches the given {@code String} (case-insensitive).
129      *
130      * @param claimSetKey The case-insensitive {@code String} representation of a {@code JwtClaimsSetKey}.
131      * @return The matching {@code JwtClaimsSetKey} or {@link #CUSTOM} for keys not in the JWT spec.
132      */
133     public static JwtClaimsSetKey getClaimSetKey(final String claimSetKey) {
134         if (claimSetKey != null && !claimSetKey.isEmpty()) {
135             final JwtClaimsSetKey value = NAME_MAP.get(claimSetKey.toLowerCase(Locale.ROOT));
136             if (value != null) {
137                 return value;
138             }
139         }
140         return CUSTOM;
141     }
142 
143     /**
144      * Turns the {@code JwtClaimsSetKey} constant into a lowercase {@code String}.
145      *
146      * @return {@inheritDoc}
147      */
148     @Override
149     public String toString() {
150         return lowerCaseName;
151     }
152 }