Skip to content

Commit cd594d9

Browse files
authored
Add Huawei provider (#742)
Add Huawei provider. Signed-off-by: Vicente Yu <^@^>
1 parent 3e256c8 commit cd594d9

12 files changed

+394
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Kroge
290290
EndProject
291291
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Xumm", "src\AspNet.Security.OAuth.Xumm\AspNet.Security.OAuth.Xumm.csproj", "{8E42EF81-A630-4BDB-B642-3F20C863F9BE}"
292292
EndProject
293+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Huawei", "src\AspNet.Security.OAuth.Huawei\AspNet.Security.OAuth.Huawei.csproj", "{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}"
294+
EndProject
293295
Global
294296
GlobalSection(SolutionConfigurationPlatforms) = preSolution
295297
Debug|Any CPU = Debug|Any CPU
@@ -664,6 +666,10 @@ Global
664666
{8E42EF81-A630-4BDB-B642-3F20C863F9BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
665667
{8E42EF81-A630-4BDB-B642-3F20C863F9BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
666668
{8E42EF81-A630-4BDB-B642-3F20C863F9BE}.Release|Any CPU.Build.0 = Release|Any CPU
669+
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
670+
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Debug|Any CPU.Build.0 = Debug|Any CPU
671+
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Release|Any CPU.ActiveCfg = Release|Any CPU
672+
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Release|Any CPU.Build.0 = Release|Any CPU
667673
EndGlobalSection
668674
GlobalSection(SolutionProperties) = preSolution
669675
HideSolutionNode = FALSE
@@ -767,6 +773,7 @@ Global
767773
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
768774
{8C7A98A6-5F61-492B-980D-0A9F5F9F5C73} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
769775
{8E42EF81-A630-4BDB-B642-3F20C863F9BE} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
776+
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
770777
EndGlobalSection
771778
GlobalSection(ExtensibilityGlobals) = postSolution
772779
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
155155
| Gitter | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Gitter?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Gitter/ "Download AspNet.Security.OAuth.Gitter from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Gitter?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Gitter "Download AspNet.Security.OAuth.Gitter from MyGet.org") | [Documentation](https://developer.gitter.im/docs/authentication "Gitter developer documentation") |
156156
| Harvest | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Harvest?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Harvest/ "Download AspNet.Security.OAuth.Harvest from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Harvest?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Harvest "Download AspNet.Security.OAuth.Harvest from MyGet.org") | [Documentation](https://help.getharvest.com/api-v1/authentication/authentication/oauth/ "Harvest developer documentation") |
157157
| HealthGraph (Runkeeper) | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.HealthGraph?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.HealthGraph/ "Download AspNet.Security.OAuth.HealthGraph from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.HealthGraph?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.HealthGraph "Download AspNet.Security.OAuth.HealthGraph from MyGet.org") | N/A |
158+
| Huawei | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Huawei?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Huawei/ "Download AspNet.Security.OAuth.Huawei from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Huawei?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Huawei "Download AspNet.Security.OAuth.Huawei from MyGet.org") | [Documentation](https://developer.huawei.com/consumer/en/hms/huawei-accountkit "Huawei developer documentation") |
158159
| HubSpot | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.HubSpot?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.HubSpot/ "Download AspNet.Security.OAuth.HubSpot from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.HubSpot?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.HubSpot "Download AspNet.Security.OAuth.HubSpot from MyGet.org") | [Documentation](https://developers.hubspot.com/docs "HubSpot developer documentation") |
159160
| Imgur | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Imgur?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Imgur/ "Download AspNet.Security.OAuth.Imgur from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Imgur?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Imgur "Download AspNet.Security.OAuth.Imgur from MyGet.org") | [Documentation](https://apidocs.imgur.com/?version=latest#authorization-and-oauth "Imgur developer documentation") |
160161
| Instagram | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Instagram?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Instagram/ "Download AspNet.Security.OAuth.Instagram from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Instagram?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Instagram "Download AspNet.Security.OAuth.Instagram from MyGet.org") | [Documentation](https://www.instagram.com/developer/authentication/ "Instagram developer documentation") |

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ covered by the section above.
5353
| Foursquare | _Optional_ | [Documentation](foursquare.md "Foursquare provider documentation") |
5454
| GitHub | _Optional_ | [Documentation](github.md "GitHub provider documentation") |
5555
| Gitee | _Optional_ | [Documentation](gitee.md "Gitee provider documentation") |
56+
| Huawei | _Optional_ | [Documentation](huawei.md "Huawei provider documentation") |
5657
| Instagram | _Optional_ | [Documentation](instagram.md "Instagram provider documentation") |
5758
| Kloudless | _Optional_ | [Documentation](kloudless.md "Kloudless provider documentation") |
5859
| Line | _Optional_ | [Documentation](line.md "Line provider documentation") |

docs/huawei.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Integrating the Huawei Provider
2+
3+
## Example
4+
5+
```csharp
6+
services.AddAuthentication(options => /* Auth configuration */)
7+
.AddHuawei(options =>
8+
{
9+
options.ClientId = "my-client-id";
10+
options.ClientSecret = "my-client-secret";
11+
12+
// Optionally.
13+
options.Scope.Add("profile");
14+
options.Scope.Add("email");
15+
16+
// Optionally.
17+
options.FetchNickName = true;
18+
});
19+
```
20+
21+
## Required Additional Settings
22+
23+
_None._
24+
25+
## Optional Settings
26+
27+
| Property Name | Property Type | Description | Default Value |
28+
|:--|:--|:--|:--|
29+
| `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` |
30+
31+
### Scope
32+
Corresponding information, such as the profile picture and email address, can be obtained only if the app has the permission to obtain the information.
33+
* `profile` basic information of a HUAWEI ID, such as the profile picture and nickname.
34+
* `email` email address of a HUAWEI ID.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
5+
<PackageValidationBaselineVersion>6.0.13</PackageValidationBaselineVersion>
6+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
<PropertyGroup>
10+
<Description>ASP.NET Core security middleware enabling Huawei authentication.</Description>
11+
<Authors>Vicente Yu</Authors>
12+
<PackageTags>aspnetcore;authentication;huawei;oauth;security</PackageTags>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
17+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
namespace AspNet.Security.OAuth.Huawei;
8+
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="HuaweiAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class HuaweiAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string Avatar = "urn:huawei:avatar";
17+
}
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
namespace AspNet.Security.OAuth.Huawei;
8+
9+
/// <summary>
10+
/// Default values for Huawei authentication.
11+
/// </summary>
12+
public static class HuaweiAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "Huawei";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "Huawei";
23+
24+
/// <summary>
25+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
26+
/// </summary>
27+
public static readonly string CallbackPath = "/signin-huawei";
28+
29+
/// <summary>
30+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
31+
/// </summary>
32+
public static readonly string Issuer = "Huawei";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://oauth-login.cloud.huawei.com/oauth2/v3/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://account.cloud.huawei.com/rest.php";
48+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using AspNet.Security.OAuth.Huawei;
8+
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
/// <summary>
12+
/// Extension methods to add Huawei authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class HuaweiAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities.
19+
/// </summary>
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
22+
public static AuthenticationBuilder AddHuawei([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddHuawei(HuaweiAuthenticationDefaults.AuthenticationScheme, _ => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities.
30+
/// </summary>
31+
/// <param name="builder">The authentication builder.</param>
32+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
33+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
34+
public static AuthenticationBuilder AddHuawei(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<HuaweiAuthenticationOptions> configuration)
37+
{
38+
return builder.AddHuawei(HuaweiAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities.
44+
/// </summary>
45+
/// <param name="builder">The authentication builder.</param>
46+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
47+
/// <param name="configuration">The delegate used to configure the Huawei options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddHuawei(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<HuaweiAuthenticationOptions> configuration)
53+
{
54+
return builder.AddHuawei(scheme, HuaweiAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="HuaweiAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables Huawei authentication capabilities.
60+
/// </summary>
61+
/// <param name="builder">The authentication builder.</param>
62+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
63+
/// <param name="caption">The optional display name associated with this instance.</param>
64+
/// <param name="configuration">The delegate used to configure the Huawei options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddHuawei(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<HuaweiAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<HuaweiAuthenticationOptions, HuaweiAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Security.Claims;
8+
using System.Text.Encodings.Web;
9+
using System.Text.Json;
10+
using Microsoft.AspNetCore.WebUtilities;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
14+
namespace AspNet.Security.OAuth.Huawei;
15+
16+
public partial class HuaweiAuthenticationHandler : OAuthHandler<HuaweiAuthenticationOptions>
17+
{
18+
public HuaweiAuthenticationHandler(
19+
[NotNull] IOptionsMonitor<HuaweiAuthenticationOptions> options,
20+
[NotNull] ILoggerFactory logger,
21+
[NotNull] UrlEncoder encoder,
22+
[NotNull] ISystemClock clock)
23+
: base(options, logger, encoder, clock)
24+
{
25+
}
26+
27+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
28+
[NotNull] ClaimsIdentity identity,
29+
[NotNull] AuthenticationProperties properties,
30+
[NotNull] OAuthTokenResponse tokens)
31+
{
32+
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "nsp_svc", "GOpen.User.getInfo");
33+
34+
var content = new FormUrlEncodedContent(new[]
35+
{
36+
new KeyValuePair<string, string>("getNickName", Options.FetchNickName ? "1" : "0"),
37+
new KeyValuePair<string, string>("access_token", tokens.AccessToken!)
38+
});
39+
40+
using var request = new HttpRequestMessage(HttpMethod.Post, address);
41+
request.Content = content;
42+
43+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
44+
if (!response.IsSuccessStatusCode)
45+
{
46+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
47+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
48+
}
49+
50+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
51+
52+
var principal = new ClaimsPrincipal(identity);
53+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
54+
context.RunClaimActions();
55+
56+
await Events.CreatingTicket(context);
57+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
58+
}
59+
60+
private static partial class Log
61+
{
62+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
63+
{
64+
UserProfileError(
65+
logger,
66+
response.StatusCode,
67+
response.Headers.ToString(),
68+
await response.Content.ReadAsStringAsync(cancellationToken));
69+
}
70+
71+
[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}.")]
72+
private static partial void UserProfileError(
73+
ILogger logger,
74+
System.Net.HttpStatusCode status,
75+
string headers,
76+
string body);
77+
}
78+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Security.Claims;
8+
using static AspNet.Security.OAuth.Huawei.HuaweiAuthenticationConstants;
9+
10+
namespace AspNet.Security.OAuth.Huawei;
11+
12+
/// <summary>
13+
/// Defines a set of options used by <see cref="HuaweiAuthenticationHandler"/>.
14+
/// </summary>
15+
public class HuaweiAuthenticationOptions : OAuthOptions
16+
{
17+
public HuaweiAuthenticationOptions()
18+
{
19+
ClaimsIssuer = HuaweiAuthenticationDefaults.Issuer;
20+
CallbackPath = HuaweiAuthenticationDefaults.CallbackPath;
21+
22+
AuthorizationEndpoint = HuaweiAuthenticationDefaults.AuthorizationEndpoint;
23+
TokenEndpoint = HuaweiAuthenticationDefaults.TokenEndpoint;
24+
UserInformationEndpoint = HuaweiAuthenticationDefaults.UserInformationEndpoint;
25+
26+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "openID");
27+
ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName");
28+
ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
29+
ClaimActions.MapJsonKey(Claims.Avatar, "headPictureURL");
30+
31+
Scope.Add("openid");
32+
}
33+
34+
public bool FetchNickName { get; set; }
35+
}

0 commit comments

Comments
 (0)