| | | 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 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | public abstract class SecurityToken |
| | | 10 | | { |
| | | 11 | | public abstract string Id { get; } |
| | | 12 | | public abstract ReadOnlyCollection<SecurityKey> SecurityKeys { get; } |
| | | 13 | | public abstract DateTime ValidFrom { get; } |
| | | 14 | | public abstract DateTime ValidTo { get; } |
| | | 15 | | |
| | | 16 | | public virtual bool CanCreateKeyIdentifierClause<T>() where T : SecurityKeyIdentifierClause |
| | | 17 | | { |
| | 0 | 18 | | return ((typeof(T) == typeof(LocalIdKeyIdentifierClause)) && CanCreateLocalKeyIdentifierClause()); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public virtual T CreateKeyIdentifierClause<T>() where T : SecurityKeyIdentifierClause |
| | | 22 | | { |
| | 10 | 23 | | if ((typeof(T) == typeof(LocalIdKeyIdentifierClause)) && CanCreateLocalKeyIdentifierClause()) |
| | | 24 | | { |
| | 10 | 25 | | return new LocalIdKeyIdentifierClause(Id, GetType()) as T; |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 29 | | SR.Format(SR.TokenDoesNotSupportKeyIdentifierClauseCreation, GetType().Name, typeof(T).Name))); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public virtual bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 33 | | { |
| | 23 | 34 | | if (keyIdentifierClause is LocalIdKeyIdentifierClause localKeyIdentifierClause) |
| | | 35 | | { |
| | 23 | 36 | | return localKeyIdentifierClause.Matches(Id, GetType()); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | return false; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public virtual SecurityKey ResolveKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 43 | | { |
| | 1 | 44 | | if (SecurityKeys.Count != 0 && MatchesKeyIdentifierClause(keyIdentifierClause)) |
| | | 45 | | { |
| | 1 | 46 | | return SecurityKeys[0]; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | return null; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private bool CanCreateLocalKeyIdentifierClause() |
| | | 53 | | { |
| | 10 | 54 | | return (Id != null); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | } |