Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Use DI activated options for auth middlewares
Browse files Browse the repository at this point in the history
UseXXX() now use DI activated options
Use ExternalAuthenticationOptions instead of DefaultSignInAs
  • Loading branch information
HaoK committed Oct 8, 2014
1 parent 23c024e commit 3426034
Show file tree
Hide file tree
Showing 41 changed files with 589 additions and 560 deletions.
4 changes: 2 additions & 2 deletions samples/CookieSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
app.UseServices(services => { });
app.UseCookieAuthentication(options =>
{

});

app.Run(async context =>
Expand Down
5 changes: 3 additions & 2 deletions samples/CookieSessionSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
app.UseServices(services => { });
app.UseCookieAuthentication(options =>
{
SessionStore = new MemoryCacheSessionStore(),
options.SessionStore = new MemoryCacheSessionStore();
});

app.Run(async context =>
Expand Down
3 changes: 2 additions & 1 deletion samples/SocialSample/SocialSample.kproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>8c73d216-332d-41d8-bfd0-45bc4bc36552</ProjectGuid>
<OutputType>Library</OutputType>
<OutputType>Web</OutputType>
</PropertyGroup>
<PropertyGroup Condition="$(OutputType) == 'Console'">
<DebuggerFlavor>ConsoleDebugger</DebuggerFlavor>
Expand All @@ -21,6 +21,7 @@
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<DevelopmentServerPort>50113</DevelopmentServerPort>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
104 changes: 57 additions & 47 deletions samples/SocialSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNet.Security.OAuth;
using Microsoft.AspNet.Security.Twitter;
using Newtonsoft.Json.Linq;
using Microsoft.Framework.DependencyInjection;

namespace CookieSample
{
Expand All @@ -23,39 +24,48 @@ public void Configure(IApplicationBuilder app)
{
app.UseErrorPage();

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions()
app.UseServices(services =>
{
LoginPath = new PathString("/login"),
services.ConfigureOptions<ExternalAuthenticationOptions>(options =>
{
options.SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType;
});
});

app.UseFacebookAuthentication(new FacebookAuthenticationOptions()
app.UseCookieAuthentication(options =>
{
options.LoginPath = new PathString("/login");
});

app.UseFacebookAuthentication(options =>
{
AppId = "569522623154478",
AppSecret = "a124463c4719c94b4228d9a240e5dc1a",
options.AppId = "569522623154478";
options.AppSecret = "a124463c4719c94b4228d9a240e5dc1a";
});

app.UseOAuthAuthentication(new OAuthAuthenticationOptions<IOAuthAuthenticationNotifications>("Google-AccessToken")
app.UseOAuthAuthentication("Google-AccessToken", options =>
{
ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com",
ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f",
CallbackPath = new PathString("/signin-google-token"),
AuthorizationEndpoint = GoogleAuthenticationDefaults.AuthorizationEndpoint,
TokenEndpoint = GoogleAuthenticationDefaults.TokenEndpoint,
Scope = { "openid", "profile", "email" },
options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com";
options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f";
options.CallbackPath = new PathString("/signin-google-token");
options.AuthorizationEndpoint = GoogleAuthenticationDefaults.AuthorizationEndpoint;
options.TokenEndpoint = GoogleAuthenticationDefaults.TokenEndpoint;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
});

app.UseGoogleAuthentication(new GoogleAuthenticationOptions()
app.UseGoogleAuthentication(options =>
{
ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com",
ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f",
options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com";
options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f";
});

app.UseTwitterAuthentication(new TwitterAuthenticationOptions()
app.UseTwitterAuthentication(options =>
{
ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g",
ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI",
options.ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g";
options.ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI";
});

/*
Expand All @@ -75,43 +85,43 @@ 127.0.0.1 MsSecSample.localhost.this
The sample app can then be run via:
k web
*/
app.UseOAuthAuthentication(new OAuthAuthenticationOptions<IOAuthAuthenticationNotifications>("Microsoft-AccessToken")
app.UseOAuthAuthentication("Microsoft-AccessToken", options =>
{
Caption = "MicrosoftAccount-AccessToken - Requires project changes",
ClientId = "00000000480FF62E",
ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr",
CallbackPath = new PathString("/signin-microsoft-token"),
AuthorizationEndpoint = MicrosoftAccountAuthenticationDefaults.AuthorizationEndpoint,
TokenEndpoint = MicrosoftAccountAuthenticationDefaults.TokenEndpoint,
Scope = { "wl.basic" },
options.Caption = "MicrosoftAccount-AccessToken - Requires project changes";
options.ClientId = "00000000480FF62E";
options.ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr";
options.CallbackPath = new PathString("/signin-microsoft-token");
options.AuthorizationEndpoint = MicrosoftAccountAuthenticationDefaults.AuthorizationEndpoint;
options.TokenEndpoint = MicrosoftAccountAuthenticationDefaults.TokenEndpoint;
options.Scope.Add("wl.basic");
});

app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions()
app.UseMicrosoftAccountAuthentication(options =>
{
Caption = "MicrosoftAccount - Requires project changes",
ClientId = "00000000480FF62E",
ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr",
options.Caption = "MicrosoftAccount - Requires project changes";
options.ClientId = "00000000480FF62E";
options.ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr";
});

app.UseOAuthAuthentication(new OAuthAuthenticationOptions<IOAuthAuthenticationNotifications>("GitHub-AccessToken")
app.UseOAuthAuthentication("GitHub-AccessToken", options =>
{
ClientId = "8c0c5a572abe8fe89588",
ClientSecret = "e1d95eaf03461d27acd6f49d4fc7bf19d6ac8cda",
CallbackPath = new PathString("/signin-github-token"),
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token",
options.ClientId = "8c0c5a572abe8fe89588";
options.ClientSecret = "e1d95eaf03461d27acd6f49d4fc7bf19d6ac8cda";
options.CallbackPath = new PathString("/signin-github-token");
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
options.TokenEndpoint = "https://github.com/login/oauth/access_token";
});

app.UseOAuthAuthentication(new OAuthAuthenticationOptions<IOAuthAuthenticationNotifications>("GitHub")
app.UseOAuthAuthentication("GitHub", options =>
{
ClientId = "49e302895d8b09ea5656",
ClientSecret = "98f1bf028608901e9df91d64ee61536fe562064b",
CallbackPath = new PathString("/signin-github"),
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token",
UserInformationEndpoint = "https://api.github.com/user",
options.ClientId = "49e302895d8b09ea5656";
options.ClientSecret = "98f1bf028608901e9df91d64ee61536fe562064b";
options.CallbackPath = new PathString("/signin-github");
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
options.TokenEndpoint = "https://github.com/login/oauth/access_token";
options.UserInformationEndpoint = "https://api.github.com/user";
// Retrieving user information is unique to each provider.
Notifications = new OAuthAuthenticationNotifications()
options.Notifications = new OAuthAuthenticationNotifications()
{
OnGetUserInformationAsync = async (context) =>
{
Expand Down Expand Up @@ -153,7 +163,7 @@ k web
context.Identity = identity;
},
},
};
});

// Choose an authentication type
Expand Down
3 changes: 2 additions & 1 deletion samples/SocialSample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"Microsoft.AspNet.Security.MicrosoftAccount": "1.0.0-*",
"Microsoft.AspNet.Security.Twitter": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.OptionsModel": "1.0.0-*"
},
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:12345" },
"frameworks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Security.Cookies;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using System;

namespace Microsoft.AspNet.Builder
{
Expand All @@ -10,15 +13,25 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class CookieAuthenticationExtensions
{
public static IServiceCollection ConfigureCookieAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<CookieAuthenticationOptions> configure)
{
return services.ConfigureOptions(configure);
}

/// <summary>
/// Adds a cookie-based authentication middleware to your web application pipeline.
/// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <param name="options">An options class that controls the middleware behavior</param>
/// <param name="configureOptions">Used to configure the options for the middleware</param>
/// <param name="optionsName">The name of the options class that controls the middleware behavior, null will use the default options</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, [NotNull] CookieAuthenticationOptions options)
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, Action<CookieAuthenticationOptions> configureOptions = null, string optionsName = "")
{
return app.UseMiddleware<CookieAuthenticationMiddleware>(options);
return app.UseMiddleware<CookieAuthenticationMiddleware>(
new OptionsAction<CookieAuthenticationOptions>(configureOptions ?? (o => { }))
{
Name = optionsName
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.AspNet.Security.Infrastructure;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;

namespace Microsoft.AspNet.Security.Cookies
{
public class CookieAuthenticationMiddleware : AuthenticationMiddleware<CookieAuthenticationOptions>
{
private readonly ILogger _logger;

public CookieAuthenticationMiddleware(RequestDelegate next, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, CookieAuthenticationOptions options)
: base(next, options)
public CookieAuthenticationMiddleware(RequestDelegate next,
IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory,
IOptionsAccessor<CookieAuthenticationOptions> options,
IOptionsAction<CookieAuthenticationOptions> configureOptions)
: base(next, options, configureOptions)
{
if (Options.Notifications == null)
{
Expand All @@ -27,11 +32,11 @@ public CookieAuthenticationMiddleware(RequestDelegate next, IDataProtectionProvi
{
Options.CookieName = CookieAuthenticationDefaults.CookiePrefix + Options.AuthenticationType;
}
if (options.TicketDataFormat == null)
if (Options.TicketDataFormat == null)
{
IDataProtector dataProtector = DataProtectionHelpers.CreateDataProtector(dataProtectionProvider,
typeof(CookieAuthenticationMiddleware).FullName, options.AuthenticationType, "v1");
options.TicketDataFormat = new TicketDataFormat(dataProtector);
typeof(CookieAuthenticationMiddleware).FullName, Options.AuthenticationType, "v1");
Options.TicketDataFormat = new TicketDataFormat(dataProtector);
}
if (Options.CookieManager == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class CookieAuthenticationOptions : AuthenticationOptions
/// Create an instance of the options initialized with the default values
/// </summary>
public CookieAuthenticationOptions()
: base(CookieAuthenticationDefaults.AuthenticationType)
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType;
ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
CookiePath = "/";
ExpireTimeSpan = TimeSpan.FromDays(14);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Security.Facebook;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using System;

namespace Microsoft.AspNet.Builder
{
Expand All @@ -10,35 +13,23 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class FacebookAuthenticationExtensions
{
/// <summary>
/// Authenticate users using Facebook.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="appId">The appId assigned by Facebook.</param>
/// <param name="appSecret">The appSecret assigned by Facebook.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, [NotNull] string appId, [NotNull] string appSecret)
public static IServiceCollection ConfigureFacebookAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<FacebookAuthenticationOptions> configure)
{
return app.UseFacebookAuthentication(new FacebookAuthenticationOptions()
{
AppId = appId,
AppSecret = appSecret,
});
return services.ConfigureOptions(configure);
}

/// <summary>
/// Authenticate users using Facebook.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="options">The middleware configuration options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, [NotNull] FacebookAuthenticationOptions options)
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, Action<FacebookAuthenticationOptions> configureOptions = null, string optionsName = "")
{
if (string.IsNullOrEmpty(options.SignInAsAuthenticationType))
{
options.SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType();
}
return app.UseMiddleware<FacebookAuthenticationMiddleware>(options);
return app.UseMiddleware<FacebookAuthenticationMiddleware>(
new OptionsAction<FacebookAuthenticationOptions>(configureOptions ?? (o => { }))
{
Name = optionsName
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNet.Security.Infrastructure;
using Microsoft.AspNet.Security.OAuth;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;

namespace Microsoft.AspNet.Security.Facebook
{
Expand All @@ -27,8 +28,10 @@ public class FacebookAuthenticationMiddleware : OAuthAuthenticationMiddleware<Fa
RequestDelegate next,
IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory,
FacebookAuthenticationOptions options)
: base(next, dataProtectionProvider, loggerFactory, options)
IOptionsAccessor<ExternalAuthenticationOptions> externalOptions,
IOptionsAccessor<FacebookAuthenticationOptions> options,
IOptionsAction<FacebookAuthenticationOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{
if (string.IsNullOrWhiteSpace(Options.AppId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class FacebookAuthenticationOptions : OAuthAuthenticationOptions<IFaceboo
/// Initializes a new <see cref="FacebookAuthenticationOptions"/>.
/// </summary>
public FacebookAuthenticationOptions()
: base(FacebookAuthenticationDefaults.AuthenticationType)
{
AuthenticationType = FacebookAuthenticationDefaults.AuthenticationType;
Caption = AuthenticationType;
CallbackPath = new PathString("/signin-facebook");
SendAppSecretProof = true;
AuthorizationEndpoint = FacebookAuthenticationDefaults.AuthorizationEndpoint;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.AspNet.Security.Facebook/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Microsoft.AspNet.Security.OAuth": "1.0.0-*",
"Microsoft.AspNet.WebUtilities": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Framework.OptionsModel": "1.0.0-*",
"Newtonsoft.Json": "6.0.4"
},
"frameworks": {
Expand Down
Loading

0 comments on commit 3426034

Please sign in to comment.