forked from dotnet/Kerberos.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KerberosMiddleware.cs
57 lines (41 loc) · 1.63 KB
/
KerberosMiddleware.cs
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
using Kerberos.NET;
using Kerberos.NET.Crypto;
using Microsoft.Owin;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using NextFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
namespace KerberosMiddlewareSample
{
public class KerberosMiddleware
{
private readonly KerberosValidator validator;
private readonly NextFunc next;
public KerberosMiddleware(NextFunc next)
{
this.next = next;
// NOTE: ValidateAfterDecrypt is a dangerous flag. It should only be used for samples
validator = new KerberosValidator(new KerberosKey("P@ssw0rd!")) { ValidateAfterDecrypt = ValidationActions.None };
}
public async Task Invoke(IDictionary<string, object> environment)
{
var context = new OwinContext(environment);
//validator.Logger = context.TraceOutput.Write;
await ParseKerberosHeader(context);
await next.Invoke(environment);
}
private async Task ParseKerberosHeader(OwinContext context)
{
string[] authzHeader = null;
if (!context.Request.Headers.TryGetValue("Authorization", out authzHeader) || authzHeader.Length != 1)
{
return;
}
var header = authzHeader.First();
var authenticator = new KerberosAuthenticator(validator);
var identity = await authenticator.Authenticate(header);
context.Request.User = new ClaimsPrincipal(identity);
}
}
}