-
Notifications
You must be signed in to change notification settings - Fork 546
Implement Kick OAuth #1157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Implement Kick OAuth #1157
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
21 changes: 21 additions & 0 deletions
21
src/AspNet.Security.OAuth.Kick/AspNet.Security.OAuth.Kick.csproj
This file contains hidden or 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,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <PackageValidationBaselineVersion>10.1.0</PackageValidationBaselineVersion> | ||
| <!-- TODO Remove once published to NuGet.org --> | ||
| <DisablePackageBaselineValidation>true</DisablePackageBaselineValidation> | ||
| <TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <Description>ASP.NET Core security middleware enabling Kick authentication.</Description> | ||
| <Authors>Daniel Beaupre</Authors> | ||
| <PackageTags>aspnetcore;authentication;kick;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.Kick/KickAuthenticationConstants.cs
This file contains hidden or 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.Kick; | ||
|
|
||
| /// <summary> | ||
| /// Contains constants specific to the <see cref="KickAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public static class KickAuthenticationConstants | ||
| { | ||
| public static class Claims | ||
| { | ||
| public static readonly string ProfilePicture = "urn:kick:profilepicture"; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/AspNet.Security.OAuth.Kick/KickAuthenticationDefaults.cs
This file contains hidden or 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.Kick; | ||
|
|
||
| /// <summary> | ||
| /// Default values used by the Kick authentication middleware. | ||
| /// </summary> | ||
| public static class KickAuthenticationDefaults | ||
| { | ||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
| /// </summary> | ||
| public const string AuthenticationScheme = "Kick"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
| /// </summary> | ||
| public static readonly string DisplayName = "Kick"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
| /// </summary> | ||
| public static readonly string Issuer = "Kick"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
| /// </summary> | ||
| public static readonly string CallbackPath = "/signin-kick"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string AuthorizationEndpoint = "https://id.kick.com/oauth/authorize"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string TokenEndpoint = "https://id.kick.com/oauth/token"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string UserInformationEndpoint = "https://api.kick.com/public/v1/users"; | ||
| } |
74 changes: 74 additions & 0 deletions
74
src/AspNet.Security.OAuth.Kick/KickAuthenticationExtensions.cs
This file contains hidden or 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.Kick; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to add Kick authentication capabilities to an HTTP application pipeline. | ||
| /// </summary> | ||
| public static class KickAuthenticationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds <see cref="KickAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Kick authentication capabilities. | ||
| /// </summary> | ||
| /// <param name="builder">The authentication builder.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddKick([NotNull] this AuthenticationBuilder builder) | ||
| { | ||
| return builder.AddKick(KickAuthenticationDefaults.AuthenticationScheme, options => { }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="KickAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Kick authentication capabilities. | ||
| /// </summary> | ||
| /// <param name="builder">The authentication builder.</param> | ||
| /// <param name="configuration">The delegate used to configure the Kick options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddKick( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] Action<KickAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddKick(KickAuthenticationDefaults.AuthenticationScheme, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="KickAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Kick 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 Kick options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddKick( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [NotNull] Action<KickAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddKick(scheme, KickAuthenticationDefaults.DisplayName, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="KickAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Kick 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 Kick options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddKick( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [CanBeNull] string caption, | ||
| [NotNull] Action<KickAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddOAuth<KickAuthenticationOptions, KickAuthenticationHandler>(scheme, caption, configuration); | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
src/AspNet.Security.OAuth.Kick/KickAuthenticationHandler.cs
This file contains hidden or 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,73 @@ | ||
| /* | ||
| * 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.Net.Http.Headers; | ||
| using System.Security.Claims; | ||
| using System.Text.Encodings.Web; | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace AspNet.Security.OAuth.Kick; | ||
|
|
||
| /// <summary> | ||
| /// Authentication handler for Kick OAuth. | ||
| /// </summary> | ||
| public partial class KickAuthenticationHandler : OAuthHandler<KickAuthenticationOptions> | ||
| { | ||
| public KickAuthenticationHandler( | ||
| [NotNull] IOptionsMonitor<KickAuthenticationOptions> options, | ||
| [NotNull] ILoggerFactory logger, | ||
| [NotNull] UrlEncoder encoder) | ||
| : base(options, logger, encoder) | ||
| { | ||
| } | ||
|
|
||
| protected override async Task<AuthenticationTicket> CreateTicketAsync( | ||
| [NotNull] ClaimsIdentity identity, | ||
| [NotNull] AuthenticationProperties properties, | ||
| [NotNull] OAuthTokenResponse tokens) | ||
| { | ||
| using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint); | ||
| request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); | ||
|
|
||
| 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); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/AspNet.Security.OAuth.Kick/KickAuthenticationOptions.cs
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * 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.Json; | ||
| using static AspNet.Security.OAuth.Kick.KickAuthenticationConstants; | ||
|
|
||
| namespace AspNet.Security.OAuth.Kick; | ||
|
|
||
| /// <summary> | ||
| /// Defines a set of options used by <see cref="KickAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public class KickAuthenticationOptions : OAuthOptions | ||
| { | ||
| public KickAuthenticationOptions() | ||
| { | ||
| ClaimsIssuer = KickAuthenticationDefaults.Issuer; | ||
| CallbackPath = KickAuthenticationDefaults.CallbackPath; | ||
|
|
||
| AuthorizationEndpoint = KickAuthenticationDefaults.AuthorizationEndpoint; | ||
| TokenEndpoint = KickAuthenticationDefaults.TokenEndpoint; | ||
| UserInformationEndpoint = KickAuthenticationDefaults.UserInformationEndpoint; | ||
|
|
||
| Scope.Add("user:read"); | ||
|
|
||
| ClaimActions.MapCustomJson(ClaimTypes.NameIdentifier, user => GetData(user, "user_id")); | ||
| ClaimActions.MapCustomJson(ClaimTypes.Name, user => GetData(user, "name")); | ||
| ClaimActions.MapCustomJson(ClaimTypes.Email, user => GetData(user, "email")); | ||
| ClaimActions.MapCustomJson(Claims.ProfilePicture, user => GetData(user, "profile_picture")); | ||
|
|
||
| // Kick requires PKCE (OAuth 2.1) | ||
| UsePkce = true; | ||
| } | ||
|
|
||
| private static string? GetData(JsonElement user, string key) | ||
| { | ||
| if (!user.TryGetProperty("data", out var data) || data.ValueKind != JsonValueKind.Array) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return data.EnumerateArray() | ||
| .Select(p => p.GetString(key)) | ||
| .FirstOrDefault(); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
test/AspNet.Security.OAuth.Providers.Tests/Kick/KickTests.cs
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * 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 Microsoft.AspNetCore.WebUtilities; | ||
| using static AspNet.Security.OAuth.Kick.KickAuthenticationConstants; | ||
|
|
||
| namespace AspNet.Security.OAuth.Kick; | ||
|
|
||
| public class KickTests(ITestOutputHelper outputHelper) : OAuthTests<KickAuthenticationOptions>(outputHelper) | ||
| { | ||
| public override string DefaultScheme => KickAuthenticationDefaults.AuthenticationScheme; | ||
|
|
||
| protected internal override void RegisterAuthentication(AuthenticationBuilder builder) | ||
| { | ||
| builder.AddKick(options => ConfigureDefaults(builder, options)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ClaimTypes.NameIdentifier, "123456")] | ||
| [InlineData(ClaimTypes.Name, "testuser")] | ||
| [InlineData(ClaimTypes.Email, "test@example.com")] | ||
| [InlineData("urn:kick:profilepicture", "https://files.kick.com/images/user/123456/profile_image.png")] | ||
| public async Task Can_Sign_In_Using_Kick(string claimType, string claimValue) | ||
| => await AuthenticateUserAndAssertClaimValue(claimType, claimValue); | ||
|
|
||
| [Fact] | ||
| public async Task BuildChallengeUrl_Generates_Correct_Url_With_Pkce() | ||
| { | ||
| // Arrange | ||
| var options = new KickAuthenticationOptions(); | ||
|
|
||
| var redirectUrl = "https://my-site.local/signin-kick"; | ||
|
|
||
| // Act | ||
| Uri actual = await BuildChallengeUriAsync( | ||
| options, | ||
| redirectUrl, | ||
| (options, loggerFactory, encoder) => new KickAuthenticationHandler(options, loggerFactory, encoder)); | ||
|
|
||
| // Assert | ||
| actual.ShouldNotBeNull(); | ||
| actual.ToString().ShouldStartWith("https://id.kick.com/oauth/authorize?"); | ||
|
|
||
| var query = QueryHelpers.ParseQuery(actual.Query); | ||
|
|
||
| query.ShouldContainKey("state"); | ||
| query.ShouldContainKeyAndValue("client_id", options.ClientId); | ||
| query.ShouldContainKeyAndValue("redirect_uri", redirectUrl); | ||
| query.ShouldContainKeyAndValue("response_type", "code"); | ||
| query.ShouldContainKeyAndValue("scope", "user:read"); | ||
|
|
||
| // Kick requires PKCE | ||
| query.ShouldContainKey(OAuthConstants.CodeChallengeKey); | ||
| query.ShouldContainKey(OAuthConstants.CodeChallengeMethodKey); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.