View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.auth;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.ObjectInputStream;
32  import java.io.ObjectOutputStream;
33  
34  import org.apache.commons.codec.binary.Base64;
35  import org.apache.http.Consts;
36  import org.apache.http.Header;
37  import org.apache.http.HttpRequest;
38  import org.apache.http.auth.AUTH;
39  import org.apache.http.auth.AuthScheme;
40  import org.apache.http.auth.MalformedChallengeException;
41  import org.apache.http.auth.UsernamePasswordCredentials;
42  import org.apache.http.message.BasicHeader;
43  import org.apache.http.message.BasicHttpRequest;
44  import org.apache.http.protocol.BasicHttpContext;
45  import org.apache.http.protocol.HttpContext;
46  import org.apache.http.util.EncodingUtils;
47  import org.junit.Assert;
48  import org.junit.Test;
49  
50  /**
51   * Basic authentication test cases.
52   */
53  public class TestBasicScheme {
54  
55      @Test(expected=MalformedChallengeException.class)
56      public void testBasicAuthenticationWithNoRealm() throws Exception {
57          final String challenge = "Basic";
58          final Header header = new BasicHeader(AUTH.WWW_AUTH, challenge);
59          final AuthScheme authscheme = new BasicScheme();
60          authscheme.processChallenge(header);
61      }
62  
63      @Test
64      public void testBasicAuthenticationWith88591Chars() throws Exception {
65          final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
66          final StringBuilder buffer = new StringBuilder();
67          for (final int germanChar : germanChars) {
68              buffer.append((char)germanChar);
69          }
70  
71          final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("dh", buffer.toString());
72          final BasicScheme authscheme = new BasicScheme(Consts.ISO_8859_1);
73  
74          final HttpRequest request = new BasicHttpRequest("GET", "/");
75          final HttpContext context = new BasicHttpContext();
76          final Header header = authscheme.authenticate(creds, request, context);
77          Assert.assertEquals("Basic ZGg65C32Lfw=", header.getValue());
78      }
79  
80      @Test
81      public void testBasicAuthentication() throws Exception {
82          final UsernamePasswordCredentials creds =
83              new UsernamePasswordCredentials("testuser", "testpass");
84  
85          final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\"");
86  
87          final BasicScheme authscheme = new BasicScheme();
88          authscheme.processChallenge(challenge);
89  
90          final HttpRequest request = new BasicHttpRequest("GET", "/");
91          final HttpContext context = new BasicHttpContext();
92          final Header authResponse = authscheme.authenticate(creds, request, context);
93  
94          final String expected = "Basic " + EncodingUtils.getAsciiString(
95              Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass")));
96          Assert.assertEquals(AUTH.WWW_AUTH_RESP, authResponse.getName());
97          Assert.assertEquals(expected, authResponse.getValue());
98          Assert.assertEquals("test", authscheme.getRealm());
99          Assert.assertTrue(authscheme.isComplete());
100         Assert.assertFalse(authscheme.isConnectionBased());
101     }
102 
103     @Test
104     public void testBasicProxyAuthentication() throws Exception {
105         final UsernamePasswordCredentials creds =
106             new UsernamePasswordCredentials("testuser", "testpass");
107 
108         final Header challenge = new BasicHeader(AUTH.PROXY_AUTH, "Basic realm=\"test\"");
109 
110         final BasicScheme authscheme = new BasicScheme();
111         authscheme.processChallenge(challenge);
112 
113         final HttpRequest request = new BasicHttpRequest("GET", "/");
114         final HttpContext context = new BasicHttpContext();
115         final Header authResponse = authscheme.authenticate(creds, request, context);
116 
117         final String expected = "Basic " + EncodingUtils.getAsciiString(
118             Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass")));
119         Assert.assertEquals(AUTH.PROXY_AUTH_RESP, authResponse.getName());
120         Assert.assertEquals(expected, authResponse.getValue());
121         Assert.assertEquals("test", authscheme.getRealm());
122         Assert.assertTrue(authscheme.isComplete());
123         Assert.assertFalse(authscheme.isConnectionBased());
124     }
125 
126     @Test
127     public void testSerialization() throws Exception {
128         final Header challenge = new BasicHeader(AUTH.PROXY_AUTH, "Basic realm=\"test\"");
129 
130         final BasicScheme basicScheme = new BasicScheme();
131         basicScheme.processChallenge(challenge);
132 
133         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
134         final ObjectOutputStream out = new ObjectOutputStream(buffer);
135         out.writeObject(basicScheme);
136         out.flush();
137         final byte[] raw = buffer.toByteArray();
138         final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
139         final BasicScheme authScheme = (BasicScheme) in.readObject();
140 
141         Assert.assertEquals(basicScheme.getSchemeName(), authScheme.getSchemeName());
142         Assert.assertEquals(basicScheme.getRealm(), authScheme.getRealm());
143         Assert.assertEquals(basicScheme.isComplete(), authScheme.isComplete());
144 
145     }
146 
147 }