-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]> Co-authored-by: Martin Costello <[email protected]>
- Loading branch information
1 parent
a86d82d
commit e01f695
Showing
12 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._ |
20 changes: 20 additions & 0 deletions
20
src/AspNet.Security.OAuth.Snapchat/AspNet.Security.OAuth.Snapchat.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
src/AspNet.Security.OAuth.Snapchat/SnapchatAuthenticationConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/AspNet.Security.OAuth.Snapchat/SnapchatAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
74 changes: 74 additions & 0 deletions
74
src/AspNet.Security.OAuth.Snapchat/SnapchatAuthenticationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/AspNet.Security.OAuth.Snapchat/SnapchatAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/AspNet.Security.OAuth.Snapchat/SnapchatAuthenticationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.