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.hc.client5.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.hc.client5.http.auth.AuthChallenge;
35  import org.apache.hc.client5.http.auth.AuthScheme;
36  import org.apache.hc.client5.http.auth.AuthScope;
37  import org.apache.hc.client5.http.auth.BearerToken;
38  import org.apache.hc.client5.http.auth.ChallengeType;
39  import org.apache.hc.client5.http.auth.CredentialsProvider;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.HttpRequest;
42  import org.apache.hc.core5.http.message.BasicHttpRequest;
43  import org.apache.hc.core5.http.message.BasicNameValuePair;
44  import org.junit.jupiter.api.Assertions;
45  import org.junit.jupiter.api.Test;
46  
47  /**
48   * Bearer authentication test cases.
49   */
50  public class TestBearerScheme {
51  
52      @Test
53      public void testBearerAuthenticationEmptyChallenge() throws Exception {
54          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "BEARER");
55          final AuthScheme authscheme = new BearerScheme();
56          authscheme.processChallenge(authChallenge, null);
57          Assertions.assertNull(authscheme.getRealm());
58      }
59  
60      @Test
61      public void testBearerAuthentication() throws Exception {
62          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Bearer",
63                  new BasicNameValuePair("realm", "test"));
64  
65          final AuthScheme authscheme = new BearerScheme();
66          authscheme.processChallenge(authChallenge, null);
67  
68          final HttpHost host  = new HttpHost("somehost", 80);
69          final CredentialsProvider credentialsProvider = CredentialsProviderBuilder.create()
70                  .add(new AuthScope(host, "test", null), new BearerToken("some token"))
71                  .build();
72  
73          final HttpRequest request = new BasicHttpRequest("GET", "/");
74          Assertions.assertTrue(authscheme.isResponseReady(host, credentialsProvider, null));
75          authscheme.generateAuthResponse(host, request, null);
76  
77          Assertions.assertEquals("test", authscheme.getRealm());
78          Assertions.assertTrue(authscheme.isChallengeComplete());
79          Assertions.assertFalse(authscheme.isConnectionBased());
80      }
81  
82      @Test
83      public void testSerialization() throws Exception {
84          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Bearer",
85                  new BasicNameValuePair("realm", "test"),
86                  new BasicNameValuePair("code", "read"));
87  
88          final AuthScheme authscheme = new BearerScheme();
89          authscheme.processChallenge(authChallenge, null);
90  
91          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
92          final ObjectOutputStream out = new ObjectOutputStream(buffer);
93          out.writeObject(authscheme);
94          out.flush();
95          final byte[] raw = buffer.toByteArray();
96          final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
97          final BearerScheme authcheme2 = (BearerScheme) in.readObject();
98  
99          Assertions.assertEquals(authcheme2.getName(), authcheme2.getName());
100         Assertions.assertEquals(authcheme2.getRealm(), authcheme2.getRealm());
101         Assertions.assertEquals(authcheme2.isChallengeComplete(), authcheme2.isChallengeComplete());
102     }
103 
104 }