AuthenticationChallenge.java

  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. import java.util.Collections;
  12. import java.util.LinkedHashMap;
  13. import java.util.Map;

  14. import org.eclipse.jgit.annotations.NonNull;

  15. /**
  16.  * A simple representation of an authentication challenge as sent in a
  17.  * "WWW-Authenticate" or "Proxy-Authenticate" header. Such challenges start with
  18.  * a mechanism name, followed either by one single token, or by a list of
  19.  * key=value pairs.
  20.  *
  21.  * @see <a href="https://tools.ietf.org/html/rfc7235#section-2.1">RFC 7235, sec.
  22.  *      2.1</a>
  23.  */
  24. public class AuthenticationChallenge {

  25.     private final String mechanism;

  26.     private String token;

  27.     private Map<String, String> arguments;

  28.     /**
  29.      * Create a new {@link AuthenticationChallenge} with the given mechanism.
  30.      *
  31.      * @param mechanism
  32.      *            for the challenge
  33.      */
  34.     public AuthenticationChallenge(String mechanism) {
  35.         this.mechanism = mechanism;
  36.     }

  37.     /**
  38.      * Retrieves the authentication mechanism specified by this challenge, for
  39.      * instance "Basic".
  40.      *
  41.      * @return the mechanism name
  42.      */
  43.     public String getMechanism() {
  44.         return mechanism;
  45.     }

  46.     /**
  47.      * Retrieves the token of the challenge, if any.
  48.      *
  49.      * @return the token, or {@code null} if there is none.
  50.      */
  51.     public String getToken() {
  52.         return token;
  53.     }

  54.     /**
  55.      * Retrieves the arguments of the challenge.
  56.      *
  57.      * @return a possibly empty map of the key=value arguments of the challenge
  58.      */
  59.     @NonNull
  60.     public Map<String, String> getArguments() {
  61.         return arguments == null ? Collections.emptyMap() : arguments;
  62.     }

  63.     void addArgument(String key, String value) {
  64.         if (arguments == null) {
  65.             arguments = new LinkedHashMap<>();
  66.         }
  67.         arguments.put(key, value);
  68.     }

  69.     void setToken(String token) {
  70.         this.token = token;
  71.     }

  72.     @Override
  73.     public String toString() {
  74.         return "AuthenticationChallenge[" + mechanism + ',' + token + ',' //$NON-NLS-1$
  75.                 + (arguments == null ? "<none>" : arguments.toString()) + ']'; //$NON-NLS-1$
  76.     }
  77. }