|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Management.Automation; |
| 19 | +using System.Net.Http; |
| 20 | +using System.Threading; |
| 21 | + |
| 22 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 23 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
| 24 | +using Microsoft.Azure.Commands.Common.Authentication.Models; |
| 25 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 26 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Profile.Token |
| 29 | +{ |
| 30 | + [Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken")] |
| 31 | + [OutputType(typeof(string))] |
| 32 | + public class GetAzureRmAccessTokenCommand : AzureRMCmdlet |
| 33 | + { |
| 34 | + private const string AuthorizationHeaderName = "Authorization"; |
| 35 | + private const string ResourceUriParameterSet = "ResourceUri"; |
| 36 | + private const string KnownResourceNameParameterSet = "KnownResourceTypeName"; |
| 37 | + |
| 38 | + //TODO: Support ResourceUri directly |
| 39 | + //[Parameter(ParameterSetName = ResourceUriParameterSet, Mandatory = false)] |
| 40 | + //public string Resource { get; set; } |
| 41 | + |
| 42 | + [Parameter(ParameterSetName = KnownResourceNameParameterSet, |
| 43 | + Mandatory = false, |
| 44 | + HelpMessage = "Optional resouce type name, supported values: AadGraph, Analysis, Arm, Attest, DataLake, KeyVault, OperationInsights, Synapse. Default value is Arm if not specified.")] |
| 45 | + [PSArgumentCompleter( |
| 46 | + SupportedResourceNames.AadGraph, |
| 47 | + SupportedResourceNames.Analysis, |
| 48 | + SupportedResourceNames.Arm, |
| 49 | + SupportedResourceNames.Attest, |
| 50 | + SupportedResourceNames.DataLake, |
| 51 | + SupportedResourceNames.KeyVault, |
| 52 | + SupportedResourceNames.OperationInsights, |
| 53 | + SupportedResourceNames.Synapse |
| 54 | + )] |
| 55 | + public string ResourceTypeName { get; set; } |
| 56 | + |
| 57 | + //Use tenant in default context if not specified |
| 58 | + [Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")] |
| 59 | + public string TenantId { get; set; } |
| 60 | + |
| 61 | + public override void ExecuteCmdlet() |
| 62 | + { |
| 63 | + base.ExecuteCmdlet(); |
| 64 | + |
| 65 | + string resourceId = null; |
| 66 | + |
| 67 | + if (ResourceTypeName == null) |
| 68 | + { |
| 69 | + ResourceTypeName = SupportedResourceNames.Arm; |
| 70 | + } |
| 71 | + if (!SupportedResourceNames.ResourceNameMap.ContainsKey(ResourceTypeName)) |
| 72 | + { |
| 73 | + throw new ArgumentException(Properties.Resources.InvalidResourceTypeName.FormatInvariant(ResourceTypeName), nameof(ResourceTypeName)); |
| 74 | + } |
| 75 | + |
| 76 | + resourceId = SupportedResourceNames.ResourceNameMap[ResourceTypeName]; |
| 77 | + |
| 78 | + resourceId = string.IsNullOrEmpty(resourceId) ? AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId : resourceId; |
| 79 | + |
| 80 | + IAzureContext context = DefaultContext; |
| 81 | + if (!string.IsNullOrEmpty(TenantId) && !string.Equals(context.Tenant.Id, TenantId, StringComparison.OrdinalIgnoreCase)) |
| 82 | + { |
| 83 | + var profile = DefaultProfile as AzureRmProfile; |
| 84 | + context = profile.Contexts.FirstOrDefault(c => |
| 85 | + string.Equals(c.Value.Tenant.Id, TenantId, StringComparison.OrdinalIgnoreCase)).Value; |
| 86 | + if (context == null) |
| 87 | + { |
| 88 | + throw new ArgumentException(Properties.Resources.InvalidTenantId.FormatInvariant(TenantId), nameof(TenantId)); |
| 89 | + } |
| 90 | + } |
| 91 | + var credential = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials( |
| 92 | + context, |
| 93 | + resourceId); |
| 94 | + var requestMessage = new HttpRequestMessage(); |
| 95 | + credential.ProcessHttpRequestAsync(requestMessage, default(CancellationToken)).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 96 | + if (requestMessage.Headers.Contains(AuthorizationHeaderName)) |
| 97 | + { |
| 98 | + var token = requestMessage.Headers.GetValues(AuthorizationHeaderName) |
| 99 | + ?.FirstOrDefault()?.Substring("Bearer ".Length); |
| 100 | + WriteObject(token); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + internal class SupportedResourceNames |
| 105 | + { |
| 106 | + //TODO: Support 'Batch' and 'ManagedHsm', need to upate AzureEnvironmentExtensions.GetTokenAudience() to support more endpoints |
| 107 | + |
| 108 | + public const string Arm = "Arm"; |
| 109 | + public const string AadGraph = "AadGraph"; |
| 110 | + public const string DataLake = "DataLake"; |
| 111 | + public const string KeyVault = "KeyVault"; |
| 112 | + |
| 113 | + public const string Analysis = "Analysis"; |
| 114 | + public const string Attest = "Attest"; |
| 115 | + public const string OperationInsights = "OperationInsights"; |
| 116 | + public const string Synapse = "Synapse"; |
| 117 | + |
| 118 | + internal static Dictionary<string, string> ResourceNameMap = new Dictionary<string, string>() |
| 119 | + { |
| 120 | + { Arm, AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId }, |
| 121 | + { AadGraph, AzureEnvironment.Endpoint.Graph}, //Only exception that not using xxxResourceId because of implementation of GetTokenAudience |
| 122 | + { DataLake, AzureEnvironment.Endpoint.DataLakeEndpointResourceId}, |
| 123 | + { KeyVault, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId}, |
| 124 | + { Analysis, AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointResourceId}, |
| 125 | + { Attest, AzureEnvironment.ExtendedEndpoint.AzureAttestationServiceEndpointResourceId }, |
| 126 | + { OperationInsights, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId}, |
| 127 | + { Synapse, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointResourceId}, |
| 128 | + }; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments