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