Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Interfaces\IClientAction.cs" />
<Compile Include="Interfaces\IClientFactory.cs" />
<Compile Include="Interfaces\IDataStore.cs" />
<Compile Include="Interfaces\IRenewableToken.cs" />
<Compile Include="Authentication\LoginType.cs" />
<Compile Include="Authentication\ShowDialog.cs" />
<Compile Include="Interfaces\IProfileProvider.cs" />
Expand Down
26 changes: 26 additions & 0 deletions src/Authentication.Abstractions/Interfaces/IRenewableToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ----------------------------------------------------------------------------------
//
// 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;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// Canonical representation of a renewable access token
/// </summary>
public interface IRenewableToken : IAccessToken
{
DateTimeOffset ExpiresOn { get; }
}
}
12 changes: 8 additions & 4 deletions src/Authentication.Test/AuthenticationFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ public void CanAuthenticateUsingMSIDefault()
};
AzureSession.Instance.RegisterComponent(HttpClientOperationsFactory.Name, () => TestHttpOperationsFactory.Create(responses, _output), true);
var authFactory = new AuthenticationFactory();
var token = authFactory.Authenticate(account, environment, tenant, null, null, null);
IRenewableToken token = (IRenewableToken) authFactory.Authenticate(account, environment, tenant, null, null, null);
_output.WriteLine($"Received access token for default Uri ${token.AccessToken}");
Assert.Equal(expectedAccessToken, token.AccessToken);
Assert.Equal(3600, Math.Round(token.ExpiresOn.DateTime.Subtract(DateTime.UtcNow).TotalSeconds));
var account2 = new AzureAccount
{
Id = userId,
Expand Down Expand Up @@ -223,9 +224,10 @@ public void CanAuthenticateUsingMSIResourceId()
};
AzureSession.Instance.RegisterComponent(HttpClientOperationsFactory.Name, () => TestHttpOperationsFactory.Create(responses, _output), true);
var authFactory = new AuthenticationFactory();
var token = authFactory.Authenticate(account, environment, tenant, null, null, null);
IRenewableToken token = (IRenewableToken) authFactory.Authenticate(account, environment, tenant, null, null, null);
_output.WriteLine($"Received access token for default Uri ${token.AccessToken}");
Assert.Equal(expectedAccessToken, token.AccessToken);
Assert.Equal(3600, Math.Round(token.ExpiresOn.DateTime.Subtract(DateTime.UtcNow).TotalSeconds));
var account2 = new AzureAccount
{
Id = userId,
Expand Down Expand Up @@ -271,9 +273,10 @@ public void CanAuthenticateUsingMSIClientId()
};
AzureSession.Instance.RegisterComponent(HttpClientOperationsFactory.Name, () => TestHttpOperationsFactory.Create(responses, _output), true);
var authFactory = new AuthenticationFactory();
var token = authFactory.Authenticate(account, environment, tenant, null, null, null);
IRenewableToken token = (IRenewableToken) authFactory.Authenticate(account, environment, tenant, null, null, null);
_output.WriteLine($"Received access token for default Uri ${token.AccessToken}");
Assert.Equal(expectedAccessToken, token.AccessToken);
Assert.Equal(3600, Math.Round(token.ExpiresOn.DateTime.Subtract(DateTime.UtcNow).TotalSeconds));
var account2 = new AzureAccount
{
Id = userId,
Expand Down Expand Up @@ -319,9 +322,10 @@ public void CanAuthenticateUsingMSIObjectId()
};
AzureSession.Instance.RegisterComponent(HttpClientOperationsFactory.Name, () => TestHttpOperationsFactory.Create(responses, _output), true);
var authFactory = new AuthenticationFactory();
var token = authFactory.Authenticate(account, environment, tenant, null, null, null);
IRenewableToken token = (IRenewableToken) authFactory.Authenticate(account, environment, tenant, null, null, null);
_output.WriteLine($"Received access token for default Uri ${token.AccessToken}");
Assert.Equal(expectedAccessToken, token.AccessToken);
Assert.Equal(3600, Math.Round(token.ExpiresOn.DateTime.Subtract(DateTime.UtcNow).TotalSeconds));
var account2 = new AzureAccount
{
Id = userId,
Expand Down
10 changes: 9 additions & 1 deletion src/Authentication/Authentication/ManagedServiceAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace Microsoft.Azure.Commands.Common.Authentication
{
public class ManagedServiceAccessToken : IAccessToken
public class ManagedServiceAccessToken : IRenewableToken
{
IAzureAccount _account;
string _tenant;
Expand Down Expand Up @@ -104,6 +104,14 @@ public string AccessToken

public string UserId => _account.Id;

public DateTimeOffset ExpiresOn
{
get
{
return _expiration;
}
}

public void AuthorizeRequest(Action<string, string> authTokenSetter)
{
authTokenSetter("Bearer", AccessToken);
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/Authentication/RawAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Microsoft.Azure.Commands.Common.Authentication
{
public class RawAccessToken : IAccessToken
public class RawAccessToken : IRenewableToken
{
public string AccessToken
{
Expand All @@ -42,5 +42,7 @@ public void AuthorizeRequest(Action<string, string> authTokenSetter)
{
authTokenSetter("Bearer", AccessToken);
}

public DateTimeOffset ExpiresOn { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void StoreAppKey(string appId, string tenantId, SecureString appKey)
ServicePrincipalKeyStore.SaveKey(appId, tenantId, appKey);
}

private class ServicePrincipalAccessToken : IAccessToken
private class ServicePrincipalAccessToken : IRenewableToken
{
internal readonly AdalConfiguration Configuration;
internal AuthenticationResult AuthResult;
Expand Down Expand Up @@ -211,6 +211,8 @@ private bool IsExpired
return timeUntilExpiration < expirationThreshold;
}
}

public DateTimeOffset ExpiresOn { get { return AuthResult.ExpiresOn; } }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ private string GetExceptionMessage(Exception ex)
}

/// <summary>
/// Implementation of <see cref="IAccessToken"/> using data from ADAL
/// Implementation of <see cref="IRenewableToken"/> using data from ADAL
/// </summary>
private class AdalAccessToken : IAccessToken
private class AdalAccessToken : IRenewableToken
{
internal readonly AdalConfiguration Configuration;
internal AuthenticationResult AuthResult;
Expand Down Expand Up @@ -276,6 +276,8 @@ public string LoginType
return Authentication.LoginType.OrgId;
}
}

public DateTimeOffset ExpiresOn { get { return AuthResult.ExpiresOn; } }
}
}
}
6 changes: 4 additions & 2 deletions src/Authentication/Authentication/UserTokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ private string GetExceptionMessage(Exception ex)
}

/// <summary>
/// Implementation of <see cref="IAccessToken"/> using data from ADAL
/// Implementation of <see cref="IRenewableToken"/> using data from ADAL
/// </summary>
private class AdalAccessToken : IAccessToken
private class AdalAccessToken : IRenewableToken
{
internal readonly AdalConfiguration Configuration;
internal AuthenticationResult AuthResult;
Expand Down Expand Up @@ -353,6 +353,8 @@ public string LoginType
return Authentication.LoginType.OrgId;
}
}

public DateTimeOffset ExpiresOn { get { return AuthResult.ExpiresOn; } }
}

public IAccessToken GetAccessTokenWithCertificate(
Expand Down
4 changes: 3 additions & 1 deletion src/ScenarioTest.ResourceManager/Mocks/MockAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.Test.Mocks
{
public class MockAccessToken : IAccessToken
public class MockAccessToken : IRenewableToken
{
private string _tenantId = String.Empty;
public void AuthorizeRequest(Action<string, string> authTokenSetter)
Expand All @@ -34,5 +34,7 @@ public string TenantId
get { return _tenantId; }
set { _tenantId = value; }
}

public DateTimeOffset ExpiresOn { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/ScenarioTest/Mocks/MockAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Microsoft.WindowsAzure.Commands.Common.Test.Mocks
{
public class MockAccessToken : IAccessToken
public class MockAccessToken : IRenewableToken
{
public void AuthorizeRequest(Action<string, string> authTokenSetter)
{
Expand All @@ -32,5 +32,7 @@ public string TenantId
{
get { return string.Empty; }
}

public DateTimeOffset ExpiresOn { get; set; }
}
}