View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.Authentication;
26  import org.eclipse.jetty.client.api.AuthenticationStore;
27  import org.eclipse.jetty.client.api.ContentResponse;
28  import org.eclipse.jetty.client.api.Request;
29  import org.eclipse.jetty.http.HttpHeader;
30  import org.eclipse.jetty.util.Attributes;
31  import org.eclipse.jetty.util.B64Code;
32  import org.eclipse.jetty.util.StringUtil;
33  
34  /**
35   * Implementation of the HTTP "Basic" authentication defined in RFC 2617.
36   * <p />
37   * Applications should create objects of this class and add them to the
38   * {@link AuthenticationStore} retrieved from the {@link HttpClient}
39   * via {@link HttpClient#getAuthenticationStore()}.
40   */
41  public class BasicAuthentication implements Authentication
42  {
43      private final URI uri;
44      private final String realm;
45      private final String user;
46      private final String password;
47  
48      /**
49       * @param uri the URI to match for the authentication
50       * @param realm the realm to match for the authentication
51       * @param user the user that wants to authenticate
52       * @param password the password of the user
53       */
54      public BasicAuthentication(URI uri, String realm, String user, String password)
55      {
56          this.uri = uri;
57          this.realm = realm;
58          this.user = user;
59          this.password = password;
60      }
61  
62      @Override
63      public boolean matches(String type, URI uri, String realm)
64      {
65          if (!"basic".equalsIgnoreCase(type))
66              return false;
67  
68          if (!uri.toString().startsWith(this.uri.toString()))
69              return false;
70  
71          return this.realm.equals(realm);
72      }
73  
74      @Override
75      public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context)
76      {
77          String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
78          return new BasicResult(headerInfo.getHeader(), uri, value);
79      }
80  
81      private static class BasicResult implements Result
82      {
83          private final HttpHeader header;
84          private final URI uri;
85          private final String value;
86  
87          public BasicResult(HttpHeader header, URI uri, String value)
88          {
89              this.header = header;
90              this.uri = uri;
91              this.value = value;
92          }
93  
94          @Override
95          public URI getURI()
96          {
97              return uri;
98          }
99  
100         @Override
101         public void apply(Request request)
102         {
103             request.header(header, value);
104         }
105 
106         @Override
107         public String toString()
108         {
109             return String.format("Basic authentication result for %s", uri);
110         }
111     }
112 }