mirror of https://github.com/Squidex/squidex.git
42 changed files with 259 additions and 310 deletions
@ -0,0 +1,62 @@ |
|||
// ==========================================================================
|
|||
// AuthenticationServices.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Config.Identity |
|||
{ |
|||
public static class AuthenticationServices |
|||
{ |
|||
public static IServiceCollection AddMyAuthentication(this IServiceCollection services, IConfiguration configuration) |
|||
{ |
|||
var identityOptions = configuration.GetSection("identity").Get<MyIdentityOptions>(); |
|||
|
|||
services.AddAuthentication() |
|||
.AddMyGoogleAuthentication(identityOptions) |
|||
.AddMyMicrosoftAuthentication(identityOptions) |
|||
.AddMyApiProtection(identityOptions, configuration); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
public static AuthenticationBuilder AddMyApiProtection(this AuthenticationBuilder authBuilder, MyIdentityOptions identityOptions, IConfiguration configuration) |
|||
{ |
|||
var apiScope = Constants.ApiScope; |
|||
|
|||
var urlsOptions = configuration.GetSection("urls").Get<MyUrlsOptions>(); |
|||
|
|||
if (!string.IsNullOrWhiteSpace(urlsOptions.BaseUrl)) |
|||
{ |
|||
string apiAuthorityUrl; |
|||
|
|||
if (!string.IsNullOrWhiteSpace(identityOptions.AuthorityUrl)) |
|||
{ |
|||
apiAuthorityUrl = identityOptions.AuthorityUrl.BuildFullUrl(Constants.IdentityPrefix); |
|||
} |
|||
else |
|||
{ |
|||
apiAuthorityUrl = urlsOptions.BuildUrl(Constants.IdentityPrefix); |
|||
} |
|||
|
|||
authBuilder.AddIdentityServerAuthentication(options => |
|||
{ |
|||
options.Authority = apiAuthorityUrl; |
|||
options.ApiName = apiScope; |
|||
options.ApiSecret = null; |
|||
options.RequireHttpsMetadata = identityOptions.RequiresHttps; |
|||
}); |
|||
} |
|||
|
|||
return authBuilder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
// ==========================================================================
|
|||
// AuthenticationUsage.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Builder; |
|||
|
|||
namespace Squidex.Config.Identity |
|||
{ |
|||
public static class AuthenticationUsage |
|||
{ |
|||
public static IApplicationBuilder UseMyAuthentication(this IApplicationBuilder app) |
|||
{ |
|||
app.UseAuthentication(); |
|||
|
|||
return app; |
|||
} |
|||
} |
|||
} |
|||
@ -1,42 +0,0 @@ |
|||
// ==========================================================================
|
|||
// 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.Shared.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); |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// 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; |
|||
|
|||
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,31 @@ |
|||
// ==========================================================================
|
|||
// GoogleAuthenticationServices.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Squidex.Config.Identity |
|||
{ |
|||
public static class GoogleAuthenticationServices |
|||
{ |
|||
public static AuthenticationBuilder AddMyGoogleAuthentication(this AuthenticationBuilder authBuilder, MyIdentityOptions identityOptions) |
|||
{ |
|||
if (identityOptions.IsGoogleAuthConfigured()) |
|||
{ |
|||
authBuilder.AddGoogle(options => |
|||
{ |
|||
options.ClientId = identityOptions.GoogleClient; |
|||
options.ClientSecret = identityOptions.GoogleSecret; |
|||
options.Events = new GoogleHandler(); |
|||
}); |
|||
} |
|||
|
|||
return authBuilder; |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// GoogleIdentityUsage.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
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; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
// ==========================================================================
|
|||
// MicrosoftAuthenticationServices.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Squidex.Config.Identity |
|||
{ |
|||
public static class MicrosoftAuthenticationServices |
|||
{ |
|||
public static AuthenticationBuilder AddMyMicrosoftAuthentication(this AuthenticationBuilder authBuilder, MyIdentityOptions identityOptions) |
|||
{ |
|||
if (identityOptions.IsMicrosoftAuthConfigured()) |
|||
{ |
|||
authBuilder.AddMicrosoftAccount(options => |
|||
{ |
|||
options.ClientId = identityOptions.MicrosoftClient; |
|||
options.ClientSecret = identityOptions.MicrosoftSecret; |
|||
options.Events = new MicrosoftHandler(); |
|||
}); |
|||
} |
|||
|
|||
return authBuilder; |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// MicrosoftIdentityUsage.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Squidex.Config.Identity |
|||
{ |
|||
public static class MicrosoftIdentityUsage |
|||
{ |
|||
public static IApplicationBuilder UseMyMicrosoftAuthentication(this IApplicationBuilder app) |
|||
{ |
|||
var options = app.ApplicationServices.GetService<IOptions<MyIdentityOptions>>().Value; |
|||
|
|||
if (options.IsMicrosoftAuthConfigured()) |
|||
{ |
|||
var googleOptions = |
|||
new MicrosoftAccountOptions |
|||
{ |
|||
ClientId = options.MicrosoftClient, |
|||
ClientSecret = options.MicrosoftSecret, |
|||
Events = new MicrosoftHandler() |
|||
}; |
|||
|
|||
app.UseMicrosoftAccountAuthentication(googleOptions); |
|||
} |
|||
|
|||
return app; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
// ==========================================================================
|
|||
// ApiAuthorizeAttribute.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using IdentityServer4.AccessTokenValidation; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
|
|||
namespace Squidex.Pipeline |
|||
{ |
|||
public class ApiAuthorizeAttribute : AuthorizeAttribute |
|||
{ |
|||
public ApiAuthorizeAttribute() |
|||
{ |
|||
AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue