| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | |
| | | 8 | | namespace Azure.Security.KeyVault.Keys |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="KeyVaultKey"/> is the resource consisting of a value and its <see cref="Properties"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public class KeyVaultKey : IJsonDeserializable |
| | | 14 | | { |
| | | 15 | | private const string KeyPropertyName = "key"; |
| | | 16 | | |
| | 302 | 17 | | internal KeyVaultKey(KeyProperties properties = null) |
| | | 18 | | { |
| | 302 | 19 | | Properties = properties ?? new KeyProperties(); |
| | 302 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="KeyVaultKey"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="name">The name of the key.</param> |
| | | 26 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | | 27 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| | 604 | 28 | | public KeyVaultKey(string name) |
| | | 29 | | { |
| | 604 | 30 | | Properties = new KeyProperties(name); |
| | 604 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the key identifier. |
| | | 35 | | /// </summary> |
| | 520 | 36 | | public Uri Id => Properties.Id; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the name of the key. |
| | | 40 | | /// </summary> |
| | 740 | 41 | | public string Name => Properties.Name; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the cryptographic key, the key type, and the operations you can perform using the key. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <remarks> |
| | | 47 | | /// See http://tools.ietf.org/html/draft-ietf-jose-json-web-key-18 for specifications of a JSON web key. |
| | | 48 | | /// </remarks> |
| | 3612 | 49 | | public JsonWebKey Key { get; internal set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the <see cref="KeyType"/> for this <see cref="JsonWebKey"/>. |
| | | 53 | | /// </summary> |
| | 24 | 54 | | public KeyType KeyType => Key.KeyType; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the operations you can perform using the key. |
| | | 58 | | /// </summary> |
| | 60 | 59 | | public IReadOnlyCollection<KeyOperation> KeyOperations => Key.KeyOps; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets additional properties of the <see cref="KeyVaultKey"/>. |
| | | 63 | | /// </summary> |
| | 3706 | 64 | | public KeyProperties Properties { get; } |
| | | 65 | | |
| | | 66 | | internal virtual void ReadProperty(JsonProperty prop) |
| | | 67 | | { |
| | 1376 | 68 | | switch (prop.Name) |
| | | 69 | | { |
| | | 70 | | case KeyPropertyName: |
| | 676 | 71 | | Key = new JsonWebKey(); |
| | 676 | 72 | | Key.ReadProperties(prop.Value); |
| | | 73 | | |
| | 676 | 74 | | Uri id = new Uri(Key.Id); |
| | 676 | 75 | | Properties.ParseId(id); |
| | 676 | 76 | | break; |
| | | 77 | | |
| | | 78 | | default: |
| | 700 | 79 | | Properties.ReadProperty(prop); |
| | | 80 | | break; |
| | | 81 | | } |
| | 700 | 82 | | } |
| | | 83 | | |
| | | 84 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | | 85 | | { |
| | 4600 | 86 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 87 | | { |
| | 1604 | 88 | | ReadProperty(prop); |
| | | 89 | | } |
| | 696 | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |