Browse Source

Github auth started.

pull/337/head
Sebastian Stehle 7 years ago
parent
commit
e951ad037a
  1. 1
      src/Squidex/Config/Authentication/AuthenticationServices.cs
  2. 30
      src/Squidex/Config/Authentication/GithubAuthenticationServices.cs
  3. 46
      src/Squidex/Config/Authentication/GithubHandler.cs

1
src/Squidex/Config/Authentication/AuthenticationServices.cs

@ -17,6 +17,7 @@ namespace Squidex.Config.Authentication
var identityOptions = config.GetSection("identity").Get<MyIdentityOptions>();
services.AddAuthentication()
.AddMyExternalGithubAuthentication(identityOptions)
.AddMyExternalGoogleAuthentication(identityOptions)
.AddMyExternalMicrosoftAuthentication(identityOptions)
.AddMyExternalOdic(identityOptions)

30
src/Squidex/Config/Authentication/GithubAuthenticationServices.cs

@ -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;
}
}
}

46
src/Squidex/Config/Authentication/GithubHandler.cs

@ -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…
Cancel
Save