| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace Azure.Security.KeyVault.Keys |
| | | 8 | | { |
| | | 9 | | internal struct KeyAttributes |
| | | 10 | | { |
| | | 11 | | private const string EnabledPropertyName = "enabled"; |
| | | 12 | | private const string NotBeforePropertyName = "nbf"; |
| | | 13 | | private const string ExpiresPropertyName = "exp"; |
| | | 14 | | private const string CreatedPropertyName = "created"; |
| | | 15 | | private const string UpdatedPropertyName = "updated"; |
| | | 16 | | private const string RecoverableDaysPropertyName = "recoverableDays"; |
| | | 17 | | private const string RecoveryLevelPropertyName = "recoveryLevel"; |
| | | 18 | | |
| | 2 | 19 | | private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode(EnabledPropertyName) |
| | 2 | 20 | | private static readonly JsonEncodedText s_notBeforePropertyNameBytes = JsonEncodedText.Encode(NotBeforePropertyN |
| | 2 | 21 | | private static readonly JsonEncodedText s_expiresPropertyNameBytes = JsonEncodedText.Encode(ExpiresPropertyName) |
| | | 22 | | |
| | 1472 | 23 | | public bool? Enabled { get; set; } |
| | | 24 | | |
| | 1040 | 25 | | public DateTimeOffset? NotBefore { get; set; } |
| | | 26 | | |
| | 1206 | 27 | | public DateTimeOffset? ExpiresOn { get; set; } |
| | | 28 | | |
| | 672 | 29 | | public DateTimeOffset? CreatedOn { get; internal set; } |
| | | 30 | | |
| | 0 | 31 | | public DateTimeOffset? UpdatedOn { get; internal set; } |
| | | 32 | | |
| | 202 | 33 | | public int? RecoverableDays { get; internal set; } |
| | | 34 | | |
| | 1020 | 35 | | public string RecoveryLevel { get; internal set; } |
| | | 36 | | |
| | 36 | 37 | | internal bool ShouldSerialize => Enabled.HasValue && NotBefore.HasValue && ExpiresOn.HasValue; |
| | | 38 | | |
| | | 39 | | internal void ReadProperties(JsonElement json) |
| | | 40 | | { |
| | 7472 | 41 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 42 | | { |
| | 3044 | 43 | | switch (prop.Name) |
| | | 44 | | { |
| | | 45 | | case EnabledPropertyName: |
| | 688 | 46 | | Enabled = prop.Value.GetBoolean(); |
| | 688 | 47 | | break; |
| | | 48 | | case NotBeforePropertyName: |
| | 8 | 49 | | NotBefore = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 8 | 50 | | break; |
| | | 51 | | case ExpiresPropertyName: |
| | 52 | 52 | | ExpiresOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 52 | 53 | | break; |
| | | 54 | | case CreatedPropertyName: |
| | 688 | 55 | | CreatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 688 | 56 | | break; |
| | | 57 | | case UpdatedPropertyName: |
| | 688 | 58 | | UpdatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 688 | 59 | | break; |
| | | 60 | | case RecoverableDaysPropertyName: |
| | 224 | 61 | | RecoverableDays = prop.Value.GetInt32(); |
| | 224 | 62 | | break; |
| | | 63 | | case RecoveryLevelPropertyName: |
| | 688 | 64 | | RecoveryLevel = prop.Value.GetString(); |
| | | 65 | | break; |
| | | 66 | | } |
| | | 67 | | } |
| | 692 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal void WriteProperties(Utf8JsonWriter json) |
| | | 71 | | { |
| | 76 | 72 | | if (Enabled.HasValue) |
| | | 73 | | { |
| | 76 | 74 | | json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); |
| | | 75 | | } |
| | | 76 | | |
| | 76 | 77 | | if (NotBefore.HasValue) |
| | | 78 | | { |
| | 4 | 79 | | json.WriteNumber(s_notBeforePropertyNameBytes, NotBefore.Value.ToUnixTimeSeconds()); |
| | | 80 | | } |
| | | 81 | | |
| | 76 | 82 | | if (ExpiresOn.HasValue) |
| | | 83 | | { |
| | 40 | 84 | | json.WriteNumber(s_expiresPropertyNameBytes, ExpiresOn.Value.ToUnixTimeSeconds()); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // Created is read-only don't serialize |
| | | 88 | | // Updated is read-only don't serialize |
| | | 89 | | // RecoverableDays is read-only don't serialize |
| | | 90 | | // RecoveryLevel is read-only don't serialize |
| | 76 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |