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-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.jose.jws;
18  
19  /**
20   * An Enum for the JWS Header parameter names.
21   * <p>
22   * As described in the JWS specification, the reserved JWS header parameters are listed,
23   * <ul>
24   *     <li>"jku"</li>
25   *     <li>"jwk"</li>
26   *     <li>"x5u"</li>
27   *     <li>"x5t"</li>
28   *     <li>"x5c"</li>
29   *     <li>"kid"</li>
30   *     <li>"cty"</li>
31   *     <li>"crit"</li>
32   * </ul>
33   * Any other header parameter name is deemed as a "custom" header parameter.
34   *
35   * @since 2.0.0
36   */
37  public enum JwsHeaderKey {
38  
39      /**
40       * JWK Set URL header parameter.
41       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.2">
42       *     JWS Specification JWK Set URL Header Parameter</a>
43       */
44      JKU,
45      /**
46       * JSON Web Key header parameter.
47       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.3">
48       *     JWS Specification JWK Web Key Header Parameter</a>
49       */
50      JWK,
51      /**
52       * X.509 URL header parameter.
53       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.4">
54       *     JWS Specification X.509 URL Header Parameter</a>
55       */
56      X5U,
57      /**
58       * X.509 Certificate Thumbprint header parameter.
59       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.5">
60       *     JWS Specification X.509 Certificate Thumbprint Header Parameter</a>
61       **/
62      X5T,  //Base64url
63      /**
64       * X.509 Certificate Chain header parameter.
65       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.6">
66       *     JWS Specification X.509 Certificate Chain Header Parameter</a>
67       */
68      X5C,   //List<Base64>
69      /**
70       * Key ID header parameter.
71       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.7">
72       *     JWS Specification Key ID Header Parameter</a>
73       */
74      KID,
75      /**
76       * Content Type header parameter.
77       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.8">
78       *     JWS Specification Content Type Header Parameter</a>
79       */
80      CTY,
81      /**
82       * Critical header parameter.
83       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.1.10">
84       *     JWS Specification Critical Header Parameter</a>
85       */
86      CRIT,
87      /**
88       * Generic header key for a custom header parameter.
89       * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#section-4.3">
90       *     Private Header Parameter Names</a>
91       */
92      CUSTOM;
93  
94      /**
95       * Returns a lowercase String of the JwsHeaderKey constant.
96       *
97       * @return Lowercase String representation of the constant.
98       * @see #toString()
99       */
100     public String value() {
101         return toString();
102     }
103 
104     /**
105      * Gets the JwsHeaderKey constant that matches the given String.
106      * <p>
107      * If the given String does not match any of the constants, then CUSTOM is returned.
108      *
109      * @param headerKey The String representation of a JwsHeaderKey.
110      * @return The matching JwsHeaderKey.
111      */
112     public static JwsHeaderKey getHeaderKey(String headerKey) {
113         try {
114             return JwsHeaderKey.valueOf(headerKey);
115         } catch (IllegalArgumentException e) {
116             return CUSTOM;
117         }
118     }
119 
120     /**
121      * Turns the JwsHeaderKey constant into a lowercase String.
122      *
123      * @return {@inheritDoc}
124      */
125     @Override
126     public String toString() {
127         return super.toString().toLowerCase();
128     }
129 }