Skip to content

Commit f1e2f76

Browse files
authored
Add Feishu provider (#709)
* Add Feishu provider Signed-off-by: Vicente Yu <[email protected]> * Add Feishu provider. Signed-off-by: Vicente Yu <[email protected]>
1 parent 34e7b72 commit f1e2f76

10 files changed

+353
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.HubSp
283283
EndProject
284284
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Snapchat", "src\AspNet.Security.OAuth.Snapchat\AspNet.Security.OAuth.Snapchat.csproj", "{ECD22287-9B9F-489A-84A7-E66D65A39D73}"
285285
EndProject
286+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Feishu", "src\AspNet.Security.OAuth.Feishu\AspNet.Security.OAuth.Feishu.csproj", "{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3}"
287+
EndProject
286288
Global
287289
GlobalSection(SolutionConfigurationPlatforms) = preSolution
288290
Debug|Any CPU = Debug|Any CPU
@@ -645,6 +647,10 @@ Global
645647
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Debug|Any CPU.Build.0 = Debug|Any CPU
646648
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Release|Any CPU.ActiveCfg = Release|Any CPU
647649
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Release|Any CPU.Build.0 = Release|Any CPU
650+
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
651+
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
652+
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
653+
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3}.Release|Any CPU.Build.0 = Release|Any CPU
648654
EndGlobalSection
649655
GlobalSection(SolutionProperties) = preSolution
650656
HideSolutionNode = FALSE
@@ -745,6 +751,7 @@ Global
745751
{289A91E9-81A9-422D-9CCD-12819081A29A} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
746752
{23E576EB-6514-4617-8F04-FE7D5540136D} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
747753
{ECD22287-9B9F-489A-84A7-E66D65A39D73} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
754+
{B8F9B052-84BF-436C-B22B-CEBD5EB1F8E3} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
748755
EndGlobalSection
749756
GlobalSection(ExtensibilityGlobals) = postSolution
750757
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ We would love it if you could help contributing to this repository.
9393
* [Yannic Smeets](https://github.com/yannicsmeets)
9494
* [zAfLu](https://github.com/zAfLu)
9595
* [zhengchun](https://github.com/zhengchun)
96+
* [Vicente Yu](https://github.com/vicenteyu)
9697
* [Volodymyr Baydalka](https://github.com/zVolodymyr)
9798
* [Logan Dam](https://github.com/biltongza)
9899

@@ -141,6 +142,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
141142
| eBay | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Ebay?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Ebay/ "Download AspNet.Security.OAuth.Ebay from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Ebay?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Ebay "Download AspNet.Security.OAuth.Ebay from MyGet.org") | [Documentation](https://developer.ebay.com/api-docs/static/oauth-tokens.html "eBay developer documentation") |
142143
| EVEOnline | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.EVEOnline?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.EVEOnline/ "Download AspNet.Security.OAuth.EVEOnline from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.EVEOnline?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.EVEOnline "Download AspNet.Security.OAuth.EVEOnline from MyGet.org") | [Documentation](https://github.com/esi/esi-docs/blob/master/docs/sso/web_based_sso_flow.md "EVEOnline developer documentation") |
143144
| ExactOnline | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.ExactOnline?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.ExactOnline/ "Download AspNet.Security.OAuth.ExactOnline from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.ExactOnline?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.ExactOnline "Download AspNet.Security.OAuth.ExactOnline from MyGet.org") | [Documentation](https://support.exactonline.com/community/s/knowledge-base#All-All-DNO-Content-gettingstarted "ExactOnline developer documentation") |
145+
| Feishu | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Feishu?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Feishu/ "Download AspNet.Security.OAuth.Feishu from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Feishu?includePreReleases=true)](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") |
144146
| Fitbit | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Fitbit?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Fitbit/ "Download AspNet.Security.OAuth.Fitbit from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Fitbit?includePreReleases=true)](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") |
145147
| Foursquare | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Foursquare?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Foursquare/ "Download AspNet.Security.OAuth.Foursquare from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Foursquare?includePreReleases=true)](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") |
146148
| Gitee | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Gitee?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Gitee/ "Download AspNet.Security.OAuth.Gitee from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Gitee?includePreReleases=true)](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") |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
5+
<PackageValidationBaselineVersion>6.0.9</PackageValidationBaselineVersion>
6+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
<PropertyGroup>
10+
<Description>ASP.NET Core security middleware enabling Feishu authentication.</Description>
11+
<Authors>Vicente Yu</Authors>
12+
<PackageTags>aspnetcore;authentication;feishu;oauth;security</PackageTags>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
17+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.Feishu;
8+
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="FeishuAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class FeishuAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string UnionId = "urn:feishu:unionid";
17+
public const string Avatar = "urn:feishu:avatar";
18+
}
19+
}
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.Feishu;
8+
9+
/// <summary>
10+
/// Default values for Feishu authentication.
11+
/// </summary>
12+
public static class FeishuAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "Feishu";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "Feishu";
23+
24+
/// <summary>
25+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
26+
/// </summary>
27+
public static readonly string CallbackPath = "/signin-feishu";
28+
29+
/// <summary>
30+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
31+
/// </summary>
32+
public static readonly string Issuer = "Feishu";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://passport.feishu.cn/suite/passport/oauth/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://passport.feishu.cn/suite/passport/oauth/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://passport.feishu.cn/suite/passport/oauth/userinfo";
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.Feishu;
8+
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
/// <summary>
12+
/// Extension methods to add Feishu authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class FeishuAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables Feishu authentication capabilities.
19+
/// </summary>
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
22+
public static AuthenticationBuilder AddFeishu([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddFeishu(FeishuAuthenticationDefaults.AuthenticationScheme, _ => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 AddFeishu(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<FeishuAuthenticationOptions> configuration)
37+
{
38+
return builder.AddFeishu(FeishuAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 Feishu options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddFeishu(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<FeishuAuthenticationOptions> configuration)
53+
{
54+
return builder.AddFeishu(scheme, FeishuAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 Feishu options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddFeishu(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<FeishuAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<FeishuAuthenticationOptions, FeishuAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.Feishu;
15+
16+
public partial class FeishuAuthenticationHandler : OAuthHandler<FeishuAuthenticationOptions>
17+
{
18+
public FeishuAuthenticationHandler(
19+
[NotNull] IOptionsMonitor<FeishuAuthenticationOptions> 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 var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
33+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
34+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
35+
36+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
37+
if (!response.IsSuccessStatusCode)
38+
{
39+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
40+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
41+
}
42+
43+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
44+
45+
var principal = new ClaimsPrincipal(identity);
46+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
47+
context.RunClaimActions();
48+
49+
await Events.CreatingTicket(context);
50+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
51+
}
52+
53+
private static partial class Log
54+
{
55+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
56+
{
57+
UserProfileError(
58+
logger,
59+
response.StatusCode,
60+
response.Headers.ToString(),
61+
await response.Content.ReadAsStringAsync(cancellationToken));
62+
}
63+
64+
[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}.")]
65+
private static partial void UserProfileError(
66+
ILogger logger,
67+
System.Net.HttpStatusCode status,
68+
string headers,
69+
string body);
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 static AspNet.Security.OAuth.Feishu.FeishuAuthenticationConstants;
9+
10+
namespace AspNet.Security.OAuth.Feishu;
11+
12+
/// <summary>
13+
/// Defines a set of options used by <see cref="FeishuAuthenticationHandler"/>.
14+
/// </summary>
15+
public class FeishuAuthenticationOptions : OAuthOptions
16+
{
17+
public FeishuAuthenticationOptions()
18+
{
19+
ClaimsIssuer = FeishuAuthenticationDefaults.Issuer;
20+
CallbackPath = FeishuAuthenticationDefaults.CallbackPath;
21+
22+
AuthorizationEndpoint = FeishuAuthenticationDefaults.AuthorizationEndpoint;
23+
TokenEndpoint = FeishuAuthenticationDefaults.TokenEndpoint;
24+
UserInformationEndpoint = FeishuAuthenticationDefaults.UserInformationEndpoint;
25+
26+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "open_id");
27+
ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
28+
ClaimActions.MapJsonKey(Claims.UnionId, "union_id");
29+
ClaimActions.MapJsonKey(Claims.Avatar, "avatar_big");
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.Feishu;
8+
9+
public class FeishuTests : OAuthTests<FeishuAuthenticationOptions>
10+
{
11+
public FeishuTests(ITestOutputHelper outputHelper)
12+
{
13+
OutputHelper = outputHelper;
14+
}
15+
16+
public override string DefaultScheme => FeishuAuthenticationDefaults.AuthenticationScheme;
17+
18+
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
19+
{
20+
builder.AddFeishu(options =>
21+
{
22+
ConfigureDefaults(builder, options);
23+
});
24+
}
25+
26+
[Theory]
27+
[InlineData(ClaimTypes.NameIdentifier, "test-open-id")]
28+
[InlineData(ClaimTypes.Name, "test-name")]
29+
[InlineData(FeishuAuthenticationConstants.Claims.UnionId, "test-union-id")]
30+
[InlineData(FeishuAuthenticationConstants.Claims.Avatar, "https://www.feishu.cn/avatar/icon_big")]
31+
public async Task Can_Sign_In_Using_Feishu(string claimType, string claimValue)
32+
{
33+
// Arrange
34+
using var server = CreateTestServer();
35+
36+
// Act
37+
var claims = await AuthenticateUserAsync(server);
38+
39+
// Assert
40+
AssertClaim(claims, claimType, claimValue);
41+
}
42+
}

0 commit comments

Comments
 (0)