-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Huawei provider. Signed-off-by: Vicente Yu <^@^>
- Loading branch information
Showing
12 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Integrating the Huawei Provider | ||
|
||
## Example | ||
|
||
```csharp | ||
services.AddAuthentication(options => /* Auth configuration */) | ||
.AddHuawei(options => | ||
{ | ||
options.ClientId = "my-client-id"; | ||
options.ClientSecret = "my-client-secret"; | ||
|
||
// Optionally. | ||
options.Scope.Add("profile"); | ||
options.Scope.Add("email"); | ||
|
||
// Optionally. | ||
options.FetchNickName = true; | ||
}); | ||
``` | ||
|
||
## Required Additional Settings | ||
|
||
_None._ | ||
|
||
## Optional Settings | ||
|
||
| Property Name | Property Type | Description | Default Value | | ||
|:--|:--|:--|:--| | ||
| `FetchNickName` | `bool` | When FetchNickName is set to false or not set, the anonymous account is returned. If the anonymous account is unavailable, the nickname is returned. When FetchNickName is set to true, the nickname is returned. If the nickname is unavailable, the anonymous account is returned. | `false` | | ||
|
||
### Scope | ||
Corresponding information, such as the profile picture and email address, can be obtained only if the app has the permission to obtain the information. | ||
* `profile` basic information of a HUAWEI ID, such as the profile picture and nickname. | ||
* `email` email address of a HUAWEI ID. |
20 changes: 20 additions & 0 deletions
20
src/AspNet.Security.OAuth.Huawei/AspNet.Security.OAuth.Huawei.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation> | ||
<PackageValidationBaselineVersion>6.0.13</PackageValidationBaselineVersion> | ||
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Description>ASP.NET Core security middleware enabling Huawei authentication.</Description> | ||
<Authors>Vicente Yu</Authors> | ||
<PackageTags>aspnetcore;authentication;huawei;oauth;security</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/AspNet.Security.OAuth.Huawei/HuaweiAuthenticationConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
namespace AspNet.Security.OAuth.Huawei; | ||
|
||
/// <summary> | ||
/// Contains constants specific to the <see cref="HuaweiAuthenticationHandler"/>. | ||
/// </summary> | ||
public static class HuaweiAuthenticationConstants | ||
{ | ||
public static class Claims | ||
{ | ||
public const string Avatar = "urn:huawei:avatar"; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/AspNet.Security.OAuth.Huawei/HuaweiAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
namespace AspNet.Security.OAuth.Huawei; | ||
|
||
/// <summary> | ||
/// Default values for Huawei authentication. | ||
/// </summary> | ||
public static class HuaweiAuthenticationDefaults | ||
{ | ||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
/// </summary> | ||
public const string AuthenticationScheme = "Huawei"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
/// </summary> | ||
public static readonly string DisplayName = "Huawei"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
/// </summary> | ||
public static readonly string CallbackPath = "/signin-huawei"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
/// </summary> | ||
public static readonly string Issuer = "Huawei"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string AuthorizationEndpoint = "https://oauth-login.cloud.huawei.com/oauth2/v3/authorize"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
/// </summary> | ||
public static readonly string TokenEndpoint = "https://oauth-login.cloud.huawei.com/oauth2/v3/token"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string UserInformationEndpoint = "https://account.cloud.huawei.com/rest.php"; | ||
} |
74 changes: 74 additions & 0 deletions
74
src/AspNet.Security.OAuth.Huawei/HuaweiAuthenticationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 AspNet.Security.OAuth.Huawei; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to add Huawei authentication capabilities to an HTTP application pipeline. | ||
/// </summary> | ||
public static class HuaweiAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddHuawei([NotNull] this AuthenticationBuilder builder) | ||
{ | ||
return builder.AddHuawei(HuaweiAuthenticationDefaults.AuthenticationScheme, _ => { }); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddHuawei( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] Action<HuaweiAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddHuawei(HuaweiAuthenticationDefaults.AuthenticationScheme, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <param name="scheme">The authentication scheme associated with this instance.</param> | ||
/// <param name="configuration">The delegate used to configure the Huawei options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddHuawei( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[NotNull] Action<HuaweiAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddHuawei(scheme, HuaweiAuthenticationDefaults.DisplayName, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <param name="scheme">The authentication scheme associated with this instance.</param> | ||
/// <param name="caption">The optional display name associated with this instance.</param> | ||
/// <param name="configuration">The delegate used to configure the Huawei options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddHuawei( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[CanBeNull] string caption, | ||
[NotNull] Action<HuaweiAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddOAuth<HuaweiAuthenticationOptions, HuaweiAuthenticationHandler>(scheme, caption, configuration); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/AspNet.Security.OAuth.Huawei/HuaweiAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace AspNet.Security.OAuth.Huawei; | ||
|
||
public partial class HuaweiAuthenticationHandler : OAuthHandler<HuaweiAuthenticationOptions> | ||
{ | ||
public HuaweiAuthenticationHandler( | ||
[NotNull] IOptionsMonitor<HuaweiAuthenticationOptions> options, | ||
[NotNull] ILoggerFactory logger, | ||
[NotNull] UrlEncoder encoder, | ||
[NotNull] ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
protected override async Task<AuthenticationTicket> CreateTicketAsync( | ||
[NotNull] ClaimsIdentity identity, | ||
[NotNull] AuthenticationProperties properties, | ||
[NotNull] OAuthTokenResponse tokens) | ||
{ | ||
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "nsp_svc", "GOpen.User.getInfo"); | ||
|
||
var content = new FormUrlEncodedContent(new[] | ||
{ | ||
new KeyValuePair<string, string>("getNickName", Options.FetchNickName ? "1" : "0"), | ||
new KeyValuePair<string, string>("access_token", tokens.AccessToken!) | ||
}); | ||
|
||
using var request = new HttpRequestMessage(HttpMethod.Post, address); | ||
request.Content = content; | ||
|
||
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted); | ||
throw new HttpRequestException("An error occurred while retrieving the user profile."); | ||
} | ||
|
||
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted)); | ||
|
||
var principal = new ClaimsPrincipal(identity); | ||
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement); | ||
context.RunClaimActions(); | ||
|
||
await Events.CreatingTicket(context); | ||
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); | ||
} | ||
|
||
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)); | ||
} | ||
|
||
[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, | ||
System.Net.HttpStatusCode status, | ||
string headers, | ||
string body); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/AspNet.Security.OAuth.Huawei/HuaweiAuthenticationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.Security.Claims; | ||
using static AspNet.Security.OAuth.Huawei.HuaweiAuthenticationConstants; | ||
|
||
namespace AspNet.Security.OAuth.Huawei; | ||
|
||
/// <summary> | ||
/// Defines a set of options used by <see cref="HuaweiAuthenticationHandler"/>. | ||
/// </summary> | ||
public class HuaweiAuthenticationOptions : OAuthOptions | ||
{ | ||
public HuaweiAuthenticationOptions() | ||
{ | ||
ClaimsIssuer = HuaweiAuthenticationDefaults.Issuer; | ||
CallbackPath = HuaweiAuthenticationDefaults.CallbackPath; | ||
|
||
AuthorizationEndpoint = HuaweiAuthenticationDefaults.AuthorizationEndpoint; | ||
TokenEndpoint = HuaweiAuthenticationDefaults.TokenEndpoint; | ||
UserInformationEndpoint = HuaweiAuthenticationDefaults.UserInformationEndpoint; | ||
|
||
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "openID"); | ||
ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName"); | ||
ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); | ||
ClaimActions.MapJsonKey(Claims.Avatar, "headPictureURL"); | ||
|
||
Scope.Add("openid"); | ||
} | ||
|
||
public bool FetchNickName { get; set; } | ||
} |
Oops, something went wrong.