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 Header parameter names.
026 * <p>
027 * As described in the JWT specification, the reserved JWT header parameters are listed,
028 * <ul>
029 *     <li>"typ"</li>
030 *     <li>"alg"</li>
031 * </ul>
032 * Any other header parameter name is deemed as a "custom" header parameter.
033 *
034 * @since 2.0.0
035 */
036public enum JwtHeaderKey {
037
038    /** Type JWT header parameter.. */
039    TYP,
040    /** Algorithm JWT header parameter. */
041    ALG,
042    /** Generic header key for a custom header parameter. */
043    CUSTOM;
044
045    /**
046     * Read-only {@code Map} of {@code JwtHeaderKey} values as lower-case {@code String}s, for fast lookup.
047     */
048    private static final Map<String, JwtHeaderKey> NAME_MAP;
049
050    static {
051        final Map<String, JwtHeaderKey> temp = new HashMap<>();
052        for (final JwtHeaderKey key : values()) {
053            temp.put(key.lowerCaseName, key);
054        }
055        NAME_MAP = Collections.unmodifiableMap(temp);
056    }
057
058    private final String lowerCaseName;
059
060    /**
061     * Creates a {@code JwtHeaderKey} with pre-allocated lower-case {@code String} representation, as a
062     * performance optimization, because this {@code enum} is often converted to a {@code String}.
063     */
064    JwtHeaderKey() {
065        this.lowerCaseName = name().toLowerCase(Locale.ROOT);
066    }
067
068    /**
069     * Returns a lowercase String of the JwtHeaderKey constant.
070     *
071     * @return Lowercase String representation of the constant.
072     * @see #toString()
073     */
074    public String value() {
075        return toString();
076    }
077
078    /**
079     * Gets the JwtHeaderKey constant that matches the given String.
080     * <p>
081     * If the given String does not match any of the constants, then CUSTOM is returned.
082     *
083     * @param headerKey The String representation of a JwtHeaderKey.
084     * @return The matching JwtHeaderKey.
085     */
086    public static JwtHeaderKey getHeaderKey(final String headerKey) {
087        if (headerKey != null && !headerKey.isEmpty()) {
088            final JwtHeaderKey value = NAME_MAP.get(headerKey.toLowerCase(Locale.ROOT));
089            if (value != null) {
090                return value;
091            }
092        }
093        return CUSTOM;
094    }
095
096    /**
097     * Turns the JwtHeaderKey constant into a lowercase String.
098     *
099     * @return Lowercase String representation of the constant.
100     */
101    @Override
102    public String toString() {
103        return lowerCaseName;
104    }
105}