View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client.util;
20  
21  import java.net.URI;
22  import java.nio.charset.StandardCharsets;
23  
24  import org.eclipse.jetty.client.HttpClient;
25  import org.eclipse.jetty.client.api.AuthenticationStore;
26  import org.eclipse.jetty.client.api.ContentResponse;
27  import org.eclipse.jetty.client.api.Request;
28  import org.eclipse.jetty.http.HttpHeader;
29  import org.eclipse.jetty.util.Attributes;
30  import org.eclipse.jetty.util.B64Code;
31  
32  /**
33   * Implementation of the HTTP "Basic" authentication defined in RFC 2617.
34   * <p />
35   * Applications should create objects of this class and add them to the
36   * {@link AuthenticationStore} retrieved from the {@link HttpClient}
37   * via {@link HttpClient#getAuthenticationStore()}.
38   */
39  public class BasicAuthentication extends AbstractAuthentication
40  {
41      private final String user;
42      private final String password;
43  
44      /**
45       * @param uri the URI to match for the authentication
46       * @param realm the realm to match for the authentication
47       * @param user the user that wants to authenticate
48       * @param password the password of the user
49       */
50      public BasicAuthentication(URI uri, String realm, String user, String password)
51      {
52          super(uri, realm);
53          this.user = user;
54          this.password = password;
55      }
56  
57      @Override
58      public String getType()
59      {
60          return "Basic";
61      }
62  
63      @Override
64      public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context)
65      {
66          String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
67          return new BasicResult(headerInfo.getHeader(), value);
68      }
69  
70      private class BasicResult implements Result
71      {
72          private final HttpHeader header;
73          private final String value;
74  
75          public BasicResult(HttpHeader header, String value)
76          {
77              this.header = header;
78              this.value = value;
79          }
80  
81          @Override
82          public URI getURI()
83          {
84              return BasicAuthentication.this.getURI();
85          }
86  
87          @Override
88          public void apply(Request request)
89          {
90              request.header(header, value);
91          }
92  
93          @Override
94          public String toString()
95          {
96              return String.format("Basic authentication result for %s", getURI());
97          }
98      }
99  }