| | | 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.IdentityModel; |
| | | 7 | | using CoreWCF.IdentityModel.Tokens; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security.Tokens |
| | | 10 | | { |
| | | 11 | | public class BinarySecretSecurityToken : SecurityToken |
| | | 12 | | { |
| | | 13 | | private readonly string _id; |
| | | 14 | | private readonly DateTime _effectiveTime; |
| | | 15 | | private readonly byte[] _key; |
| | | 16 | | private readonly ReadOnlyCollection<SecurityKey> _securityKeys; |
| | | 17 | | |
| | | 18 | | public BinarySecretSecurityToken(int keySizeInBits) |
| | 0 | 19 | | : this(SecurityUniqueId.Create().Value, keySizeInBits) |
| | | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public BinarySecretSecurityToken(string id, int keySizeInBits) |
| | 0 | 24 | | : this(id, keySizeInBits, true) |
| | | 25 | | { |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public BinarySecretSecurityToken(byte[] key) |
| | 2 | 29 | | : this(SecurityUniqueId.Create().Value, key) |
| | | 30 | | { |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | public BinarySecretSecurityToken(string id, byte[] key) |
| | 2 | 34 | | : this(id, key, true) |
| | | 35 | | { |
| | 2 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | protected BinarySecretSecurityToken(string id, int keySizeInBits, bool allowCrypto) |
| | | 39 | | { |
| | 0 | 40 | | throw new NotImplementedException(); |
| | | 41 | | } |
| | | 42 | | |
| | 22 | 43 | | protected BinarySecretSecurityToken(string id, byte[] key, bool allowCrypto) |
| | | 44 | | { |
| | 22 | 45 | | if (key == null) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 48 | | } |
| | | 49 | | |
| | 22 | 50 | | _id = id; |
| | 22 | 51 | | _effectiveTime = DateTime.UtcNow; |
| | 22 | 52 | | _key = new byte[key.Length]; |
| | 22 | 53 | | Buffer.BlockCopy(key, 0, _key, 0, key.Length); |
| | 22 | 54 | | if (allowCrypto) |
| | | 55 | | { |
| | 2 | 56 | | _securityKeys = SecurityUtils.CreateSymmetricSecurityKeys(_key); |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 20 | 60 | | _securityKeys = EmptyReadOnlyCollection<SecurityKey>.Instance; |
| | | 61 | | } |
| | 20 | 62 | | } |
| | | 63 | | |
| | | 64 | | public override string Id |
| | | 65 | | { |
| | 20 | 66 | | get { return _id; } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public override DateTime ValidFrom |
| | | 70 | | { |
| | 0 | 71 | | get { return _effectiveTime; } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public override DateTime ValidTo |
| | | 75 | | { |
| | | 76 | | // Never expire |
| | 0 | 77 | | get { return DateTime.MaxValue; } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public int KeySize |
| | | 81 | | { |
| | 0 | 82 | | get { return (_key.Length * 8); } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys |
| | | 86 | | { |
| | 2 | 87 | | get { return _securityKeys; } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public byte[] GetKeyBytes() |
| | | 91 | | { |
| | 20 | 92 | | return SecurityUtils.CloneBuffer(_key); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |