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-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jwt;
018
019/**
020 * An Enum for the JWT Header parameter names.
021 * <p>
022 * As described in the JWT specification, the reserved JWT header parameters are listed,
023 * <ul>
024 *     <li>"typ"</li>
025 *     <li>"alg"</li>
026 * </ul>
027 * Any other header parameter name is deemed as a "custom" header parameter.
028 *
029 * @since 2.0.0
030 */
031public enum JwtHeaderKey {
032
033    /** Type JWT header parameter.. */
034    TYP,
035    /** Algorithm JWT header parameter. */
036    ALG,
037    /** Generic header key for a custom header parameter. */
038    CUSTOM;
039
040    /**
041     * Returns a lowercase String of the JwtHeaderKey constant.
042     *
043     * @return Lowercase String representation of the constant.
044     * @see #toString()
045     */
046    public String value() {
047        return toString();
048    }
049
050    /**
051     * Gets the JwtHeaderKey constant that matches the given String.
052     * <p>
053     * If the given String does not match any of the constants, then CUSTOM is returned.
054     *
055     * @param headerKey The String representation of a JwtHeaderKey.
056     * @return The matching JwtHeaderKey.
057     */
058    public static JwtHeaderKey getHeaderKey(String headerKey) {
059        try {
060            return JwtHeaderKey.valueOf(headerKey.toUpperCase());
061        } catch (IllegalArgumentException e) {
062            return CUSTOM;
063        }
064    }
065
066    /**
067     * Turns the JwtHeaderKey constant into a lowercase String.
068     *
069     * @return {@inheritDoc}
070     */
071    @Override
072    public String toString() {
073        return super.toString().toLowerCase();
074    }
075}