Merge branch 'vnext' of github.com:pazof/yavsc into vnext
This commit is contained in:
commit
c27a477614
1 changed files with 160 additions and 180 deletions
|
|
@ -1,33 +1,31 @@
|
|||
using System;
|
||||
using System.Security.Claims;
|
||||
using Google.Apis.Auth.OAuth2.Responses;
|
||||
using Google.Apis.Util.Store;
|
||||
using Microsoft.AspNet.Authentication;
|
||||
using Microsoft.AspNet.Authentication.Cookies;
|
||||
using Microsoft.AspNet.Authentication.Facebook;
|
||||
using Microsoft.AspNet.Authentication.Twitter;
|
||||
using Microsoft.AspNet.Authentication.JwtBearer;
|
||||
using Microsoft.AspNet.Authentication.OAuth;
|
||||
using Microsoft.AspNet.Authentication.Twitter;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.EntityFramework;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.OptionsModel;
|
||||
using Microsoft.Extensions.WebEncoders;
|
||||
using OAuth.AspNet.AuthServer;
|
||||
using OAuth.AspNet.Tokens;
|
||||
using Google.Apis.Util.Store;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Google.Apis.Auth.OAuth2.Responses;
|
||||
|
||||
namespace Yavsc
|
||||
{
|
||||
namespace Yavsc {
|
||||
using Auth;
|
||||
using Extensions;
|
||||
using Models;
|
||||
using Helpers.Google;
|
||||
using Models;
|
||||
|
||||
public partial class Startup
|
||||
{
|
||||
public partial class Startup {
|
||||
public static CookieAuthenticationOptions ExternalCookieAppOptions { get; private set; }
|
||||
|
||||
public static IdentityOptions IdentityAppOptions { get; set; }
|
||||
|
|
@ -36,22 +34,19 @@ namespace Yavsc
|
|||
public static TwitterOptions TwitterAppOptions { get; private set; }
|
||||
public static OAuthAuthorizationServerOptions OAuthServerAppOptions { get; private set; }
|
||||
|
||||
|
||||
public static YavscGoogleOptions YavscGoogleAppOptions { get; private set; }
|
||||
public static MonoDataProtectionProvider ProtectionProvider { get; private set; }
|
||||
|
||||
// public static CookieAuthenticationOptions BearerCookieOptions { get; private set; }
|
||||
|
||||
private void ConfigureOAuthServices(IServiceCollection services)
|
||||
{
|
||||
private void ConfigureOAuthServices (IServiceCollection services) {
|
||||
services.Configure<SharedAuthenticationOptions> (options => options.SignInScheme = Constants.ApplicationAuthenticationSheme);
|
||||
|
||||
services.Add (ServiceDescriptor.Singleton (typeof (IOptions<OAuth2AppSettings>), typeof (OptionsManager<OAuth2AppSettings>)));
|
||||
// used by the YavscGoogleOAuth middelware (TODO drop it)
|
||||
services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder> ();
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
{
|
||||
services.AddAuthentication (options => {
|
||||
options.SignInScheme = Constants.ExternalAuthenticationSheme;
|
||||
});
|
||||
|
||||
|
|
@ -60,8 +55,7 @@ namespace Yavsc
|
|||
(ProtectionProvider);
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole> (
|
||||
option =>
|
||||
{
|
||||
option => {
|
||||
IdentityAppOptions = option;
|
||||
option.User.AllowedUserNameCharacters += " ";
|
||||
option.User.RequireUniqueEmail = true;
|
||||
|
|
@ -93,16 +87,13 @@ namespace Yavsc
|
|||
;
|
||||
}
|
||||
private void ConfigureOAuthApp (IApplicationBuilder app,
|
||||
SiteSettings settingsOptions, ILogger logger)
|
||||
{
|
||||
SiteSettings settingsOptions, ILogger logger) {
|
||||
|
||||
app.UseIdentity ();
|
||||
app.UseWhen (context => context.Request.Path.StartsWithSegments ("/api"),
|
||||
branch =>
|
||||
{
|
||||
branch => {
|
||||
branch.UseJwtBearerAuthentication (
|
||||
options =>
|
||||
{
|
||||
options => {
|
||||
options.AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.AutomaticAuthenticate = true;
|
||||
options.SecurityTokenValidators.Clear ();
|
||||
|
|
@ -114,11 +105,9 @@ namespace Yavsc
|
|||
|
||||
});
|
||||
app.UseWhen (context => !context.Request.Path.StartsWithSegments ("/api"),
|
||||
branch =>
|
||||
{
|
||||
branch => {
|
||||
// External authentication shared cookie:
|
||||
branch.UseCookieAuthentication(options =>
|
||||
{
|
||||
branch.UseCookieAuthentication (options => {
|
||||
ExternalCookieAppOptions = options;
|
||||
options.AuthenticationScheme = Constants.ExternalAuthenticationSheme;
|
||||
options.AutomaticAuthenticate = true;
|
||||
|
|
@ -128,26 +117,23 @@ namespace Yavsc
|
|||
options.AccessDeniedPath = new PathString (Constants.LoginPath.Substring (1));
|
||||
});
|
||||
|
||||
|
||||
|
||||
YavscGoogleAppOptions = new YavscGoogleOptions
|
||||
{
|
||||
YavscGoogleAppOptions = new YavscGoogleOptions {
|
||||
ClientId = GoogleWebClientConfiguration["web:client_id"],
|
||||
ClientSecret = GoogleWebClientConfiguration["web:client_secret"],
|
||||
AccessType = "offline",
|
||||
Scope = { "profile", "https://www.googleapis.com/auth/plus.login",
|
||||
Scope = {
|
||||
"profile",
|
||||
"https://www.googleapis.com/auth/plus.login",
|
||||
"https://www.googleapis.com/auth/admin.directory.resource.calendar",
|
||||
"https://www.googleapis.com/auth/calendar",
|
||||
"https://www.googleapis.com/auth/calendar.events"},
|
||||
"https://www.googleapis.com/auth/calendar.events"
|
||||
},
|
||||
SaveTokensAsClaims = true,
|
||||
UserInformationEndpoint = "https://www.googleapis.com/plus/v1/people/me",
|
||||
Events = new OAuthEvents
|
||||
{
|
||||
OnCreatingTicket = async context =>
|
||||
{
|
||||
Events = new OAuthEvents {
|
||||
OnCreatingTicket = async context => {
|
||||
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory> ()
|
||||
.CreateScope())
|
||||
{
|
||||
.CreateScope ()) {
|
||||
var gcontext = context as GoogleOAuthCreatingTicketContext;
|
||||
context.Identity.AddClaim (new Claim (YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId));
|
||||
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext> ();
|
||||
|
|
@ -182,11 +168,9 @@ namespace Yavsc
|
|||
options.ConsumerSecret = Configuration["Authentication:Twitter:ClientSecret"];
|
||||
}); */
|
||||
|
||||
|
||||
branch.UseOAuthAuthorizationServer (
|
||||
|
||||
options =>
|
||||
{
|
||||
options => {
|
||||
OAuthServerAppOptions = options;
|
||||
options.AuthorizeEndpointPath = new PathString (Constants.AuthorizePath.Substring (1));
|
||||
options.TokenEndpointPath = new PathString (Constants.TokenPath.Substring (1));
|
||||
|
|
@ -195,22 +179,19 @@ namespace Yavsc
|
|||
options.AuthenticationScheme = OAuthDefaults.AuthenticationType;
|
||||
options.TokenDataProtector = ProtectionProvider.CreateProtector ("Bearer protection");
|
||||
|
||||
options.Provider = new OAuthAuthorizationServerProvider
|
||||
{
|
||||
options.Provider = new OAuthAuthorizationServerProvider {
|
||||
OnValidateClientRedirectUri = ValidateClientRedirectUri,
|
||||
OnValidateClientAuthentication = ValidateClientAuthentication,
|
||||
OnGrantResourceOwnerCredentials = GrantResourceOwnerCredentials,
|
||||
OnGrantClientCredentials = GrantClientCredetails
|
||||
};
|
||||
|
||||
options.AuthorizationCodeProvider = new AuthenticationTokenProvider
|
||||
{
|
||||
options.AuthorizationCodeProvider = new AuthenticationTokenProvider {
|
||||
OnCreate = CreateAuthenticationCode,
|
||||
OnReceive = ReceiveAuthenticationCode,
|
||||
};
|
||||
|
||||
options.RefreshTokenProvider = new AuthenticationTokenProvider
|
||||
{
|
||||
options.RefreshTokenProvider = new AuthenticationTokenProvider {
|
||||
OnCreate = CreateRefreshToken,
|
||||
OnReceive = ReceiveRefreshToken,
|
||||
};
|
||||
|
|
@ -223,7 +204,6 @@ namespace Yavsc
|
|||
|
||||
Environment.SetEnvironmentVariable ("GOOGLE_APPLICATION_CREDENTIALS", "google-secret.json");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue