-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Feishu provider Signed-off-by: Vicente Yu <[email protected]> * Add Feishu provider. Signed-off-by: Vicente Yu <[email protected]>
- Loading branch information
Showing
10 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
20 changes: 20 additions & 0 deletions
20
src/AspNet.Security.OAuth.Feishu/AspNet.Security.OAuth.Feishu.csproj
This file contains 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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation> | ||
<PackageValidationBaselineVersion>6.0.9</PackageValidationBaselineVersion> | ||
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Description>ASP.NET Core security middleware enabling Feishu authentication.</Description> | ||
<Authors>Vicente Yu</Authors> | ||
<PackageTags>aspnetcore;authentication;feishu;oauth;security</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/AspNet.Security.OAuth.Feishu/FeishuAuthenticationConstants.cs
This file contains 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,19 @@ | ||
/* | ||
* 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.Feishu; | ||
|
||
/// <summary> | ||
/// Contains constants specific to the <see cref="FeishuAuthenticationHandler"/>. | ||
/// </summary> | ||
public static class FeishuAuthenticationConstants | ||
{ | ||
public static class Claims | ||
{ | ||
public const string UnionId = "urn:feishu:unionid"; | ||
public const string Avatar = "urn:feishu:avatar"; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/AspNet.Security.OAuth.Feishu/FeishuAuthenticationDefaults.cs
This file contains 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.Feishu; | ||
|
||
/// <summary> | ||
/// Default values for Feishu authentication. | ||
/// </summary> | ||
public static class FeishuAuthenticationDefaults | ||
{ | ||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
/// </summary> | ||
public const string AuthenticationScheme = "Feishu"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
/// </summary> | ||
public static readonly string DisplayName = "Feishu"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
/// </summary> | ||
public static readonly string CallbackPath = "/signin-feishu"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
/// </summary> | ||
public static readonly string Issuer = "Feishu"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string AuthorizationEndpoint = "https://passport.feishu.cn/suite/passport/oauth/authorize"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
/// </summary> | ||
public static readonly string TokenEndpoint = "https://passport.feishu.cn/suite/passport/oauth/token"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string UserInformationEndpoint = "https://passport.feishu.cn/suite/passport/oauth/userinfo"; | ||
} |
74 changes: 74 additions & 0 deletions
74
src/AspNet.Security.OAuth.Feishu/FeishuAuthenticationExtensions.cs
This file contains 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.Feishu; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to add Feishu authentication capabilities to an HTTP application pipeline. | ||
/// </summary> | ||
public static class FeishuAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Feishu authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddFeishu([NotNull] this AuthenticationBuilder builder) | ||
{ | ||
return builder.AddFeishu(FeishuAuthenticationDefaults.AuthenticationScheme, _ => { }); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 AddFeishu( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] Action<FeishuAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddFeishu(FeishuAuthenticationDefaults.AuthenticationScheme, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 Feishu options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddFeishu( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[NotNull] Action<FeishuAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddFeishu(scheme, FeishuAuthenticationDefaults.DisplayName, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="FeishuAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables Feishu 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 Feishu options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddFeishu( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[CanBeNull] string caption, | ||
[NotNull] Action<FeishuAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddOAuth<FeishuAuthenticationOptions, FeishuAuthenticationHandler>(scheme, caption, configuration); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/AspNet.Security.OAuth.Feishu/FeishuAuthenticationHandler.cs
This file contains 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,71 @@ | ||
/* | ||
* 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.Feishu; | ||
|
||
public partial class FeishuAuthenticationHandler : OAuthHandler<FeishuAuthenticationOptions> | ||
{ | ||
public FeishuAuthenticationHandler( | ||
[NotNull] IOptionsMonitor<FeishuAuthenticationOptions> options, | ||
[NotNull] ILoggerFactory logger, | ||
[NotNull] UrlEncoder encoder, | ||
[NotNull] ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/AspNet.Security.OAuth.Feishu/FeishuAuthenticationOptions.cs
This file contains 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,31 @@ | ||
/* | ||
* 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.Feishu.FeishuAuthenticationConstants; | ||
|
||
namespace AspNet.Security.OAuth.Feishu; | ||
|
||
/// <summary> | ||
/// Defines a set of options used by <see cref="FeishuAuthenticationHandler"/>. | ||
/// </summary> | ||
public class FeishuAuthenticationOptions : OAuthOptions | ||
{ | ||
public FeishuAuthenticationOptions() | ||
{ | ||
ClaimsIssuer = FeishuAuthenticationDefaults.Issuer; | ||
CallbackPath = FeishuAuthenticationDefaults.CallbackPath; | ||
|
||
AuthorizationEndpoint = FeishuAuthenticationDefaults.AuthorizationEndpoint; | ||
TokenEndpoint = FeishuAuthenticationDefaults.TokenEndpoint; | ||
UserInformationEndpoint = FeishuAuthenticationDefaults.UserInformationEndpoint; | ||
|
||
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "open_id"); | ||
ClaimActions.MapJsonKey(ClaimTypes.Name, "name"); | ||
ClaimActions.MapJsonKey(Claims.UnionId, "union_id"); | ||
ClaimActions.MapJsonKey(Claims.Avatar, "avatar_big"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
test/AspNet.Security.OAuth.Providers.Tests/Feishu/FeishuTests.cs
This file contains 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,42 @@ | ||
/* | ||
* 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.Feishu; | ||
|
||
public class FeishuTests : OAuthTests<FeishuAuthenticationOptions> | ||
{ | ||
public FeishuTests(ITestOutputHelper outputHelper) | ||
{ | ||
OutputHelper = outputHelper; | ||
} | ||
|
||
public override string DefaultScheme => FeishuAuthenticationDefaults.AuthenticationScheme; | ||
|
||
protected internal override void RegisterAuthentication(AuthenticationBuilder builder) | ||
{ | ||
builder.AddFeishu(options => | ||
{ | ||
ConfigureDefaults(builder, options); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ClaimTypes.NameIdentifier, "test-open-id")] | ||
[InlineData(ClaimTypes.Name, "test-name")] | ||
[InlineData(FeishuAuthenticationConstants.Claims.UnionId, "test-union-id")] | ||
[InlineData(FeishuAuthenticationConstants.Claims.Avatar, "https://www.feishu.cn/avatar/icon_big")] | ||
public async Task Can_Sign_In_Using_Feishu(string claimType, string claimValue) | ||
{ | ||
// Arrange | ||
using var server = CreateTestServer(); | ||
|
||
// Act | ||
var claims = await AuthenticateUserAsync(server); | ||
|
||
// Assert | ||
AssertClaim(claims, claimType, claimValue); | ||
} | ||
} |
Oops, something went wrong.