Skip to content

Commit c36c45b

Browse files
committed
Merge pull request android-async-http#988 from lelemm/master
Bearer Authentication
2 parents 01d8ba9 + cffb89b commit c36c45b

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1+
package com.loopj.android.http;
2+
13
/*
24
Android Asynchronous Http Client
35
Copyright (c) 2011 James Smith <[email protected]>
46
https://loopj.com
5-
67
Licensed under the Apache License, Version 2.0 (the "License");
78
you may not use this file except in compliance with the License.
89
You may obtain a copy of the License at
9-
1010
https://www.apache.org/licenses/LICENSE-2.0
11-
1211
Unless required by applicable law or agreed to in writing, software
1312
distributed under the License is distributed on an "AS IS" BASIS,
1413
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1514
See the License for the specific language governing permissions and
1615
limitations under the License.
1716
*/
1817

19-
package com.loopj.android.http;
20-
2118
import android.content.Context;
19+
import android.media.session.MediaSession;
2220
import android.os.Looper;
2321

2422
import java.io.IOException;
@@ -50,6 +48,7 @@
5048
import cz.msebera.android.httpclient.HttpResponse;
5149
import cz.msebera.android.httpclient.HttpResponseInterceptor;
5250
import cz.msebera.android.httpclient.HttpVersion;
51+
import cz.msebera.android.httpclient.auth.AuthSchemeRegistry;
5352
import cz.msebera.android.httpclient.auth.AuthScope;
5453
import cz.msebera.android.httpclient.auth.AuthState;
5554
import cz.msebera.android.httpclient.auth.Credentials;
@@ -76,6 +75,7 @@
7675
import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory;
7776
import cz.msebera.android.httpclient.entity.HttpEntityWrapper;
7877
import cz.msebera.android.httpclient.impl.auth.BasicScheme;
78+
import cz.msebera.android.httpclient.impl.client.BasicCredentialsProvider;
7979
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
8080
import cz.msebera.android.httpclient.impl.conn.tsccm.ThreadSafeClientConnManager;
8181
import cz.msebera.android.httpclient.params.BasicHttpParams;
@@ -251,6 +251,11 @@ public void process(HttpResponse response, HttpContext context) {
251251
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
252252
@Override
253253
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
254+
255+
AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
256+
authSchemeRegistry.register("Bearer", new BearerAuthSchemeFactory());
257+
context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
258+
254259
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
255260
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
256261
ClientContext.CREDS_PROVIDER);
@@ -259,7 +264,11 @@ public void process(final HttpRequest request, final HttpContext context) throws
259264
if (authState.getAuthScheme() == null) {
260265
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
261266
Credentials creds = credsProvider.getCredentials(authScope);
262-
if (creds != null) {
267+
if(creds instanceof TokenCredentials) {
268+
authState.setAuthScheme(new BearerAuthSchemeFactory.BearerAuthScheme());
269+
authState.setCredentials(creds);
270+
}
271+
else if (creds != null) {
263272
authState.setAuthScheme(new BasicScheme());
264273
authState.setCredentials(creds);
265274
}
@@ -791,6 +800,29 @@ public void removeHeader(String header) {
791800
clientHeaderMap.remove(header);
792801
}
793802

803+
/**
804+
* Sets bearer authentication for the request. Uses AuthScope.ANY. This is the same as
805+
* setBearerAuth('token',AuthScope.ANY, false)
806+
* @param token Bearer Token
807+
*/
808+
public void setBearerAuth(String token) {
809+
setBearerAuth(token, AuthScope.ANY, false);
810+
}
811+
812+
813+
/**
814+
* Sets bearer authentication for the request. You should pass in your AuthScope for security. It
815+
* should be like this setBearerAuth("token", new AuthScope("host",port,AuthScope.ANY_REALM), false)
816+
* @param token Bearer Token
817+
* @param scope an AuthScope object
818+
* @param preemptive sets authorization in preemptive manner
819+
*/
820+
public void setBearerAuth(String token, AuthScope scope, boolean preemptive) {
821+
TokenCredentials credentials = new TokenCredentials(token);
822+
setCredentials(scope, credentials);
823+
setAuthenticationPreemptive(preemptive);
824+
}
825+
794826
/**
795827
* Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
796828
* setBasicAuth('username','password',AuthScope.ANY)
@@ -1635,4 +1667,4 @@ public void consumeContent() throws IOException {
16351667
super.consumeContent();
16361668
}
16371669
}
1638-
}
1670+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.loopj.android.http;
2+
3+
import cz.msebera.android.httpclient.Header;
4+
import cz.msebera.android.httpclient.HttpRequest;
5+
import cz.msebera.android.httpclient.auth.AUTH;
6+
import cz.msebera.android.httpclient.auth.AuthScheme;
7+
import cz.msebera.android.httpclient.auth.AuthSchemeFactory;
8+
import cz.msebera.android.httpclient.auth.AuthenticationException;
9+
import cz.msebera.android.httpclient.auth.ContextAwareAuthScheme;
10+
import cz.msebera.android.httpclient.auth.Credentials;
11+
import cz.msebera.android.httpclient.auth.MalformedChallengeException;
12+
import cz.msebera.android.httpclient.message.BufferedHeader;
13+
import cz.msebera.android.httpclient.params.HttpParams;
14+
import cz.msebera.android.httpclient.protocol.HttpContext;
15+
import cz.msebera.android.httpclient.util.CharArrayBuffer;
16+
17+
/**
18+
* Created by chase on 08/10/2015.
19+
*/
20+
public class BearerAuthSchemeFactory implements AuthSchemeFactory {
21+
22+
@Override
23+
public AuthScheme newInstance(HttpParams params) {
24+
return new BearerAuthScheme();
25+
}
26+
27+
public static class BearerAuthScheme implements ContextAwareAuthScheme {
28+
private boolean complete = false;
29+
30+
@Override
31+
public void processChallenge(Header header) throws MalformedChallengeException {
32+
this.complete = true;
33+
}
34+
35+
@Override
36+
public Header authenticate(Credentials credentials, HttpRequest request) throws AuthenticationException {
37+
return authenticate(credentials, request, null);
38+
}
39+
40+
@Override
41+
public Header authenticate(Credentials credentials, HttpRequest request, HttpContext httpContext)
42+
throws AuthenticationException {
43+
CharArrayBuffer buffer = new CharArrayBuffer(32);
44+
buffer.append(AUTH.WWW_AUTH_RESP);
45+
buffer.append(": Bearer ");
46+
buffer.append(credentials.getUserPrincipal().getName());
47+
return new BufferedHeader(buffer);
48+
}
49+
50+
@Override
51+
public String getSchemeName() {
52+
return "Bearer";
53+
}
54+
55+
@Override
56+
public String getParameter(String name) {
57+
return null;
58+
}
59+
60+
@Override
61+
public String getRealm() {
62+
return null;
63+
}
64+
65+
@Override
66+
public boolean isConnectionBased() {
67+
return false;
68+
}
69+
70+
@Override
71+
public boolean isComplete() {
72+
return this.complete;
73+
}
74+
}
75+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.loopj.android.http;
2+
3+
/**
4+
* Created by chase on 08/10/2015.
5+
*/
6+
import java.security.Principal;
7+
8+
import cz.msebera.android.httpclient.auth.BasicUserPrincipal;
9+
import cz.msebera.android.httpclient.auth.Credentials;
10+
11+
public class TokenCredentials implements Credentials {
12+
private Principal userPrincipal;
13+
14+
public TokenCredentials(String token) {
15+
this.userPrincipal = new BasicUserPrincipal(token);
16+
}
17+
18+
@Override
19+
public Principal getUserPrincipal() {
20+
return userPrincipal;
21+
}
22+
23+
@Override
24+
public String getPassword() {
25+
return null;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)