-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthMessageHandler.cs
77 lines (62 loc) · 2.93 KB
/
AuthMessageHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Tavis.Auth
{
public class AuthMessageHandler : DelegatingHandler
{
private readonly CredentialService _authorizationService;
public AuthMessageHandler(HttpMessageHandler innerHandler, CredentialService authorizationService)
{
InnerHandler = innerHandler;
_authorizationService = authorizationService;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Attempt to reuse last credentials used for the same origin server (Preauthenticate)
if (request.Headers.Authorization == null)
{
request.Headers.Authorization = _authorizationService.CreateAuthenticationHeaderFromRequest(request);
}
_authorizationService.CreateGatewayAuthKeys(request);
var response = await base.SendAsync(request, cancellationToken);
// If request failed and challenge issued
if (response.StatusCode == HttpStatusCode.Unauthorized && response.Headers.WwwAuthenticate.Count > 0)
{
// Can't automatically resend the request if it is not buffered.
// Not sure how to detect this
var authHeader = _authorizationService.CreateAuthenticationHeaderFromChallenge(request, response.Headers.WwwAuthenticate);
if (authHeader != null)
{
var newRequest = await CopyRequest(request);
newRequest.Headers.Authorization = authHeader;
// Resend request with auth header based on challenge
response = await base.SendAsync(newRequest, cancellationToken);
}
}
return response;
}
private static async Task<HttpRequestMessage> CopyRequest(HttpRequestMessage oldRequest) {
var newrequest = new HttpRequestMessage(oldRequest.Method, oldRequest.RequestUri);
foreach (var header in oldRequest.Headers) {
newrequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
foreach (var property in oldRequest.Properties) {
newrequest.Properties.Add(property);
}
if (oldRequest.Content != null)
{
var stream = await oldRequest.Content.ReadAsStreamAsync();
if (stream.Position != 0)
{
if (!stream.CanSeek) throw new Exception("Cannot resend this request as the content is not re-readable");
stream.Position = 0;
}
newrequest.Content = new StreamContent(stream);
}
return newrequest;
}
}
}