forked from wictorwilen/SharePointContextSaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharePointContextSaml.cs
More file actions
552 lines (470 loc) · 22.4 KB
/
SharePointContextSaml.cs
File metadata and controls
552 lines (470 loc) · 22.4 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
///////////////////////////////////////////
// SharePointContextSaml.cs
//
// Authors and credits:
// Wictor Wilén
// Steve Peschka
//
// Download:
// https://github.com/wictorwilen/SharePointContextSaml
//
// License:
// Copyright 2014 Wictor Wilén
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
///////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Web;
using System.Web.Configuration;
using Microsoft.SharePoint.Client;
using Microsoft.IdentityModel.S2S.Tokens;
// TODO: Replace this namespace with your web project default namespace
namespace Replace.This.With.Your.Custom.Namespace
{
/// <summary>
/// TokenHelper.cs extensions
/// </summary>
/// <remarks>
/// You need to modify the default TokenHelper.cs class declaration and add the partial keyword
/// </remarks>
public static partial class TokenHelper
{
/// <summary>
/// Identity Claim Type options
/// </summary>
/// <remarks>
/// Configured in web.config appSettings using the setting <c>spsaml:IdentityClaimType</c>
/// </remarks>
/// <example>
/// <code>
/// <add key="spsaml:IdentityClaimType" value="SMTP"/>
/// </code>
/// </example>
public enum IdentityClaimType
{
/// <summary>
/// Use e-mail address as identity claim
/// </summary>
SMTP,
/// <summary>
/// Use UPN as identity claim
/// </summary>
UPN,
/// <summary>
/// Use SIP address as identity claim
/// </summary>
SIP
}
/// <summary>
/// Claim provider types
/// </summary>
public enum ClaimProviderType
{
SAML,
FBA //NOTE: Not tested at all as of now
}
private static readonly string TrustedProviderName = WebConfigurationManager.AppSettings.Get("spsaml:TrustedProviderName");
private static readonly string MembershipProviderName = WebConfigurationManager.AppSettings.Get("spsaml:MembershipProviderName");
public static readonly IdentityClaimType DefaultIdentityClaimType = (IdentityClaimType)Enum.Parse(typeof(IdentityClaimType), WebConfigurationManager.AppSettings.Get("spsaml:IdentityClaimType"));
public static readonly ClaimProviderType DefaultClaimProviderType = (ClaimProviderType)Enum.Parse(typeof(ClaimProviderType), WebConfigurationManager.AppSettings.Get("spsaml:ClaimProviderType"));
private const string CLAIM_TYPE_EMAIL = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";
private const string CLAIM_TYPE_UPN = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn";
private const string CLAIM_TYPE_SIP = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sip";
private const string CLAIMS_ID_TYPE_EMAIL = "smtp";
private const string CLAIMS_ID_TYPE_UPN = "upn";
private const string CLAIMS_ID_TYPE_SIP = "sip";
private const int TokenLifetimeMinutes = 1000000;
//simple class used to hold instance variables for ID claim values
private class ClaimsUserIdClaim
{
public string ClaimsIdClaimType { get; set; }
public string ClaimsIdClaimValue { get; set; }
}
/// <summary>
/// Retrieves an S2S client context with an access token signed by the application's private certificate on
/// behalf of the specified Claims Identity and intended for application at the targetApplicationUri using the
/// targetRealm. If no Realm is specified in web.config, an auth challenge will be issued to the
/// targetApplicationUri to discover it.
/// </summary>
/// <param name="targetApplicationUri">Url of the target SharePoint site</param>
/// <param name="identity">Identity of the user on whose behalf to create the access token; use HttpContext.Current.User</param>
/// <param name="UserIdentityClaimType">The claim type that is used as the identity claim for the user</param>
/// <param name="IdentityClaimProviderType">The type of identity provider being used</param>
/// <param name="UseAppOnlyClaim">Use an App Only claim</param>
/// <returns>A ClientContext using an access token with an audience of the target application</returns>
public static ClientContext GetS2SClientContextWithClaimsIdentity(
Uri targetApplicationUri,
ClaimsIdentity identity,
IdentityClaimType UserIdentityClaimType,
ClaimProviderType IdentityClaimProviderType,
bool UseAppOnlyClaim)
{
string realm = string.IsNullOrEmpty(Realm) ? GetRealmFromTargetUrl(targetApplicationUri) : Realm;
string accessToken = GetS2SClaimsAccessTokenWithClaims(
targetApplicationUri,
identity,
UserIdentityClaimType,
IdentityClaimProviderType,
UseAppOnlyClaim);
return GetClientContextWithAccessToken(targetApplicationUri.ToString(), accessToken);
}
/// <summary>
/// Retrieves an S2S access token signed by the application's private certificate on
/// behalf of the specified Claims Identity and intended for application at the targetApplicationUri using the
/// targetRealm. If no Realm is specified in web.config, an auth challenge will be issued to the
/// targetApplicationUri to discover it.
/// </summary>
/// <param name="targetApplicationUri">Url of the target SharePoint site</param>
/// <param name="identity">Identity of the user on whose behalf to create the access token; use HttpContext.Current.User</param>
/// <param name="UserIdentityClaimType">The claim type that is used as the identity claim for the user</param>
/// <param name="IdentityClaimProviderType">The type of identity provider being used</param>
/// <param name="UseAppOnlyClaim">Use an App Only claim</param>
/// <returns></returns>
public static string GetS2SClaimsAccessTokenWithClaims(
Uri targetApplicationUri,
ClaimsIdentity identity,
IdentityClaimType UserIdentityClaimType,
ClaimProviderType IdentityClaimProviderType,
bool UseAppOnlyClaim)
{
//get the identity claim info first
TokenHelper.ClaimsUserIdClaim id = null;
if (IdentityClaimProviderType == ClaimProviderType.SAML)
id = RetrieveIdentityForSamlClaimsUser(identity, UserIdentityClaimType);
else
{
id = RetrieveIdentityForFbaClaimsUser(identity, UserIdentityClaimType);
}
string realm = string.IsNullOrEmpty(Realm) ? GetRealmFromTargetUrl(targetApplicationUri) : Realm;
JsonWebTokenClaim[] claims = identity != null ? GetClaimsWithClaimsIdentity(identity, UserIdentityClaimType, id, IdentityClaimProviderType) : null;
return IssueToken(
ClientId,
IssuerId,
realm,
SharePointPrincipal,
realm,
targetApplicationUri.Authority,
true,
claims,
UseAppOnlyClaim,
id.ClaimsIdClaimType != CLAIMS_ID_TYPE_UPN,
id.ClaimsIdClaimType,
id.ClaimsIdClaimValue);
}
private static string IssueToken(
string sourceApplication,
string issuerApplication,
string sourceRealm,
string targetApplication,
string targetRealm,
string targetApplicationHostName,
bool trustedForDelegation,
IEnumerable<JsonWebTokenClaim> claims,
bool appOnly = false,
bool addSamlClaim = false,
string samlClaimType = "",
string samlClaimValue = "")
{
if (null == SigningCredentials)
{
throw new InvalidOperationException("SigningCredentials was not initialized");
}
#region Actor token
string issuer = string.IsNullOrEmpty(sourceRealm) ? issuerApplication : string.Format("{0}@{1}", issuerApplication, sourceRealm);
string nameid = string.IsNullOrEmpty(sourceRealm) ? sourceApplication : string.Format("{0}@{1}", sourceApplication, sourceRealm);
string audience = string.Format("{0}/{1}@{2}", targetApplication, targetApplicationHostName, targetRealm);
List<JsonWebTokenClaim> actorClaims = new List<JsonWebTokenClaim>();
actorClaims.Add(new JsonWebTokenClaim(JsonWebTokenConstants.ReservedClaims.NameIdentifier, nameid));
if (trustedForDelegation && !appOnly)
{
actorClaims.Add(new JsonWebTokenClaim(TokenHelper.TrustedForImpersonationClaimType, "true"));
}
if (addSamlClaim)
{
actorClaims.Add(new JsonWebTokenClaim(samlClaimType, samlClaimValue));
}
// Create token
JsonWebSecurityToken actorToken = new JsonWebSecurityToken(
issuer: issuer,
audience: audience,
validFrom: DateTime.UtcNow,
validTo: DateTime.UtcNow.AddMinutes(TokenLifetimeMinutes),
signingCredentials: SigningCredentials,
claims: actorClaims);
string actorTokenString = new JsonWebSecurityTokenHandler().WriteTokenAsString(actorToken);
if (appOnly)
{
// App-only token is the same as actor token for delegated case
return actorTokenString;
}
#endregion Actor token
#region Outer token
List<JsonWebTokenClaim> outerClaims = null == claims ? new List<JsonWebTokenClaim>() : new List<JsonWebTokenClaim>(claims);
outerClaims.Add(new JsonWebTokenClaim(ActorTokenClaimType, actorTokenString));
if (addSamlClaim)
{
outerClaims.Add(new JsonWebTokenClaim(samlClaimType, samlClaimValue));
}
JsonWebSecurityToken jsonToken = new JsonWebSecurityToken(
nameid, // outer token issuer should match actor token nameid
audience,
DateTime.UtcNow,
DateTime.UtcNow.AddMinutes(10),
outerClaims);
string accessToken = new JsonWebSecurityTokenHandler().WriteTokenAsString(jsonToken);
#endregion Outer token
return accessToken;
}
/// <summary>
/// Retrieves the identity for the ClaimsIdentity
/// </summary>
/// <param name="identity">The Claims Identity</param>
/// <param name="SamlIdentityClaimType">The Claims Identity Type</param>
/// <returns></returns>
private static TokenHelper.ClaimsUserIdClaim RetrieveIdentityForSamlClaimsUser(ClaimsIdentity identity, IdentityClaimType SamlIdentityClaimType)
{
TokenHelper.ClaimsUserIdClaim id = new ClaimsUserIdClaim();
try
{
if (identity.IsAuthenticated)
{
//get the claim type we're looking for
string claimType = CLAIM_TYPE_EMAIL;
id.ClaimsIdClaimType = CLAIMS_ID_TYPE_EMAIL;
//since the vast majority of the time the id claim is email, we'll start out with that
//as our default position and only check if that isn't the case
if (SamlIdentityClaimType != IdentityClaimType.SMTP)
{
switch (SamlIdentityClaimType)
{
case IdentityClaimType.UPN:
claimType = CLAIM_TYPE_UPN;
id.ClaimsIdClaimType = CLAIMS_ID_TYPE_UPN;
break;
default:
claimType = CLAIM_TYPE_SIP;
id.ClaimsIdClaimType = CLAIMS_ID_TYPE_SIP;
break;
}
}
foreach (Claim claim in identity.Claims)
{
if (claim.Type == claimType)
{
id.ClaimsIdClaimValue = claim.Value;
break;
}
}
}
}
catch (Exception ex)
{
//not going to do anything here; could look for a missing identity claim but instead will just
//return an empty string
Debug.WriteLine(ex.Message);
}
return id;
}
/// <summary>
/// NOT IMPLEMENTED
/// </summary>
/// <param name="identity"></param>
/// <param name="SamlIdentityClaimType"></param>
/// <returns></returns>
private static TokenHelper.ClaimsUserIdClaim RetrieveIdentityForFbaClaimsUser(ClaimsIdentity identity, IdentityClaimType SamlIdentityClaimType)
{
throw new NotImplementedException();
}
private static JsonWebTokenClaim[] GetClaimsWithClaimsIdentity(ClaimsIdentity indentity, IdentityClaimType SamlIdentityClaimType, TokenHelper.ClaimsUserIdClaim id, ClaimProviderType IdentityClaimProviderType)
{
//if an identity claim was not found, then exit
if (string.IsNullOrEmpty(id.ClaimsIdClaimValue))
return null;
Hashtable claimSet = new Hashtable();
//you always need nii claim, so add that
claimSet.Add("nii", "temp");
//set up the nii claim and then add the smtp or sip claim separately
if (IdentityClaimProviderType == ClaimProviderType.SAML)
claimSet["nii"] = "trusted:" + TrustedProviderName.ToLower(); //was urn:office:idp:trusted:, but this does not seem to align with what SPIdentityClaimMapper uses
else
claimSet["nii"] = "urn:office:idp:forms:" + MembershipProviderName.ToLower();
//plug in UPN claim if we're using that
if (id.ClaimsIdClaimType == CLAIMS_ID_TYPE_UPN)
claimSet.Add("upn", id.ClaimsIdClaimValue.ToLower());
//now create the JsonWebTokenClaim array
List<JsonWebTokenClaim> claimList = new List<JsonWebTokenClaim>();
foreach (string key in claimSet.Keys)
{
claimList.Add(new JsonWebTokenClaim(key, (string)claimSet[key]));
}
return claimList.ToArray();
}
}
#region HighTrust with SAML
/// <summary>
/// Encapsulates all the information from SharePoint in HighTrust mode with SAML Claims.
/// </summary>
public class SharePointHighTrustSamlContext : SharePointContext
{
private readonly ClaimsIdentity logonUserIdentity;
/// <summary>
/// The Claims identity for the current user.
/// </summary>
public ClaimsIdentity LogonUserIdentity
{
get { return this.logonUserIdentity; }
}
public override string UserAccessTokenForSPHost
{
get
{
if (this.SPHostUrl == null)
{
return null;
}
return GetAccessTokenString(ref this.userAccessTokenForSPHost,
() => TokenHelper.GetS2SClaimsAccessTokenWithClaims(
this.SPHostUrl,
this.LogonUserIdentity,
TokenHelper.DefaultIdentityClaimType,
TokenHelper.DefaultClaimProviderType,
false
));
}
}
public override string UserAccessTokenForSPAppWeb
{
get
{
if (this.SPAppWebUrl == null)
{
return null;
}
return GetAccessTokenString(ref this.userAccessTokenForSPAppWeb,
() => TokenHelper.GetS2SClaimsAccessTokenWithClaims(
this.SPAppWebUrl,
this.LogonUserIdentity,
TokenHelper.DefaultIdentityClaimType,
TokenHelper.DefaultClaimProviderType,
false
));
}
}
public override string AppOnlyAccessTokenForSPHost
{
get
{
return GetAccessTokenString(ref this.appOnlyAccessTokenForSPHost,
() => TokenHelper.GetS2SClaimsAccessTokenWithClaims(
this.SPAppWebUrl,
null,
TokenHelper.DefaultIdentityClaimType,
TokenHelper.DefaultClaimProviderType,
false));
}
}
public override string AppOnlyAccessTokenForSPAppWeb
{
get
{
if (this.SPAppWebUrl == null)
{
return null;
}
return GetAccessTokenString(ref this.appOnlyAccessTokenForSPAppWeb,
() => TokenHelper.GetS2SClaimsAccessTokenWithClaims(
this.SPAppWebUrl,
null,
TokenHelper.DefaultIdentityClaimType,
TokenHelper.DefaultClaimProviderType,
false));
}
}
public SharePointHighTrustSamlContext(Uri spHostUrl, Uri spAppWebUrl, string spLanguage, string spClientTag, string spProductNumber, ClaimsIdentity logonUserIdentity)
: base(spHostUrl, spAppWebUrl, spLanguage, spClientTag, spProductNumber)
{
if (logonUserIdentity == null)
{
throw new ArgumentNullException("logonUserIdentity");
}
this.logonUserIdentity = logonUserIdentity;
}
/// <summary>
/// Ensures the access token is valid and returns it.
/// </summary>
/// <param name="accessToken">The access token to verify.</param>
/// <param name="tokenRenewalHandler">The token renewal handler.</param>
/// <returns>The access token string.</returns>
private static string GetAccessTokenString(ref Tuple<string, DateTime> accessToken, Func<string> tokenRenewalHandler)
{
RenewAccessTokenIfNeeded(ref accessToken, tokenRenewalHandler);
return IsAccessTokenValid(accessToken) ? accessToken.Item1 : null;
}
/// <summary>
/// Renews the access token if it is not valid.
/// </summary>
/// <param name="accessToken">The access token to renew.</param>
/// <param name="tokenRenewalHandler">The token renewal handler.</param>
private static void RenewAccessTokenIfNeeded(ref Tuple<string, DateTime> accessToken, Func<string> tokenRenewalHandler)
{
if (IsAccessTokenValid(accessToken))
{
return;
}
DateTime expiresOn = DateTime.UtcNow.Add(TokenHelper.HighTrustAccessTokenLifetime);
if (TokenHelper.HighTrustAccessTokenLifetime > AccessTokenLifetimeTolerance)
{
// Make the access token get renewed a bit earlier than the time when it expires
// so that the calls to SharePoint with it will have enough time to complete successfully.
expiresOn -= AccessTokenLifetimeTolerance;
}
accessToken = Tuple.Create(tokenRenewalHandler(), expiresOn);
}
}
/// <summary>
/// Default provider for SharePointHighTrustSamlContext.
/// </summary>
public class SharePointHighTrustSamlContextProvider : SharePointHighTrustContextProvider
{
protected override SharePointContext CreateSharePointContext(Uri spHostUrl, Uri spAppWebUrl, string spLanguage, string spClientTag, string spProductNumber, HttpRequestBase httpRequest)
{
ClaimsIdentity logonUserIdentity = HttpContext.Current.User.Identity as ClaimsIdentity;
if (logonUserIdentity == null || !logonUserIdentity.IsAuthenticated )
{
return null;
}
return new SharePointHighTrustSamlContext(spHostUrl, spAppWebUrl, spLanguage, spClientTag, spProductNumber, logonUserIdentity);
}
protected override bool ValidateSharePointContext(SharePointContext spContext, HttpContextBase httpContext)
{
SharePointHighTrustSamlContext spHighTrustContext = spContext as SharePointHighTrustSamlContext;
if (spHighTrustContext != null)
{
Uri spHostUrl = SharePointContext.GetSPHostUrl(httpContext.Request);
ClaimsIdentity logonUserIdentity = httpContext.Request.LogonUserIdentity;
return spHostUrl == spHighTrustContext.SPHostUrl &&
logonUserIdentity != null &&
logonUserIdentity.IsAuthenticated;
}
return false;
}
}
#endregion HighTrust
}