| | 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.Secrets |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Represents a Key Vault secret that has been deleted, allowing it to be recovered, if needed. |
| | 11 | | /// </summary> |
| | 12 | | public class DeletedSecret : KeyVaultSecret |
| | 13 | | { |
| | 14 | | private const string RecoveryIdPropertyName = "recoveryId"; |
| | 15 | | private const string DeletedOnPropertyName = "deletedDate"; |
| | 16 | | private const string ScheduledPurgeDatePropertyName = "scheduledPurgeDate"; |
| | 17 | |
|
| 0 | 18 | | private static readonly JsonEncodedText s_recoveryIdPropertyNameBytes = JsonEncodedText.Encode(RecoveryIdPropert |
| 0 | 19 | | private static readonly JsonEncodedText s_deletedOnPropertyNameBytes = JsonEncodedText.Encode(DeletedOnPropertyN |
| 0 | 20 | | private static readonly JsonEncodedText s_scheduledPurgeDatePropertyNameBytes = JsonEncodedText.Encode(Scheduled |
| | 21 | |
|
| | 22 | | private string _recoveryId; |
| | 23 | |
|
| 436 | 24 | | internal DeletedSecret(SecretProperties properties = null) : base(properties) |
| | 25 | | { |
| 436 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets a <see cref="Uri"/> of the deleted secret that can be used to recover it. |
| | 30 | | /// </summary> |
| | 31 | | public Uri RecoveryId |
| | 32 | | { |
| 220 | 33 | | get => _recoveryId is null ? null : new Uri(_recoveryId); |
| 0 | 34 | | internal set => _recoveryId = value?.ToString(); |
| | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the secret was deleted. |
| | 39 | | /// </summary> |
| 444 | 40 | | public DateTimeOffset? DeletedOn { get; internal set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets a <see cref="DateTimeOffset"/> for when the deleted secret will be purged. |
| | 44 | | /// </summary> |
| 444 | 45 | | public DateTimeOffset? ScheduledPurgeDate { get; internal set; } |
| | 46 | |
|
| | 47 | | internal override void ReadProperty(JsonProperty prop) |
| | 48 | | { |
| 2188 | 49 | | switch (prop.Name) |
| | 50 | | { |
| | 51 | | case RecoveryIdPropertyName: |
| 436 | 52 | | _recoveryId = prop.Value.GetString(); |
| 436 | 53 | | break; |
| | 54 | |
|
| | 55 | | case DeletedOnPropertyName: |
| 436 | 56 | | DeletedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 436 | 57 | | break; |
| | 58 | |
|
| | 59 | | case ScheduledPurgeDatePropertyName: |
| 436 | 60 | | ScheduledPurgeDate = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 436 | 61 | | break; |
| | 62 | |
|
| | 63 | | default: |
| 880 | 64 | | base.ReadProperty(prop); |
| | 65 | | break; |
| | 66 | | } |
| 880 | 67 | | } |
| | 68 | |
|
| | 69 | | internal override void WriteProperties(Utf8JsonWriter json) |
| | 70 | | { |
| 0 | 71 | | base.WriteProperties(json); |
| | 72 | |
|
| 0 | 73 | | if (RecoveryId != null) |
| | 74 | | { |
| 0 | 75 | | json.WriteString(s_recoveryIdPropertyNameBytes, RecoveryId.ToString()); |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | if (DeletedOn.HasValue) |
| | 79 | | { |
| 0 | 80 | | json.WriteNumber(s_deletedOnPropertyNameBytes, DeletedOn.Value.ToUnixTimeSeconds()); |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | if (ScheduledPurgeDate.HasValue) |
| | 84 | | { |
| 0 | 85 | | json.WriteNumber(s_scheduledPurgeDatePropertyNameBytes, ScheduledPurgeDate.Value.ToUnixTimeSeconds()); |
| | 86 | | } |
| 0 | 87 | | } |
| | 88 | | } |
| | 89 | | } |