Skip to content

Commit

Permalink
Add Snapchat provider (#706)
Browse files Browse the repository at this point in the history
* add Snapchat provider

* merge comments

* added scope

* adding snapchat.md

* added row to README.md file

* add to readme file

* Removed the Google Analytics tracking gunk

* Fix formatting

Fix formatting by uses spaces not tabs.

Co-authored-by: nikola.maksimovic <nikola.maksimovic@hunchads.com>
Co-authored-by: Martin Costello <martin@martincostello.com>
  • Loading branch information
3 people committed Aug 19, 2022
1 parent a86d82d commit e01f695
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AspNet.Security.OAuth.Providers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C2CA4B38-A
docs\reddit.md = docs\reddit.md
docs\salesforce.md = docs\salesforce.md
docs\sign-in-with-apple.md = docs\sign-in-with-apple.md
docs\snapchat.md = docs\snapchat.md
docs\stackexchange.md = docs\stackexchange.md
docs\superoffice.md = docs\superoffice.md
docs\trakt.md = docs\trakt.md
Expand Down Expand Up @@ -280,6 +281,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Naver
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.HubSpot", "src\AspNet.Security.OAuth.HubSpot\AspNet.Security.OAuth.HubSpot.csproj", "{23E576EB-6514-4617-8F04-FE7D5540136D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Snapchat", "src\AspNet.Security.OAuth.Snapchat\AspNet.Security.OAuth.Snapchat.csproj", "{ECD22287-9B9F-489A-84A7-E66D65A39D73}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -638,6 +641,10 @@ Global
{23E576EB-6514-4617-8F04-FE7D5540136D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23E576EB-6514-4617-8F04-FE7D5540136D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23E576EB-6514-4617-8F04-FE7D5540136D}.Release|Any CPU.Build.0 = Release|Any CPU
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECD22287-9B9F-489A-84A7-E66D65A39D73}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -737,6 +744,7 @@ Global
{FAE6E4C3-5A0D-45A5-9D17-57F4F6DA2593} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{289A91E9-81A9-422D-9CCD-12819081A29A} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{23E576EB-6514-4617-8F04-FE7D5540136D} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{ECD22287-9B9F-489A-84A7-E66D65A39D73} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
| ServiceChannel | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.ServiceChannel?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.ServiceChannel/ "Download AspNet.Security.OAuth.ServiceChannel from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.ServiceChannel?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.ServiceChannel "Download AspNet.Security.OAuth.ServiceChannel from MyGet.org") | [Documentation](https://developer.servicechannel.com/basics/general/authentication "ServiceChannel developer documentation") |
| Shopify | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Shopify?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Shopify/ "Download AspNet.Security.OAuth.Shopify from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Shopify?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Shopify "Download AspNet.Security.OAuth.Shopify from MyGet.org") | [Documentation](https://help.shopify.com/en/api/getting-started/authentication/oauth "Shopify developer documentation") |
| Slack | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Slack?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Slack/ "Download AspNet.Security.OAuth.Slack from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Slack?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Slack "Download AspNet.Security.OAuth.Slack from MyGet.org") | [Documentation](https://api.slack.com/docs/oauth "Slack developer documentation") |
| Snapchat | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Snapchat?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Snapchat/ "Download AspNet.Security.OAuth.Snapchat from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Snapchat?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Snapchat "Download AspNet.Security.OAuth.Snapchat from MyGet.org") | [Documentation](https://marketingapi.snapchat.com/docs/#authentication "Snapchat developer documentation") |
| SoundCloud | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.SoundCloud?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.SoundCloud/ "Download AspNet.Security.OAuth.SoundCloud from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.SoundCloud?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.SoundCloud "Download AspNet.Security.OAuth.SoundCloud from MyGet.org") | [Documentation](https://developers.soundcloud.com/docs#authentication "SoundCloud developer documentation") |
| Spotify | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Spotify?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Spotify/ "Download AspNet.Security.OAuth.Spotify from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Spotify?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Spotify "Download AspNet.Security.OAuth.Spotify from MyGet.org") | [Documentation](https://developer.spotify.com/documentation/general/guides/authorization-guide/ "Spotify developer documentation") |
| Stack Exchange | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.StackExchange?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.StackExchange/ "Download AspNet.Security.OAuth.StackExchange from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.StackExchange?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.StackExchange "Download AspNet.Security.OAuth.StackExchange from MyGet.org") | [Documentation](https://api.stackexchange.com/docs/authentication "Stack Exchange developer documentation") |
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ covered by the section above.
| QQ | _Optional_ | [Documentation](qq.md "QQ provider documentation") |
| Reddit | _Optional_ | [Documentation](reddit.md "Reddit provider documentation") |
| Salesforce | _Optional_ | [Documentation](salesforce.md "Salesforce provider documentation") |
| Snapchat | _Optional_ | [Documentation](snapchat.md "Snapchat provider documentation") |
| StackExchange | _Optional_ | [Documentation](stackexchange.md "StackExchange provider documentation") |
| SuperOffice | **Required** | [Documentation](superoffice.md "SuperOffice provider documentation") |
| Trakt | _Optional_ | [Documentation](trakt.md "Trakt provider documentation") |
Expand Down
25 changes: 25 additions & 0 deletions docs/snapchat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Integrating the Snapchat Provider

## Example

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options => /* Auth configuration */)
.AddSnapchat(options =>
{
options.ClientId = "my-client-id";
options.ClientSecret = "my-client-secret";
});
}

public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
app.UseAuthorization();
}
```

## Required Additional Settings

_None._
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<PackageValidationBaselineVersion>6.0.8</PackageValidationBaselineVersion>
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
</PropertyGroup>

<PropertyGroup>
<Description>ASP.NET Core security middleware enabling Snapchat authentication.</Description>
<Authors>Nikola Maksimovic</Authors>
<PackageTags>aspnetcore;authentication;oauth;security;snapchat</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

namespace AspNet.Security.OAuth.Snapchat;

/// <summary>
/// Contains constants specific to the <see cref="SnapchatAuthenticationHandler"/>.
/// </summary>
public static class SnapchatAuthenticationConstants
{
public static class Claims
{
public const string TeamId = "organization_id";
public const string MemberStatus = "member_status";
public const string UserId = "id";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

namespace AspNet.Security.OAuth.Snapchat;

/// <summary>
/// Default values used by the Snapchat authentication middleware.
/// </summary>
public static class SnapchatAuthenticationDefaults
{
/// <summary>
/// Default value for <see cref="AuthenticationScheme.Name"/>.
/// </summary>
public const string AuthenticationScheme = "Snapchat";

/// <summary>
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
/// </summary>
public static readonly string DisplayName = "Snapchat";

/// <summary>
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
/// </summary>
public static readonly string Issuer = "Snapchat";

/// <summary>
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
/// </summary>
public static readonly string CallbackPath = "/signin-snapchat";

/// <summary>
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
/// </summary>
public static readonly string AuthorizationEndpoint = "https://accounts.snapchat.com/login/oauth2/authorize";

/// <summary>
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
/// </summary>
public static readonly string TokenEndpoint = "https://accounts.snapchat.com/login/oauth2/access_token";

/// <summary>
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
/// For more info about this endpoint, see <c>https://marketingapi.snapchat.com/docs/#user</c>.
/// </summary>
public static readonly string UserInformationEndpoint = "https://adsapi.snapchat.com/v1/me";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using AspNet.Security.OAuth.Snapchat;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods to add Snapchat authentication capabilities to an HTTP application pipeline.
/// </summary>
public static class SnapchatAuthenticationExtensions
{
/// <summary>
/// Adds <see cref="SnapchatAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Snapchat authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSnapchat([NotNull] this AuthenticationBuilder builder)
{
return builder.AddSnapchat(SnapchatAuthenticationDefaults.AuthenticationScheme, options => { });
}

/// <summary>
/// Adds <see cref="SnapchatAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Snapchat authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSnapchat(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<SnapchatAuthenticationOptions> configuration)
{
return builder.AddSnapchat(SnapchatAuthenticationDefaults.AuthenticationScheme, configuration);
}

/// <summary>
/// Adds <see cref="SnapchatAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Snapchat authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Snapchat options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSnapchat(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[NotNull] Action<SnapchatAuthenticationOptions> configuration)
{
return builder.AddSnapchat(scheme, SnapchatAuthenticationDefaults.DisplayName, configuration);
}

/// <summary>
/// Adds <see cref="SnapchatAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Snapchat authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="caption">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Snapchat options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSnapchat(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[CanBeNull] string caption,
[NotNull] Action<SnapchatAuthenticationOptions> configuration)
{
return builder.AddOAuth<SnapchatAuthenticationOptions, SnapchatAuthenticationHandler>(scheme, caption, configuration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AspNet.Security.OAuth.Snapchat;

public partial class SnapchatAuthenticationHandler : OAuthHandler<SnapchatAuthenticationOptions>
{
public SnapchatAuthenticationHandler(
[NotNull] IOptionsMonitor<SnapchatAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder,
[NotNull] ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
string endpoint = Options.UserInformationEndpoint;

using var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
throw new HttpRequestException("An error occurred while retrieving the user profile from Snapchat.");
}

using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
}

private static partial class Log
{
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
{
UserProfileError(
logger,
response.StatusCode,
response.Headers.ToString(),
await response.Content.ReadAsStringAsync(cancellationToken));
}

[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}.")]
private static partial void UserProfileError(
ILogger logger,
System.Net.HttpStatusCode status,
string headers,
string body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using static AspNet.Security.OAuth.Snapchat.SnapchatAuthenticationConstants;

namespace AspNet.Security.OAuth.Snapchat;

/// <summary>
/// Defines a set of options used by <see cref="SnapchatAuthenticationHandler"/>.
/// </summary>
public class SnapchatAuthenticationOptions : OAuthOptions
{
public SnapchatAuthenticationOptions()
{
ClaimsIssuer = SnapchatAuthenticationDefaults.Issuer;

CallbackPath = SnapchatAuthenticationDefaults.CallbackPath;

AuthorizationEndpoint = SnapchatAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = SnapchatAuthenticationDefaults.TokenEndpoint;
UserInformationEndpoint = SnapchatAuthenticationDefaults.UserInformationEndpoint;

ClaimActions.MapJsonSubKey(ClaimTypes.NameIdentifier, "me", "id");
ClaimActions.MapJsonSubKey(ClaimTypes.Name, "me", "display_name");
ClaimActions.MapJsonSubKey(ClaimTypes.Email, "me", "email");
ClaimActions.MapJsonSubKey(Claims.UserId, "me", "id");
ClaimActions.MapJsonSubKey(Claims.TeamId, "me", "organization_id");
ClaimActions.MapJsonSubKey(Claims.MemberStatus, "me", "member_status");
Scope.Add("snapchat-marketing-api");
}
}
Loading

0 comments on commit e01f695

Please sign in to comment.