Skip to content

Commit b44e276

Browse files
lukefullitonmartincostello
authored andcommitted
Add Basecamp provider (#406)
* Adding Luke Fulliton as a contributor. * Added new provider, Basecamp. * Moved the basecamp provider project location to the same location that the other providers reside. * Added Basecamp Tests. Basecamp Tests pass successfully. * Updated the README.md file * Removed unnecessary JSON property check in the BasecampAuthenticationHandler.
1 parent a25b0a0 commit b44e276

File tree

9 files changed

+338
-0
lines changed

9 files changed

+338
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C2CA4B38-A
185185
docs\sign-in-with-apple.md = docs\sign-in-with-apple.md
186186
EndProjectSection
187187
EndProject
188+
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}"
189+
EndProject
188190
Global
189191
GlobalSection(SolutionConfigurationPlatforms) = preSolution
190192
Debug|Any CPU = Debug|Any CPU
@@ -435,6 +437,10 @@ Global
435437
{E82424B3-0E73-4954-B6A6-BFF1029A08DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
436438
{E82424B3-0E73-4954-B6A6-BFF1029A08DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
437439
{E82424B3-0E73-4954-B6A6-BFF1029A08DE}.Release|Any CPU.Build.0 = Release|Any CPU
440+
{42306484-B2BF-4B52-B950-E0CDFA58B02A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
441+
{42306484-B2BF-4B52-B950-E0CDFA58B02A}.Debug|Any CPU.Build.0 = Debug|Any CPU
442+
{42306484-B2BF-4B52-B950-E0CDFA58B02A}.Release|Any CPU.ActiveCfg = Release|Any CPU
443+
{42306484-B2BF-4B52-B950-E0CDFA58B02A}.Release|Any CPU.Build.0 = Release|Any CPU
438444
EndGlobalSection
439445
GlobalSection(SolutionProperties) = preSolution
440446
HideSolutionNode = FALSE
@@ -506,6 +512,7 @@ Global
506512
{0D9EB03D-99AF-4A80-B7CE-2302A8D3747B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
507513
{E82424B3-0E73-4954-B6A6-BFF1029A08DE} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
508514
{C2CA4B38-AA21-4CA4-8799-2E8C8C06754F} = {E9DAB098-A902-4EF5-9AEE-CF735DF31E35}
515+
{42306484-B2BF-4B52-B950-E0CDFA58B02A} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
509516
EndGlobalSection
510517
GlobalSection(ExtensibilityGlobals) = postSolution
511518
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ We would love it if you could help contributing to this repository.
6767
* [Jordan Knight](https://github.com/jakkaj)
6868
* [Kévin Chalet](https://github.com/kevinchalet)
6969
* [Konstantin Mamaev](https://github.com/MrMeison)
70+
* [Luke Fulliton](https://github.com/lukefulliton)
7071
* [Mariusz Zieliński](https://github.com/mariozski)
7172
* [Martin Costello](https://github.com/martincostello)
7273
* [Maxime Roussin-Bélanger](https://github.com/Lorac)
@@ -113,6 +114,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
113114
| Autodesk | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Autodesk?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Autodesk/ "Download AspNet.Security.OAuth.Autodesk from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Autodesk?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Autodesk "Download AspNet.Security.OAuth.Autodesk from MyGet.org") | [Documentation](https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/overview/ "Autodesk developer documentation") |
114115
| Automatic | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Automatic?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Automatic/ "Download AspNet.Security.OAuth.Automatic from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Automatic?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Automatic "Download AspNet.Security.OAuth.Automatic from MyGet.org") | [Documentation](https://developer.automatic.com/documentation/ "Automatic developer documentation") |
115116
| Baidu | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Baidu?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Baidu/ "Download AspNet.Security.OAuth.Baidu from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Baidu?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Baidu "Download AspNet.Security.OAuth.Baidu from MyGet.org") | [Documentation](https://developer.baidu.com/ "Baidu developer documentation") |
117+
| Basecamp | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Basecamp?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Basecamp/ "Download AspNet.Security.OAuth.Basecamp from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Basecamp?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Basecamp "Download AspNet.Security.OAuth.Basecamp from MyGet.org") | [Documentation](https://github.com/basecamp/api/blob/master/sections/authentication.md "Basecamp developer documentation") |
116118
| BattleNet | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.BattleNet?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.BattleNet/ "Download AspNet.Security.OAuth.BattleNet from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.BattleNet?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.BattleNet "Download AspNet.Security.OAuth.BattleNet from MyGet.org") | [Documentation](https://develop.battle.net/documentation/guides/using-oauth "BattleNet developer documentation") |
117119
| Beam (Mixer) | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Beam?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Beam/ "Download AspNet.Security.OAuth.Beam from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Beam?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Beam "Download AspNet.Security.OAuth.Beam from MyGet.org") | [Documentation](https://dev.mixer.com/reference/oauth "Mixer developer documentation") |
118120
| Bitbucket | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Bitbucket?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Bitbucket/ "Download AspNet.Security.OAuth.Bitbucket from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Bitbucket?includePreReleases=false)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Bitbucket "Download AspNet.Security.OAuth.Bitbucket from MyGet.org") | [Documentation](https://developer.atlassian.com/bitbucket/api/2/reference/meta/authentication "Bitbucket developer documentation") |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<Description>ASP.NET Core security middleware enabling Basecamp authentication.</Description>
9+
<Authors>Luke Fulliton</Authors>
10+
<PackageTags>basecamp;37signal;aspnetcore;authentication;oauth;security</PackageTags>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 Microsoft.AspNetCore.Authentication;
8+
using Microsoft.AspNetCore.Authentication.OAuth;
9+
10+
namespace AspNet.Security.OAuth.Basecamp
11+
{
12+
/// <summary>
13+
/// Default values used by the Basecamp authentication middleware.
14+
/// </summary>
15+
public static class BasecampAuthenticationDefaults
16+
{
17+
/// <summary>
18+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
19+
/// </summary>
20+
public const string AuthenticationScheme = "Basecamp";
21+
22+
/// <summary>
23+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
24+
/// </summary>
25+
public const string DisplayName = "Basecamp";
26+
27+
/// <summary>
28+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
29+
/// </summary>
30+
public const string Issuer = "Basecamp";
31+
32+
/// <summary>
33+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
34+
/// </summary>
35+
public const string CallbackPath = "/signin-basecamp";
36+
37+
/// <summary>
38+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
39+
/// </summary>
40+
public const string AuthorizationEndpoint = "https://launchpad.37signals.com/authorization/new?type=web_server";
41+
42+
/// <summary>
43+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
44+
/// </summary>
45+
public const string TokenEndpoint = "https://launchpad.37signals.com/authorization/token?type=web_server";
46+
47+
/// <summary>
48+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
49+
/// </summary>
50+
public const string UserInformationEndpoint = "https://launchpad.37signals.com/authorization.json";
51+
}
52+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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;
8+
using AspNet.Security.OAuth.Basecamp;
9+
using JetBrains.Annotations;
10+
using Microsoft.AspNetCore.Authentication;
11+
12+
namespace Microsoft.Extensions.DependencyInjection
13+
{
14+
/// <summary>
15+
/// Extension methods to add Basecamp authentication capabilities to an HTTP application pipeline.
16+
/// </summary>
17+
public static class BasecampAuthenticationExtensions
18+
{
19+
/// <summary>
20+
/// Adds <see cref="BasecampAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables Basecamp authentication capabilities.
22+
/// </summary>
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>A reference to this instance after the operation has completed.</returns>
25+
public static AuthenticationBuilder AddBasecamp([NotNull] this AuthenticationBuilder builder)
26+
{
27+
return builder.AddBasecamp(BasecampAuthenticationDefaults.AuthenticationScheme, options => { });
28+
}
29+
30+
/// <summary>
31+
/// Adds <see cref="BasecampAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables Basecamp authentication capabilities.
33+
/// </summary>
34+
/// <param name="builder">The authentication builder.</param>
35+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
36+
/// <returns>A reference to this instance after the operation has completed.</returns>
37+
public static AuthenticationBuilder AddBasecamp(
38+
[NotNull] this AuthenticationBuilder builder,
39+
[NotNull] Action<BasecampAuthenticationOptions> configuration)
40+
{
41+
return builder.AddBasecamp(BasecampAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
43+
44+
/// <summary>
45+
/// Adds <see cref="BasecampAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables Basecamp authentication capabilities.
47+
/// </summary>
48+
/// <param name="builder">The authentication builder.</param>
49+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
50+
/// <param name="configuration">The delegate used to configure the Basecamp options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddBasecamp(
53+
[NotNull] this AuthenticationBuilder builder,
54+
[NotNull] string scheme,
55+
[NotNull] Action<BasecampAuthenticationOptions> configuration)
56+
{
57+
return builder.AddBasecamp(scheme, BasecampAuthenticationDefaults.DisplayName, configuration);
58+
}
59+
60+
/// <summary>
61+
/// Adds <see cref="BasecampAuthenticationHandler"/> to the specified
62+
/// <see cref="AuthenticationBuilder"/>, which enables Basecamp authentication capabilities.
63+
/// </summary>
64+
/// <param name="builder">The authentication builder.</param>
65+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
66+
/// <param name="caption">The optional display name associated with this instance.</param>
67+
/// <param name="configuration">The delegate used to configure the Basecamp options.</param>
68+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
69+
public static AuthenticationBuilder AddBasecamp(
70+
[NotNull] this AuthenticationBuilder builder,
71+
[NotNull] string scheme,
72+
[CanBeNull] string caption,
73+
[NotNull] Action<BasecampAuthenticationOptions> configuration)
74+
{
75+
return builder.AddOAuth<BasecampAuthenticationOptions, BasecampAuthenticationHandler>(scheme, caption, configuration);
76+
}
77+
}
78+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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;
8+
using System.Net.Http.Headers;
9+
using System.Security.Claims;
10+
using System.Text.Encodings.Web;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
13+
using JetBrains.Annotations;
14+
using Microsoft.AspNetCore.Authentication;
15+
using Microsoft.AspNetCore.Authentication.OAuth;
16+
using Microsoft.Extensions.Logging;
17+
using Microsoft.Extensions.Options;
18+
19+
namespace AspNet.Security.OAuth.Basecamp
20+
{
21+
public class BasecampAuthenticationHandler : OAuthHandler<BasecampAuthenticationOptions>
22+
{
23+
public BasecampAuthenticationHandler(
24+
[NotNull] IOptionsMonitor<BasecampAuthenticationOptions> options,
25+
[NotNull] ILoggerFactory logger,
26+
[NotNull] UrlEncoder encoder,
27+
[NotNull] ISystemClock clock)
28+
: base(options, logger, encoder, clock)
29+
{
30+
}
31+
32+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
33+
[NotNull] ClaimsIdentity identity,
34+
[NotNull] AuthenticationProperties properties,
35+
[NotNull] OAuthTokenResponse tokens)
36+
{
37+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
38+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
39+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
40+
41+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
42+
if (!response.IsSuccessStatusCode)
43+
{
44+
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
45+
"returned a {Status} response with the following payload: {Headers} {Body}.",
46+
/* Status: */ response.StatusCode,
47+
/* Headers: */ response.Headers.ToString(),
48+
/* Body: */ await response.Content.ReadAsStringAsync());
49+
50+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
51+
}
52+
53+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
54+
55+
var principal = new ClaimsPrincipal(identity);
56+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
57+
context.RunClaimActions();
58+
59+
await Options.Events.CreatingTicket(context);
60+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
61+
}
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Authentication.OAuth;
10+
11+
namespace AspNet.Security.OAuth.Basecamp
12+
{
13+
/// <summary>
14+
/// Defines a set of options used by <see cref="BasecampAuthenticationHandler"/>.
15+
/// </summary>
16+
public class BasecampAuthenticationOptions : OAuthOptions
17+
{
18+
public BasecampAuthenticationOptions()
19+
{
20+
ClaimsIssuer = BasecampAuthenticationDefaults.Issuer;
21+
CallbackPath = BasecampAuthenticationDefaults.CallbackPath;
22+
AuthorizationEndpoint = BasecampAuthenticationDefaults.AuthorizationEndpoint;
23+
TokenEndpoint = BasecampAuthenticationDefaults.TokenEndpoint;
24+
UserInformationEndpoint = BasecampAuthenticationDefaults.UserInformationEndpoint;
25+
26+
ClaimActions.MapJsonSubKey(ClaimTypes.NameIdentifier, "identity", "id");
27+
ClaimActions.MapJsonSubKey(ClaimTypes.GivenName, "identity", "first_name");
28+
ClaimActions.MapJsonSubKey(ClaimTypes.Surname, "identity", "last_name");
29+
ClaimActions.MapJsonSubKey(ClaimTypes.Email, "identity", "email_address");
30+
}
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Authentication;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Xunit;
12+
using Xunit.Abstractions;
13+
14+
namespace AspNet.Security.OAuth.Basecamp
15+
{
16+
public class BasecampTests : OAuthTests<BasecampAuthenticationOptions>
17+
{
18+
public BasecampTests(ITestOutputHelper outputHelper)
19+
{
20+
OutputHelper = outputHelper;
21+
}
22+
23+
public override string DefaultScheme => BasecampAuthenticationDefaults.AuthenticationScheme;
24+
25+
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
26+
{
27+
builder.AddBasecamp(options => ConfigureDefaults(builder, options));
28+
}
29+
30+
[Theory]
31+
[InlineData(ClaimTypes.NameIdentifier, "12345678")]
32+
[InlineData(ClaimTypes.GivenName, "John")]
33+
[InlineData(ClaimTypes.Surname, "Smith")]
34+
[InlineData(ClaimTypes.Email, "[email protected]")]
35+
public async Task Can_Sign_In_Using_Basecamp(string claimType, string claimValue)
36+
{
37+
// Arrange
38+
using var server = CreateTestServer();
39+
40+
// Act
41+
var claims = await AuthenticateUserAsync(server);
42+
43+
// Assert
44+
AssertClaim(claims, claimType, claimValue);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)