Skip to content

Commit d55afcb

Browse files
wplong11martincostello
authored andcommitted
Add KakaoTalk provider (#493)
Add KakaoTalk provider and unit tests
1 parent 9243ff0 commit d55afcb

10 files changed

+411
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
200200
EndProject
201201
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Kloudless", "src\AspNet.Security.OAuth.Kloudless\AspNet.Security.OAuth.Kloudless.csproj", "{947E1C60-BCE1-402E-9E5A-6D7094A68DE9}"
202202
EndProject
203+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.KakaoTalk", "src\AspNet.Security.OAuth.KakaoTalk\AspNet.Security.OAuth.KakaoTalk.csproj", "{1B19314C-9F33-4E71-AF0C-46ED8AB621CE}"
204+
EndProject
203205
Global
204206
GlobalSection(SolutionConfigurationPlatforms) = preSolution
205207
Debug|Any CPU = Debug|Any CPU
@@ -470,6 +472,10 @@ Global
470472
{947E1C60-BCE1-402E-9E5A-6D7094A68DE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
471473
{947E1C60-BCE1-402E-9E5A-6D7094A68DE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
472474
{947E1C60-BCE1-402E-9E5A-6D7094A68DE9}.Release|Any CPU.Build.0 = Release|Any CPU
475+
{1B19314C-9F33-4E71-AF0C-46ED8AB621CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
476+
{1B19314C-9F33-4E71-AF0C-46ED8AB621CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
477+
{1B19314C-9F33-4E71-AF0C-46ED8AB621CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
478+
{1B19314C-9F33-4E71-AF0C-46ED8AB621CE}.Release|Any CPU.Build.0 = Release|Any CPU
473479
EndGlobalSection
474480
GlobalSection(SolutionProperties) = preSolution
475481
HideSolutionNode = FALSE
@@ -547,6 +553,7 @@ Global
547553
{F8A4A5C4-86D9-4CF2-A2FC-3B46CCC51750} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
548554
{B1792D2A-6D6C-4484-968E-D68DF376BA40} = {3FA3F7B5-5373-4E43-8F45-8EC18249E526}
549555
{947E1C60-BCE1-402E-9E5A-6D7094A68DE9} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
556+
{1B19314C-9F33-4E71-AF0C-46ED8AB621CE} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
550557
EndGlobalSection
551558
GlobalSection(ExtensibilityGlobals) = postSolution
552559
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ We would love it if you could help contributing to this repository.
8282
* [Tolbxela](https://github.com/tolbxela)
8383
* [Tommy Parnell](https://github.com/tparnell8)
8484
* [twsl](https://github.com/twsI)
85+
* [wplong11](https://github.com/wplong11)
8586
* [Yannic Smeets](https://github.com/yannicsmeets)
8687
* [zAfLu](https://github.com/zAfLu)
8788
* [zhengchun](https://github.com/zhengchun)
@@ -137,6 +138,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
137138
| 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 |
138139
| 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") |
139140
| 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") |
141+
| KakaoTalk | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.KakaoTalk?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.KakaoTalk/ "Download AspNet.Security.OAuth.KakaoTalk from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.KakaoTalk?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.KakaoTalk "Download AspNet.Security.OAuth.KakaoTalk from MyGet.org") | [Documentation](https://developers.kakao.com/docs/latest/en/kakaologin/common "KakaoTalk developer documentation") |
140142
| Kloudless | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Kloudless?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Kloudless/ "Download AspNet.Security.OAuth.Kloudless from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Kloudless?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Kloudless "Download AspNet.Security.OAuth.Kloudless from MyGet.org") | [Documentation](https://developers.kloudless.com/docs/v1/authentication "Kloudless developer documentation") |
141143
| LinkedIn | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.LinkedIn?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.LinkedIn/ "Download AspNet.Security.OAuth.LinkedIn from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.LinkedIn?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.LinkedIn "Download AspNet.Security.OAuth.LinkedIn from MyGet.org") | [Documentation](https://docs.microsoft.com/en-us/linkedin/shared/authentication/authentication "LinkedIn developer documentation") |
142144
| MailChimp | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.MailChimp?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.MailChimp/ "Download AspNet.Security.OAuth.MailChimp from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.MailChimp?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.MailChimp "Download AspNet.Security.OAuth.MailChimp from MyGet.org") | [Documentation](https://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-oauth2/ "MailChimp developer documentation") |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<Description>ASP.NET Core security middleware enabling KakaoTalk authentication.</Description>
9+
<Authors>wplong11</Authors>
10+
<PackageTags>aspnetcore;authentication;kakaotalk;oauth;security</PackageTags>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.KakaoTalk
8+
{
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="KakaoTalkAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class KakaoTalkAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
/// <summary>
17+
/// The claim for the user's age range.
18+
/// </summary>
19+
public const string AgeRange = "urn:kakaotalk:age_range";
20+
21+
/// <summary>
22+
/// The claim for the user's year of birth
23+
/// </summary>
24+
public const string YearOfBirth = "urn:kakaotalk:year_of_birth";
25+
}
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 Microsoft.AspNetCore.Authentication;
8+
using Microsoft.AspNetCore.Authentication.OAuth;
9+
10+
namespace AspNet.Security.OAuth.KakaoTalk
11+
{
12+
/// <summary>
13+
/// Default values used by the KakaoTalk authentication middleware.
14+
/// </summary>
15+
public static class KakaoTalkAuthenticationDefaults
16+
{
17+
/// <summary>
18+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
19+
/// </summary>
20+
public const string AuthenticationScheme = "KakaoTalk";
21+
22+
/// <summary>
23+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
24+
/// </summary>
25+
public const string DisplayName = "KakaoTalk";
26+
27+
/// <summary>
28+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
29+
/// </summary>
30+
public const string Issuer = "KakaoTalk";
31+
32+
/// <summary>
33+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
34+
/// </summary>
35+
public const string CallbackPath = "/signin-kakaotalk";
36+
37+
/// <summary>
38+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
39+
/// </summary>
40+
public const string AuthorizationEndpoint = "https://kauth.kakao.com/oauth/authorize";
41+
42+
/// <summary>
43+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
44+
/// </summary>
45+
public const string TokenEndpoint = "https://kauth.kakao.com/oauth/token";
46+
47+
/// <summary>
48+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
49+
/// </summary>
50+
public const string UserInformationEndpoint = "https://kapi.kakao.com/v2/user/me";
51+
}
52+
}
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;
8+
using AspNet.Security.OAuth.KakaoTalk;
9+
using JetBrains.Annotations;
10+
using Microsoft.AspNetCore.Authentication;
11+
12+
namespace Microsoft.Extensions.DependencyInjection
13+
{
14+
/// <summary>
15+
/// Extension methods to add KakaoTalk authentication capabilities to an HTTP application pipeline.
16+
/// </summary>
17+
public static class KakaoTalkAuthenticationExtensions
18+
{
19+
/// <summary>
20+
/// Adds <see cref="KakaoTalkAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables KakaoTalk authentication capabilities.
22+
/// </summary>
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddKakaoTalk([NotNull] this AuthenticationBuilder builder)
26+
{
27+
return builder.AddKakaoTalk(KakaoTalkAuthenticationDefaults.AuthenticationScheme, options => { });
28+
}
29+
30+
/// <summary>
31+
/// Adds <see cref="KakaoTalkAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables KakaoTalk authentication capabilities.
33+
/// </summary>
34+
/// <param name="builder">The authentication builder.</param>
35+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
36+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
37+
public static AuthenticationBuilder AddKakaoTalk(
38+
[NotNull] this AuthenticationBuilder builder,
39+
[NotNull] Action<KakaoTalkAuthenticationOptions> configuration)
40+
{
41+
return builder.AddKakaoTalk(KakaoTalkAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
43+
44+
/// <summary>
45+
/// Adds <see cref="KakaoTalkAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables KakaoTalk authentication capabilities.
47+
/// </summary>
48+
/// <param name="builder">The authentication builder.</param>
49+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
50+
/// <param name="configuration">The delegate used to configure the KakaoTalk options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddKakaoTalk(
53+
[NotNull] this AuthenticationBuilder builder,
54+
[NotNull] string scheme,
55+
[NotNull] Action<KakaoTalkAuthenticationOptions> configuration)
56+
{
57+
return builder.AddKakaoTalk(scheme, KakaoTalkAuthenticationDefaults.DisplayName, configuration);
58+
}
59+
60+
/// <summary>
61+
/// Adds <see cref="KakaoTalkAuthenticationHandler"/> to the specified
62+
/// <see cref="AuthenticationBuilder"/>, which enables KakaoTalk authentication capabilities.
63+
/// </summary>
64+
/// <param name="builder">The authentication builder.</param>
65+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
66+
/// <param name="caption">The optional display name associated with this instance.</param>
67+
/// <param name="configuration">The delegate used to configure the KakaoTalk options.</param>
68+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
69+
public static AuthenticationBuilder AddKakaoTalk(
70+
[NotNull] this AuthenticationBuilder builder,
71+
[NotNull] string scheme,
72+
[CanBeNull] string caption,
73+
[NotNull] Action<KakaoTalkAuthenticationOptions> configuration)
74+
{
75+
return builder.AddOAuth<KakaoTalkAuthenticationOptions, KakaoTalkAuthenticationHandler>(scheme, caption, configuration);
76+
}
77+
}
78+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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;
8+
using System.Net.Http.Headers;
9+
using System.Security.Claims;
10+
using System.Text.Encodings.Web;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
13+
using JetBrains.Annotations;
14+
using Microsoft.AspNetCore.Authentication;
15+
using Microsoft.AspNetCore.Authentication.OAuth;
16+
using Microsoft.Extensions.Logging;
17+
using Microsoft.Extensions.Options;
18+
19+
namespace AspNet.Security.OAuth.KakaoTalk
20+
{
21+
public class KakaoTalkAuthenticationHandler : OAuthHandler<KakaoTalkAuthenticationOptions>
22+
{
23+
public KakaoTalkAuthenticationHandler(
24+
[NotNull] IOptionsMonitor<KakaoTalkAuthenticationOptions> options,
25+
[NotNull] ILoggerFactory logger,
26+
[NotNull] UrlEncoder encoder,
27+
[NotNull] ISystemClock clock)
28+
: base(options, logger, encoder, clock)
29+
{
30+
}
31+
32+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
33+
[NotNull] ClaimsIdentity identity,
34+
[NotNull] AuthenticationProperties properties,
35+
[NotNull] OAuthTokenResponse tokens)
36+
{
37+
using JsonDocument userProfile = await GetUserProfileAsync(tokens);
38+
var principal = new ClaimsPrincipal(identity);
39+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, userProfile.RootElement);
40+
context.RunClaimActions();
41+
42+
await Options.Events.CreatingTicket(context);
43+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
44+
}
45+
46+
private async Task<JsonDocument> GetUserProfileAsync(
47+
[NotNull] OAuthTokenResponse tokens)
48+
{
49+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
50+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
51+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
52+
53+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
54+
if (!response.IsSuccessStatusCode)
55+
{
56+
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
57+
"returned a {Status} response with the following payload: {Headers} {Body}.",
58+
/* Status: */ response.StatusCode,
59+
/* Headers: */ response.Headers.ToString(),
60+
/* Body: */ await response.Content.ReadAsStringAsync());
61+
62+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
63+
}
64+
65+
return JsonDocument.Parse(await response.Content.ReadAsStringAsync());
66+
}
67+
}
68+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;
8+
using System.Security.Claims;
9+
using System.Text.Json;
10+
using Microsoft.AspNetCore.Authentication;
11+
using Microsoft.AspNetCore.Authentication.OAuth;
12+
using static AspNet.Security.OAuth.KakaoTalk.KakaoTalkAuthenticationConstants;
13+
14+
namespace AspNet.Security.OAuth.KakaoTalk
15+
{
16+
/// <summary>
17+
/// Defines a set of options used by <see cref="KakaoTalkAuthenticationHandler"/>.
18+
/// </summary>
19+
public class KakaoTalkAuthenticationOptions : OAuthOptions
20+
{
21+
public KakaoTalkAuthenticationOptions()
22+
{
23+
ClaimsIssuer = KakaoTalkAuthenticationDefaults.Issuer;
24+
CallbackPath = KakaoTalkAuthenticationDefaults.CallbackPath;
25+
26+
AuthorizationEndpoint = KakaoTalkAuthenticationDefaults.AuthorizationEndpoint;
27+
TokenEndpoint = KakaoTalkAuthenticationDefaults.TokenEndpoint;
28+
UserInformationEndpoint = KakaoTalkAuthenticationDefaults.UserInformationEndpoint;
29+
30+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
31+
ClaimActions.MapCustomJson(ClaimTypes.Name, user =>
32+
{
33+
JsonElement property = user;
34+
bool hasProperty = property.TryGetProperty("kakao_account", out property)
35+
&& property.TryGetProperty("profile", out property)
36+
&& property.TryGetProperty("nickname", out property)
37+
&& property.ValueKind == JsonValueKind.String;
38+
return hasProperty
39+
? property.GetString()
40+
: null;
41+
});
42+
ClaimActions.MapJsonSubKey(ClaimTypes.Email, "kakao_account", "email");
43+
ClaimActions.MapJsonSubKey(ClaimTypes.DateOfBirth, "kakao_account", "birthday");
44+
ClaimActions.MapJsonSubKey(ClaimTypes.Gender, "kakao_account", "gender");
45+
ClaimActions.MapJsonSubKey(ClaimTypes.MobilePhone, "kakao_account", "phone_number");
46+
ClaimActions.MapJsonSubKey(Claims.AgeRange, "kakao_account", "age_range");
47+
ClaimActions.MapJsonSubKey(Claims.YearOfBirth, "kakao_account", "birthyear");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)