-
Notifications
You must be signed in to change notification settings - Fork 547
Adds Linear provider #1045
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
Adds Linear provider #1045
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Integrating the Linear Provider | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| services.AddAuthentication(options => /* Auth configuration */) | ||
| .AddLinear(options => | ||
| { | ||
| options.ClientId = configuration["Linear:ClientId"] ?? string.Empty; | ||
| options.ClientSecret = configuration["Linear:ClientSecret"] ?? string.Empty; | ||
|
|
||
| // 'read' scope is added by default. Add additional scopes required | ||
| // options.Scope.Add("write"); | ||
|
|
||
| // Additional authorization parameters can also be added | ||
| // options.AdditionalAuthorizationParameters.Add("prompt", "consent"); | ||
| }) | ||
| ``` | ||
|
|
||
| ## Required Additional Settings | ||
|
|
||
| _None._ | ||
|
|
||
| ## Optional Settings | ||
|
|
||
| _None._ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <Description>ASP.NET Core security middleware enabling Linear authentication.</Description> | ||
| <Authors>Jerrie Pelser</Authors> | ||
| <PackageTags>aspnetcore;authentication;linear;oauth;security</PackageTags> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| <PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * 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.Linear; | ||
|
|
||
| /// <summary> | ||
| /// Contains constants specific to the <see cref="LinearAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public static class LinearAuthenticationConstants | ||
| { | ||
| public static class Claims | ||
| { | ||
| public const string OrganizationId = "urn:linear:organization_id"; | ||
|
|
||
| public const string OrganizationName = "urn:linear:organization_name"; | ||
|
|
||
| public const string OrganizationUrlKey = "urn:linear:organization_urlkey"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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.Linear; | ||
|
|
||
| /// <summary> | ||
| /// Default values used by the Linear authentication middleware. | ||
| /// </summary> | ||
| public static class LinearAuthenticationDefaults | ||
| { | ||
| /// <summary> | ||
| /// Default value for the <see cref="AuthenticationScheme.Name"/>. | ||
| /// </summary> | ||
| public const string AuthenticationScheme = "Linear"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for the <see cref="AuthenticationScheme.DisplayName"/>. | ||
| /// </summary> | ||
| public static readonly string DisplayName = "Linear"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for the <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
| /// </summary> | ||
| public static readonly string Issuer = "Linear"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for the <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
| /// </summary> | ||
| public static readonly string CallbackPath = "/signin-linear"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for the <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string AuthorizationEndpoint = "https://linear.app/oauth/authorize"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for the <see cref="OAuthOptions.TokenEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string TokenEndpointFormat = "https://api.linear.app/oauth/token"; | ||
| } |
| 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 AspNet.Security.OAuth.Linear; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| public static class LinearAuthenticationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds <see cref="LinearAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Linear authentication capabilities. | ||
| /// </summary> | ||
| /// <param name="builder">The authentication builder.</param> | ||
| /// <returns>A reference to this instance after the operation has completed.</returns> | ||
| public static AuthenticationBuilder AddLinear([NotNull] this AuthenticationBuilder builder) | ||
| { | ||
| return builder.AddLinear(LinearAuthenticationDefaults.AuthenticationScheme, options => { }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="LinearAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Linear authentication capabilities. | ||
| /// </summary> | ||
| /// <param name="builder">The authentication builder.</param> | ||
| /// <param name="configuration">The delegate used to configure the Linear options.</param> | ||
| /// <returns>A reference to this instance after the operation has completed.</returns> | ||
| public static AuthenticationBuilder AddLinear( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] Action<LinearAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddLinear(LinearAuthenticationDefaults.AuthenticationScheme, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="LinearAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Linear 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 Linear options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddLinear( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [NotNull] Action<LinearAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddLinear(scheme, LinearAuthenticationDefaults.DisplayName, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="LinearAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Linear 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 Linear options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddLinear( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [NotNull] string caption, | ||
| [NotNull] Action<LinearAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddOAuth<LinearAuthenticationOptions, LinearAuthenticationHandler>(scheme, caption, configuration); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * 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.Net.Mime; | ||
| using System.Security.Claims; | ||
| using System.Text; | ||
| using System.Text.Encodings.Web; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace AspNet.Security.OAuth.Linear; | ||
|
|
||
| public partial class LinearAuthenticationHandler( | ||
| [NotNull] IOptionsMonitor<LinearAuthenticationOptions> options, | ||
| [NotNull] ILoggerFactory logger, | ||
| [NotNull] UrlEncoder encoder) : OAuthHandler<LinearAuthenticationOptions>(options, logger, encoder) | ||
| { | ||
| private const string UserQuery = """ | ||
| query { | ||
| viewer { | ||
| id | ||
| name | ||
| organization { | ||
| id | ||
| name | ||
| urlKey | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| protected override async Task<AuthenticationTicket> CreateTicketAsync( | ||
| [NotNull] ClaimsIdentity identity, | ||
| [NotNull] AuthenticationProperties properties, | ||
| [NotNull] OAuthTokenResponse tokens) | ||
| { | ||
| using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.linear.app/graphql"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Linear does not have a user info endpoint and instead has a single GraphQL endpoint for all API requests. To retrieve the user info, you must build a GraphQL query and send it to the GraphQL endpoint. Therefore, I did not consider it appropriate to use the usual pattern of setting and using the |
||
| request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); | ||
| request.Content = new StringContent( | ||
| JsonSerializer.Serialize(new GraphqlQuery(UserQuery), AppJsonSerializerContext.Default.GraphqlQuery), | ||
| Encoding.UTF8, | ||
| MediaTypeNames.Application.Json); | ||
|
|
||
| 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 from Linear."); | ||
| } | ||
|
|
||
| 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.GetProperty("data").GetProperty("viewer")); | ||
| context.RunClaimActions(); | ||
|
|
||
| await Events.CreatingTicket(context); | ||
| return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); | ||
| } | ||
|
|
||
| [JsonSerializable(typeof(GraphqlQuery))] | ||
| internal sealed partial class AppJsonSerializerContext : JsonSerializerContext; | ||
|
|
||
| internal sealed record GraphqlQuery([property: JsonPropertyName("query")] string Query); | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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.Linear.LinearAuthenticationConstants; | ||
|
|
||
| namespace AspNet.Security.OAuth.Linear; | ||
|
|
||
| /// <summary> | ||
| /// Defines a set of options used by <see cref="LinearAuthenticationHandler"/>. | ||
| /// </summary> | ||
| public class LinearAuthenticationOptions : OAuthOptions | ||
| { | ||
| public LinearAuthenticationOptions() | ||
| { | ||
| ClaimsIssuer = LinearAuthenticationDefaults.Issuer; | ||
| CallbackPath = LinearAuthenticationDefaults.CallbackPath; | ||
|
|
||
| AuthorizationEndpoint = LinearAuthenticationDefaults.AuthorizationEndpoint; | ||
| TokenEndpoint = LinearAuthenticationDefaults.TokenEndpointFormat; | ||
|
|
||
| Scope.Add("read"); | ||
|
|
||
| ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Name, "name"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); | ||
| ClaimActions.MapJsonSubKey(Claims.OrganizationId, "organization", "id"); | ||
| ClaimActions.MapJsonSubKey(Claims.OrganizationName, "organization", "name"); | ||
| ClaimActions.MapJsonSubKey(Claims.OrganizationUrlKey, "organization", "urlKey"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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.Linear; | ||
|
|
||
| public class LinearTests(ITestOutputHelper outputHelper) : OAuthTests<LinearAuthenticationOptions>(outputHelper) | ||
| { | ||
| public override string DefaultScheme => LinearAuthenticationDefaults.AuthenticationScheme; | ||
|
|
||
| protected internal override void RegisterAuthentication(AuthenticationBuilder builder) | ||
| { | ||
| builder.AddLinear(options => | ||
| { | ||
| ConfigureDefaults(builder, options); | ||
| }); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ClaimTypes.NameIdentifier, "the-user-id")] | ||
| [InlineData(ClaimTypes.Name, "Marty McFly")] | ||
| [InlineData(ClaimTypes.Email, "marty@hillvalley.com")] | ||
| [InlineData(LinearAuthenticationConstants.Claims.OrganizationId, "the-org-id")] | ||
| [InlineData(LinearAuthenticationConstants.Claims.OrganizationName, "Hill Valley Ventures")] | ||
| [InlineData(LinearAuthenticationConstants.Claims.OrganizationUrlKey, "hill-valley")] | ||
| public async Task Can_Sign_In_Using_Linear(string claimType, string claimValue) | ||
| { | ||
| await AuthenticateUserAndAssertClaimValue(claimType, claimValue); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.