-
Notifications
You must be signed in to change notification settings - Fork 529
/
AlipayAuthenticationHandler.cs
301 lines (251 loc) · 11.9 KB
/
AlipayAuthenticationHandler.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/
using System.Globalization;
using System.Net;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
namespace AspNet.Security.OAuth.Alipay;
/// <summary>
/// Defines a handler for authentication using Alipay.
/// </summary>
public partial class AlipayAuthenticationHandler : OAuthHandler<AlipayAuthenticationOptions>
{
public AlipayAuthenticationHandler(
[NotNull] IOptionsMonitor<AlipayAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder)
: base(options, logger, encoder)
{
}
private const string AuthCode = "auth_code";
protected override Task<HandleRequestResult> HandleRemoteAuthenticateAsync()
{
if (TryStandardizeRemoteAuthenticateQuery(Request.Query, out var queryString))
{
Request.QueryString = queryString;
}
return base.HandleRemoteAuthenticateAsync();
}
protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OAuthCodeExchangeContext context)
{
// See https://opendocs.alipay.com/apis/api_9/alipay.system.oauth.token for details.
var tokenRequestParameters = new SortedDictionary<string, string?>()
{
["app_id"] = Options.ClientId,
["charset"] = "utf-8",
["code"] = context.Code,
["format"] = "JSON",
["grant_type"] = "authorization_code",
["method"] = "alipay.system.oauth.token",
["sign_type"] = "RSA2",
["timestamp"] = TimeProvider.GetUtcNow().ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
["version"] = "1.0",
};
tokenRequestParameters.Add("sign", GetRSA2Signature(tokenRequestParameters));
// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
if (context.Properties.Items.TryGetValue(OAuthConstants.CodeVerifierKey, out var codeVerifier))
{
tokenRequestParameters.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
}
var address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
using var response = await Backchannel.GetAsync(address, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
await Log.AccessTokenError(Logger, response, Context.RequestAborted);
return OAuthTokenResponse.Failed(new Exception("An error occurred while retrieving an access token."));
}
using var stream = await response.Content.ReadAsStreamAsync(Context.RequestAborted);
using var document = await JsonDocument.ParseAsync(stream);
var mainElement = document.RootElement.GetProperty("alipay_system_oauth_token_response");
if (!ValidateReturnCode(mainElement, out var code))
{
return OAuthTokenResponse.Failed(new Exception($"An error (Code:{code}) occurred while retrieving an access token."));
}
var payload = JsonDocument.Parse(mainElement.GetRawText());
return OAuthTokenResponse.Success(payload);
}
protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
// See https://opendocs.alipay.com/apis/api_2/alipay.user.info.share for details.
var parameters = new SortedDictionary<string, string?>()
{
["app_id"] = Options.ClientId,
["auth_token"] = tokens.AccessToken,
["charset"] = "utf-8",
["format"] = "JSON",
["method"] = "alipay.user.info.share",
["sign_type"] = "RSA2",
["timestamp"] = TimeProvider.GetUtcNow().ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
["version"] = "1.0",
};
parameters.Add("sign", GetRSA2Signature(parameters));
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);
using var response = await Backchannel.GetAsync(address, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
throw new HttpRequestException("An error occurred while retrieving user information.");
}
using var stream = await response.Content.ReadAsStreamAsync(Context.RequestAborted);
using var document = await JsonDocument.ParseAsync(stream);
var rootElement = document.RootElement;
if (!rootElement.TryGetProperty("alipay_user_info_share_response", out JsonElement mainElement))
{
var errorCode = rootElement.GetProperty("error_response").GetProperty("code").GetString()!;
throw new AuthenticationFailureException($"An error (Code:{errorCode}) occurred while retrieving user information.");
}
if (!ValidateReturnCode(mainElement, out var code))
{
throw new AuthenticationFailureException($"An error (Code:{code}) occurred while retrieving user information.");
}
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, mainElement.GetString("user_id")!, ClaimValueTypes.String, Options.ClaimsIssuer));
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, mainElement);
context.RunClaimActions();
await Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
}
protected override string FormatScope([NotNull] IEnumerable<string> scopes) => string.Join(',', Options.Scope);
/// <summary>
/// Check the code sent back by server for potential server errors.
/// </summary>
/// <param name="element">Main part of json document from response</param>
/// <param name="code">Returned code from server</param>
/// <remarks>See https://opendocs.alipay.com/open/common/105806 for details.</remarks>
/// <returns>True if succeed, otherwise false.</returns>
private static bool ValidateReturnCode(JsonElement element, out string code)
{
if (!element.TryGetProperty("code", out JsonElement codeElement))
{
code = string.Empty;
return true;
}
code = codeElement.GetString()!;
return code == "10000";
}
/// <summary>
/// Gets the RSA2(SHA256 with RSA) signature.
/// </summary>
/// <param name="sortedPairs">Sorted key-value pairs</param>
private string GetRSA2Signature([NotNull] SortedDictionary<string, string?> sortedPairs)
{
var builder = new StringBuilder(128);
foreach (var pair in sortedPairs)
{
if (string.IsNullOrEmpty(pair.Value))
{
continue;
}
builder.Append(pair.Key)
.Append('=')
.Append(pair.Value)
.Append('&');
}
var plainText = builder.ToString();
var plainBytes = Encoding.UTF8.GetBytes(plainText, 0, plainText.Length - 1); // Skip the last '&'
var privateKeyBytes = Convert.FromBase64String(Options.ClientSecret);
using var rsa = RSA.Create();
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
var encryptedBytes = rsa.SignData(plainBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(encryptedBytes);
}
/// <inheritdoc />
protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
{
var scopeParameter = properties.GetParameter<ICollection<string>>(OAuthChallengeProperties.ScopeKey);
var scope = scopeParameter != null ? FormatScope(scopeParameter) : FormatScope();
var parameters = new Dictionary<string, string?>
{
["app_id"] = Options.ClientId, // Used instead of "client_id"
["scope"] = scope,
["response_type"] = "code",
["redirect_uri"] = redirectUri,
};
if (Options.UsePkce)
{
var bytes = RandomNumberGenerator.GetBytes(256 / 8);
var codeVerifier = WebEncoders.Base64UrlEncode(bytes);
// Store this for use during the code redemption.
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
var challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
}
parameters["state"] = Options.StateDataFormat.Protect(properties);
return QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, parameters);
}
private static bool TryStandardizeRemoteAuthenticateQuery(IQueryCollection query, out QueryString queryString)
{
if (!query.TryGetValue(AuthCode, out var authCode))
{
queryString = default;
return false;
}
// Before: mydomain/signin-alipay?auth_code=xxx&state=xxx&...
// After: mydomain/signin-alipay?code=xxx&state=xxx&...
var queryParams = new List<KeyValuePair<string, StringValues>>(query.Count)
{
new("code", authCode)
};
foreach (var item in query)
{
switch (item.Key)
{
case "code":
case AuthCode: // No need in fact, skip it
break;
default:
queryParams.Add(item);
break;
}
}
queryString = QueryString.Create(queryParams);
return true;
}
private static partial class Log
{
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
{
UserProfileError(
logger,
response.StatusCode,
response.Headers.ToString(),
await response.Content.ReadAsStringAsync(cancellationToken));
}
internal static async Task AccessTokenError(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
{
AccessTokenError(
logger,
response.StatusCode,
response.Headers.ToString(),
await response.Content.ReadAsStringAsync(cancellationToken));
}
[LoggerMessage(1, LogLevel.Error, "An error occurred while retrieving the user profile: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
private static partial void UserProfileError(
ILogger logger,
HttpStatusCode status,
string headers,
string body);
[LoggerMessage(2, LogLevel.Error, "An error occurred while retrieving an access token: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
private static partial void AccessTokenError(
ILogger logger,
HttpStatusCode status,
string headers,
string body);
}
}