Skip to content

Commit 1926b62

Browse files
committed
Fix login issues, serialization issues with profile, and issues with default datastore
1 parent 1e67494 commit 1926b62

File tree

9 files changed

+128
-9
lines changed

9 files changed

+128
-9
lines changed

src/Common/Commands.Common/AzureRmProfileProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Common.Authentication;
1516
using Microsoft.Azure.Common.Authentication.Models;
1617

1718
namespace Microsoft.WindowsAzure.Commands.Common

src/Common/Commands.Common/PSAzureAccount.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ public class PSAzureAccount
2323
public string Subscriptions { get; set; }
2424

2525
public string Tenants { get; set; }
26+
27+
internal string AccessToken { get; set; }
2628
}
2729
}

src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public abstract class AzureRMCmdlet : AzurePSCmdlet
3737
/// </summary>
3838
static AzureRMCmdlet()
3939
{
40-
if (AzureSession.DataStore == null)
40+
if (!TestMockSupport.RunningMocked)
4141
{
4242
AzureSession.DataStore = new DiskDataStore();
43-
}
43+
}
4444
}
4545

4646
public AzureRMCmdlet()

src/ResourceManager/Profile/Commands.Profile/Account/AddAzureRmAccount.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
5555
[ValidateNotNullOrEmpty]
5656
public string AccessToken { get; set; }
5757

58-
[Parameter(Mandatory = false, HelpMessage = "Subscription")]
58+
[Parameter(ParameterSetName = "AccessToken", Mandatory = true, HelpMessage = "Account Id for access token")]
59+
[ValidateNotNullOrEmpty]
60+
public string AccountId { get; set; }
61+
62+
[Parameter(Mandatory = false, HelpMessage = "Subscription Id")]
5963
[ValidateNotNullOrEmpty]
6064
public string SubscriptionId { get; set; }
6165

62-
[Parameter(Mandatory = false, HelpMessage = "Subscription")]
66+
[Parameter(Mandatory = false, HelpMessage = "Subscription name")]
6367
[ValidateNotNullOrEmpty]
6468
public string SubscriptionName { get; set; }
6569

@@ -97,7 +101,14 @@ protected override void ProcessRecord()
97101

98102
if (!string.IsNullOrEmpty(AccessToken))
99103
{
104+
if (string.IsNullOrWhiteSpace(AccountId) )
105+
{
106+
throw new PSInvalidOperationException("Access token credentials must contain AccountId parameter.");
107+
}
108+
100109
azureAccount.Type = AzureAccount.AccountType.AccessToken;
110+
azureAccount.Id = AccountId;
111+
azureAccount.SetProperty(AzureAccount.Property.AccessToken, AccessToken);
101112
}
102113
else if (ServicePrincipal.IsPresent)
103114
{

src/ResourceManager/Profile/Commands.Profile/Commands.Profile.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="Models\PSAzureSubscription.cs" />
147147
<Compile Include="Models\PSAzureTenant.cs" />
148148
<Compile Include="Models\RMProfileClient.cs" />
149+
<Compile Include="Models\SimpleAccessToken.cs" />
149150
<Compile Include="Subscription\GetAzureRMSubscription.cs" />
150151
<Compile Include="Account\AddAzureRmAccount.cs" />
151152
<Compile Include="Context\GetAzureRMContext.cs" />

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureProfile.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static implicit operator PSAzureProfile(AzureRMProfile profile)
3434
};
3535

3636
profile.Environments
37-
.ForEach((e) => result.Environments.Add(e.Key, (PSAzureEnvironment)e.Value));
37+
.ForEach((e) => result.Environments[e.Key] = (PSAzureEnvironment)e.Value);
3838
return result;
3939
}
4040

@@ -54,8 +54,7 @@ public static implicit operator AzureRMProfile(PSAzureProfile profile)
5454
{
5555
Context = profile.Context
5656
};
57-
profile.Environments
58-
.ForEach((e) => result.Environments.Add(e.Key, (AzureEnvironment)e.Value));
57+
profile.Environments.ForEach((e) => result.Environments[e.Key] = (AzureEnvironment) e.Value);
5958
return result;
6059
}
6160

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureRmAccount.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ public static implicit operator PSAzureRmAccount(AzureAccount account)
3636
return null;
3737
}
3838

39-
return new PSAzureRmAccount
39+
var result = new PSAzureRmAccount
4040
{
4141
Id = account.Id,
4242
AccountType = account.Type.ToString()
4343
};
44+
45+
if (account.IsPropertySet(AzureAccount.Property.AccessToken))
46+
{
47+
result.AccessToken = account.GetProperty(AzureAccount.Property.AccessToken);
48+
}
49+
50+
if (account.IsPropertySet(AzureAccount.Property.Tenants))
51+
{
52+
result.Tenants = account.GetProperty(AzureAccount.Property.Tenants);
53+
}
54+
55+
return result;
4456
}
4557

4658
/// <summary>
@@ -65,6 +77,16 @@ public static implicit operator AzureAccount(PSAzureRmAccount account)
6577
result.Type = accountType;
6678
}
6779

80+
if (!string.IsNullOrWhiteSpace(account.AccessToken))
81+
{
82+
result.SetProperty(AzureAccount.Property.AccessToken, account.AccessToken);
83+
}
84+
85+
if (!string.IsNullOrWhiteSpace(account.Tenants))
86+
{
87+
result.SetProperty(AzureAccount.Property.Tenants, account.Tenants);
88+
}
89+
6890
return result;
6991
}
7092

@@ -77,6 +99,16 @@ public static implicit operator AzureAccount(PSAzureRmAccount account)
7799
/// </summary>
78100
public string AccountType { get; set; }
79101

102+
/// <summary>
103+
/// The tenant ids for the account
104+
/// </summary>
105+
public string Tenants { get; set; }
106+
107+
/// <summary>
108+
/// The access token for the account (if any)
109+
/// </summary>
110+
public string AccessToken { get; set; }
111+
80112
public override string ToString()
81113
{
82114
return this.Id;

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Linq;
2626
using System.Management.Automation;
2727
using System.Security;
28+
using Microsoft.Azure.Commands.Profile.Models;
2829

2930
namespace Microsoft.Azure.Commands.ResourceManager.Common
3031
{
@@ -48,7 +49,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
4849
{
4950
AzureSubscription newSubscription = null;
5051
AzureTenant newTenant = null;
51-
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
52+
ShowDialog promptBehavior = (password == null && account.Type != AzureAccount.AccountType.AccessToken) ? ShowDialog.Always : ShowDialog.Never;
5253

5354
// (tenant and subscription are present) OR
5455
// (tenant is present and subscription is not provided)
@@ -280,6 +281,12 @@ private IAccessToken AcquireAccessToken(AzureAccount account,
280281
SecureString password,
281282
ShowDialog promptBehavior)
282283
{
284+
if (account.Type == AzureAccount.AccountType.AccessToken)
285+
{
286+
tenantId = tenantId ?? "Common";
287+
return new SimpleAccessToken(account, tenantId);
288+
}
289+
283290
return AzureSession.AuthenticationFactory.Authenticate(
284291
account,
285292
environment,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 Microsoft.Azure.Common.Authentication;
17+
using Microsoft.Azure.Common.Authentication.Models;
18+
19+
namespace Microsoft.Azure.Commands.Profile.Models
20+
{
21+
public class SimpleAccessToken : IAccessToken
22+
{
23+
public const string _defaultTokenType = "Bearer";
24+
private string _tokenType;
25+
26+
public SimpleAccessToken(AzureAccount account, string tenantId)
27+
{
28+
if (account == null)
29+
{
30+
throw new ArgumentNullException("account");
31+
}
32+
if (string.IsNullOrWhiteSpace(account.Id))
33+
{
34+
throw new ArgumentOutOfRangeException("account", "AccountId must be provided to use an AccessToken credential.");
35+
}
36+
if (account.Type != AzureAccount.AccountType.AccessToken ||
37+
!account.IsPropertySet(AzureAccount.Property.AccessToken))
38+
{
39+
throw new ArgumentException("To create an access token credential, you must provide an access token account");
40+
}
41+
this.UserId = account.Id;
42+
this._tokenType = _defaultTokenType;
43+
this.AccessToken = account.GetProperty(AzureAccount.Property.AccessToken);
44+
this.TenantId = tenantId;
45+
}
46+
public string AccessToken { get; private set; }
47+
48+
public void AuthorizeRequest(System.Action<string, string> authTokenSetter)
49+
{
50+
authTokenSetter(_tokenType, AccessToken);
51+
}
52+
53+
public LoginType LoginType
54+
{
55+
get { return LoginType.OrgId; }
56+
}
57+
58+
public string TenantId
59+
{
60+
get;
61+
private set;
62+
}
63+
64+
public string UserId { get; private set; }
65+
}
66+
}

0 commit comments

Comments
 (0)