Skip to content

Commit 8d73594

Browse files
authored
Add GitCode Provider (#1020)
Add GitCode Provider.
1 parent e6c467f commit 8d73594

File tree

13 files changed

+387
-1
lines changed

13 files changed

+387
-1
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C2CA4B38-A
220220
docs\xumm.md = docs\xumm.md
221221
docs\zendesk.md = docs\zendesk.md
222222
docs\docusign.md = docs\docusign.md
223+
docs\gitcode.md = docs\gitcode.md
223224
EndProjectSection
224225
EndProject
225226
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Basecamp", "src\AspNet.Security.OAuth.Basecamp\AspNet.Security.OAuth.Basecamp.csproj", "{42306484-B2BF-4B52-B950-E0CDFA58B02A}"
@@ -313,6 +314,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Zoho"
313314
EndProject
314315
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.VkId", "src\AspNet.Security.OAuth.VkId\AspNet.Security.OAuth.VkId.csproj", "{F3E62C24-5F82-4CF5-A994-0E10D04FB495}"
315316
EndProject
317+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.GitCode", "src\AspNet.Security.OAuth.GitCode\AspNet.Security.OAuth.GitCode.csproj", "{668833D5-DB6A-475F-B0FD-A03462B037B8}"
318+
EndProject
316319
Global
317320
GlobalSection(SolutionConfigurationPlatforms) = preSolution
318321
Debug|Any CPU = Debug|Any CPU
@@ -727,6 +730,10 @@ Global
727730
{F3E62C24-5F82-4CF5-A994-0E10D04FB495}.Debug|Any CPU.Build.0 = Debug|Any CPU
728731
{F3E62C24-5F82-4CF5-A994-0E10D04FB495}.Release|Any CPU.ActiveCfg = Release|Any CPU
729732
{F3E62C24-5F82-4CF5-A994-0E10D04FB495}.Release|Any CPU.Build.0 = Release|Any CPU
733+
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
734+
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
735+
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
736+
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Release|Any CPU.Build.0 = Release|Any CPU
730737
EndGlobalSection
731738
GlobalSection(SolutionProperties) = preSolution
732739
HideSolutionNode = FALSE
@@ -840,6 +847,7 @@ Global
840847
{4E96BD06-04CD-4014-BA42-10D2CDB820D6} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
841848
{CD56ABE4-1CD2-4029-B556-E110A31A2CC4} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
842849
{F3E62C24-5F82-4CF5-A994-0E10D04FB495} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
850+
{668833D5-DB6A-475F-B0FD-A03462B037B8} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
843851
EndGlobalSection
844852
GlobalSection(ExtensibilityGlobals) = postSolution
845853
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
183183
| Feishu | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.Feishu?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.Feishu/ "Download AspNet.Security.OAuth.Feishu from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.Feishu?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Feishu "Download AspNet.Security.OAuth.Feishu from MyGet.org") | [Documentation](https://open.feishu.cn/document/common-capabilities/sso/web-application-sso/web-app-overview "Feishu developer documentation") |
184184
| Fitbit | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.Fitbit?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.Fitbit/ "Download AspNet.Security.OAuth.Fitbit from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.Fitbit?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Fitbit "Download AspNet.Security.OAuth.Fitbit from MyGet.org") | [Documentation](https://dev.fitbit.com/build/reference/web-api/oauth2/ "Fitbit developer documentation") |
185185
| Foursquare | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.Foursquare?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.Foursquare/ "Download AspNet.Security.OAuth.Foursquare from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.Foursquare?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Foursquare "Download AspNet.Security.OAuth.Foursquare from MyGet.org") | [Documentation](https://developer.foursquare.com/docs/api/configuration/authentication "Foursquare developer documentation") |
186+
| GitCode | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.GitCode?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.GitCode/ "Download AspNet.Security.OAuth.GitCode from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.GitCode?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.GitCode "Download AspNet.Security.OAuth.GitCode from MyGet.org") | [Documentation](https://docs.gitcode.com/en/docs/oauth/ "GitCode developer documentation") |
186187
| Gitee | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.Gitee?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.Gitee/ "Download AspNet.Security.OAuth.Gitee from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.Gitee?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Gitee "Download AspNet.Security.OAuth.Gitee from MyGet.org") | [Documentation](https://gitee.com/api/v5/oauth_doc#/ "Gitee developer documentation") |
187188
| GitHub | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.GitHub?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.GitHub/ "Download AspNet.Security.OAuth.GitHub from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.GitHub?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.GitHub "Download AspNet.Security.OAuth.GitHub from MyGet.org") | [Documentation](https://developer.github.com/apps/building-oauth-apps/ "GitHub developer documentation") |
188189
| GitLab | [![NuGet](https://img.shields.io/nuget/v/AspNet.Security.OAuth.GitLab?logo=nuget&label=NuGet&color=blue)](https://www.nuget.org/packages/AspNet.Security.OAuth.GitLab/ "Download AspNet.Security.OAuth.GitLab from NuGet.org") | [![MyGet](https://img.shields.io/myget/aspnet-contrib/vpre/AspNet.Security.OAuth.GitLab?logo=nuget&label=MyGet&color=blue)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.GitLab "Download AspNet.Security.OAuth.GitLab from MyGet.org") | [Documentation](https://docs.gitlab.com/ee/api/oauth2.html "GitLab developer documentation") |

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ covered by the section above.
5252
| eBay | **Required** | [Documentation](ebay.md "eBay provider documentation") |
5353
| EVEOnline | _Optional_ | [Documentation](eveonline.md "EVEOnline provider documentation") |
5454
| Foursquare | _Optional_ | [Documentation](foursquare.md "Foursquare provider documentation") |
55+
| GitCode | _Optional_ | [Documentation](gitcode.md "GitCode provider documentation") |
5556
| GitHub | _Optional_ | [Documentation](github.md "GitHub provider documentation") |
5657
| Gitee | _Optional_ | [Documentation](gitee.md "Gitee provider documentation") |
5758
| Huawei | _Optional_ | [Documentation](huawei.md "Huawei provider documentation") |

docs/gitcode.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Integrating the GitCode Provider
2+
3+
## Example
4+
5+
```csharp
6+
services.AddAuthentication(options => /* Auth configuration */)
7+
.AddGitCode(options =>
8+
{
9+
options.ClientId = "my-client-id";
10+
options.ClientSecret = "my-client-secret";
11+
12+
// GitCode does not support setting scopes via requests to the API.
13+
// The configuration of scopes (App permissions) are instead managed
14+
// within the OAuth app management in GitCode itself.
15+
});
16+
```
17+
18+
## Required Additional Settings
19+
20+
_None._
21+
22+
## Optional Settings
23+
24+
_None._

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<MajorVersion>9</MajorVersion>
5-
<MinorVersion>0</MinorVersion>
5+
<MinorVersion>1</MinorVersion>
66
<PatchVersion>0</PatchVersion>
77
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix>
88
<PackageValidationBaselineVersion Condition=" '$(EnablePackageValidation)' == 'true' AND '$(PackageValidationBaselineVersion)' == '' ">9.0.0</PackageValidationBaselineVersion>
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>9.1.0</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 GitCode authentication.</Description>
15+
<Authors>Gehongyan</Authors>
16+
<PackageTags>aspnetcore;authentication;gitcode;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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.GitCode;
8+
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="GitCodeAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class GitCodeAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string AvatarUrl = "urn:gitcode:avatar_url";
17+
public const string Bio = "urn:gitcode:bio";
18+
public const string Blog = "urn:gitcode:blog";
19+
public const string Company = "urn:gitcode:company";
20+
public const string HtmlUrl = "urn:gitcode:html_url";
21+
public const string Name = "urn:gitcode:name";
22+
}
23+
}
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.GitCode;
8+
9+
/// <summary>
10+
/// Default values used by the GitCode authentication middleware.
11+
/// </summary>
12+
public static class GitCodeAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "GitCode";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "GitCode";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "GitCode";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-gitcode";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://gitcode.com/oauth/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://gitcode.com/oauth/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://api.gitcode.com/api/v5/user";
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.GitCode;
8+
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
/// <summary>
12+
/// Extension methods to add GitCode authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class GitCodeAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables GitCode authentication capabilities.
19+
/// </summary>
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
22+
public static AuthenticationBuilder AddGitCode([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddGitCode(GitCodeAuthenticationDefaults.AuthenticationScheme, options => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 AddGitCode(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<GitCodeAuthenticationOptions> configuration)
37+
{
38+
return builder.AddGitCode(GitCodeAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 GitCode options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddGitCode(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<GitCodeAuthenticationOptions> configuration)
53+
{
54+
return builder.AddGitCode(scheme, GitCodeAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="GitCodeAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables GitCode 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 GitCode options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddGitCode(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<GitCodeAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<GitCodeAuthenticationOptions, GitCodeAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
8+
using System.Net.Http.Headers;
9+
using System.Net.Mime;
10+
using System.Security.Claims;
11+
using System.Text.Encodings.Web;
12+
using System.Text.Json;
13+
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Options;
15+
16+
namespace AspNet.Security.OAuth.GitCode;
17+
18+
public partial class GitCodeAuthenticationHandler : OAuthHandler<GitCodeAuthenticationOptions>
19+
{
20+
public GitCodeAuthenticationHandler(
21+
[NotNull] IOptionsMonitor<GitCodeAuthenticationOptions> options,
22+
[NotNull] ILoggerFactory logger,
23+
[NotNull] UrlEncoder encoder)
24+
: base(options, logger, encoder)
25+
{
26+
}
27+
28+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
29+
[NotNull] ClaimsIdentity identity,
30+
[NotNull] AuthenticationProperties properties,
31+
[NotNull] OAuthTokenResponse tokens)
32+
{
33+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
34+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
35+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
36+
37+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
38+
if (!response.IsSuccessStatusCode)
39+
{
40+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
41+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
42+
}
43+
44+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
45+
46+
var principal = new ClaimsPrincipal(identity);
47+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
48+
context.RunClaimActions();
49+
50+
await Events.CreatingTicket(context);
51+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
52+
}
53+
54+
private static partial class Log
55+
{
56+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
57+
{
58+
UserProfileError(
59+
logger,
60+
response.StatusCode,
61+
response.Headers.ToString(),
62+
await response.Content.ReadAsStringAsync(cancellationToken));
63+
}
64+
65+
[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}.")]
66+
private static partial void UserProfileError(
67+
ILogger logger,
68+
HttpStatusCode status,
69+
string headers,
70+
string body);
71+
}
72+
}

0 commit comments

Comments
 (0)