-
Notifications
You must be signed in to change notification settings - Fork 547
Add Atlassian provider #839 #1037
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
martincostello
merged 12 commits into
aspnet-contrib:dev
from
smnsht:atlassian-provider
Mar 13, 2025
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97cf80a
project Atlassian created
smnsht afffc42
claims from extended profile
smnsht f39f624
Can_Sign_In_Using_Atlassian looks OK
smnsht 4117ce4
add scope "read:me"
smnsht 72aafb8
remove comment
smnsht b273d5f
add docs
smnsht ee745ad
add AdditionalAuthorizationParameters
28a6301
remove unnecessary docs
8826869
remove 'atlassian.md' entry from the solution
6484400
Update src/AspNet.Security.OAuth.Atlassian/AspNet.Security.OAuth.Atla…
smnsht 5894cf4
Update src/AspNet.Security.OAuth.Atlassian/AspNet.Security.OAuth.Atla…
smnsht afa47f8
requested my martincostello #1
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
18 changes: 18 additions & 0 deletions
18
src/AspNet.Security.OAuth.Atlassian/AspNet.Security.OAuth.Atlassian.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,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
|
smnsht marked this conversation as resolved.
Outdated
|
||
| <Description>ASP.NET Core security middleware enabling Atlassian authentication.</Description> | ||
| <Authors>smnsht</Authors> | ||
| <PackageTags>atlassian;aspnetcore;authentication;oauth;security</PackageTags> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| <PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
53 changes: 53 additions & 0 deletions
53
src/AspNet.Security.OAuth.Atlassian/AtlassianAuthenticationDefaults.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,53 @@ | ||
| // 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.Atlassian; | ||
|
|
||
| public class AtlassianAuthenticationDefaults | ||
| { | ||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
| /// </summary> | ||
| public const string AuthenticationScheme = "Atlassian"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
| /// </summary> | ||
| public static readonly string DisplayName = "Atlassian"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
| /// </summary> | ||
| public static readonly string Issuer = "Atlassian"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
| /// </summary> | ||
| public static readonly string CallbackPath = "/signin-atlassian"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string AuthorizationEndpoint = "https://auth.atlassian.com/authorize"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string TokenEndpoint = "https://auth.atlassian.com/oauth/token"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
| /// </summary> | ||
| public static readonly string UserInformationEndpoint = "https://api.atlassian.com/me"; | ||
|
|
||
| /// <summary> | ||
| /// Default value for <see cref="OAuthOptions.AdditionalAuthorizationParameters"/>. | ||
| /// </summary> | ||
| public static readonly IDictionary<string, string> AdditionalAuthorizationParameters = | ||
| new Dictionary<string, string>( | ||
| [ | ||
| new KeyValuePair<string, string>("audience", "api.atlassian.com"), | ||
| new KeyValuePair<string, string>("prompt", "consent") | ||
| ]); | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
| } | ||
72 changes: 72 additions & 0 deletions
72
src/AspNet.Security.OAuth.Atlassian/AtlassianAuthenticationExtensions.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 Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace AspNet.Security.OAuth.Atlassian; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to add Atlassian authentication capabilities to an HTTP application pipeline. | ||
| /// </summary> | ||
| public static class AtlassianAuthenticationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds <see cref="AtlassianAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Atlassian 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 AddAtlassian([NotNull] this AuthenticationBuilder builder) | ||
| { | ||
| return builder.AddAtlassian(AtlassianAuthenticationDefaults.AuthenticationScheme, options => { }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="AtlassianAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Atlassian 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>A reference to this instance after the operation has completed.</returns> | ||
| public static AuthenticationBuilder AddAtlassian( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] Action<AtlassianAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddAtlassian(AtlassianAuthenticationDefaults.AuthenticationScheme, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="AtlassianAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Atlassian 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 Atlassian options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddAtlassian( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [NotNull] Action<AtlassianAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddAtlassian(scheme, AtlassianAuthenticationDefaults.DisplayName, configuration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="AtlassianAuthenticationHandler"/> to the specified | ||
| /// <see cref="AuthenticationBuilder"/>, which enables Atlassian 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 Atlassian options.</param> | ||
| /// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
| public static AuthenticationBuilder AddAtlassian( | ||
| [NotNull] this AuthenticationBuilder builder, | ||
| [NotNull] string scheme, | ||
| [CanBeNull] string caption, | ||
| [NotNull] Action<AtlassianAuthenticationOptions> configuration) | ||
| { | ||
| return builder.AddOAuth<AtlassianAuthenticationOptions, AtlassianAuthenticationHandler>(scheme, caption, configuration); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/AspNet.Security.OAuth.Atlassian/AtlassianAuthenticationHandler.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,70 @@ | ||
| /* | ||
| * 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.Atlassian; | ||
|
|
||
| public partial class AtlassianAuthenticationHandler : OAuthHandler<AtlassianAuthenticationOptions> | ||
| { | ||
| public AtlassianAuthenticationHandler( | ||
| [NotNull] IOptionsMonitor<AtlassianAuthenticationOptions> 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("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); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/AspNet.Security.OAuth.Atlassian/AtlassianAuthenticationOptions.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,47 @@ | ||
| /* | ||
| * 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.Linq; | ||
| using System.Security.Claims; | ||
| using static AspNet.Security.OAuth.Atlassian.AtlassianOAuthenticationConstants; | ||
|
|
||
| namespace AspNet.Security.OAuth.Atlassian; | ||
|
|
||
| public partial class AtlassianAuthenticationOptions : OAuthOptions | ||
| { | ||
| public AtlassianAuthenticationOptions() | ||
| { | ||
| ClaimsIssuer = AtlassianAuthenticationDefaults.Issuer; | ||
| CallbackPath = AtlassianAuthenticationDefaults.CallbackPath; | ||
|
|
||
| AuthorizationEndpoint = AtlassianAuthenticationDefaults.AuthorizationEndpoint; | ||
| TokenEndpoint = AtlassianAuthenticationDefaults.TokenEndpoint; | ||
| UserInformationEndpoint = AtlassianAuthenticationDefaults.UserInformationEndpoint; | ||
|
|
||
| foreach (var additionalParameter in AtlassianAuthenticationDefaults.AdditionalAuthorizationParameters) | ||
| { | ||
| AdditionalAuthorizationParameters.Add(additionalParameter); | ||
| } | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
|
|
||
| Scope.Add("read:me"); | ||
|
|
||
| ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "account_id"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); | ||
| ClaimActions.MapJsonKey(ClaimTypes.Name, "name"); | ||
|
|
||
| ClaimActions.MapJsonKey(Claims.AccountType, "account_type"); | ||
| ClaimActions.MapJsonKey(Claims.Picture, "picture"); | ||
| ClaimActions.MapJsonKey(Claims.AccountStatus, "account_status"); | ||
| ClaimActions.MapJsonKey(Claims.Nickname, "nickname"); | ||
| ClaimActions.MapJsonKey(Claims.ZoneInfo, "zoneinfo"); | ||
| ClaimActions.MapJsonKey(Claims.Locale, "locale"); | ||
|
|
||
| ClaimActions.MapJsonSubKey(Claims.JobTitle, "extended_profile", "job_title"); | ||
| ClaimActions.MapJsonSubKey(Claims.Organization, "extended_profile", "organization"); | ||
| ClaimActions.MapJsonSubKey(Claims.Department, "extended_profile", "department"); | ||
| ClaimActions.MapJsonSubKey(Claims.Location, "extended_profile", "location"); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/AspNet.Security.OAuth.Atlassian/AtlassianOAuthenticationConstants.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,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.Atlassian; | ||
|
|
||
| public static class AtlassianOAuthenticationConstants | ||
| { | ||
| public static class Claims | ||
| { | ||
| public const string AccountType = "urn:atlassian:account_type"; | ||
| public const string Picture = "urn:atlassian:picture"; | ||
| public const string AccountStatus = "urn:atlassian:account_status"; | ||
| public const string Nickname = "urn:atlassian:nickname"; | ||
| public const string ZoneInfo = "urn:atlassian:zoneinfo"; | ||
| public const string Locale = "urn:atlassian:locale"; | ||
| public const string JobTitle = "urn:atlassian:job_title"; | ||
| public const string Organization = "urn:atlassian:organization"; | ||
| public const string Department = "urn:atlassian:department"; | ||
| public const string Location = "urn:atlassian:location"; | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
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.