| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | |
|
| | 7 | | namespace Azure.Security.KeyVault |
| | 8 | | { |
| | 9 | | internal struct KeyVaultIdentifier |
| | 10 | | { |
| | 11 | | public const string SecretsCollection = "secrets"; |
| | 12 | | public const string KeysCollection = "keys"; |
| | 13 | | public const string CertificatesCollection = "certificates"; |
| | 14 | |
|
| 0 | 15 | | public Uri Id { get; private set; } |
| | 16 | |
|
| 36 | 17 | | public Uri VaultUri { get; set; } |
| | 18 | |
|
| 36 | 19 | | public string Name { get; set; } |
| | 20 | |
|
| 60 | 21 | | public string Collection { get; set; } |
| | 22 | |
|
| 36 | 23 | | public string Version { get; set; } |
| | 24 | |
|
| | 25 | | public static KeyVaultIdentifier Parse(string collection, Uri id) |
| | 26 | | { |
| 0 | 27 | | KeyVaultIdentifier identifier = Parse(id); |
| | 28 | |
|
| 0 | 29 | | if (!string.Equals(identifier.Collection, collection + "/", StringComparison.OrdinalIgnoreCase)) |
| 0 | 30 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 31 | |
|
| 0 | 32 | | return identifier; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static KeyVaultIdentifier Parse(Uri id) |
| | 36 | | { |
| | 37 | | // We expect an identifier with either 3 or 4 segments: host + collection + name [+ version] |
| 32 | 38 | | if (id.Segments.Length != 3 && id.Segments.Length != 4) |
| 0 | 39 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 40 | |
|
| 32 | 41 | | KeyVaultIdentifier identifier = new KeyVaultIdentifier |
| 32 | 42 | | { |
| 32 | 43 | |
|
| 32 | 44 | | Id = id, |
| 32 | 45 | | VaultUri = new Uri($"{id.Scheme}://{id.Authority}"), |
| 32 | 46 | | Collection = id.Segments[1].Trim('/'), |
| 32 | 47 | | Name = id.Segments[2].Trim('/'), |
| 32 | 48 | | Version = (id.Segments.Length == 4) ? id.Segments[3].TrimEnd('/') : null |
| 32 | 49 | | }; |
| | 50 | |
|
| 32 | 51 | | return identifier; |
| | 52 | | } |
| | 53 | | } |
| | 54 | | } |