-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathQQConnectOAuthHandler.cs
179 lines (147 loc) · 5.91 KB
/
QQConnectOAuthHandler.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Microsoft.Owin.Logging;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Infrastructure;
using OAuth2.QQConnect.Extensions;
using System;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
namespace OAuth2.QQConnect.Owin
{
public class QQConnectOAuthHandler : AuthenticationHandler<QQConnectOAuthOptions>
{
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
private QQConnectClient _innerClient;
private QQConnectClient InnerClient
{
get
{
if (_innerClient == null)
{
var qqConnectOptions = Options.BuildQQConnectOptions(GetRedirectUrl);
_innerClient = new QQConnectClient(_httpClient, qqConnectOptions);
}
return _innerClient;
}
}
public QQConnectOAuthHandler(ILogger logger, HttpClient httpClient)
{
_logger = logger;
_httpClient = httpClient;
}
public override async Task<bool> InvokeAsync()
{
if (Options.CallbackPath != Request.Path.Value)
{
return false;
}
var ticket = await AuthenticateAsync();
if (ticket?.Identity != null)
{
var identity = ticket.Identity;
if (identity.AuthenticationType != Options.SignInAsAuthenticationType)
{
identity = new ClaimsIdentity(
ticket.Identity.Claims,
Options.SignInAsAuthenticationType,
ticket.Identity.NameClaimType,
ticket.Identity.RoleClaimType);
}
Context.Authentication.SignIn(ticket.Properties, identity);
Context.Response.Redirect(ticket.Properties.RedirectUri);
}
else
{
_logger.WriteError("Invalid return state, unable to redirect.");
Response.StatusCode = 500;
}
return true;
}
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
{
AuthenticationProperties properties = null;
try
{
var code = Request.Query.Get("code");
var state = Request.Query.Get("state");
properties = Options.StateDataFormat.Unprotect(state);
if (properties == null)
{
return null;
}
if (!ValidateCorrelationId(properties, _logger))
{
return new AuthenticationTicket(null, properties);
}
if (code == null)
{
return new AuthenticationTicket(null, properties);
}
var token = await InnerClient.GetTokenAsync(
code,
Request.CallCancelled);
if (string.IsNullOrWhiteSpace(token.AccessToken))
{
_logger.WriteError("access_token was not found");
return new AuthenticationTicket(null, properties);
}
var openId = await InnerClient.GetOpenIdAsync(
token.AccessToken,
Request.CallCancelled);
if (string.IsNullOrWhiteSpace(openId.OpenId))
{
_logger.WriteError("openid was not found");
return new AuthenticationTicket(null, properties);
}
var user = await InnerClient.GetUserAsync(
token.AccessToken,
openId.OpenId,
Request.CallCancelled);
var qqConnectProfile = QQConnectProfile.From(Options.AuthenticationType, token, openId, user);
var identity = qqConnectProfile.BuildClaimsIdentity();
return new AuthenticationTicket(identity, properties);
}
catch (Exception ex)
{
_logger.WriteError("Authentication failed", ex);
return new AuthenticationTicket(null, properties);
}
}
protected override Task ApplyResponseChallengeAsync()
{
if (Response.StatusCode != 401)
{
return Task.FromResult<object>(null);
}
var authenticationChallenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);
if (authenticationChallenge != null)
{
var authenticationProperties = authenticationChallenge.Properties;
if (string.IsNullOrWhiteSpace(authenticationProperties.RedirectUri))
{
authenticationProperties.RedirectUri = Request.Uri.ToString();
}
GenerateCorrelationId(authenticationProperties);
var authorizationUrl = BuildAuthorizationUrl(authenticationProperties);
Context.Response.Redirect(authorizationUrl);
}
return Task.FromResult<object>(null);
}
private string BuildAuthorizationUrl(AuthenticationProperties authenticationProperties)
{
var qqConnectProperties = authenticationProperties.Dictionary.GetQQConnectProperties();
authenticationProperties.Dictionary.RemoveQQConnectProperties();
var state = Options.StateDataFormat.Protect(authenticationProperties);
return InnerClient.BuildAuthorizationUrl(qqConnectProperties, state);
}
private string GetRedirectUrl()
{
return Request.Scheme +
Uri.SchemeDelimiter +
Request.Host +
Request.PathBase +
Options.CallbackPath;
}
}
}