1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18 package io.undertow.security.impl;
19
20 import io.undertow.util.HeaderToken;
21 import io.undertow.util.HeaderTokenParser;
22 import io.undertow.util.Headers;
23 import io.undertow.util.HttpString;
24
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 /**
30  * Enumeration of tokens expected in a HTTP Digest 'Authorization' header.
31  *
32  * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
33  */

34 public enum DigestAuthorizationToken implements HeaderToken {
35
36     USERNAME(Headers.USERNAME, true),
37     REALM(Headers.REALM, true),
38     NONCE(Headers.NONCE, true),
39     DIGEST_URI(Headers.URI, true),
40     RESPONSE(Headers.RESPONSE, true),
41     ALGORITHM(Headers.ALGORITHM, true),
42     CNONCE(Headers.CNONCE, true),
43     OPAQUE(Headers.OPAQUE, true),
44     MESSAGE_QOP(Headers.QOP, true),
45     NONCE_COUNT(Headers.NONCE_COUNT, false),
46     AUTH_PARAM(Headers.AUTH_PARAM, false);
47
48     private static final HeaderTokenParser<DigestAuthorizationToken> TOKEN_PARSER;
49
50     static {
51         Map<String, DigestAuthorizationToken> expected = new LinkedHashMap<>(
52                 DigestAuthorizationToken.values().length);
53         for (DigestAuthorizationToken current : DigestAuthorizationToken.values()) {
54             expected.put(current.getName(), current);
55         }
56
57         TOKEN_PARSER = new HeaderTokenParser<>(Collections.unmodifiableMap(expected));
58     }
59
60     private final String name;
61     private final boolean quoted;
62
63     DigestAuthorizationToken(final HttpString name, final boolean quoted) {
64         this.name = name.toString();
65         this.quoted = quoted;
66     }
67
68     @Override
69     public String getName() {
70         return name;
71     }
72
73     @Override
74     public boolean isAllowQuoted() {
75         return quoted;
76     }
77
78     public static Map<DigestAuthorizationToken, String> parseHeader(final String header) {
79         return TOKEN_PARSER.parseHeader(header);
80     }
81 }
82