| | | 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.Globalization; |
| | | 6 | | using CoreWCF.IdentityModel.Selectors; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | public sealed class UserNamePasswordServiceCredential |
| | | 12 | | { |
| | | 13 | | internal const UserNamePasswordValidationMode DefaultUserNamePasswordValidationMode = UserNamePasswordValidation |
| | | 14 | | internal const bool DefaultCacheLogonTokens = false; |
| | | 15 | | internal const int DefaultMaxCachedLogonTokens = 128; |
| | | 16 | | internal const string DefaultCachedLogonTokenLifetimeString = "00:15:00"; |
| | 6 | 17 | | internal static readonly TimeSpan DefaultCachedLogonTokenLifetime = TimeSpan.Parse(DefaultCachedLogonTokenLifeti |
| | | 18 | | private UserNamePasswordValidationMode _validationMode = DefaultUserNamePasswordValidationMode; |
| | | 19 | | private UserNamePasswordValidator _validator; |
| | | 20 | | private readonly object _membershipProvider; |
| | 118 | 21 | | private bool _includeWindowsGroups = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims; |
| | | 22 | | private bool _cacheLogonTokens = DefaultCacheLogonTokens; |
| | 118 | 23 | | private int _maxCachedLogonTokens = DefaultMaxCachedLogonTokens; |
| | 118 | 24 | | private TimeSpan _cachedLogonTokenLifetime = DefaultCachedLogonTokenLifetime; |
| | | 25 | | private bool _isReadOnly; |
| | | 26 | | |
| | 52 | 27 | | internal UserNamePasswordServiceCredential() |
| | | 28 | | { |
| | | 29 | | // empty |
| | 52 | 30 | | } |
| | | 31 | | |
| | 66 | 32 | | internal UserNamePasswordServiceCredential(UserNamePasswordServiceCredential other) |
| | | 33 | | { |
| | 66 | 34 | | _includeWindowsGroups = other._includeWindowsGroups; |
| | 66 | 35 | | _membershipProvider = other._membershipProvider; |
| | 66 | 36 | | _validationMode = other._validationMode; |
| | 66 | 37 | | _validator = other._validator; |
| | 66 | 38 | | _cacheLogonTokens = other._cacheLogonTokens; |
| | 66 | 39 | | _maxCachedLogonTokens = other._maxCachedLogonTokens; |
| | 66 | 40 | | _cachedLogonTokenLifetime = other._cachedLogonTokenLifetime; |
| | 66 | 41 | | _isReadOnly = other._isReadOnly; |
| | 66 | 42 | | } |
| | | 43 | | |
| | | 44 | | public UserNamePasswordValidationMode UserNamePasswordValidationMode |
| | | 45 | | { |
| | | 46 | | get |
| | | 47 | | { |
| | 19 | 48 | | return _validationMode; |
| | | 49 | | } |
| | | 50 | | set |
| | | 51 | | { |
| | 19 | 52 | | UserNamePasswordValidationModeHelper.Validate(value); |
| | 19 | 53 | | ThrowIfImmutable(); |
| | 19 | 54 | | if (value == UserNamePasswordValidationMode.MembershipProvider) |
| | | 55 | | { |
| | 0 | 56 | | throw new PlatformNotSupportedException("MembershipProvider not supported"); |
| | | 57 | | } |
| | | 58 | | |
| | 19 | 59 | | _validationMode = value; |
| | 19 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public UserNamePasswordValidator CustomUserNamePasswordValidator |
| | | 64 | | { |
| | | 65 | | get |
| | | 66 | | { |
| | 0 | 67 | | return _validator; |
| | | 68 | | } |
| | | 69 | | set |
| | | 70 | | { |
| | 19 | 71 | | ThrowIfImmutable(); |
| | 19 | 72 | | _validator = value; |
| | 19 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public bool IncludeWindowsGroups |
| | | 77 | | { |
| | | 78 | | get |
| | | 79 | | { |
| | 0 | 80 | | return _includeWindowsGroups; |
| | | 81 | | } |
| | | 82 | | set |
| | | 83 | | { |
| | 0 | 84 | | ThrowIfImmutable(); |
| | 0 | 85 | | _includeWindowsGroups = value; |
| | 0 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public bool CacheLogonTokens |
| | | 90 | | { |
| | | 91 | | get |
| | | 92 | | { |
| | 0 | 93 | | return _cacheLogonTokens; |
| | | 94 | | } |
| | | 95 | | set |
| | | 96 | | { |
| | 0 | 97 | | ThrowIfImmutable(); |
| | 0 | 98 | | _cacheLogonTokens = value; |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public int MaxCachedLogonTokens |
| | | 103 | | { |
| | | 104 | | get |
| | | 105 | | { |
| | 0 | 106 | | return _maxCachedLogonTokens; |
| | | 107 | | } |
| | | 108 | | set |
| | | 109 | | { |
| | 0 | 110 | | if (value <= 0) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 113 | | } |
| | 0 | 114 | | ThrowIfImmutable(); |
| | 0 | 115 | | _maxCachedLogonTokens = value; |
| | 0 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public TimeSpan CachedLogonTokenLifetime |
| | | 120 | | { |
| | | 121 | | get |
| | | 122 | | { |
| | 0 | 123 | | return _cachedLogonTokenLifetime; |
| | | 124 | | } |
| | | 125 | | set |
| | | 126 | | { |
| | 0 | 127 | | if (value <= TimeSpan.Zero) |
| | | 128 | | { |
| | 0 | 129 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 133 | | { |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 135 | | SRCommon.SFxTimeoutOutOfRangeTooBig)); |
| | | 136 | | } |
| | 0 | 137 | | ThrowIfImmutable(); |
| | 0 | 138 | | _cachedLogonTokenLifetime = value; |
| | 0 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | internal UserNamePasswordValidator GetUserNamePasswordValidator() |
| | | 143 | | { |
| | 15 | 144 | | if (_validationMode == UserNamePasswordValidationMode.MembershipProvider) |
| | | 145 | | { |
| | 0 | 146 | | throw new PlatformNotSupportedException("MembershipProvider not supported"); |
| | | 147 | | } |
| | 15 | 148 | | else if (_validationMode == UserNamePasswordValidationMode.Custom) |
| | | 149 | | { |
| | 15 | 150 | | if (_validator == null) |
| | | 151 | | { |
| | 0 | 152 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.MissingCu |
| | | 153 | | } |
| | 15 | 154 | | return _validator; |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | internal void MakeReadOnly() |
| | | 161 | | { |
| | 0 | 162 | | _isReadOnly = true; |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | private void ThrowIfImmutable() |
| | | 166 | | { |
| | 38 | 167 | | if (_isReadOnly) |
| | | 168 | | { |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 170 | | } |
| | 38 | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |