-
Notifications
You must be signed in to change notification settings - Fork 551
Add GitCode Provider #1020
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
Merged
Merged
Add GitCode Provider #1020
Changes from 1 commit
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Integrating the GitCode Provider | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| services.AddAuthentication(options => /* Auth configuration */) | ||
| .AddGitCode(options => | ||
| { | ||
| options.ClientId = "my-client-id"; | ||
| options.ClientSecret = "my-client-secret"; | ||
|
|
||
| // It appears that GitCode does not support setting the scopes through query strings. | ||
| // The configuration of scopes (App permissions) is instead managed | ||
| // within the OAuth app management on the official web. | ||
gehongyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| ``` | ||
|
|
||
| ## Required Additional Settings | ||
|
|
||
| _None._ | ||
|
|
||
| ## Optional Settings | ||
|
|
||
| _None._ | ||
24 changes: 24 additions & 0 deletions
24
src/AspNet.Security.OAuth.GitCode/AspNet.Security.OAuth.GitCode.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,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks> | ||
gehongyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </PropertyGroup> | ||
|
|
||
| <!-- TODO Remove once published to NuGet.org --> | ||
| <PropertyGroup> | ||
| <PackageValidationBaselineVersion>9.1.0</PackageValidationBaselineVersion> | ||
gehongyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <DisablePackageBaselineValidation>true</DisablePackageBaselineValidation> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <Description>ASP.NET Core security middleware enabling GitCode authentication.</Description> | ||
| <Authors>Gehongyan</Authors> | ||
| <PackageTags>aspnetcore;authentication;gitcode;oauth;security</PackageTags> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| <PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
23 changes: 23 additions & 0 deletions
23
src/AspNet.Security.OAuth.GitCode/GitCodeAuthenticationConstants.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,23 @@ | ||
| /* | ||
| * 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.GitCode; | ||
|
|
||
| /// <summary> | ||
| /// Contains constants specific to the <see cref="GitCodeAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public static class GitCodeAuthenticationConstants | ||
| { | ||
| public static class Claims | ||
| { | ||
| public const string AvatarUrl = "urn:gitcode:avatar_url"; | ||
| public const string Bio = "urn:gitcode:bio"; | ||
| public const string Blog = "urn:gitcode:blog"; | ||
| public const string Company = "urn:gitcode:company"; | ||
| public const string HtmlUrl = "urn:gitcode:html_url"; | ||
| public const string Name = "urn:gitcode:name"; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/AspNet.Security.OAuth.GitCode/GitCodeAuthenticationDefaults.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.GitCode; | ||
|
|
||
| /// <summary> | ||
| /// Default values used by the GitCode authentication middleware. | ||
| /// </summary> | ||
| public static class GitCodeAuthenticationDefaults | ||
| { | ||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
| /// </summary> | ||
| public const string AuthenticationScheme = "GitCode"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
| /// </summary> | ||
| public static readonly string DisplayName = "GitCode"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
| /// </summary> | ||
| public static readonly string Issuer = "GitCode"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
| /// </summary> | ||
| public static readonly string CallbackPath = "/signin-gitcode"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string AuthorizationEndpoint = "https://gitcode.com/oauth/authorize"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string TokenEndpoint = "https://gitcode.com/oauth/token"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string UserInformationEndpoint = "https://api.gitcode.com/api/v5/user"; | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
74 changes: 74 additions & 0 deletions
74
src/AspNet.Security.OAuth.GitCode/GitCodeAuthenticationExtensions.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.GitCode; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to add GitCode authentication capabilities to an HTTP application pipeline. | ||
| /// </summary> | ||
| public static class GitCodeAuthenticationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables GitCode authentication capabilities. | ||
| /// </summary> | ||
| /// <param name="builder">The authentication builder.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddGitCode([NotNull] this AuthenticationBuilder builder) | ||
| { | ||
| return builder.AddGitCode(GitCodeAuthenticationDefaults.AuthenticationScheme, options => { }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 AddGitCode( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] Action<GitCodeAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddGitCode(GitCodeAuthenticationDefaults.AuthenticationScheme, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 GitCode options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddGitCode( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [NotNull] Action<GitCodeAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddGitCode(scheme, GitCodeAuthenticationDefaults.DisplayName, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 GitCode options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddGitCode( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [CanBeNull] string caption, | ||
| [NotNull] Action<GitCodeAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddOAuth<GitCodeAuthenticationOptions, GitCodeAuthenticationHandler>(scheme, caption, configuration); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/AspNet.Security.OAuth.GitCode/GitCodeAuthenticationHandler.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,72 @@ | ||
| /* | ||
| * 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; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Mime; | ||
| 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.GitCode; | ||
|
|
||
| public partial class GitCodeAuthenticationHandler : OAuthHandler<GitCodeAuthenticationOptions> | ||
| { | ||
| public GitCodeAuthenticationHandler( | ||
| [NotNull] IOptionsMonitor<GitCodeAuthenticationOptions> 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(MediaTypeNames.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, | ||
| HttpStatusCode status, | ||
| string headers, | ||
| string body); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/AspNet.Security.OAuth.GitCode/GitCodeAuthenticationOptions.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,37 @@ | ||
| /* | ||
| * 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.GitCode.GitCodeAuthenticationConstants; | ||
|
|
||
| namespace AspNet.Security.OAuth.GitCode; | ||
|
|
||
| /// <summary> | ||
| /// Defines a set of options used by <see cref="GitCodeAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public class GitCodeAuthenticationOptions : OAuthOptions | ||
| { | ||
| public GitCodeAuthenticationOptions() | ||
| { | ||
| ClaimsIssuer = GitCodeAuthenticationDefaults.Issuer; | ||
|
|
||
gehongyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CallbackPath = GitCodeAuthenticationDefaults.CallbackPath; | ||
|
|
||
| AuthorizationEndpoint = GitCodeAuthenticationDefaults.AuthorizationEndpoint; | ||
| TokenEndpoint = GitCodeAuthenticationDefaults.TokenEndpoint; | ||
| UserInformationEndpoint = GitCodeAuthenticationDefaults.UserInformationEndpoint; | ||
|
|
||
| ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Name, "login"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); | ||
| ClaimActions.MapJsonKey(Claims.AvatarUrl, "avatar_url"); | ||
| ClaimActions.MapJsonKey(Claims.Bio, "bio"); | ||
| ClaimActions.MapJsonKey(Claims.Blog, "blog"); | ||
| ClaimActions.MapJsonKey(Claims.Company, "company"); | ||
| ClaimActions.MapJsonKey(Claims.HtmlUrl, "html_url"); | ||
| ClaimActions.MapJsonKey(Claims.Name, "name"); | ||
| } | ||
| } | ||
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.