Skip to content

Commit d096da5

Browse files
author
Chris Sung
authored
Add Naver Provider (#665)
* Add Naver Provider
1 parent c2cecd1 commit d096da5

File tree

10 files changed

+389
-0
lines changed

10 files changed

+389
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Xero"
278278
EndProject
279279
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.DigitalOcean", "src\AspNet.Security.OAuth.DigitalOcean\AspNet.Security.OAuth.DigitalOcean.csproj", "{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593}"
280280
EndProject
281+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Naver", "src\AspNet.Security.OAuth.Naver\AspNet.Security.OAuth.Naver.csproj", "{289A91E9-81A9-422D-9CCD-12819081A29A}"
282+
EndProject
281283
Global
282284
GlobalSection(SolutionConfigurationPlatforms) = preSolution
283285
Debug|Any CPU = Debug|Any CPU
@@ -632,6 +634,10 @@ Global
632634
{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593}.Debug|Any CPU.Build.0 = Debug|Any CPU
633635
{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593}.Release|Any CPU.ActiveCfg = Release|Any CPU
634636
{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593}.Release|Any CPU.Build.0 = Release|Any CPU
637+
{289A91E9-81A9-422D-9CCD-12819081A29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
638+
{289A91E9-81A9-422D-9CCD-12819081A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU
639+
{289A91E9-81A9-422D-9CCD-12819081A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU
640+
{289A91E9-81A9-422D-9CCD-12819081A29A}.Release|Any CPU.Build.0 = Release|Any CPU
635641
EndGlobalSection
636642
GlobalSection(SolutionProperties) = preSolution
637643
HideSolutionNode = FALSE
@@ -730,6 +736,7 @@ Global
730736
{82314BA9-8AB2-46B9-AB12-9109619B8331} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
731737
{1F869094-FAC8-49B5-92A4-706C028CDF93} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
732738
{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
739+
{289A91E9-81A9-422D-9CCD-12819081A29A} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
733740
EndGlobalSection
734741
GlobalSection(ExtensibilityGlobals) = postSolution
735742
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ We would love it if you could help contributing to this repository.
5252
* [Andrii Chebukin](https://github.com/xperiandri)
5353
* [Anthony Yates](https://github.com/AnthonyYates)
5454
* [Chino Chang](https://github.com/kinosang)
55+
* [Chris Sung](https://github.com/christallire)
5556
* [CoCo Lin](https://github.com/linmasaki)
5657
* [Dave Timmins](https://github.com/davetimmins)
5758
* [Dmitry Popov](https://github.com/justdmitry)
@@ -162,6 +163,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
162163
| Mixcloud | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Mixcloud?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Mixcloud/ "Download AspNet.Security.OAuth.Mixcloud from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Mixcloud?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Mixcloud "Download AspNet.Security.OAuth.Mixcloud from MyGet.org") | [Documentation](https://www.mixcloud.com/developers/#authorization "Mixcloud developer documentation") |
163164
| Moodle | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Moodle?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Moodle/ "Download AspNet.Security.OAuth.Moodle from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Moodle?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Moodle "Download AspNet.Security.OAuth.Moodle from MyGet.org") | [Documentation](https://github.com/HIT-ReFreSH/moodle-local_oauth "Moodle OAuth2 plugin developer documentation") |
164165
| Myob | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Myob?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Myob/ "Download AspNet.Security.OAuth.Myob from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Myob?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Myob "Download AspNet.Security.OAuth.Myob from MyGet.org") | [Documentation](https://developer.myob.com/api/accountright/api-overview/authentication/ "Myob developer documentation") |
166+
| Naver | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Naver?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Naver/ "Download AspNet.Security.OAuth.Naver from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Naver?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Naver "Download AspNet.Security.OAuth.Naver from MyGet.org") | [Documentation](https://developers.naver.com/docs/login/api/api.md "Naver login API developer documentation") |
165167
| NetEase | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.NetEase?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.NetEase/ "Download AspNet.Security.OAuth.NetEase from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.NetEase?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.NetEase "Download AspNet.Security.OAuth.NetEase from MyGet.org") | [Documentation](https://reg.163.com/help/help_oauth2.html "NetEase developer documentation") |
166168
| Nextcloud | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Nextcloud?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Nextcloud/ "Download AspNet.Security.OAuth.Nextcloud from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Nextcloud?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Nextcloud "Download AspNet.Security.OAuth.Nextcloud from MyGet.org") | [Documentation](https://docs.nextcloud.com/server/14/admin_manual/configuration_server/oauth2.html "Nextcloud developer documentation") [User EndPoint Documentation](https://docs.nextcloud.com/server/15/developer_manual/client_apis/OCS/index.html#user-metadata "Nextcloud developer documentation") |
167169
| Notion | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Notion?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Notion/ "Download AspNet.Security.OAuth.Notion from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Notion?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Notion "Download AspNet.Security.OAuth.Notion from MyGet.org") | [Documentation](https://developers.notion.com/docs/authorization#authorizing-public-integrations "Notion developer documentation") |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageValidationBaselineVersion>6.0.5</PackageValidationBaselineVersion>
5+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<!-- TODO Remove once published to NuGet.org -->
9+
<PropertyGroup>
10+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
11+
</PropertyGroup>
12+
13+
<PropertyGroup>
14+
<Description>ASP.NET Core security middleware enabling Naver authentication.</Description>
15+
<Authors>christallire</Authors>
16+
<PackageTags>aspnetcore;authentication;naver;oauth;security</PackageTags>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
21+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.Naver;
8+
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="NaverAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class NaverAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
/// <summary>
17+
/// The claim for the user's nickname.
18+
/// </summary>
19+
public const string Nickname = "urn:naver:nickname";
20+
21+
/// <summary>
22+
/// The claim for the user's age range.
23+
/// </summary>
24+
public const string Age = "urn:naver:age";
25+
26+
/// <summary>
27+
/// The claim for the user's year of birth
28+
/// </summary>
29+
public const string YearOfBirth = "urn:naver:birthyear";
30+
31+
/// <summary>
32+
/// The claim for the user's profile image url
33+
/// </summary>
34+
public const string ProfileImage = "urn:naver:profile_image";
35+
}
36+
}
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.Naver;
8+
9+
/// <summary>
10+
/// Default values used by the Naver authentication middleware.
11+
/// </summary>
12+
public static class NaverAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "Naver";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "Naver";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "Naver";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-naver";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://nid.naver.com/oauth2.0/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = " https://nid.naver.com/oauth2.0/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://openapi.naver.com/v1/nid/me";
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.Naver;
8+
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
/// <summary>
12+
/// Extension methods to add Naver authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class NaverAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="NaverAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables Naver authentication capabilities.
19+
/// </summary>
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
22+
public static AuthenticationBuilder AddNaver([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddNaver(NaverAuthenticationDefaults.AuthenticationScheme, options => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="NaverAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables Naver 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 AddNaver(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<NaverAuthenticationOptions> configuration)
37+
{
38+
return builder.AddNaver(NaverAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="NaverAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables Naver 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 Naver options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddNaver(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<NaverAuthenticationOptions> configuration)
53+
{
54+
return builder.AddNaver(scheme, NaverAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="NaverAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables Naver 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 Naver options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddNaver(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<NaverAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<NaverAuthenticationOptions, NaverAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.Net.Http.Headers;
8+
using System.Security.Claims;
9+
using System.Text.Encodings.Web;
10+
using System.Text.Json;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
14+
namespace AspNet.Security.OAuth.Naver;
15+
16+
public partial class NaverAuthenticationHandler : OAuthHandler<NaverAuthenticationOptions>
17+
{
18+
public NaverAuthenticationHandler(
19+
[NotNull] IOptionsMonitor<NaverAuthenticationOptions> 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+
using JsonDocument userProfile = await GetUserProfileAsync(tokens);
33+
var principal = new ClaimsPrincipal(identity);
34+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, userProfile.RootElement);
35+
context.RunClaimActions(userProfile.RootElement.GetProperty("response"));
36+
37+
await Events.CreatingTicket(context);
38+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
39+
}
40+
41+
private async Task<JsonDocument> GetUserProfileAsync(
42+
[NotNull] OAuthTokenResponse tokens)
43+
{
44+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
45+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
46+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
47+
48+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
49+
if (!response.IsSuccessStatusCode)
50+
{
51+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
52+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
53+
}
54+
55+
return JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
56+
}
57+
58+
private static partial class Log
59+
{
60+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
61+
{
62+
UserProfileError(
63+
logger,
64+
response.StatusCode,
65+
response.Headers.ToString(),
66+
await response.Content.ReadAsStringAsync(cancellationToken));
67+
}
68+
69+
[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}.")]
70+
private static partial void UserProfileError(
71+
ILogger logger,
72+
System.Net.HttpStatusCode status,
73+
string headers,
74+
string body);
75+
}
76+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.Json;
9+
using static AspNet.Security.OAuth.Naver.NaverAuthenticationConstants;
10+
11+
namespace AspNet.Security.OAuth.Naver;
12+
13+
/// <summary>
14+
/// Defines a set of options used by <see cref="NaverAuthenticationHandler"/>.
15+
/// </summary>
16+
public class NaverAuthenticationOptions : OAuthOptions
17+
{
18+
public NaverAuthenticationOptions()
19+
{
20+
ClaimsIssuer = NaverAuthenticationDefaults.Issuer;
21+
CallbackPath = NaverAuthenticationDefaults.CallbackPath;
22+
23+
AuthorizationEndpoint = NaverAuthenticationDefaults.AuthorizationEndpoint;
24+
TokenEndpoint = NaverAuthenticationDefaults.TokenEndpoint;
25+
UserInformationEndpoint = NaverAuthenticationDefaults.UserInformationEndpoint;
26+
27+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
28+
ClaimActions.MapJsonKey(Claims.Nickname, "nickname");
29+
ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
30+
ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
31+
ClaimActions.MapJsonKey(ClaimTypes.Gender, "gender");
32+
ClaimActions.MapJsonKey(Claims.Age, "age");
33+
ClaimActions.MapJsonKey(ClaimTypes.DateOfBirth, "birthday");
34+
ClaimActions.MapJsonKey(Claims.ProfileImage, "profile_image");
35+
ClaimActions.MapJsonKey(Claims.YearOfBirth, "birthyear");
36+
ClaimActions.MapJsonKey(ClaimTypes.MobilePhone, "mobile");
37+
}
38+
}

0 commit comments

Comments
 (0)