-
Notifications
You must be signed in to change notification settings - Fork 4k
Fix access token login and related issues [Fixes #105057182] #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using Microsoft.Azure.Commands.Profile.Properties; | ||
using Microsoft.Azure.Common.Authentication; | ||
using Microsoft.Azure.Common.Authentication.Models; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Models | ||
{ | ||
/// <summary> | ||
/// Provides access token information for a bearer token | ||
/// </summary> | ||
public class SimpleAccessToken : IAccessToken | ||
{ | ||
public const string _defaultTokenType = "Bearer"; | ||
private string _tokenType; | ||
|
||
/// <summary> | ||
/// Create a new access token from the given account and tenant id | ||
/// </summary> | ||
/// <param name="account">The account, containing user id, access token information</param> | ||
/// <param name="tenantId">The tenant id for the given access token</param> | ||
/// <param name="tokenType">The token type for the given token.</param> | ||
public SimpleAccessToken(AzureAccount account, string tenantId, string tokenType = _defaultTokenType) | ||
{ | ||
if (account == null) | ||
{ | ||
throw new ArgumentNullException("account"); | ||
} | ||
if (string.IsNullOrWhiteSpace(account.Id)) | ||
{ | ||
throw new ArgumentOutOfRangeException("account", Resources.AccessTokenRequiresAccount); | ||
} | ||
if (account.Type != AzureAccount.AccountType.AccessToken || | ||
!account.IsPropertySet(AzureAccount.Property.AccessToken)) | ||
{ | ||
throw new ArgumentException(Resources.TypeNotAccessToken); | ||
} | ||
this.UserId = account.Id; | ||
this._tokenType = tokenType; | ||
this.AccessToken = account.GetProperty(AzureAccount.Property.AccessToken); | ||
this.TenantId = tenantId; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// The access token to be applied to a request message | ||
/// </summary> | ||
public string AccessToken { get; private set; } | ||
|
||
/// <summary> | ||
/// Authorize a request using an authorization setter function. | ||
/// </summary> | ||
/// <param name="authTokenSetter">The authorization token setter function.</param> | ||
public void AuthorizeRequest(System.Action<string, string> authTokenSetter) | ||
{ | ||
authTokenSetter(_tokenType, AccessToken); | ||
} | ||
|
||
/// <summary> | ||
/// The login type for this token | ||
/// </summary> | ||
public LoginType LoginType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: formatting public LoginType LoginType { get { return LoginType.OrgId; } } |
||
{ | ||
get { return LoginType.OrgId; } | ||
} | ||
|
||
/// <summary> | ||
/// The tenant Id for this token. | ||
/// </summary> | ||
public string TenantId { get; private set; } | ||
|
||
/// <summary> | ||
/// The User Id associated with this token. | ||
/// </summary> | ||
public string UserId { get; private set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: add comments to the properties |
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can be moved to
SimpleAccessToken
and have an 2nd constructor that takes account and defaults tenant to"Common"