mirror of https://github.com/Squidex/squidex.git
13 changed files with 259 additions and 111 deletions
@ -0,0 +1,42 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GitHubHandler.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authentication.OAuth; |
||||
|
using Squidex.Core.Identity; |
||||
|
|
||||
|
namespace Squidex.Config.Identity |
||||
|
{ |
||||
|
public sealed class GitHubHandler : OAuthEvents |
||||
|
{ |
||||
|
public override Task CreatingTicket(OAuthCreatingTicketContext context) |
||||
|
{ |
||||
|
var userLogin = context.User.Value<string>("login"); |
||||
|
var userName = context.User.Value<string>("name"); |
||||
|
|
||||
|
if (!string.IsNullOrEmpty(userName)) |
||||
|
{ |
||||
|
context.Identity.AddClaim(new Claim(SquidexClaimTypes.SquidexDisplayName, userName)); |
||||
|
} |
||||
|
else if (!string.IsNullOrWhiteSpace(userLogin)) |
||||
|
{ |
||||
|
context.Identity.AddClaim(new Claim(SquidexClaimTypes.SquidexDisplayName, userName)); |
||||
|
} |
||||
|
|
||||
|
var pictureUrl = context.User.Value<string>("avatar_url"); |
||||
|
|
||||
|
if (!string.IsNullOrEmpty(pictureUrl)) |
||||
|
{ |
||||
|
context.Identity.AddClaim(new Claim(SquidexClaimTypes.SquidexPictureUrl, pictureUrl)); |
||||
|
} |
||||
|
|
||||
|
return base.CreatingTicket(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GithubIdentityUsage.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using AspNet.Security.OAuth.GitHub; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
// ReSharper disable InvertIf
|
||||
|
|
||||
|
namespace Squidex.Config.Identity |
||||
|
{ |
||||
|
public static class GitHubIdentityUsage |
||||
|
{ |
||||
|
public static IApplicationBuilder UseMyGithubAuthentication(this IApplicationBuilder app) |
||||
|
{ |
||||
|
var options = app.ApplicationServices.GetService<IOptions<MyIdentityOptions>>().Value; |
||||
|
|
||||
|
if (options.IsGithubAuthConfigured()) |
||||
|
{ |
||||
|
var githubOptions = |
||||
|
new GitHubAuthenticationOptions |
||||
|
{ |
||||
|
ClientId = options.GithubClient, |
||||
|
ClientSecret = options.GithubSecret |
||||
|
}; |
||||
|
|
||||
|
app.UseGitHubAuthentication(githubOptions); |
||||
|
} |
||||
|
|
||||
|
return app; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GoogleHandler.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Linq; |
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authentication.OAuth; |
||||
|
using Squidex.Core.Identity; |
||||
|
using Squidex.Infrastructure.Tasks; |
||||
|
|
||||
|
// ReSharper disable InvertIf
|
||||
|
|
||||
|
namespace Squidex.Config.Identity |
||||
|
{ |
||||
|
public sealed class GoogleHandler : OAuthEvents |
||||
|
{ |
||||
|
public override Task RedirectToAuthorizationEndpoint(OAuthRedirectToAuthorizationContext context) |
||||
|
{ |
||||
|
context.Response.Redirect(context.RedirectUri + "&prompt=select_account"); |
||||
|
|
||||
|
return TaskHelper.Done; |
||||
|
} |
||||
|
|
||||
|
public override Task CreatingTicket(OAuthCreatingTicketContext context) |
||||
|
{ |
||||
|
var displayNameClaim = context.Identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name); |
||||
|
if (displayNameClaim != null) |
||||
|
{ |
||||
|
context.Identity.AddClaim(new Claim(SquidexClaimTypes.SquidexDisplayName, displayNameClaim.Value)); |
||||
|
} |
||||
|
|
||||
|
var pictureUrl = context.User?.Value<string>("picture"); |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(pictureUrl)) |
||||
|
{ |
||||
|
context.Identity.AddClaim(new Claim(SquidexClaimTypes.SquidexPictureUrl, pictureUrl)); |
||||
|
} |
||||
|
|
||||
|
return base.CreatingTicket(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GoogleIdentityUsage.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
// ReSharper disable InvertIf
|
||||
|
|
||||
|
namespace Squidex.Config.Identity |
||||
|
{ |
||||
|
public static class GoogleIdentityUsage |
||||
|
{ |
||||
|
public static IApplicationBuilder UseMyGoogleAuthentication(this IApplicationBuilder app) |
||||
|
{ |
||||
|
var options = app.ApplicationServices.GetService<IOptions<MyIdentityOptions>>().Value; |
||||
|
|
||||
|
if (options.IsGoogleAuthConfigured()) |
||||
|
{ |
||||
|
var googleOptions = |
||||
|
new GoogleOptions |
||||
|
{ |
||||
|
ClientId = options.GoogleClient, |
||||
|
ClientSecret = options.GoogleSecret, |
||||
|
Events = new GoogleHandler() |
||||
|
}; |
||||
|
|
||||
|
app.UseGoogleAuthentication(googleOptions); |
||||
|
} |
||||
|
|
||||
|
return app; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue