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

Commit 14d1b46

Browse files
committed
ClaimsXform and RIP AutoAuthHandler
- Initial support for ClaimsTransformation - merge automatic auth handler back into base
1 parent bd7f070 commit 14d1b46

File tree

34 files changed

+523
-227
lines changed

34 files changed

+523
-227
lines changed

samples/CookieSessionSample/CookieSessionSample.xproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313
<PropertyGroup>
1414
<SchemaVersion>2.0</SchemaVersion>
15-
<DevelopmentServerPort>22571</DevelopmentServerPort>
15+
<DevelopmentServerPort>36505</DevelopmentServerPort>
1616
</PropertyGroup>
1717
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
1818
</Project>

samples/SocialSample/SocialSample.xproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313
<PropertyGroup>
1414
<SchemaVersion>2.0</SchemaVersion>
15-
<DevelopmentServerPort>22570</DevelopmentServerPort>
15+
<DevelopmentServerPort>36504</DevelopmentServerPort>
1616
</PropertyGroup>
1717
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
1818
</Project>

samples/SocialSample/Startup.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Net.Http;
22
using System.Net.Http.Headers;
33
using System.Security.Claims;
4+
using System.Threading.Tasks;
45
using Microsoft.AspNet.Builder;
56
using Microsoft.AspNet.DataProtection;
67
using Microsoft.AspNet.Http;
@@ -28,6 +29,13 @@ public void Configure(IApplicationBuilder app)
2829
{
2930
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
3031
});
32+
services.ConfigureClaimsTransformation(p =>
33+
{
34+
var id = new ClaimsIdentity("xform");
35+
id.AddClaim(new Claim("ClaimsTransformation", "TransformAddedClaim"));
36+
p.AddIdentity(id);
37+
return p;
38+
});
3139
});
3240

3341
app.UseCookieAuthentication(options =>

src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.AspNet.Authentication.Cookies
1515
{
16-
internal class CookieAuthenticationHandler : AutomaticAuthenticationHandler<CookieAuthenticationOptions>
16+
internal class CookieAuthenticationHandler : AuthenticationHandler<CookieAuthenticationOptions>
1717
{
1818
private const string HeaderNameCacheControl = "Cache-Control";
1919
private const string HeaderNamePragma = "Pragma";

src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ public class CookieAuthenticationMiddleware : AuthenticationMiddleware<CookieAut
1515
{
1616
private readonly ILogger _logger;
1717

18-
public CookieAuthenticationMiddleware(RequestDelegate next,
19-
IServiceProvider services,
20-
IDataProtectionProvider dataProtectionProvider,
21-
ILoggerFactory loggerFactory,
22-
IOptions<CookieAuthenticationOptions> options,
18+
public CookieAuthenticationMiddleware(
19+
[NotNull] RequestDelegate next,
20+
[NotNull] IServiceProvider services,
21+
[NotNull] IDataProtectionProvider dataProtectionProvider,
22+
[NotNull] ILoggerFactory loggerFactory,
23+
[NotNull] IOptions<CookieAuthenticationOptions> options,
2324
ConfigureOptions<CookieAuthenticationOptions> configureOptions)
2425
: base(next, services, options, configureOptions)
2526
{

src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationOptions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
1111
/// <summary>
1212
/// Contains the options used by the CookiesAuthenticationMiddleware
1313
/// </summary>
14-
public class CookieAuthenticationOptions : AutomaticAuthenticationOptions
14+
public class CookieAuthenticationOptions : AuthenticationOptions
1515
{
1616
private string _cookieName;
1717

@@ -20,7 +20,6 @@ public class CookieAuthenticationOptions : AutomaticAuthenticationOptions
2020
/// </summary>
2121
public CookieAuthenticationOptions()
2222
{
23-
AutomaticAuthentication = true;
2423
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2524
ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
2625
ExpireTimeSpan = TimeSpan.FromDays(14);

src/Microsoft.AspNet.Authentication.Facebook/FacebookAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class FacebookAuthenticationMiddleware : OAuthAuthenticationMiddleware<Fa
2525
/// <param name="loggerFactory"></param>
2626
/// <param name="options">Configuration options for the middleware.</param>
2727
public FacebookAuthenticationMiddleware(
28-
RequestDelegate next,
29-
IServiceProvider services,
30-
IDataProtectionProvider dataProtectionProvider,
31-
ILoggerFactory loggerFactory,
32-
IOptions<ExternalAuthenticationOptions> externalOptions,
33-
IOptions<FacebookAuthenticationOptions> options,
28+
[NotNull] RequestDelegate next,
29+
[NotNull] IServiceProvider services,
30+
[NotNull] IDataProtectionProvider dataProtectionProvider,
31+
[NotNull] ILoggerFactory loggerFactory,
32+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
33+
[NotNull] IOptions<FacebookAuthenticationOptions> options,
3434
ConfigureOptions<FacebookAuthenticationOptions> configureOptions = null)
3535
: base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
3636
{

src/Microsoft.AspNet.Authentication.Google/GoogleAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public class GoogleAuthenticationMiddleware : OAuthAuthenticationMiddleware<Goog
3030
/// <param name="loggerFactory"></param>
3131
/// <param name="options">Configuration options for the middleware.</param>
3232
public GoogleAuthenticationMiddleware(
33-
RequestDelegate next,
34-
IServiceProvider services,
35-
IDataProtectionProvider dataProtectionProvider,
36-
ILoggerFactory loggerFactory,
37-
IOptions<ExternalAuthenticationOptions> externalOptions,
38-
IOptions<GoogleAuthenticationOptions> options,
33+
[NotNull] RequestDelegate next,
34+
[NotNull] IServiceProvider services,
35+
[NotNull] IDataProtectionProvider dataProtectionProvider,
36+
[NotNull] ILoggerFactory loggerFactory,
37+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
38+
[NotNull] IOptions<GoogleAuthenticationOptions> options,
3939
ConfigureOptions<GoogleAuthenticationOptions> configureOptions = null)
4040
: base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
4141
{

src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class MicrosoftAccountAuthenticationMiddleware : OAuthAuthenticationMiddl
2626
/// <param name="loggerFactory"></param>
2727
/// <param name="options">Configuration options for the middleware.</param>
2828
public MicrosoftAccountAuthenticationMiddleware(
29-
RequestDelegate next,
30-
IServiceProvider services,
31-
IDataProtectionProvider dataProtectionProvider,
32-
ILoggerFactory loggerFactory,
33-
IOptions<ExternalAuthenticationOptions> externalOptions,
34-
IOptions<MicrosoftAccountAuthenticationOptions> options,
29+
[NotNull] RequestDelegate next,
30+
[NotNull] IServiceProvider services,
31+
[NotNull] IDataProtectionProvider dataProtectionProvider,
32+
[NotNull] ILoggerFactory loggerFactory,
33+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
34+
[NotNull] IOptions<MicrosoftAccountAuthenticationOptions> options,
3535
ConfigureOptions<MicrosoftAccountAuthenticationOptions> configureOptions = null)
3636
: base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
3737
{

src/Microsoft.AspNet.Authentication.OAuth/OAuthAuthenticationHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,19 @@ protected virtual async Task<AuthenticationTicket> GetUserInformationAsync(Authe
176176

177177
protected override void ApplyResponseChallenge()
178178
{
179+
if (ShouldConvertChallengeToForbidden())
180+
{
181+
Response.StatusCode = 403;
182+
return;
183+
}
184+
179185
if (Response.StatusCode != 401)
180186
{
181187
return;
182188
}
183189

184-
// Only redirect on challenges
185-
if (ChallengeContext == null)
190+
// When Automatic should redirect on 401 even if there wasn't an explicit challenge.
191+
if (ChallengeContext == null && !Options.AutomaticAuthentication)
186192
{
187193
return;
188194
}

src/Microsoft.AspNet.Authentication.OAuth/OAuthAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public class OAuthAuthenticationMiddleware<TOptions, TNotifications> : Authentic
3131
/// <param name="loggerFactory"></param>
3232
/// <param name="options">Configuration options for the middleware.</param>
3333
public OAuthAuthenticationMiddleware(
34-
RequestDelegate next,
35-
IServiceProvider services,
36-
IDataProtectionProvider dataProtectionProvider,
37-
ILoggerFactory loggerFactory,
38-
IOptions<ExternalAuthenticationOptions> externalOptions,
39-
IOptions<TOptions> options,
34+
[NotNull] RequestDelegate next,
35+
[NotNull] IServiceProvider services,
36+
[NotNull] IDataProtectionProvider dataProtectionProvider,
37+
[NotNull] ILoggerFactory loggerFactory,
38+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
39+
[NotNull] IOptions<TOptions> options,
4040
ConfigureOptions<TOptions> configureOptions = null)
4141
: base(next, services, options, configureOptions)
4242
{

src/Microsoft.AspNet.Authentication.OAuthBearer/OAuthBearerAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.AspNet.Authentication.OAuthBearer
1717
{
18-
public class OAuthBearerAuthenticationHandler : AutomaticAuthenticationHandler<OAuthBearerAuthenticationOptions>
18+
public class OAuthBearerAuthenticationHandler : AuthenticationHandler<OAuthBearerAuthenticationOptions>
1919
{
2020
private readonly ILogger _logger;
2121
private OpenIdConnectConfiguration _configuration;
@@ -197,7 +197,7 @@ protected override async Task ApplyResponseChallengeAsync()
197197
return;
198198
}
199199

200-
if ((Response.StatusCode != 401) || (ChallengeContext == null))
200+
if ((Response.StatusCode != 401) || (ChallengeContext == null && !Options.AutomaticAuthentication))
201201
{
202202
return;
203203
}

src/Microsoft.AspNet.Authentication.OAuthBearer/OAuthBearerAuthenticationMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class OAuthBearerAuthenticationMiddleware : AuthenticationMiddleware<OAut
2929
/// extension method.
3030
/// </summary>
3131
public OAuthBearerAuthenticationMiddleware(
32-
RequestDelegate next,
33-
IServiceProvider services,
34-
ILoggerFactory loggerFactory,
35-
IOptions<OAuthBearerAuthenticationOptions> options,
32+
[NotNull] RequestDelegate next,
33+
[NotNull] IServiceProvider services,
34+
[NotNull] ILoggerFactory loggerFactory,
35+
[NotNull] IOptions<OAuthBearerAuthenticationOptions> options,
3636
ConfigureOptions<OAuthBearerAuthenticationOptions> configureOptions)
3737
: base(next, services, options, configureOptions)
3838
{

src/Microsoft.AspNet.Authentication.OAuthBearer/OAuthBearerAuthenticationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.AspNet.Authentication.OAuthBearer
1313
/// <summary>
1414
/// Options class provides information needed to control Bearer Authentication middleware behavior
1515
/// </summary>
16-
public class OAuthBearerAuthenticationOptions : AutomaticAuthenticationOptions
16+
public class OAuthBearerAuthenticationOptions : AuthenticationOptions
1717
{
1818
private ICollection<ISecurityTokenValidator> _securityTokenValidators;
1919
private TokenValidationParameters _tokenValidationParameters;

src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public class OpenIdConnectAuthenticationMiddleware : AuthenticationMiddleware<Op
3535
/// <param name="options">Configuration options for the middleware</param>
3636
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")]
3737
public OpenIdConnectAuthenticationMiddleware(
38-
RequestDelegate next,
39-
IServiceProvider services,
40-
IDataProtectionProvider dataProtectionProvider,
41-
ILoggerFactory loggerFactory,
42-
IOptions<ExternalAuthenticationOptions> externalOptions,
43-
IOptions<OpenIdConnectAuthenticationOptions> options,
38+
[NotNull] RequestDelegate next,
39+
[NotNull] IServiceProvider services,
40+
[NotNull] IDataProtectionProvider dataProtectionProvider,
41+
[NotNull] ILoggerFactory loggerFactory,
42+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
43+
[NotNull] IOptions<OpenIdConnectAuthenticationOptions> options,
4444
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions)
4545
: base(next, services, options, configureOptions)
4646
{

src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenidConnectAuthenticationHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,19 @@ protected override void ApplyResponseChallenge()
117117
/// <returns></returns>
118118
protected override async Task ApplyResponseChallengeAsync()
119119
{
120+
if (ShouldConvertChallengeToForbidden())
121+
{
122+
Response.StatusCode = 403;
123+
return;
124+
}
125+
120126
if (Response.StatusCode != 401)
121127
{
122128
return;
123129
}
124130

125-
// Only redirect on challenges
126-
if (ChallengeContext == null)
131+
// When Automatic should redirect on 401 even if there wasn't an explicit challenge.
132+
if (ChallengeContext == null && !Options.AutomaticAuthentication)
127133
{
128134
return;
129135
}

src/Microsoft.AspNet.Authentication.Twitter/TwitterAuthenticationHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,19 @@ protected override void ApplyResponseChallenge()
131131

132132
protected override async Task ApplyResponseChallengeAsync()
133133
{
134+
if (ShouldConvertChallengeToForbidden())
135+
{
136+
Response.StatusCode = 403;
137+
return;
138+
}
139+
134140
if (Response.StatusCode != 401)
135141
{
136142
return;
137143
}
138144

139-
// Only redirect on challenges
140-
if (ChallengeContext == null)
145+
// When Automatic should redirect on 401 even if there wasn't an explicit challenge.
146+
if (ChallengeContext == null && !Options.AutomaticAuthentication)
141147
{
142148
return;
143149
}

src/Microsoft.AspNet.Authentication.Twitter/TwitterAuthenticationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public class TwitterAuthenticationMiddleware : AuthenticationMiddleware<TwitterA
3333
/// <param name="loggerFactory"></param>
3434
/// <param name="options">Configuration options for the middleware</param>
3535
public TwitterAuthenticationMiddleware(
36-
RequestDelegate next,
37-
IServiceProvider services,
38-
IDataProtectionProvider dataProtectionProvider,
39-
ILoggerFactory loggerFactory,
40-
IOptions<ExternalAuthenticationOptions> externalOptions,
41-
IOptions<TwitterAuthenticationOptions> options,
36+
[NotNull] RequestDelegate next,
37+
[NotNull] IServiceProvider services,
38+
[NotNull] IDataProtectionProvider dataProtectionProvider,
39+
[NotNull] ILoggerFactory loggerFactory,
40+
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
41+
[NotNull] IOptions<TwitterAuthenticationOptions> options,
4242
ConfigureOptions<TwitterAuthenticationOptions> configureOptions = null)
4343
: base(next, services, options, configureOptions)
4444
{

0 commit comments

Comments
 (0)