| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections.ObjectModel; |
| | 6 | | using System.Text.Json; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace Azure.Security.KeyVault.Certificates |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A collection of subject alternative names (SANs) for a X.509 certificate. SANs can be DNS entries, emails, or un |
| | 13 | | /// </summary> |
| | 14 | | public class SubjectAlternativeNames : IJsonSerializable, IJsonDeserializable |
| | 15 | | { |
| | 16 | | private const string DnsPropertyName = "dns_names"; |
| | 17 | | private const string EmailsPropertyName = "emails"; |
| | 18 | | private const string UpnsPropertyName = "upns"; |
| | 19 | |
|
| 2 | 20 | | private static readonly JsonEncodedText s_dnsPropertyNameBytes = JsonEncodedText.Encode(DnsPropertyName); |
| 2 | 21 | | private static readonly JsonEncodedText s_emailsPropertyNameBytes = JsonEncodedText.Encode(EmailsPropertyName); |
| 2 | 22 | | private static readonly JsonEncodedText s_upnsPropertyNameBytes = JsonEncodedText.Encode(UpnsPropertyName); |
| | 23 | |
|
| | 24 | | private Collection<string> _dnsNames; |
| | 25 | | private Collection<string> _emails; |
| | 26 | | private Collection<string> _userPrincipalNames; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Initializes a new instance of the <see cref="CertificateContact"/> class. |
| | 30 | | /// </summary> |
| 16 | 31 | | public SubjectAlternativeNames() |
| | 32 | | { |
| 16 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gets a collection of DNS names. |
| | 37 | | /// </summary> |
| 12 | 38 | | public IList<string> DnsNames => LazyInitializer.EnsureInitialized(ref _dnsNames); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets a collection of email addresses. |
| | 42 | | /// </summary> |
| 6 | 43 | | public IList<string> Emails => LazyInitializer.EnsureInitialized(ref _emails); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets a collection of user principal names (UPNs). |
| | 47 | | /// </summary> |
| 4 | 48 | | public IList<string> UserPrincipalNames => LazyInitializer.EnsureInitialized(ref _userPrincipalNames); |
| | 49 | |
|
| 16 | 50 | | internal bool IsEmpty => _dnsNames.IsNullOrEmpty() && _emails.IsNullOrEmpty() && _userPrincipalNames.IsNullOrEmp |
| | 51 | |
|
| | 52 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 53 | | { |
| 0 | 54 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 55 | | { |
| 0 | 56 | | switch (prop.Name) |
| | 57 | | { |
| | 58 | | case DnsPropertyName: |
| 0 | 59 | | foreach (JsonElement element in prop.Value.EnumerateArray()) |
| | 60 | | { |
| 0 | 61 | | DnsNames.Add(element.ToString()); |
| | 62 | | } |
| | 63 | | break; |
| | 64 | |
|
| | 65 | | case EmailsPropertyName: |
| 0 | 66 | | foreach (JsonElement element in prop.Value.EnumerateArray()) |
| | 67 | | { |
| 0 | 68 | | Emails.Add(element.ToString()); |
| | 69 | | } |
| | 70 | | break; |
| | 71 | |
|
| | 72 | | case UpnsPropertyName: |
| 0 | 73 | | foreach (JsonElement element in prop.Value.EnumerateArray()) |
| | 74 | | { |
| 0 | 75 | | UserPrincipalNames.Add(element.ToString()); |
| | 76 | | } |
| | 77 | | break; |
| | 78 | | } |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | 83 | | { |
| 10 | 84 | | if (!_dnsNames.IsNullOrEmpty()) |
| | 85 | | { |
| 4 | 86 | | json.WriteStartArray(s_dnsPropertyNameBytes); |
| 20 | 87 | | foreach (string dnsName in _dnsNames) |
| | 88 | | { |
| 6 | 89 | | json.WriteStringValue(dnsName); |
| | 90 | | } |
| 4 | 91 | | json.WriteEndArray(); |
| | 92 | | } |
| | 93 | |
|
| 10 | 94 | | if (!_emails.IsNullOrEmpty()) |
| | 95 | | { |
| 6 | 96 | | json.WriteStartArray(s_emailsPropertyNameBytes); |
| 24 | 97 | | foreach (string email in _emails) |
| | 98 | | { |
| 6 | 99 | | json.WriteStringValue(email); |
| | 100 | | } |
| 6 | 101 | | json.WriteEndArray(); |
| | 102 | | } |
| | 103 | |
|
| 10 | 104 | | if (!_userPrincipalNames.IsNullOrEmpty()) |
| | 105 | | { |
| 4 | 106 | | json.WriteStartArray(s_upnsPropertyNameBytes); |
| 16 | 107 | | foreach (string userPrincipalName in _userPrincipalNames) |
| | 108 | | { |
| 4 | 109 | | json.WriteStringValue(userPrincipalName); |
| | 110 | | } |
| 4 | 111 | | json.WriteEndArray(); |
| | 112 | | } |
| 10 | 113 | | } |
| | 114 | | } |
| | 115 | | } |