|
| 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.Management.Automation; |
| 18 | + |
| 19 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 20 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
| 21 | +using Microsoft.Azure.Commands.Profile.Models; |
| 22 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 23 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 24 | +using Microsoft.Azure.PowerShell.Authenticators; |
| 25 | + |
| 26 | +namespace Microsoft.Azure.Commands.Profile |
| 27 | +{ |
| 28 | + [Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken", DefaultParameterSetName = KnownResourceNameParameterSet)] |
| 29 | + [OutputType(typeof(PSAccessToken))] |
| 30 | + public class GetAzureRmAccessTokenCommand : AzureRMCmdlet |
| 31 | + { |
| 32 | + private const string ResourceUrlParameterSet = "ResourceUrl"; |
| 33 | + private const string KnownResourceNameParameterSet = "KnownResourceTypeName"; |
| 34 | + |
| 35 | + [Parameter(ParameterSetName = ResourceUrlParameterSet, |
| 36 | + Mandatory = true, |
| 37 | + HelpMessage = "Resource url for that you're requesting token, e.g. 'http://graph.windows.net/'.")] |
| 38 | + [ValidateNotNullOrEmpty] |
| 39 | + [Alias("Resource", "ResourceUri")] |
| 40 | + public string ResourceUrl { get; set; } |
| 41 | + |
| 42 | + [Parameter(ParameterSetName = KnownResourceNameParameterSet, |
| 43 | + Mandatory = false, |
| 44 | + HelpMessage = "Optional resouce type name, supported values: AadGraph, AnalysisServices, Arm, Attestation, Batch, DataLake, KeyVault, OperationalInsights, ResourceManager, Synapse. Default value is Arm if not specified.")] |
| 45 | + [PSArgumentCompleter( |
| 46 | + SupportedResourceNames.AadGraph, |
| 47 | + SupportedResourceNames.AnalysisServices, |
| 48 | + SupportedResourceNames.Arm, |
| 49 | + SupportedResourceNames.Attestation, |
| 50 | + SupportedResourceNames.Batch, |
| 51 | + SupportedResourceNames.DataLake, |
| 52 | + SupportedResourceNames.KeyVault, |
| 53 | + SupportedResourceNames.ManagedHsm, |
| 54 | + SupportedResourceNames.OperationalInsights, |
| 55 | + SupportedResourceNames.ResourceManager, |
| 56 | + SupportedResourceNames.Synapse |
| 57 | + )] |
| 58 | + public string ResourceTypeName { get; set; } |
| 59 | + |
| 60 | + //Use tenant in default context if not specified |
| 61 | + //TODO: Should not specify TenantId for MSI, CloudShell(?) |
| 62 | + [Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")] |
| 63 | + public string TenantId { get; set; } |
| 64 | + |
| 65 | + public override void ExecuteCmdlet() |
| 66 | + { |
| 67 | + base.ExecuteCmdlet(); |
| 68 | + |
| 69 | + string resourceUrlOrId; |
| 70 | + |
| 71 | + if (ParameterSetName == KnownResourceNameParameterSet) |
| 72 | + { |
| 73 | + if (ResourceTypeName == null) |
| 74 | + { |
| 75 | + ResourceTypeName = SupportedResourceNames.Arm; |
| 76 | + } |
| 77 | + if (!SupportedResourceNames.ResourceNameMap.ContainsKey(ResourceTypeName)) |
| 78 | + { |
| 79 | + throw new ArgumentException(Properties.Resources.InvalidResourceTypeName.FormatInvariant(ResourceTypeName), nameof(ResourceTypeName)); |
| 80 | + } |
| 81 | + resourceUrlOrId = SupportedResourceNames.ResourceNameMap[ResourceTypeName]; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + resourceUrlOrId = ResourceUrl; |
| 86 | + } |
| 87 | + |
| 88 | + IAzureContext context = DefaultContext; |
| 89 | + if(TenantId == null) |
| 90 | + { |
| 91 | + TenantId = context.Tenant?.Id; |
| 92 | + } |
| 93 | + |
| 94 | + IAccessToken accessToken = AzureSession.Instance.AuthenticationFactory.Authenticate( |
| 95 | + context.Account, |
| 96 | + context.Environment, |
| 97 | + TenantId, |
| 98 | + null, |
| 99 | + ShowDialog.Never, |
| 100 | + null, |
| 101 | + null, |
| 102 | + resourceUrlOrId); |
| 103 | + |
| 104 | + var result = new PSAccessToken() |
| 105 | + { |
| 106 | + Token = accessToken.AccessToken, |
| 107 | + TenantId = TenantId, |
| 108 | + UserId = accessToken.UserId, |
| 109 | + }; |
| 110 | + result.ExpiresOn = (accessToken as MsalAccessToken)?.ExpiresOn ?? result.ExpiresOn; |
| 111 | + |
| 112 | + WriteObject(result); |
| 113 | + } |
| 114 | + |
| 115 | + internal class SupportedResourceNames |
| 116 | + { |
| 117 | + public const string Arm = "Arm"; |
| 118 | + public const string AadGraph = "AadGraph"; |
| 119 | + public const string Batch = "Batch"; |
| 120 | + public const string DataLake = "DataLake"; |
| 121 | + public const string KeyVault = "KeyVault"; |
| 122 | + public const string ResourceManager = "ResourceManager"; //endpoint is same as Arm |
| 123 | + |
| 124 | + public const string AnalysisServices = "AnalysisServices"; |
| 125 | + public const string Attestation = "Attestation"; |
| 126 | + public const string OperationalInsights = "OperationalInsights"; |
| 127 | + public const string Synapse = "Synapse"; |
| 128 | + public const string ManagedHsm = "ManagedHsm"; |
| 129 | + |
| 130 | + internal static Dictionary<string, string> ResourceNameMap = new Dictionary<string, string>() |
| 131 | + { |
| 132 | + { Arm, AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId }, |
| 133 | + { AadGraph, AzureEnvironment.Endpoint.GraphEndpointResourceId }, |
| 134 | + { Batch, AzureEnvironment.Endpoint.BatchEndpointResourceId }, |
| 135 | + { DataLake, AzureEnvironment.Endpoint.DataLakeEndpointResourceId }, |
| 136 | + { KeyVault, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId }, |
| 137 | + { ResourceManager, AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId }, |
| 138 | + |
| 139 | + { AnalysisServices, AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointResourceId }, |
| 140 | + { Attestation, AzureEnvironment.ExtendedEndpoint.AzureAttestationServiceEndpointResourceId }, |
| 141 | + { OperationalInsights, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId }, |
| 142 | + { Synapse, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointResourceId }, |
| 143 | + { ManagedHsm, AzureEnvironment.ExtendedEndpoint.ManagedHsmServiceEndpointResourceId } |
| 144 | + }; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments