Repository navigation
Expand file tree
/
Copy pathClaimsTransformationAuthenticationHandler.cs
More file actions
104 lines (89 loc) · 3.01 KB
/
Copy pathClaimsTransformationAuthenticationHandler.cs
File metadata and controls
104 lines (89 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Http.Authentication;
namespace Microsoft.AspNet.Authentication
{
/// <summary>
/// Handler that applies ClaimsTransformation to authentication
/// </summary>
public class ClaimsTransformationAuthenticationHandler : IAuthenticationHandler
{
private readonly Func<ClaimsPrincipal, ClaimsPrincipal> _transform;
public ClaimsTransformationAuthenticationHandler(Func<ClaimsPrincipal, ClaimsPrincipal> transform)
{
_transform = transform;
}
public IAuthenticationHandler PriorHandler { get; set; }
private void ApplyTransform(AuthenticateContext context)
{
if (_transform != null)
{
// REVIEW: this cast seems really bad (missing interface way to get the result back out?)
var authContext = context as AuthenticateContext;
if (authContext?.Principal != null)
{
context.Authenticated(
_transform.Invoke(authContext.Principal),
authContext.Properties,
authContext.Description);
}
}
}
public void Authenticate(AuthenticateContext context)
{
if (PriorHandler != null)
{
PriorHandler.Authenticate(context);
ApplyTransform(context);
}
}
public async Task AuthenticateAsync(AuthenticateContext context)
{
if (PriorHandler != null)
{
await PriorHandler.AuthenticateAsync(context);
ApplyTransform(context);
}
}
public void Challenge(ChallengeContext context)
{
if (PriorHandler != null)
{
PriorHandler.Challenge(context);
}
}
public void GetDescriptions(DescribeSchemesContext context)
{
if (PriorHandler != null)
{
PriorHandler.GetDescriptions(context);
}
}
public void SignIn(SignInContext context)
{
if (PriorHandler != null)
{
PriorHandler.SignIn(context);
}
}
public void SignOut(SignOutContext context)
{
if (PriorHandler != null)
{
PriorHandler.SignOut(context);
}
}
public void RegisterAuthenticationHandler(IHttpAuthenticationFeature auth)
{
PriorHandler = auth.Handler;
auth.Handler = this;
}
public void UnregisterAuthenticationHandler(IHttpAuthenticationFeature auth)
{
auth.Handler = PriorHandler;
}
}
}