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          return new BasicResult(getURI(), headerInfo.getHeader(), user, password);
67      }
68  
69      /**
70       * Basic authentication result.
71       * <p>
72       * Application may utilize this class directly via
73       * {@link AuthenticationStore#addAuthenticationResult(Result)}
74       * to perform preemptive authentication, that is immediately
75       * sending the authorization header based on the fact that the
76       * URI is known to require authentication and that username
77       * and password are known a priori.
78       */
79      public static class BasicResult implements Result
80      {
81          private final URI uri;
82          private final HttpHeader header;
83          private final String value;
84  
85          public BasicResult(URI uri, String user, String password)
86          {
87              this(uri, HttpHeader.AUTHORIZATION, user, password);
88          }
89  
90          public BasicResult(URI uri, HttpHeader header, String user, String password)
91          {
92              this.uri = uri;
93              this.header = header;
94              this.value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
95          }
96  
97          @Override
98          public URI getURI()
99          {
100             return uri;
101         }
102 
103         @Override
104         public void apply(Request request)
105         {
106             request.header(header, value);
107         }
108 
109         @Override
110         public String toString()
111         {
112             return String.format("Basic authentication result for %s", getURI());
113         }
114     }
115 }