1
+ package com .loopj .android .http ;
2
+
1
3
/*
2
4
Android Asynchronous Http Client
3
5
Copyright (c) 2011 James Smith <[email protected] >
4
6
https://loopj.com
5
-
6
7
Licensed under the Apache License, Version 2.0 (the "License");
7
8
you may not use this file except in compliance with the License.
8
9
You may obtain a copy of the License at
9
-
10
10
https://www.apache.org/licenses/LICENSE-2.0
11
-
12
11
Unless required by applicable law or agreed to in writing, software
13
12
distributed under the License is distributed on an "AS IS" BASIS,
14
13
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
14
See the License for the specific language governing permissions and
16
15
limitations under the License.
17
16
*/
18
17
19
- package com .loopj .android .http ;
20
-
21
18
import android .content .Context ;
19
+ import android .media .session .MediaSession ;
22
20
import android .os .Looper ;
23
21
24
22
import java .io .IOException ;
50
48
import cz .msebera .android .httpclient .HttpResponse ;
51
49
import cz .msebera .android .httpclient .HttpResponseInterceptor ;
52
50
import cz .msebera .android .httpclient .HttpVersion ;
51
+ import cz .msebera .android .httpclient .auth .AuthSchemeRegistry ;
53
52
import cz .msebera .android .httpclient .auth .AuthScope ;
54
53
import cz .msebera .android .httpclient .auth .AuthState ;
55
54
import cz .msebera .android .httpclient .auth .Credentials ;
76
75
import cz .msebera .android .httpclient .conn .ssl .SSLSocketFactory ;
77
76
import cz .msebera .android .httpclient .entity .HttpEntityWrapper ;
78
77
import cz .msebera .android .httpclient .impl .auth .BasicScheme ;
78
+ import cz .msebera .android .httpclient .impl .client .BasicCredentialsProvider ;
79
79
import cz .msebera .android .httpclient .impl .client .DefaultHttpClient ;
80
80
import cz .msebera .android .httpclient .impl .conn .tsccm .ThreadSafeClientConnManager ;
81
81
import cz .msebera .android .httpclient .params .BasicHttpParams ;
@@ -251,6 +251,11 @@ public void process(HttpResponse response, HttpContext context) {
251
251
httpClient .addRequestInterceptor (new HttpRequestInterceptor () {
252
252
@ Override
253
253
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
+
254
259
AuthState authState = (AuthState ) context .getAttribute (ClientContext .TARGET_AUTH_STATE );
255
260
CredentialsProvider credsProvider = (CredentialsProvider ) context .getAttribute (
256
261
ClientContext .CREDS_PROVIDER );
@@ -259,7 +264,11 @@ public void process(final HttpRequest request, final HttpContext context) throws
259
264
if (authState .getAuthScheme () == null ) {
260
265
AuthScope authScope = new AuthScope (targetHost .getHostName (), targetHost .getPort ());
261
266
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 ) {
263
272
authState .setAuthScheme (new BasicScheme ());
264
273
authState .setCredentials (creds );
265
274
}
@@ -791,6 +800,29 @@ public void removeHeader(String header) {
791
800
clientHeaderMap .remove (header );
792
801
}
793
802
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
+
794
826
/**
795
827
* Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
796
828
* setBasicAuth('username','password',AuthScope.ANY)
@@ -1635,4 +1667,4 @@ public void consumeContent() throws IOException {
1635
1667
super .consumeContent ();
1636
1668
}
1637
1669
}
1638
- }
1670
+ }
0 commit comments