Skip to content

Commit

Permalink
initial commit for Typeform provider
Browse files Browse the repository at this point in the history
  • Loading branch information
stefann42 committed Feb 15, 2018
1 parent ee3a414 commit 775f6ee
Show file tree
Hide file tree
Showing 16 changed files with 967 additions and 1 deletion.
11 changes: 10 additions & 1 deletion OwinOAuthProvidersDemo/App_Start/Startup.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Owin.Security.Providers.Evernote;
using Owin.Security.Providers.PayPal;
using Owin.Security.Providers.ArcGISPortal;
using Owin.Security.Providers.Typeform;

namespace OwinOAuthProvidersDemo
{
Expand Down Expand Up @@ -357,6 +358,14 @@ public void ConfigureAuth(IAppBuilder app)
// AppSecret = "",
// DebugUsingRequestHeadersToBuildBaseUri = true
//});

//var typeformOptions = new Owin.Security.Providers.Typeform.TypeformAuthenticationOptions
//{
// ClientId = "",
// ClientSecret = "",
//};
//typeformOptions.Scope.Add("forms:read");
//app.UseTypeformAuthentication(typeformOptions);
}
}
}
}
4 changes: 4 additions & 0 deletions OwinOAuthProvidersDemo/OwinOAuthProvidersDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@
<Project>{c3cf8734-6aac-4f59-9a3e-1cba8582cd48}</Project>
<Name>Owin.Security.Providers.Twitch</Name>
</ProjectReference>
<ProjectReference Include="..\src\Owin.Security.Providers.Typeform\Owin.Security.Providers.Typeform.csproj">
<Project>{c8862b45-e1d1-4ab7-a83d-3a2fd2a22526}</Project>
<Name>Owin.Security.Providers.Typeform</Name>
</ProjectReference>
<ProjectReference Include="..\src\Owin.Security.Providers.Untappd\Owin.Security.Providers.Untappd.csproj">
<Project>{3e89eca3-f4e7-4181-b26b-8250d5151044}</Project>
<Name>Owin.Security.Providers.Untappd</Name>
Expand Down
7 changes: 7 additions & 0 deletions src/Owin.Security.Providers.Typeform/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Owin.Security.Providers.Typeform
{
public static class Constants
{
public const string DefaultAuthenticationType = "Typeform";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C8862B45-E1D1-4AB7-A83D-3A2FD2A22526}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Owin.Security.Providers.Typeform</RootNamespace>
<AssemblyName>Owin.Security.Providers.Typeform</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>6</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>6</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.Security.3.0.1\lib\net45\Microsoft.Owin.Security.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Resources.Designer.cs">
<DependentUpon>Resources.resx</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="TypeformAuthenticationExtensions.cs" />
<Compile Include="TypeformAuthenticationHandler.cs" />
<Compile Include="TypeformAuthenticationMiddleware.cs" />
<Compile Include="TypeformAuthenticationOptions.cs" />
<Compile Include="Provider\ITypeformAuthenticationProvider.cs" />
<Compile Include="Provider\TypeformAuthenticatedContext.cs" />
<Compile Include="Provider\TypeformAuthenticationProvider.cs" />
<Compile Include="Provider\TypeformReturnEndpointContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="C:\projects\GitHub\OwinOAuthProviders\Owin.Security.Providers.Typeform\Owin.Security.Providers.Typeform.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="@(Targets->'%(Version)')" />
</ItemGroup>
</Target>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions src/Owin.Security.Providers.Typeform/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Owin.Security.Providers.Typeform")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Owin.Security.Providers.Typeform")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("b6a4a4cb-d365-41a0-b4c9-966e7ef19de0")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading.Tasks;

namespace Owin.Security.Providers.Typeform
{
/// <summary>
/// Specifies callback methods which the <see cref="TypeformAuthenticationMiddleware"></see> invokes to enable developer control over the authentication process. />
/// </summary>
public interface ITypeformAuthenticationProvider
{
/// <summary>
/// Invoked whenever Typeform successfully authenticates a user
/// </summary>
/// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
Task Authenticated(TypeformAuthenticatedContext context);

/// <summary>
/// Invoked prior to the <see cref="System.Security.Claims.ClaimsIdentity"/> being saved in a local cookie and the browser being redirected to the originally requested URL.
/// </summary>
/// <param name="context"></param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
Task ReturnEndpoint(TypeformReturnEndpointContext context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Provider;

namespace Owin.Security.Providers.Typeform
{
/// <summary>
/// Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.
/// </summary>
public class TypeformAuthenticatedContext : BaseContext
{
/// <summary>
/// Initializes a <see cref="TypeformAuthenticatedContext"/>
/// </summary>
/// <param name="context">The OWIN environment</param>
/// <param name="userJson">The JSON-serialized user</param>
/// <param name="accessToken">Typeform Access token</param>
/// <param name="refreshToken">Typeform Refresh token</param>
/// <param name="instanceUrl">Typeform instance url</param>
public TypeformAuthenticatedContext(IOwinContext context, string accessToken)
: base(context)
{
AccessToken = accessToken;

// Typeform doesn't supply a unique identifier for the user,
// however according to https://developer.typeform.com/get-started/applications/
// the access token doesn't expire so we can use it as a proxy for UserId
UserId = ComputeHash(accessToken);
}

/// <summary>
/// Gets the Typeform access token
/// </summary>
public string AccessToken { get; private set; }

/// <summary>
/// Gets the Typeform User ID
/// </summary>
public string UserId { get; private set; }


/// <summary>
/// Gets the <see cref="ClaimsIdentity"/> representing the user
/// </summary>
public ClaimsIdentity Identity { get; set; }

/// <summary>
/// Gets or sets a property bag for common authentication properties
/// </summary>
public AuthenticationProperties Properties { get; set; }

/// <summary>
///
/// </summary>
private static string ComputeHash(string input)
{
if (String.IsNullOrEmpty(input)) return null;

byte[] bytes;
using (var hash = System.Security.Cryptography.SHA1.Create()) {
bytes = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
}

return String.Concat(bytes.Select(x => x.ToString("x2")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Threading.Tasks;

namespace Owin.Security.Providers.Typeform
{
/// <summary>
/// Default <see cref="ITypeformAuthenticationProvider"/> implementation.
/// </summary>
public class TypeformAuthenticationProvider : ITypeformAuthenticationProvider
{
/// <summary>
/// Initializes a <see cref="TypeformAuthenticationProvider"/>
/// </summary>
public TypeformAuthenticationProvider()
{
OnAuthenticated = context => Task.FromResult<object>(null);
OnReturnEndpoint = context => Task.FromResult<object>(null);
}

/// <summary>
/// Gets or sets the function that is invoked when the Authenticated method is invoked.
/// </summary>
public Func<TypeformAuthenticatedContext, Task> OnAuthenticated { get; set; }

/// <summary>
/// Gets or sets the function that is invoked when the ReturnEndpoint method is invoked.
/// </summary>
public Func<TypeformReturnEndpointContext, Task> OnReturnEndpoint { get; set; }

/// <summary>
/// Invoked whenever Typeform successfully authenticates a user
/// </summary>
/// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
public virtual Task Authenticated(TypeformAuthenticatedContext context)
{
return OnAuthenticated(context);
}

/// <summary>
/// Invoked prior to the <see cref="System.Security.Claims.ClaimsIdentity"/> being saved in a local cookie and the browser being redirected to the originally requested URL.
/// </summary>
/// <param name="context"></param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
public virtual Task ReturnEndpoint(TypeformReturnEndpointContext context)
{
return OnReturnEndpoint(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Provider;

namespace Owin.Security.Providers.Typeform
{
/// <summary>
/// Provides context information to middleware providers.
/// </summary>
public class TypeformReturnEndpointContext : ReturnEndpointContext
{
/// <summary>
///
/// </summary>
/// <param name="context">OWIN environment</param>
/// <param name="ticket">The authentication ticket</param>
public TypeformReturnEndpointContext(
IOwinContext context,
AuthenticationTicket ticket)
: base(context, ticket)
{
}
}
}
Loading

0 comments on commit 775f6ee

Please sign in to comment.