1   package org.mortbay.jetty.client.security;
2   
3   //========================================================================
4   //Copyright 2006-2008 Mort Bay Consulting Pty. Ltd.
5   //------------------------------------------------------------------------
6   //Licensed under the Apache License, Version 2.0 (the "License");
7   //you may not use this file except in compliance with the License.
8   //You may obtain a copy of the License at
9   //http://www.apache.org/licenses/LICENSE-2.0
10  //Unless required by applicable law or agreed to in writing, software
11  //distributed under the License is distributed on an "AS IS" BASIS,
12  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  //See the License for the specific language governing permissions and
14  //limitations under the License.
15  //========================================================================
16  
17  import java.io.IOException;
18  
19  import org.mortbay.io.Buffer;
20  import org.mortbay.io.ByteArrayBuffer;
21  import org.mortbay.jetty.HttpHeaders;
22  import org.mortbay.jetty.client.HttpExchange;
23  import org.mortbay.jetty.security.B64Code;
24  import org.mortbay.util.StringUtil;
25  
26  /**
27   * Sets authentication headers for BASIC authentication challenges
28   * 
29   * @author jesse
30   */
31  public class BasicAuthentication implements Authentication
32  {
33      private Buffer _authorization;
34      
35      public BasicAuthentication(SecurityRealm realm) throws IOException
36      {
37          String authenticationString = "basic " + B64Code.encode( realm.getPrincipal() + ":" + realm.getCredentials(), StringUtil.__ISO_8859_1);
38          _authorization= new ByteArrayBuffer(authenticationString);
39      }
40      
41      /**
42       * BASIC authentication is of the form
43       * 
44       * encoded credentials are of the form: username:password
45       * 
46       * 
47       */
48      public void setCredentials( HttpExchange exchange ) throws IOException
49      {
50          exchange.setRequestHeader( HttpHeaders.AUTHORIZATION_BUFFER, _authorization);
51      }
52  }