|
14 | 14 |
|
15 | 15 | using System; |
16 | 16 | using System.Collections.Generic; |
17 | | -using System.Linq; |
18 | 17 | using System.Management.Automation; |
19 | | -using System.Net.Http; |
20 | | -using System.Threading; |
21 | 18 |
|
22 | 19 | using Microsoft.Azure.Commands.Common.Authentication; |
23 | 20 | using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
24 | | -using Microsoft.Azure.Commands.Common.Authentication.Models; |
| 21 | +using Microsoft.Azure.Commands.Profile.Models; |
25 | 22 | using Microsoft.Azure.Commands.ResourceManager.Common; |
26 | 23 | using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 24 | +using Microsoft.Azure.PowerShell.Authenticators; |
27 | 25 |
|
28 | | -namespace Microsoft.Azure.Commands.Profile.Token |
| 26 | +namespace Microsoft.Azure.Commands.Profile |
29 | 27 | { |
30 | | - [Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken")] |
31 | | - [OutputType(typeof(string))] |
| 28 | + [Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken", DefaultParameterSetName = KnownResourceNameParameterSet)] |
| 29 | + [OutputType(typeof(PSAccessToken))] |
32 | 30 | public class GetAzureRmAccessTokenCommand : AzureRMCmdlet |
33 | 31 | { |
34 | | - private const string AuthorizationHeaderName = "Authorization"; |
35 | | - private const string ResourceUriParameterSet = "ResourceUri"; |
| 32 | + private const string ResourceUrlParameterSet = "ResourceUrl"; |
36 | 33 | private const string KnownResourceNameParameterSet = "KnownResourceTypeName"; |
37 | 34 |
|
38 | | - //TODO: Support ResourceUri directly |
39 | | - //[Parameter(ParameterSetName = ResourceUriParameterSet, Mandatory = false)] |
40 | | - //public string Resource { get; set; } |
| 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 | 41 |
|
42 | 42 | [Parameter(ParameterSetName = KnownResourceNameParameterSet, |
43 | 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.")] |
| 44 | + HelpMessage = "Optional resouce type name, supported values: AadGraph, Analysis, Arm, Attestation, Batch, DataLake, KeyVault, OperationInsights, ResourceManager, Synapse. Default value is Arm if not specified.")] |
45 | 45 | [PSArgumentCompleter( |
46 | 46 | SupportedResourceNames.AadGraph, |
47 | 47 | SupportedResourceNames.Analysis, |
48 | 48 | SupportedResourceNames.Arm, |
49 | 49 | SupportedResourceNames.Attest, |
| 50 | + SupportedResourceNames.Batch, |
50 | 51 | SupportedResourceNames.DataLake, |
51 | 52 | SupportedResourceNames.KeyVault, |
| 53 | + SupportedResourceNames.ManagedHsm, |
52 | 54 | SupportedResourceNames.OperationInsights, |
| 55 | + SupportedResourceNames.ResourceManager, |
53 | 56 | SupportedResourceNames.Synapse |
54 | 57 | )] |
55 | 58 | public string ResourceTypeName { get; set; } |
56 | 59 |
|
57 | 60 | //Use tenant in default context if not specified |
| 61 | + //TODO: Should not specify TenantId for MSI, CloudShell(?) |
58 | 62 | [Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")] |
59 | 63 | public string TenantId { get; set; } |
60 | 64 |
|
61 | 65 | public override void ExecuteCmdlet() |
62 | 66 | { |
63 | 67 | base.ExecuteCmdlet(); |
64 | 68 |
|
65 | | - string resourceId = null; |
| 69 | + string resourceUrlOrId; |
66 | 70 |
|
67 | | - if (ResourceTypeName == null) |
| 71 | + if (ParameterSetName == KnownResourceNameParameterSet) |
68 | 72 | { |
69 | | - ResourceTypeName = SupportedResourceNames.Arm; |
| 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]; |
70 | 82 | } |
71 | | - if (!SupportedResourceNames.ResourceNameMap.ContainsKey(ResourceTypeName)) |
| 83 | + else |
72 | 84 | { |
73 | | - throw new ArgumentException(Properties.Resources.InvalidResourceTypeName.FormatInvariant(ResourceTypeName), nameof(ResourceTypeName)); |
| 85 | + resourceUrlOrId = ResourceUrl; |
74 | 86 | } |
75 | 87 |
|
76 | | - resourceId = SupportedResourceNames.ResourceNameMap[ResourceTypeName]; |
77 | | - |
78 | | - resourceId = string.IsNullOrEmpty(resourceId) ? AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId : resourceId; |
79 | | - |
80 | 88 | IAzureContext context = DefaultContext; |
81 | | - if (!string.IsNullOrEmpty(TenantId) && !string.Equals(context.Tenant.Id, TenantId, StringComparison.OrdinalIgnoreCase)) |
| 89 | + if(TenantId == null) |
82 | 90 | { |
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 | | - } |
| 91 | + TenantId = context.Tenant?.Id; |
90 | 92 | } |
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)) |
| 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() |
97 | 105 | { |
98 | | - var token = requestMessage.Headers.GetValues(AuthorizationHeaderName) |
99 | | - ?.FirstOrDefault()?.Substring("Bearer ".Length); |
100 | | - WriteObject(token); |
101 | | - } |
| 106 | + Token = accessToken.AccessToken, |
| 107 | + TenantId = TenantId, |
| 108 | + UserId = accessToken.UserId, |
| 109 | + }; |
| 110 | + result.ExpiresOn = (accessToken as MsalAccessToken)?.ExpiredOn ?? result.ExpiresOn; |
| 111 | + |
| 112 | + WriteObject(result); |
102 | 113 | } |
103 | 114 |
|
104 | 115 | internal class SupportedResourceNames |
105 | 116 | { |
106 | | - //TODO: Support 'Batch' and 'ManagedHsm', need to upate AzureEnvironmentExtensions.GetTokenAudience() to support more endpoints |
107 | | - |
108 | 117 | public const string Arm = "Arm"; |
109 | 118 | public const string AadGraph = "AadGraph"; |
| 119 | + public const string Batch = "Batch"; |
110 | 120 | public const string DataLake = "DataLake"; |
111 | 121 | public const string KeyVault = "KeyVault"; |
| 122 | + public const string ResourceManager = "ResourceManager"; //endpoint is same as Arm |
112 | 123 |
|
113 | 124 | public const string Analysis = "Analysis"; |
114 | | - public const string Attest = "Attest"; |
| 125 | + public const string Attest = "Attestation"; |
115 | 126 | public const string OperationInsights = "OperationInsights"; |
116 | 127 | public const string Synapse = "Synapse"; |
| 128 | + public const string ManagedHsm = "ManagedHsm"; |
117 | 129 |
|
118 | 130 | internal static Dictionary<string, string> ResourceNameMap = new Dictionary<string, string>() |
119 | 131 | { |
120 | 132 | { 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}, |
| 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 | + { Analysis, AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointResourceId }, |
125 | 140 | { Attest, AzureEnvironment.ExtendedEndpoint.AzureAttestationServiceEndpointResourceId }, |
126 | | - { OperationInsights, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId}, |
127 | | - { Synapse, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointResourceId}, |
| 141 | + { OperationInsights, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId }, |
| 142 | + { Synapse, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointResourceId }, |
| 143 | + { ManagedHsm, AzureEnvironment.ExtendedEndpoint.ManagedHsmServiceEndpointResourceId } |
128 | 144 | }; |
129 | 145 | } |
130 | 146 | } |
|
0 commit comments