mirror of https://github.com/Squidex/squidex.git
3 changed files with 77 additions and 0 deletions
@ -0,0 +1,30 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Squidex.Config.Authentication |
|||
{ |
|||
public static class GithubAuthenticationServices |
|||
{ |
|||
public static AuthenticationBuilder AddMyExternalGithubAuthentication(this AuthenticationBuilder authBuilder, MyIdentityOptions identityOptions) |
|||
{ |
|||
if (identityOptions.IsGithubAuthConfigured()) |
|||
{ |
|||
authBuilder.AddGitHub(options => |
|||
{ |
|||
options.ClientId = identityOptions.GithubClient; |
|||
options.ClientSecret = identityOptions.GithubSecret; |
|||
options.Events = new GithubHandler(); |
|||
}); |
|||
} |
|||
|
|||
return authBuilder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication.OAuth; |
|||
using Squidex.Shared.Identity; |
|||
|
|||
namespace Squidex.Config.Authentication |
|||
{ |
|||
public sealed class GithubHandler : OAuthEvents |
|||
{ |
|||
public override Task CreatingTicket(OAuthCreatingTicketContext context) |
|||
{ |
|||
var displayNameClaim = context.Identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name); |
|||
if (displayNameClaim != null) |
|||
{ |
|||
context.Identity.SetDisplayName(displayNameClaim.Value); |
|||
} |
|||
|
|||
var pictureUrl = context.User?.Value<string>("picture"); |
|||
|
|||
if (string.IsNullOrWhiteSpace(pictureUrl)) |
|||
{ |
|||
pictureUrl = context.User?["image"]?.Value<string>("url"); |
|||
|
|||
if (pictureUrl != null && pictureUrl.EndsWith("?sz=50", System.StringComparison.Ordinal)) |
|||
{ |
|||
pictureUrl = pictureUrl.Substring(0, pictureUrl.Length - 6); |
|||
} |
|||
} |
|||
|
|||
if (!string.IsNullOrWhiteSpace(pictureUrl)) |
|||
{ |
|||
context.Identity.SetPictureUrl(pictureUrl); |
|||
} |
|||
|
|||
return base.CreatingTicket(context); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue