@ -3,6 +3,7 @@ using System.Security.Claims;
using Microsoft.AspNet.Authentication ;
using Microsoft.AspNet.Authentication ;
using Microsoft.AspNet.Authentication.Cookies ;
using Microsoft.AspNet.Authentication.Cookies ;
using Microsoft.AspNet.Authentication.Facebook ;
using Microsoft.AspNet.Authentication.Facebook ;
using Microsoft.AspNet.Authentication.JwtBearer ;
using Microsoft.AspNet.Authentication.OAuth ;
using Microsoft.AspNet.Authentication.OAuth ;
using Microsoft.AspNet.Builder ;
using Microsoft.AspNet.Builder ;
using Microsoft.AspNet.Http ;
using Microsoft.AspNet.Http ;
@ -12,7 +13,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.OptionsModel ;
using Microsoft.Extensions.OptionsModel ;
using Microsoft.Extensions.WebEncoders ;
using Microsoft.Extensions.WebEncoders ;
using OAuth.AspNet.AuthServer ;
using OAuth.AspNet.AuthServer ;
using OAuth.AspNet.Tokens ;
using Yavsc.Auth ;
using Yavsc.Auth ;
using Yavsc.Extensions ;
using Yavsc.Models ;
using Yavsc.Models ;
namespace Yavsc
namespace Yavsc
@ -26,6 +29,8 @@ namespace Yavsc
public static FacebookOptions FacebookAppOptions { get ; private set ; }
public static FacebookOptions FacebookAppOptions { get ; private set ; }
public static OAuthAuthorizationServerOptions OAuthServerAppOptions { get ; private set ; }
public static OAuthAuthorizationServerOptions OAuthServerAppOptions { get ; private set ; }
public static OAuthOptions OAuthClientOptions { get ; set ; }
public static YavscGoogleOptions YavscGoogleAppOptions { get ; private set ; }
public static YavscGoogleOptions YavscGoogleAppOptions { get ; private set ; }
public static MonoDataProtectionProvider ProtectionProvider { get ; private set ; }
public static MonoDataProtectionProvider ProtectionProvider { get ; private set ; }
@ -38,23 +43,7 @@ namespace Yavsc
services . Add ( ServiceDescriptor . Singleton ( typeof ( IOptions < OAuth2AppSettings > ) , typeof ( OptionsManager < OAuth2AppSettings > ) ) ) ;
services . Add ( ServiceDescriptor . Singleton ( typeof ( IOptions < OAuth2AppSettings > ) , typeof ( OptionsManager < OAuth2AppSettings > ) ) ) ;
// used by the YavscGoogleOAuth middelware (TODO drop it)
// used by the YavscGoogleOAuth middelware (TODO drop it)
services . AddTransient < Microsoft . Extensions . WebEncoders . UrlEncoder , UrlEncoder > ( ) ;
services . AddTransient < Microsoft . Extensions . WebEncoders . UrlEncoder , UrlEncoder > ( ) ;
/ * Obsolete :
var keyParamsFileInfo =
new FileInfo ( Configuration [ "DataProtection:RSAParamFile" ] ) ;
var keyParams = ( keyParamsFileInfo . Exists ) ?
RSAKeyUtils . GetKeyParameters ( keyParamsFileInfo . Name ) :
RSAKeyUtils . GenerateKeyAndSave ( keyParamsFileInfo . Name ) ;
key = new RsaSecurityKey ( keyParams ) ;
services . Configure < TokenAuthOptions > (
to = >
{
to . Audience = Configuration [ "Site:Audience" ] ;
to . Issuer = Configuration [ "Site:Authority" ] ;
to . SigningCredentials =
new SigningCredentials ( key , SecurityAlgorithms . RsaSha256Signature ) ;
}
) ; * /
services . AddAuthentication ( options = >
services . AddAuthentication ( options = >
{
{
options . SignInScheme = Constants . ExternalAuthenticationSheme ;
options . SignInScheme = Constants . ExternalAuthenticationSheme ;
@ -94,19 +83,9 @@ namespace Yavsc
;
;
}
}
private void ConfigureOAuthApp ( IApplicationBuilder app )
private void ConfigureOAuthApp ( IApplicationBuilder app , SiteSettings settings )
{
// External authentication shared cookie:
app . UseCookieAuthentication ( options = >
{
{
ExternalCookieAppOptions = options ;
app . UseIdentity ( ) ;
options . AuthenticationScheme = Constants . ExternalAuthenticationSheme ;
options . AutomaticAuthenticate = true ;
options . ExpireTimeSpan = TimeSpan . FromMinutes ( 5 ) ;
options . LoginPath = new PathString ( Constants . LoginPath . Substring ( 1 ) ) ;
options . AccessDeniedPath = new PathString ( Constants . AccessDeniedPath . Substring ( 1 ) ) ;
} ) ;
app . UseOAuthAuthorizationServer (
app . UseOAuthAuthorizationServer (
options = >
options = >
@ -117,6 +96,7 @@ namespace Yavsc
options . ApplicationCanDisplayErrors = true ;
options . ApplicationCanDisplayErrors = true ;
options . AllowInsecureHttp = true ;
options . AllowInsecureHttp = true ;
options . AuthenticationScheme = OAuthDefaults . AuthenticationType ;
options . AuthenticationScheme = OAuthDefaults . AuthenticationType ;
options . TokenDataProtector = ProtectionProvider . CreateProtector ( "Bearer protection" ) ;
options . Provider = new OAuthAuthorizationServerProvider
options . Provider = new OAuthAuthorizationServerProvider
{
{
@ -143,7 +123,38 @@ namespace Yavsc
}
}
) ;
) ;
app . UseIdentity ( ) ;
app . UseWhen ( context = > context . Request . Path . StartsWithSegments ( "/api" ) ,
branch = >
{
branch . UseJwtBearerAuthentication (
options = >
{
options . AuthenticationScheme = JwtBearerDefaults . AuthenticationScheme ;
options . AutomaticAuthenticate = true ;
options . SecurityTokenValidators . Clear ( ) ;
options . SecurityTokenValidators . Add ( new TicketDataFormatTokenValidator (
ProtectionProvider
) ) ;
}
) ;
} ) ;
app . UseWhen ( context = > ! context . Request . Path . StartsWithSegments ( "/api" ) ,
branch = >
{
// External authentication shared cookie:
branch . UseCookieAuthentication ( options = >
{
ExternalCookieAppOptions = options ;
options . AuthenticationScheme = Constants . ExternalAuthenticationSheme ;
options . AutomaticAuthenticate = true ;
options . ExpireTimeSpan = TimeSpan . FromMinutes ( 5 ) ;
options . LoginPath = new PathString ( Constants . LoginPath . Substring ( 1 ) ) ;
// TODO implement an access denied page
options . AccessDeniedPath = new PathString ( Constants . LoginPath . Substring ( 1 ) ) ;
} ) ;
var gvents = new OAuthEvents ( ) ;
var gvents = new OAuthEvents ( ) ;
YavscGoogleAppOptions = new YavscGoogleOptions
YavscGoogleAppOptions = new YavscGoogleOptions
@ -170,10 +181,10 @@ namespace Yavsc
}
}
} ;
} ;
YavscGoogleAppOptions . Scope . Add ( "https://www.googleapis.com/auth/calendar" ) ;
YavscGoogleAppOptions . Scope . Add ( "https://www.googleapis.com/auth/calendar" ) ;
app . UseMiddleware < Yavsc . Auth . GoogleMiddleware > ( YavscGoogleAppOptions ) ;
branch . UseMiddleware < Yavsc . Auth . GoogleMiddleware > ( YavscGoogleAppOptions ) ;
// Facebook
// Facebook
app . UseFacebookAuthentication ( options = >
branch . UseFacebookAuthentication ( options = >
{
{
FacebookAppOptions = options ;
FacebookAppOptions = options ;
options . AppId = Configuration [ "Authentication:Facebook:AppId" ] ;
options . AppId = Configuration [ "Authentication:Facebook:AppId" ] ;
@ -181,6 +192,10 @@ namespace Yavsc
options . Scope . Add ( "email" ) ;
options . Scope . Add ( "email" ) ;
options . UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email,first_name,last_name" ;
options . UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email,first_name,last_name" ;
} ) ;
} ) ;
} ) ;
}
}
}
}