Skip to content

Commit ab98a32

Browse files
Implement Calendly OAuth provider (#896)
Implement Calendly OAuth provider.
1 parent b6c69ec commit ab98a32

File tree

9 files changed

+301
-0
lines changed

9 files changed

+301
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.PingO
296296
EndProject
297297
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.JumpCloud", "src\AspNet.Security.OAuth.JumpCloud\AspNet.Security.OAuth.JumpCloud.csproj", "{8AF5DDBE-2631-4E71-9045-73A6356CE86B}"
298298
EndProject
299+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Calendly", "src\AspNet.Security.OAuth.Calendly\AspNet.Security.OAuth.Calendly.csproj", "{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}"
300+
EndProject
299301
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Airtable", "src\AspNet.Security.OAuth.Airtable\AspNet.Security.OAuth.Airtable.csproj", "{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3}"
300302
EndProject
301303
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Pipedrive", "src\AspNet.Security.OAuth.Pipedrive\AspNet.Security.OAuth.Pipedrive.csproj", "{55975423-C9C0-4C47-AD00-0F012F30AD3C}"
@@ -682,6 +684,10 @@ Global
682684
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
683685
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
684686
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.Build.0 = Release|Any CPU
687+
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
688+
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Debug|Any CPU.Build.0 = Debug|Any CPU
689+
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Release|Any CPU.ActiveCfg = Release|Any CPU
690+
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Release|Any CPU.Build.0 = Release|Any CPU
685691
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
686692
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
687693
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -795,6 +801,7 @@ Global
795801
{101681FB-569F-4941-B943-2AD380039BE0} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
796802
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
797803
{8AF5DDBE-2631-4E71-9045-73A6356CE86B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
804+
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
798805
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
799806
{55975423-C9C0-4C47-AD00-0F012F30AD3C} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
800807
EndGlobalSection

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
173173
| 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=true)](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") |
174174
| 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") |
175175
| 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") |
176+
| Calendly | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Calendly?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Calendly/ "Download AspNet.Security.OAuth.Calendly from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Calendly?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Calendly "Download AspNet.Security.OAuth.Calendly from MyGet.org") | [Documentation](https://developer.calendly.com/api-docs/3cefb59b832eb-calendly-o-auth-2-0 "Calendly developer documentation") |
176177
| 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") |
177178
| 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") |
178179
| 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") |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<!-- TODO Enable once this provider is published to NuGet.org -->
8+
<PropertyGroup>
9+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
10+
<PackageValidationBaselineVersion>8.0.1</PackageValidationBaselineVersion>
11+
</PropertyGroup>
12+
13+
<PropertyGroup>
14+
<Description>ASP.NET Core security middleware enabling Calendly authentication.</Description>
15+
<Authors>Denys Goncharenko</Authors>
16+
<PackageTags>aspnetcore;authentication;oauth;calendly;security</PackageTags>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
21+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.Calendly;
8+
9+
/// <summary>
10+
/// Default values used by the Calendly authentication middleware.
11+
/// </summary>
12+
public static class CalendlyAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "Calendly";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "Calendly";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "Calendly";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-calendly";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://auth.calendly.com/oauth/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://auth.calendly.com/oauth/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://api.calendly.com/users/me";
48+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.Extensions.DependencyInjection;
8+
9+
namespace AspNet.Security.OAuth.Calendly;
10+
11+
/// <summary>
12+
/// Extension methods to add Calendly authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class CalendlyAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="CalendlyAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables Calendly authentication capabilities.
19+
/// </summary>
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>A reference to this instance after the operation has completed.</returns>
22+
public static AuthenticationBuilder AddCalendly([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddCalendly(CalendlyAuthenticationDefaults.AuthenticationScheme, options => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="CalendlyAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables Calendly authentication capabilities.
30+
/// </summary>
31+
/// <param name="builder">The authentication builder.</param>
32+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
33+
/// <returns>A reference to this instance after the operation has completed.</returns>
34+
public static AuthenticationBuilder AddCalendly(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<CalendlyAuthenticationOptions> configuration)
37+
{
38+
return builder.AddCalendly(CalendlyAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="CalendlyAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables Calendly authentication capabilities.
44+
/// </summary>
45+
/// <param name="builder">The authentication builder.</param>
46+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
47+
/// <param name="configuration">The delegate used to configure the Calendly options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddCalendly(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<CalendlyAuthenticationOptions> configuration)
53+
{
54+
return builder.AddCalendly(scheme, CalendlyAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="CalendlyAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables Calendly authentication capabilities.
60+
/// </summary>
61+
/// <param name="builder">The authentication builder.</param>
62+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
63+
/// <param name="caption">The optional display name associated with this instance.</param>
64+
/// <param name="configuration">The delegate used to configure the Calendly options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddCalendly(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<CalendlyAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<CalendlyAuthenticationOptions, CalendlyAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.Headers;
8+
using System.Security.Claims;
9+
using System.Text.Encodings.Web;
10+
using System.Text.Json;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
14+
namespace AspNet.Security.OAuth.Calendly;
15+
16+
public partial class CalendlyAuthenticationHandler : OAuthHandler<CalendlyAuthenticationOptions>
17+
{
18+
public CalendlyAuthenticationHandler(
19+
[NotNull] IOptionsMonitor<CalendlyAuthenticationOptions> options,
20+
[NotNull] ILoggerFactory logger,
21+
[NotNull] UrlEncoder encoder)
22+
: base(options, logger, encoder)
23+
{
24+
}
25+
26+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
27+
[NotNull] ClaimsIdentity identity,
28+
[NotNull] AuthenticationProperties properties,
29+
[NotNull] OAuthTokenResponse tokens)
30+
{
31+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
32+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
33+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
34+
35+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
36+
if (!response.IsSuccessStatusCode)
37+
{
38+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
39+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
40+
}
41+
42+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
43+
44+
var principal = new ClaimsPrincipal(identity);
45+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
46+
context.RunClaimActions();
47+
48+
await Events.CreatingTicket(context);
49+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
50+
}
51+
52+
private static partial class Log
53+
{
54+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
55+
{
56+
UserProfileError(
57+
logger,
58+
response.StatusCode,
59+
response.Headers.ToString(),
60+
await response.Content.ReadAsStringAsync(cancellationToken));
61+
}
62+
63+
[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}.")]
64+
private static partial void UserProfileError(
65+
ILogger logger,
66+
System.Net.HttpStatusCode status,
67+
string headers,
68+
string body);
69+
}
70+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
9+
namespace AspNet.Security.OAuth.Calendly;
10+
11+
/// <summary>
12+
/// Defines a set of options used by <see cref="CalendlyAuthenticationHandler"/>.
13+
/// </summary>
14+
public class CalendlyAuthenticationOptions : OAuthOptions
15+
{
16+
public CalendlyAuthenticationOptions()
17+
{
18+
ClaimsIssuer = CalendlyAuthenticationDefaults.Issuer;
19+
CallbackPath = CalendlyAuthenticationDefaults.CallbackPath;
20+
21+
AuthorizationEndpoint = CalendlyAuthenticationDefaults.AuthorizationEndpoint;
22+
TokenEndpoint = CalendlyAuthenticationDefaults.TokenEndpoint;
23+
UserInformationEndpoint = CalendlyAuthenticationDefaults.UserInformationEndpoint;
24+
25+
ClaimActions.MapCustomJson(ClaimTypes.Email, user => user.GetProperty("resource").GetString("email"));
26+
ClaimActions.MapCustomJson(ClaimTypes.Name, user => user.GetProperty("resource").GetString("name"));
27+
}
28+
}
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.Calendly;
8+
9+
public class CalendlyTests(ITestOutputHelper outputHelper) : OAuthTests<CalendlyAuthenticationOptions>(outputHelper)
10+
{
11+
public override string DefaultScheme => CalendlyAuthenticationDefaults.AuthenticationScheme;
12+
13+
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
14+
{
15+
builder.AddCalendly(options => ConfigureDefaults(builder, options));
16+
}
17+
18+
[Theory]
19+
[InlineData(ClaimTypes.Name, "Test User")]
20+
[InlineData(ClaimTypes.Email, "[email protected]")]
21+
public async Task Can_Sign_In_Using_Pipedrive(string claimType, string claimValue)
22+
=> await AuthenticateUserAndAssertClaimValue(claimType, claimValue);
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
3+
"items": [
4+
{
5+
"uri": "https://auth.calendly.com/oauth/token",
6+
"method": "POST",
7+
"contentFormat": "json",
8+
"contentJson": {
9+
"access_token": "secret-access-token",
10+
"token_type": "Bearer",
11+
"refresh_token": "secret-refresh-token",
12+
"expires_in": 3600
13+
}
14+
},
15+
{
16+
"uri": "https://api.calendly.com/users/me",
17+
"contentFormat": "json",
18+
"contentJson": {
19+
"resource": {
20+
"email": "[email protected]",
21+
"name": "Test User"
22+
}
23+
}
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)