Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions AspNet.Security.OAuth.Providers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C2CA4B38-A
docs\bitbucket.md = docs\bitbucket.md
docs\digitalocean.md = docs\digitalocean.md
docs\discord.md = docs\discord.md
docs\docusign.md = docs\docusign.md
docs\dropbox.md = docs\dropbox.md
docs\ebay.md = docs\ebay.md
docs\eveonline.md = docs\eveonline.md
docs\foursquare.md = docs\foursquare.md
docs\gitcode.md = docs\gitcode.md
docs\gitee.md = docs\gitee.md
docs\github.md = docs\github.md
docs\instagram.md = docs\instagram.md
Expand Down Expand Up @@ -219,8 +221,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C2CA4B38-A
docs\workweixin.md = docs\workweixin.md
docs\xumm.md = docs\xumm.md
docs\zendesk.md = docs\zendesk.md
docs\docusign.md = docs\docusign.md
docs\gitcode.md = docs\gitcode.md
EndProjectSection
EndProject
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}"
Expand Down Expand Up @@ -316,6 +316,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.VkId"
EndProject
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Atlassian", "src\AspNet.Security.OAuth.Atlassian\AspNet.Security.OAuth.Atlassian.csproj", "{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -734,6 +736,10 @@ Global
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{668833D5-DB6A-475F-B0FD-A03462B037B8}.Release|Any CPU.Build.0 = Release|Any CPU
{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -848,6 +854,7 @@ Global
{CD56ABE4-1CD2-4029-B556-E110A31A2CC4} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{F3E62C24-5F82-4CF5-A994-0E10D04FB495} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{668833D5-DB6A-475F-B0FD-A03462B037B8} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{D2110C1B-6FE1-4D9A-81ED-93FB2AC85049} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
</PropertyGroup>
Comment thread
smnsht marked this conversation as resolved.

<PropertyGroup>
Comment thread
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>
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")
]);
Comment thread
martincostello marked this conversation as resolved.
Outdated
}
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);
}
}
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);
}
}
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);
}
Comment thread
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");
}
}
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";
Comment thread
martincostello marked this conversation as resolved.
Outdated
}
}
Loading