Skip to content

Commit e2d2ced

Browse files
Implement Typeform OAuth provider (#897)
Implement Typeform OAuth provider.
1 parent ab98a32 commit e2d2ced

File tree

9 files changed

+305
-0
lines changed

9 files changed

+305
-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.Typeform", "src\AspNet.Security.OAuth.Typeform\AspNet.Security.OAuth.Typeform.csproj", "{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}"
300+
EndProject
299301
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}"
300302
EndProject
301303
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}"
@@ -684,6 +686,10 @@ Global
684686
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
685687
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
686688
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.Build.0 = Release|Any CPU
689+
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
690+
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Debug|Any CPU.Build.0 = Debug|Any CPU
691+
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Release|Any CPU.ActiveCfg = Release|Any CPU
692+
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Release|Any CPU.Build.0 = Release|Any CPU
687693
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
688694
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Debug|Any CPU.Build.0 = Debug|Any CPU
689695
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -801,6 +807,7 @@ Global
801807
{101681FB-569F-4941-B943-2AD380039BE0} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
802808
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
803809
{8AF5DDBE-2631-4E71-9045-73A6356CE86B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
810+
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
804811
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
805812
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
806813
{55975423-C9C0-4C47-AD00-0F012F30AD3C} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
239239
| Trovo | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Trovo?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Trovo/ "Download AspNet.Security.OAuth.Trovo from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Trovo?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Trovo "Download AspNet.Security.OAuth.Trovo from MyGet.org") | [Documentation](https://developer.trovo.live/docs/APIs.html "Trovo developer documentation") |
240240
| Twitch | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Twitch?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Twitch/ "Download AspNet.Security.OAuth.Twitch from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Twitch?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Twitch "Download AspNet.Security.OAuth.Twitch from MyGet.org") | [Documentation](https://dev.twitch.tv/docs/authentication/ "Twitch developer documentation") |
241241
| Twitter | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Twitter?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Twitter/ "Download AspNet.Security.OAuth.Twitter from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Twitter?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Twitter "Download AspNet.Security.OAuth.Twitter from MyGet.org") | [Documentation](https://developer.twitter.com/en/docs/authentication/oauth-2-0 "Twitter developer documentation") |
242+
| Typeform | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Typeform?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Typeform/ "Download AspNet.Security.OAuth.Typeform from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Typeform?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Typeform "Download AspNet.Security.OAuth.Typeform from MyGet.org") | [Documentation](https://www.typeform.com/developers/get-started/applications/ "Typeform developer documentation") |
242243
| Untappd | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Untappd?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Untappd/ "Download AspNet.Security.OAuth.Untappd from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Untappd?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Untappd "Download AspNet.Security.OAuth.Untappd from MyGet.org") | [Documentation](https://untappd.com/api/docs#authentication "Untappd developer documentation") |
243244
| Vimeo | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Vimeo?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Vimeo/ "Download AspNet.Security.OAuth.Vimeo from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Vimeo?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Vimeo "Download AspNet.Security.OAuth.Vimeo from MyGet.org") | [Documentation](https://developer.vimeo.com/api/authentication "Vimeo developer documentation") |
244245
| Visual Studio (Azure DevOps) | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.VisualStudio?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.VisualStudio/ "Download AspNet.Security.OAuth.VisualStudio from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.VisualStudio?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.VisualStudio "Download AspNet.Security.OAuth.VisualStudio from MyGet.org") | [Documentation](https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops "Azure DevOps 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 Typeform authentication.</Description>
15+
<Authors>Denys Goncharenko</Authors>
16+
<PackageTags>aspnetcore;authentication;oauth;typeform;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.Typeform;
8+
9+
/// <summary>
10+
/// Default values used by the Typeform authentication middleware.
11+
/// </summary>
12+
public static class TypeformAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "Typeform";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "Typeform";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "Typeform";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-typeform";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://api.typeform.com/oauth/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://api.typeform.com/oauth/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://api.typeform.com/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.Typeform;
10+
11+
/// <summary>
12+
/// Extension methods to add Typeform authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class TypeformAuthenticationExtensions
15+
{
16+
/// <summary>
17+
/// Adds <see cref="TypeformAuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables Typeform 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 AddTypeform([NotNull] this AuthenticationBuilder builder)
23+
{
24+
return builder.AddTypeform(TypeformAuthenticationDefaults.AuthenticationScheme, options => { });
25+
}
26+
27+
/// <summary>
28+
/// Adds <see cref="TypeformAuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables Typeform 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 AddTypeform(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<TypeformAuthenticationOptions> configuration)
37+
{
38+
return builder.AddTypeform(TypeformAuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
40+
41+
/// <summary>
42+
/// Adds <see cref="TypeformAuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables Typeform 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 Typeform options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddTypeform(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<TypeformAuthenticationOptions> configuration)
53+
{
54+
return builder.AddTypeform(scheme, TypeformAuthenticationDefaults.DisplayName, configuration);
55+
}
56+
57+
/// <summary>
58+
/// Adds <see cref="TypeformAuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables Typeform 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 Typeform options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder AddTypeform(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<TypeformAuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<TypeformAuthenticationOptions, TypeformAuthenticationHandler>(scheme, caption, configuration);
73+
}
74+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.Net.Mime;
9+
using System.Security.Claims;
10+
using System.Text.Encodings.Web;
11+
using System.Text.Json;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Options;
14+
15+
namespace AspNet.Security.OAuth.Typeform;
16+
17+
public partial class TypeformAuthenticationHandler : OAuthHandler<TypeformAuthenticationOptions>
18+
{
19+
public TypeformAuthenticationHandler(
20+
[NotNull] IOptionsMonitor<TypeformAuthenticationOptions> options,
21+
[NotNull] ILoggerFactory logger,
22+
[NotNull] UrlEncoder encoder)
23+
: base(options, logger, encoder)
24+
{
25+
}
26+
27+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
28+
[NotNull] ClaimsIdentity identity,
29+
[NotNull] AuthenticationProperties properties,
30+
[NotNull] OAuthTokenResponse tokens)
31+
{
32+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
33+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
34+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
35+
36+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
37+
if (!response.IsSuccessStatusCode)
38+
{
39+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
40+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
41+
}
42+
43+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
44+
45+
var principal = new ClaimsPrincipal(identity);
46+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
47+
context.RunClaimActions();
48+
49+
await Events.CreatingTicket(context);
50+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
51+
}
52+
53+
private static partial class Log
54+
{
55+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
56+
{
57+
UserProfileError(
58+
logger,
59+
response.StatusCode,
60+
response.Headers.ToString(),
61+
await response.Content.ReadAsStringAsync(cancellationToken));
62+
}
63+
64+
[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}.")]
65+
private static partial void UserProfileError(
66+
ILogger logger,
67+
System.Net.HttpStatusCode status,
68+
string headers,
69+
string body);
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.Typeform;
10+
11+
/// <summary>
12+
/// Defines a set of options used by <see cref="TypeformAuthenticationHandler"/>.
13+
/// </summary>
14+
public class TypeformAuthenticationOptions : OAuthOptions
15+
{
16+
public TypeformAuthenticationOptions()
17+
{
18+
ClaimsIssuer = TypeformAuthenticationDefaults.Issuer;
19+
CallbackPath = TypeformAuthenticationDefaults.CallbackPath;
20+
21+
AuthorizationEndpoint = TypeformAuthenticationDefaults.AuthorizationEndpoint;
22+
TokenEndpoint = TypeformAuthenticationDefaults.TokenEndpoint;
23+
UserInformationEndpoint = TypeformAuthenticationDefaults.UserInformationEndpoint;
24+
25+
Scope.Add("accounts:read");
26+
27+
ClaimActions.MapCustomJson(ClaimTypes.NameIdentifier, user => user.GetString("user_id"));
28+
ClaimActions.MapCustomJson(ClaimTypes.Name, user => user.GetString("alias"));
29+
ClaimActions.MapCustomJson(ClaimTypes.Email, user => user.GetString("email"));
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Typeform;
8+
9+
public class TypeformTests(ITestOutputHelper outputHelper) : OAuthTests<TypeformAuthenticationOptions>(outputHelper)
10+
{
11+
public override string DefaultScheme => TypeformAuthenticationDefaults.AuthenticationScheme;
12+
13+
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
14+
{
15+
builder.AddTypeform(options => ConfigureDefaults(builder, options));
16+
}
17+
18+
[Theory]
19+
[InlineData(ClaimTypes.NameIdentifier, "id")]
20+
[InlineData(ClaimTypes.Name, "Test Name")]
21+
[InlineData(ClaimTypes.Email, "[email protected]")]
22+
public async Task Can_Sign_In_Using_Typeform(string claimType, string claimValue)
23+
=> await AuthenticateUserAndAssertClaimValue(claimType, claimValue);
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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://api.typeform.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.typeform.com/me",
17+
"contentFormat": "json",
18+
"contentJson": {
19+
"user_id": "id",
20+
"email": "[email protected]",
21+
"alias": "Test Name"
22+
}
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)