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 Header parameter names. 26 * <p> 27 * As described in the JWT specification, the reserved JWT header parameters are listed, 28 * <ul> 29 * <li>"typ"</li> 30 * <li>"alg"</li> 31 * </ul> 32 * Any other header parameter name is deemed as a "custom" header parameter. 33 * 34 * @since 2.0.0 35 */ 36 public enum JwtHeaderKey { 37 38 /** Type JWT header parameter.. */ 39 TYP, 40 /** Algorithm JWT header parameter. */ 41 ALG, 42 /** Generic header key for a custom header parameter. */ 43 CUSTOM; 44 45 /** 46 * Read-only {@code Map} of {@code JwtHeaderKey} values as lower-case {@code String}s, for fast lookup. 47 */ 48 private static final Map<String, JwtHeaderKey> NAME_MAP; 49 50 static { 51 final Map<String, JwtHeaderKey> temp = new HashMap<>(); 52 for (final JwtHeaderKey key : values()) { 53 temp.put(key.lowerCaseName, key); 54 } 55 NAME_MAP = Collections.unmodifiableMap(temp); 56 } 57 58 private final String lowerCaseName; 59 60 /** 61 * Creates a {@code JwtHeaderKey} with pre-allocated lower-case {@code String} representation, as a 62 * performance optimization, because this {@code enum} is often converted to a {@code String}. 63 */ 64 JwtHeaderKey() { 65 this.lowerCaseName = name().toLowerCase(Locale.ROOT); 66 } 67 68 /** 69 * Returns a lowercase String of the JwtHeaderKey constant. 70 * 71 * @return Lowercase String representation of the constant. 72 * @see #toString() 73 */ 74 public String value() { 75 return toString(); 76 } 77 78 /** 79 * Gets the JwtHeaderKey constant that matches the given String. 80 * <p> 81 * If the given String does not match any of the constants, then CUSTOM is returned. 82 * 83 * @param headerKey The String representation of a JwtHeaderKey. 84 * @return The matching JwtHeaderKey. 85 */ 86 public static JwtHeaderKey getHeaderKey(final String headerKey) { 87 if (headerKey != null && !headerKey.isEmpty()) { 88 final JwtHeaderKey value = NAME_MAP.get(headerKey.toLowerCase(Locale.ROOT)); 89 if (value != null) { 90 return value; 91 } 92 } 93 return CUSTOM; 94 } 95 96 /** 97 * Turns the JwtHeaderKey constant into a lowercase String. 98 * 99 * @return Lowercase String representation of the constant. 100 */ 101 @Override 102 public String toString() { 103 return lowerCaseName; 104 } 105 }