| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | using CoreWCF.Security; |
| | | 8 | | using MSIdentitySAML2 = Microsoft.IdentityModel.Tokens.Saml2; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// A security token backed by a SAML2 assertion. |
| | | 14 | | /// </summary> |
| | | 15 | | public class Saml2SecurityToken : SecurityToken |
| | | 16 | | { |
| | | 17 | | private readonly ReadOnlyCollection<SecurityKey> _keys; |
| | | 18 | | |
| | 2 | 19 | | protected Saml2SecurityToken() |
| | | 20 | | { |
| | 2 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | public Saml2SecurityToken(MSIdentitySAML2.Saml2SecurityToken saml2SecurityToken, ReadOnlyCollection<SecurityKey> |
| | | 24 | | { |
| | | 25 | | Fx.Assert(saml2SecurityToken != null, "Saml2SecurityToken can't be null"); |
| | 0 | 26 | | WrappedSaml2SecurityToken = saml2SecurityToken; |
| | 0 | 27 | | Assertion = WrappedSaml2SecurityToken.Assertion; |
| | 0 | 28 | | _keys = keys; |
| | 0 | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public Saml2SecurityToken(MSIdentitySAML2.Saml2Assertion assertion) |
| | | 32 | | { |
| | 0 | 33 | | Initialize(assertion); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static explicit operator MSIdentitySAML2.Saml2SecurityToken(Saml2SecurityToken samlSecurityToken) |
| | | 37 | | { |
| | 0 | 38 | | return samlSecurityToken.WrappedSaml2SecurityToken; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | protected void Initialize(MSIdentitySAML2.Saml2Assertion assertion) |
| | | 42 | | { |
| | 0 | 43 | | Assertion = assertion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(assertion)) |
| | 0 | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | internal string AssertionXML { get; set; } |
| | | 47 | | |
| | 0 | 48 | | public override string Id => Assertion.Id.Value; |
| | | 49 | | |
| | 0 | 50 | | internal MSIdentitySAML2.Saml2SecurityToken WrappedSaml2SecurityToken { get; } |
| | | 51 | | |
| | 1 | 52 | | public MSIdentitySAML2.Saml2Assertion Assertion { get; private set; } |
| | | 53 | | |
| | 0 | 54 | | internal SecurityToken SigningToken { get; set; } |
| | | 55 | | |
| | | 56 | | public override DateTime ValidFrom |
| | | 57 | | { |
| | | 58 | | get |
| | | 59 | | { |
| | 0 | 60 | | if (Assertion.Conditions != null && Assertion.Conditions.NotBefore.HasValue) |
| | | 61 | | { |
| | 0 | 62 | | return Assertion.Conditions.NotBefore.Value; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return SecurityUtils.MinUtcDateTime; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public override DateTime ValidTo |
| | | 70 | | { |
| | | 71 | | get |
| | | 72 | | { |
| | 0 | 73 | | if (Assertion.Conditions != null && Assertion.Conditions.NotOnOrAfter.HasValue) |
| | | 74 | | { |
| | 0 | 75 | | return Assertion.Conditions.NotOnOrAfter.Value; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | return SecurityUtils.MaxUtcDateTime; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the collection of <see cref="SecurityKey"/> contained in this token. |
| | | 84 | | /// </summary> |
| | 0 | 85 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys => _keys; |
| | | 86 | | } |
| | | 87 | | } |