forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaml2AuthenticationHandler.cs
254 lines (218 loc) · 10.2 KB
/
Saml2AuthenticationHandler.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Sustainsys.Saml2.Configuration;
using Sustainsys.Saml2.Metadata;
using Sustainsys.Saml2.WebSso;
using Microsoft.Owin;
using Microsoft.Owin.Infrastructure;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace Sustainsys.Saml2.Owin
{
class Saml2AuthenticationHandler : AuthenticationHandler<Saml2AuthenticationOptions>
{
protected async override Task<AuthenticationTicket> AuthenticateCoreAsync()
{
var acsPath = new PathString(Options.SPOptions.ModulePath)
.Add(new PathString("/" + CommandFactory.AcsCommandName));
if (Request.Path != acsPath)
{
return null;
}
var httpRequestData = await Context.ToHttpRequestData(Options.DataProtector.Unprotect);
try
{
var result = CommandFactory.GetCommand(CommandFactory.AcsCommandName)
.Run(httpRequestData, Options);
if (!result.HandledResult)
{
result.Apply(Context, Options.DataProtector);
}
var identities = result.Principal.Identities.Select(i =>
new ClaimsIdentity(i, null, Options.SignInAsAuthenticationType, i.NameClaimType, i.RoleClaimType));
var authProperties = new AuthenticationProperties(result.RelayData);
authProperties.RedirectUri = result.Location.OriginalString;
if (result.SessionNotOnOrAfter.HasValue)
{
authProperties.AllowRefresh = false;
authProperties.ExpiresUtc = result.SessionNotOnOrAfter.Value;
}
return new MultipleIdentityAuthenticationTicket(identities, authProperties);
}
catch (Exception ex)
{
return CreateErrorAuthenticationTicket(httpRequestData, ex);
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "SPOptions")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "ReturnUrl")]
private AuthenticationTicket CreateErrorAuthenticationTicket(HttpRequestData httpRequestData, Exception ex)
{
var authProperties = new AuthenticationProperties();
if (httpRequestData.StoredRequestState?.ReturnUrl != null)
{
// ReturnUrl is removed from AuthProps dictionary to save space, need to put it back.
authProperties.RedirectUri = httpRequestData.StoredRequestState.ReturnUrl.OriginalString;
}
else
{
var redirectUrl = Options.SPOptions.ReturnUrl;
if (redirectUrl == null)
{
Options.SPOptions.Logger.WriteError(
"An error occurred and no request state with a return url is available. " +
"The fallback behavior is to redirect to the location configured in " +
"SPOptions.ReturnUrl. However, that is null so a redirect is done to the " +
"application root instead.", null);
redirectUrl = httpRequestData.ApplicationUrl;
}
authProperties.RedirectUri = redirectUrl.OriginalString;
}
// The Google middleware adds this, so let's follow that example.
authProperties.RedirectUri = WebUtilities.AddQueryString(
authProperties.RedirectUri, "error", "access_denied");
string samlResponse = ex.Data.Contains("Saml2Response")
? " The received SAML data is\n" + ex.Data["Saml2Response"]
: "";
Options.SPOptions.Logger.WriteError("Saml2 Authentication failed." + samlResponse, ex);
return new MultipleIdentityAuthenticationTicket(
Enumerable.Empty<ClaimsIdentity>(),
authProperties);
}
protected override async Task ApplyResponseChallengeAsync()
{
if (Response.StatusCode == 401)
{
var challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);
if (challenge != null)
{
EntityId idp;
string strIdp;
if (challenge.Properties.Dictionary.TryGetValue("idp", out strIdp))
{
idp = new EntityId(strIdp);
}
else
{
object objIdp = null;
Context.Environment.TryGetValue("saml2.idp", out objIdp);
idp = objIdp as EntityId;
}
var redirectUri = challenge.Properties.RedirectUri;
// Don't serialize the RedirectUri twice.
challenge.Properties.RedirectUri = null;
if (redirectUri == null)
{
redirectUri = Context.Request.Uri.ToString();
}
var result = SignInCommand.Run(
idp,
redirectUri,
await Context.ToHttpRequestData(Options.DataProtector.Unprotect),
Options,
challenge.Properties.Dictionary);
if (!result.HandledResult)
{
result.Apply(Context, Options.DataProtector);
}
}
}
}
protected async override Task ApplyResponseGrantAsync()
{
// Automatically sign out, even if passive because passive sign in and auto sign out
// is typically most common scenario. Unless strict compatibility is set.
var mode = Options.SPOptions.Compatibility.StrictOwinAuthenticationMode ?
Options.AuthenticationMode : AuthenticationMode.Active;
var revoke = Helper.LookupSignOut(Options.AuthenticationType, mode);
if (revoke != null)
{
var request = await Context.ToHttpRequestData(Options.DataProtector.Unprotect);
var urls = new Saml2Urls(request, Options);
string redirectUrl = revoke.Properties.RedirectUri;
if (string.IsNullOrEmpty(redirectUrl))
{
if (Context.Response.StatusCode / 100 == 3)
{
redirectUrl = Context.Response.Headers["Location"];
}
else
{
redirectUrl = Context.Request.Path.ToUriComponent();
}
}
var result = LogoutCommand.Run(request, redirectUrl, Options);
if (!result.HandledResult)
{
result.Apply(Context, Options.DataProtector);
}
}
await AugmentAuthenticationGrantWithLogoutClaims(Context);
}
public override async Task<bool> InvokeAsync()
{
var Saml2Path = new PathString(Options.SPOptions.ModulePath);
PathString remainingPath;
if (Request.Path.StartsWithSegments(Saml2Path, out remainingPath))
{
if (remainingPath == new PathString("/" + CommandFactory.AcsCommandName))
{
var ticket = (MultipleIdentityAuthenticationTicket)await AuthenticateAsync();
if (ticket.Identities.Any())
{
Context.Authentication.SignIn(ticket.Properties, ticket.Identities.ToArray());
// No need to redirect here. Command result is applied in AuthenticateCoreAsync.
}
else
{
Response.Redirect(ticket.Properties.RedirectUri);
}
return true;
}
try
{
var result = CommandFactory.GetCommand(remainingPath.Value)
.Run(await Context.ToHttpRequestData(Options.DataProtector.Unprotect), Options);
if (!result.HandledResult)
{
result.Apply(Context, Options.DataProtector);
}
return true;
}
catch(Exception ex)
{
Options.SPOptions.Logger.WriteError("Error in Saml2 for " + Request.Path, ex);
throw;
}
}
return false;
}
private async Task AugmentAuthenticationGrantWithLogoutClaims(IOwinContext context)
{
var grant = context.Authentication.AuthenticationResponseGrant;
var externalIdentity = await context.Authentication.AuthenticateAsync(Options.SignInAsAuthenticationType);
var sessionIdClaim = externalIdentity?.Identity.FindFirst(Saml2ClaimTypes.SessionIndex);
var externalLogutNameIdClaim = externalIdentity?.Identity.FindFirst(Saml2ClaimTypes.LogoutNameIdentifier);
if (grant == null || externalIdentity == null || sessionIdClaim == null || externalLogutNameIdClaim == null)
{
return;
}
// Need to create new claims because the claim has a back pointer
// to the identity it belongs to.
grant.Identity.AddClaim(new Claim(
sessionIdClaim.Type,
sessionIdClaim.Value,
sessionIdClaim.ValueType,
sessionIdClaim.Issuer));
grant.Identity.AddClaim(new Claim(
externalLogutNameIdClaim.Type,
externalLogutNameIdClaim.Value,
externalLogutNameIdClaim.ValueType,
externalLogutNameIdClaim.Issuer));
}
}
}