|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.ComponentModel; |
| 6 | + |
| 7 | +namespace Azure.Data.Tables |
| 8 | +{ |
| 9 | + /// <summary> Cloud audiences available for Azure Tables. </summary> |
| 10 | + public readonly struct TableAudience : IEquatable<TableAudience> |
| 11 | + { |
| 12 | + private readonly string _value; |
| 13 | + |
| 14 | + private const string AzureChinaValue = "AzureChina"; |
| 15 | + private const string AzureGovernmentValue = "AzureGov"; |
| 16 | + private const string AzurePublicCloudValue = "AzurePublic"; |
| 17 | + |
| 18 | + private const string AzureStorageChinaValue = "https://storage.azure.cn"; |
| 19 | + private const string AzureStorageGovernmentValue = "https://storage.azure.us"; |
| 20 | + private const string AzureStoragePublicCloudValue = "https://storage.azure.com"; |
| 21 | + private const string AzureCosmosChinaValue = "https://cosmos.azure.cn"; |
| 22 | + private const string AzureCosmosGovernmentValue = "https://cosmos.azure.us"; |
| 23 | + private const string AzureCosmosPublicCloudValue = "https://cosmos.azure.com"; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="TableAudience"/> object. |
| 27 | + /// </summary> |
| 28 | + /// <param name="value">The Microsoft Entra audience to use when forming authorization scopes. |
| 29 | + /// For the Azure Tables service, this value corresponds to a URL that identifies the Azure cloud where the resource is located.</param> |
| 30 | + /// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception> |
| 31 | + /// <remarks>Please use one of the constant members over creating a custom value unless you have special needs for doing so.</remarks> |
| 32 | + public TableAudience(string value) |
| 33 | + { |
| 34 | + Argument.AssertNotNullOrEmpty(value, nameof(value)); |
| 35 | + _value = value; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> The authorization audience used to authenticate with the Azure China cloud. </summary> |
| 39 | + public static TableAudience AzureChina { get; } = new TableAudience(AzureChinaValue); |
| 40 | + |
| 41 | + /// <summary> The authorization audience used to authenticate with the Azure Government cloud. </summary> |
| 42 | + public static TableAudience AzureGovernment { get; } = new TableAudience(AzureGovernmentValue); |
| 43 | + |
| 44 | + /// <summary> The authorization audience used to authenticate with the Azure Public cloud. </summary> |
| 45 | + public static TableAudience AzurePublicCloud { get; } = new TableAudience(AzurePublicCloudValue); |
| 46 | + |
| 47 | + /// <summary> Determines if two <see cref="TableAudience"/> values are the same. </summary> |
| 48 | + public static bool operator ==(TableAudience left, TableAudience right) => left.Equals(right); |
| 49 | + /// <summary> Determines if two <see cref="TableAudience"/> values are not the same. </summary> |
| 50 | + public static bool operator !=(TableAudience left, TableAudience right) => !left.Equals(right); |
| 51 | + /// <summary> Converts a string to a <see cref="TableAudience"/>. </summary> |
| 52 | + public static implicit operator TableAudience(string value) => new TableAudience(value); |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 56 | + public override bool Equals(object obj) => obj is TableAudience other && Equals(other); |
| 57 | + /// <inheritdoc /> |
| 58 | + public bool Equals(TableAudience other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); |
| 59 | + |
| 60 | + /// <inheritdoc /> |
| 61 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 62 | + public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| 63 | + /// <inheritdoc /> |
| 64 | + public override string ToString() => _value; |
| 65 | + |
| 66 | + internal string GetDefaultScope(bool isCosmosEndpoint) |
| 67 | + { |
| 68 | + var audience = _value switch |
| 69 | + { |
| 70 | + AzureChinaValue when isCosmosEndpoint => AzureCosmosChinaValue, |
| 71 | + AzureGovernmentValue when isCosmosEndpoint => AzureCosmosGovernmentValue, |
| 72 | + AzurePublicCloudValue when isCosmosEndpoint => AzureCosmosPublicCloudValue, |
| 73 | + AzureChinaValue => AzureStorageChinaValue, |
| 74 | + AzureGovernmentValue => AzureStorageGovernmentValue, |
| 75 | + AzurePublicCloudValue => AzureStoragePublicCloudValue, |
| 76 | + _ => _value |
| 77 | + }; |
| 78 | + |
| 79 | + if (audience.EndsWith($"/{TableConstants.DefaultScope}", StringComparison.InvariantCultureIgnoreCase)) |
| 80 | + { |
| 81 | + return audience; |
| 82 | + } |
| 83 | + |
| 84 | + return audience.EndsWith("/", StringComparison.InvariantCultureIgnoreCase) |
| 85 | + ? $"{audience}{TableConstants.DefaultScope}" |
| 86 | + : $"{audience}/{TableConstants.DefaultScope}"; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments