1 /*
2 * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10 package org.eclipse.jgit.internal.transport.sshd.proxy;
11
12 import java.util.Collections;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15
16 import org.eclipse.jgit.annotations.NonNull;
17
18 /**
19 * A simple representation of an authentication challenge as sent in a
20 * "WWW-Authenticate" or "Proxy-Authenticate" header. Such challenges start with
21 * a mechanism name, followed either by one single token, or by a list of
22 * key=value pairs.
23 *
24 * @see <a href="https://tools.ietf.org/html/rfc7235#section-2.1">RFC 7235, sec.
25 * 2.1</a>
26 */
27 public class AuthenticationChallenge {
28
29 private final String mechanism;
30
31 private String token;
32
33 private Map<String, String> arguments;
34
35 /**
36 * Create a new {@link AuthenticationChallenge} with the given mechanism.
37 *
38 * @param mechanism
39 * for the challenge
40 */
41 public AuthenticationChallenge(String mechanism) {
42 this.mechanism = mechanism;
43 }
44
45 /**
46 * Retrieves the authentication mechanism specified by this challenge, for
47 * instance "Basic".
48 *
49 * @return the mechanism name
50 */
51 public String getMechanism() {
52 return mechanism;
53 }
54
55 /**
56 * Retrieves the token of the challenge, if any.
57 *
58 * @return the token, or {@code null} if there is none.
59 */
60 public String getToken() {
61 return token;
62 }
63
64 /**
65 * Retrieves the arguments of the challenge.
66 *
67 * @return a possibly empty map of the key=value arguments of the challenge
68 */
69 @NonNull
70 public Map<String, String> getArguments() {
71 return arguments == null ? Collections.emptyMap() : arguments;
72 }
73
74 void addArgument(String key, String value) {
75 if (arguments == null) {
76 arguments = new LinkedHashMap<>();
77 }
78 arguments.put(key, value);
79 }
80
81 void setToken(String token) {
82 this.token = token;
83 }
84
85 @Override
86 public String toString() {
87 return "AuthenticationChallenge[" + mechanism + ',' + token + ',' //$NON-NLS-1$
88 + (arguments == null ? "<none>" : arguments.toString()) + ']'; //$NON-NLS-1$
89 }
90 }