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-2016 ForgeRock AS. 015 */ 016 017package org.forgerock.json.jose.jwt; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Locale; 022import java.util.Map; 023 024/** 025 * An Enum for the JWT Claims Set names. 026 * <p> 027 * As described in the JWT specification, this Enum class represents all the reserved JWT Claim Names, any other Claim 028 * name is deemed as a "custom" Claim name. 029 * <p> 030 * @see <a href="http://tools.ietf.org/html/draft-jones-json-web-token-10#section-4.1">Reserved Claim Names</a> 031 * 032 * @since 2.0.0 033 */ 034public enum JwtClaimsSetKey { 035 036 /** 037 * Type Claim. 038 * <p> 039 * Used to declare a type for the contents of this JWT Claims Set. 040 * <p> 041 * The values used for the "typ" claim SHOULD come from the same value space as the "typ" header parameter, with 042 * the same rules applying. 043 */ 044 TYP, 045 /** 046 * JWT ID Claim. 047 * <p> 048 * Provides a unique identifier for the JWT. 049 */ 050 JTI, 051 /** 052 * Issuer Claim. 053 * <p> 054 * Identifies the principal that issued the JWT. 055 */ 056 ISS, 057 /** 058 * Subject Claim. 059 * <p> 060 * Identifies the subject of the JWT. 061 */ 062 SUB, 063 /** 064 * Audience Claim. 065 * <p> 066 * Identifies the audience that the JWT is intended for. 067 */ 068 AUD, 069 /** 070 * Issued At Claim. 071 * <p> 072 * Identifies the time at which the JWT was issued. This claim can be used to determine the age of the token. 073 */ 074 IAT, 075 /** 076 * Not Before Claim. 077 * <p> 078 * Identifies the time before which the token MUST NOT be accepted for processing. 079 */ 080 NBF, 081 /** 082 * Expiration Time Claim. 083 * <p> 084 * Identifies the expiration time on or after which the token MUST NOT be accepted for processing. 085 */ 086 EXP, 087 /** 088 * Custom (private) Claim. 089 * <p> 090 * Represents any claim not registered in the JWT spec. 091 */ 092 CUSTOM; 093 094 /** 095 * Read-only {@code Map} of {@code JwtClaimsSetKey} values as lower-case {@code String}s, for fast lookup. 096 */ 097 private static final Map<String, JwtClaimsSetKey> NAME_MAP; 098 099 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}