Skip to content

Commit d0a313c

Browse files
Add Coinbase provider (#551)
Co-authored-by: Martin Costello <[email protected]>
1 parent dd1c560 commit d0a313c

11 files changed

+356
-1
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Line"
212212
EndProject
213213
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Moodle", "src\AspNet.Security.OAuth.Moodle\AspNet.Security.OAuth.Moodle.csproj", "{1745B11F-F700-4604-B61F-54B962A6BE9A}"
214214
EndProject
215+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Coinbase", "src\AspNet.Security.OAuth.Coinbase\AspNet.Security.OAuth.Coinbase.csproj", "{839267B9-492D-47B6-A6AF-454282142123}"
216+
EndProject
215217
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Streamlabs", "src\AspNet.Security.OAuth.Streamlabs\AspNet.Security.OAuth.Streamlabs.csproj", "{514D2D1E-E72F-4998-9DF3-D841F399AB37}"
216218
EndProject
217219
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Keycloak", "src\AspNet.Security.OAuth.Keycloak\AspNet.Security.OAuth.Keycloak.csproj", "{01C2AC98-B615-49F8-80A5-475A2B75A8FD}"
@@ -514,6 +516,10 @@ Global
514516
{1745B11F-F700-4604-B61F-54B962A6BE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
515517
{1745B11F-F700-4604-B61F-54B962A6BE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
516518
{1745B11F-F700-4604-B61F-54B962A6BE9A}.Release|Any CPU.Build.0 = Release|Any CPU
519+
{839267B9-492D-47B6-A6AF-454282142123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
520+
{839267B9-492D-47B6-A6AF-454282142123}.Debug|Any CPU.Build.0 = Debug|Any CPU
521+
{839267B9-492D-47B6-A6AF-454282142123}.Release|Any CPU.ActiveCfg = Release|Any CPU
522+
{839267B9-492D-47B6-A6AF-454282142123}.Release|Any CPU.Build.0 = Release|Any CPU
517523
{514D2D1E-E72F-4998-9DF3-D841F399AB37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
518524
{514D2D1E-E72F-4998-9DF3-D841F399AB37}.Debug|Any CPU.Build.0 = Debug|Any CPU
519525
{514D2D1E-E72F-4998-9DF3-D841F399AB37}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -613,6 +619,7 @@ Global
613619
{61AB67B0-0F4A-47A2-A4D8-9738AA34C468} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
614620
{6A2FEBAB-2372-4043-BD37-B2E94887BA9B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
615621
{1745B11F-F700-4604-B61F-54B962A6BE9A} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
622+
{839267B9-492D-47B6-A6AF-454282142123} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
616623
{514D2D1E-E72F-4998-9DF3-D841F399AB37} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
617624
{DF2786DF-234D-4A8C-B166-0B8F8B7D527B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
618625
{01C2AC98-B615-49F8-80A5-475A2B75A8FD} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
126126
| 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=true)](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") |
127127
| Buffer | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Buffer?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Buffer/ "Download AspNet.Security.OAuth.Buffer from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Buffer?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Buffer "Download AspNet.Security.OAuth.Buffer from MyGet.org") | [Documentation](https://buffer.com/developers/api/oauth "Buffer developer documentation") |
128128
| CiscoSpark (Webex Teams) | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.CiscoSpark?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.CiscoSpark/ "Download AspNet.Security.OAuth.CiscoSpark from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.CiscoSpark?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.CiscoSpark "Download AspNet.Security.OAuth.CiscoSpark from MyGet.org") | [Documentation](https://developer.webex.com/docs/api/getting-started/accounts-and-authentication "Webex Teams developer documentation") |
129+
| Coinbase | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Coinbase?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Coinbase/ "Download AspNet.Security.OAuth.Coinbase from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Coinbase?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Coinbase "Download AspNet.Security.OAuth.Coinbase from MyGet.org") | [Documentation](https://developers.coinbase.com/docs/wallet/coinbase-connect/integrating "Coinbase developer documentation") |
129130
| DeviantArt | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.DeviantArt?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.DeviantArt/ "Download AspNet.Security.OAuth.DeviantArt from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.DeviantArt?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.DeviantArt "Download AspNet.Security.OAuth.DeviantArt from MyGet.org") | [Documentation](https://www.deviantart.com/developers/ "DeviantArt developer documentation") |
130131
| Deezer | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Deezer?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Deezer/ "Download AspNet.Security.OAuth.Deezer from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Deezer?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Deezer "Download AspNet.Security.OAuth.Deezer from MyGet.org") | [Documentation](https://developers.deezer.com/api/oauth "Deezer developer documentation") |
131132
| Discord | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Discord?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Discord/ "Download AspNet.Security.OAuth.Discord from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Discord?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Discord "Download AspNet.Security.OAuth.Discord from MyGet.org") | [Documentation](https://discord.com/developers/docs/topics/oauth2 "Discord developer documentation") |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
9+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
namespace AspNet.Security.OAuth.Coinbase
8+
{
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="CoinbaseAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class CoinbaseAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string Username = "urn:coinbase:username";
17+
public const string ProfileLocation = "urn:coinbase:profile_location";
18+
public const string ProfileBio = "urn:coinbase:profile_bio";
19+
public const string ProfileUrl = "urn:coinbase:profile_url";
20+
public const string AvatarUrl = "urn:coinbase:avatar_url";
21+
}
22+
}
23+
}
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.Coinbase
11+
{
12+
/// <summary>
13+
/// Default values used by the Coinbase authentication middleware.
14+
/// </summary>
15+
public static class CoinbaseAuthenticationDefaults
16+
{
17+
/// <summary>
18+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
19+
/// </summary>
20+
public const string AuthenticationScheme = "Coinbase";
21+
22+
/// <summary>
23+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
24+
/// </summary>
25+
public const string DisplayName = "Coinbase";
26+
27+
/// <summary>
28+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
29+
/// </summary>
30+
public const string Issuer = "Coinbase";
31+
32+
/// <summary>
33+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
34+
/// </summary>
35+
public const string CallbackPath = "/signin-coinbase";
36+
37+
/// <summary>
38+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
39+
/// </summary>
40+
public const string AuthorizationEndpoint = "https://www.coinbase.com/oauth/authorize";
41+
42+
/// <summary>
43+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
44+
/// </summary>
45+
public const string TokenEndpoint = "https://www.coinbase.com/oauth/token";
46+
47+
/// <summary>
48+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
49+
/// </summary>
50+
public const string UserInformationEndpoint = "https://api.coinbase.com/v2/user";
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.Coinbase;
9+
using JetBrains.Annotations;
10+
using Microsoft.AspNetCore.Authentication;
11+
12+
namespace Microsoft.Extensions.DependencyInjection
13+
{
14+
/// <summary>
15+
/// Extension methods to add Coinbase authentication capabilities to an HTTP application pipeline.
16+
/// </summary>
17+
public static class CoinbaseAuthenticationExtensions
18+
{
19+
/// <summary>
20+
/// Adds <see cref="CoinbaseAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables Coinbase 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 AddCoinbase([NotNull] this AuthenticationBuilder builder)
26+
{
27+
return builder.AddCoinbase(CoinbaseAuthenticationDefaults.AuthenticationScheme, options => { });
28+
}
29+
30+
/// <summary>
31+
/// Adds <see cref="CoinbaseAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables Coinbase 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 AddCoinbase(
38+
[NotNull] this AuthenticationBuilder builder,
39+
[NotNull] Action<CoinbaseAuthenticationOptions> configuration)
40+
{
41+
return builder.AddCoinbase(CoinbaseAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
43+
44+
/// <summary>
45+
/// Adds <see cref="CoinbaseAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables Coinbase 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 Coinbase options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddCoinbase(
53+
[NotNull] this AuthenticationBuilder builder,
54+
[NotNull] string scheme,
55+
[NotNull] Action<CoinbaseAuthenticationOptions> configuration)
56+
{
57+
return builder.AddCoinbase(scheme, CoinbaseAuthenticationDefaults.DisplayName, configuration);
58+
}
59+
60+
/// <summary>
61+
/// Adds <see cref="CoinbaseAuthenticationHandler"/> to the specified
62+
/// <see cref="AuthenticationBuilder"/>, which enables Coinbase 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 Coinbase options.</param>
68+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
69+
public static AuthenticationBuilder AddCoinbase(
70+
[NotNull] this AuthenticationBuilder builder,
71+
[NotNull] string scheme,
72+
[CanBeNull] string caption,
73+
[NotNull] Action<CoinbaseAuthenticationOptions> configuration)
74+
{
75+
return builder.AddOAuth<CoinbaseAuthenticationOptions, CoinbaseAuthenticationHandler>(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.Coinbase
20+
{
21+
public class CoinbaseAuthenticationHandler : OAuthHandler<CoinbaseAuthenticationOptions>
22+
{
23+
public CoinbaseAuthenticationHandler(
24+
[NotNull] IOptionsMonitor<CoinbaseAuthenticationOptions> 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.Post, 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(Context.RequestAborted));
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(Context.RequestAborted));
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
using static AspNet.Security.OAuth.Coinbase.CoinbaseAuthenticationConstants;
11+
12+
namespace AspNet.Security.OAuth.Coinbase
13+
{
14+
/// <summary>
15+
/// Defines a set of options used by <see cref="CoinbaseAuthenticationHandler"/>.
16+
/// </summary>
17+
public class CoinbaseAuthenticationOptions : OAuthOptions
18+
{
19+
public CoinbaseAuthenticationOptions()
20+
{
21+
ClaimsIssuer = CoinbaseAuthenticationDefaults.Issuer;
22+
23+
CallbackPath = CoinbaseAuthenticationDefaults.CallbackPath;
24+
25+
AuthorizationEndpoint = CoinbaseAuthenticationDefaults.AuthorizationEndpoint;
26+
TokenEndpoint = CoinbaseAuthenticationDefaults.TokenEndpoint;
27+
UserInformationEndpoint = CoinbaseAuthenticationDefaults.UserInformationEndpoint;
28+
29+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
30+
ClaimActions.MapJsonKey(ClaimTypes.Name, "name", "name");
31+
ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
32+
33+
ClaimActions.MapJsonKey(Claims.Username, "username");
34+
ClaimActions.MapJsonKey(Claims.ProfileLocation, "profile_location");
35+
ClaimActions.MapJsonKey(Claims.ProfileBio, "profile_bio");
36+
ClaimActions.MapJsonKey(Claims.ProfileUrl, "profile_url");
37+
ClaimActions.MapJsonKey(Claims.AvatarUrl, "avatar_url");
38+
}
39+
}
40+
}

test/AspNet.Security.OAuth.Providers.Tests/AspNet.Security.OAuth.Providers.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@
3030
<PackageReference Include="Moq" />
3131
<PackageReference Include="Shouldly" />
3232
</ItemGroup>
33-
3433
</Project>

0 commit comments

Comments
 (0)