OLD | NEW |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Collections.Generic; | 18 using System.Collections.Generic; |
19 using System.Linq; | 19 using System.Linq; |
20 using System.Net; | 20 using System.Net; |
21 using System.Net.Http; | 21 using System.Net.Http; |
22 using System.Text; | 22 using System.Text; |
| 23 using System.Threading; |
| 24 using System.Threading.Tasks; |
23 | 25 |
24 using Google.Apis.Http; | 26 using Google.Apis.Http; |
25 using Google.Apis.Testing; | 27 using Google.Apis.Testing; |
26 | 28 |
27 namespace Google.Apis.Authentication | 29 namespace Google.Apis.Authentication |
28 { | 30 { |
29 // TODO(peleyal): this class should be removed when implementing our OAuth2
library. Our future credential | 31 // TODO(peleyal): this class should be removed when implementing our OAuth2
library. Our future credential |
30 // object will implement both IHttpExecuteInterceptor and IHttpUnsuccessfulR
esponseHandler | 32 // object will implement both IHttpExecuteInterceptor and IHttpUnsuccessfulR
esponseHandler |
31 [VisibleForTestOnly] | 33 [VisibleForTestOnly] |
32 internal class AuthenticatorInterceptor : IHttpExecuteInterceptor | 34 internal class AuthenticatorInterceptor : IHttpExecuteInterceptor |
33 { | 35 { |
34 private IAuthenticator Authenticator { get; set; } | 36 private IAuthenticator Authenticator { get; set; } |
35 | 37 |
36 /// <summary> Constructs a new Authenticator interceptor. </summary> | 38 /// <summary> Constructs a new Authenticator interceptor. </summary> |
37 public AuthenticatorInterceptor(IAuthenticator authenticator) | 39 public AuthenticatorInterceptor(IAuthenticator authenticator) |
38 { | 40 { |
39 Authenticator = authenticator; | 41 Authenticator = authenticator; |
40 } | 42 } |
41 | 43 |
42 public void Intercept(HttpRequestMessage request) | 44 public Task InterceptAsync(HttpRequestMessage request, CancellationToken
cancellationToken) |
43 { | 45 { |
44 // we create a regular WebHttpRequest because our Authenticator (and
DotNetOpenAuth) implementation | 46 // we create a regular WebHttpRequest because our Authenticator (and
DotNetOpenAuth) implementation |
45 // works with it only.· | 47 // works with it only.· |
46 // We are going to create our own OAuth2 implementation and as a res
ult this hack will be removed! | 48 // We are going to create our own OAuth2 implementation and as a res
ult this hack will be removed! |
47 var authorization = HttpRequestHeader.Authorization.ToString(); | 49 var authorization = HttpRequestHeader.Authorization.ToString(); |
48 var request2 = WebRequest.Create("http://www.hack.com") as HttpWebRe
quest; | 50 var request2 = WebRequest.Create("http://www.hack.com") as HttpWebRe
quest; |
49 Authenticator.ApplyAuthenticationToRequest(request2); | 51 Authenticator.ApplyAuthenticationToRequest(request2); |
50 if (request2.Headers[authorization] != null) | 52 if (request2.Headers[authorization] != null) |
51 { | 53 { |
52 request.Headers.Remove(authorization); | 54 request.Headers.Remove(authorization); |
53 request.Headers.Add(authorization, request2.Headers[authorizatio
n]); | 55 request.Headers.Add(authorization, request2.Headers[authorizatio
n]); |
54 } | 56 } |
| 57 |
| 58 return TaskEx.Delay(0); |
55 } | 59 } |
56 } | 60 } |
57 | 61 |
58 // TODO(peleyal): this class should be removed when implementing our OAuth2
library. Our future credential | 62 // TODO(peleyal): this class should be removed when implementing our OAuth2
library. Our future credential |
59 // object will implement both IHttpExecuteInterceptor and IHttpUnsuccessfulR
esponseHandler | 63 // object will implement both IHttpExecuteInterceptor and IHttpUnsuccessfulR
esponseHandler |
60 internal class AuthenticatorMessageHandlerInitializer : IConfigurableHttpCli
entInitializer | 64 internal class AuthenticatorMessageHandlerInitializer : IConfigurableHttpCli
entInitializer |
61 { | 65 { |
62 private IAuthenticator Authenticator { get; set; } | 66 private IAuthenticator Authenticator { get; set; } |
63 | 67 |
64 /// <summary> Constructs a new Authenticator message handler initializer
. </summary> | 68 /// <summary> Constructs a new Authenticator message handler initializer
. </summary> |
65 public AuthenticatorMessageHandlerInitializer(IAuthenticator authenticat
or) | 69 public AuthenticatorMessageHandlerInitializer(IAuthenticator authenticat
or) |
66 { | 70 { |
67 Authenticator = authenticator; | 71 Authenticator = authenticator; |
68 } | 72 } |
69 | 73 |
70 public void Initialize(ConfigurableHttpClient httpClient) | 74 public void Initialize(ConfigurableHttpClient httpClient) |
71 { | 75 { |
72 httpClient.MessageHandler.ExecuteInterceptors.Add(new AuthenticatorI
nterceptor(Authenticator)); | 76 httpClient.MessageHandler.ExecuteInterceptors.Add(new AuthenticatorI
nterceptor(Authenticator)); |
73 var unsuccessfulHandler = Authenticator as IHttpUnsuccessfulResponse
Handler; | 77 var unsuccessfulHandler = Authenticator as IHttpUnsuccessfulResponse
Handler; |
74 if (unsuccessfulHandler != null) | 78 if (unsuccessfulHandler != null) |
75 { | 79 { |
76 httpClient.MessageHandler.UnsuccessfulResponseHandlers.Add(unsuc
cessfulHandler); | 80 httpClient.MessageHandler.UnsuccessfulResponseHandlers.Add(unsuc
cessfulHandler); |
77 } | 81 } |
78 } | 82 } |
79 } | 83 } |
80 } | 84 } |
OLD | NEW |