Skip to content

Commit

Permalink
update to ADAL 2.14.201151115 version
Browse files Browse the repository at this point in the history
  • Loading branch information
suwatch committed Jan 25, 2015
1 parent 8aa6567 commit 5563bbe
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 285 deletions.
388 changes: 218 additions & 170 deletions ARMClient.Authentication/AADAuthentication/BaseAuthHelper.cs

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions ARMClient.Authentication/ARMClient.Authentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.1.0.5\lib\net40\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.14.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.14.201151115\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.1.0.5\lib\net40\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.14.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.14.201151115\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -67,6 +69,8 @@
<Compile Include="Contracts\TenantCacheInfo.cs" />
<Compile Include="Contracts\TenantDetails.cs" />
<Compile Include="Contracts\TenantInfo.cs" />
<Compile Include="Contracts\CustomTokenCache.cs" />
<Compile Include="Contracts\TokenCacheInfo.cs" />
<Compile Include="Contracts\VerifiedDomain.cs" />
<Compile Include="EnvironmentStorage\FileEnvironmentStorage.cs" />
<Compile Include="EnvironmentStorage\IEnvironmentStorage.cs" />
Expand All @@ -80,6 +84,7 @@
<Compile Include="TokenStorage\ITokenStorage.cs" />
<Compile Include="TokenStorage\MemoryTokenStorage.cs" />
<Compile Include="Utilities\ProtectedFile.cs" />
<Compile Include="Utilities\Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
56 changes: 56 additions & 0 deletions ARMClient.Authentication/Contracts/CustomTokenCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ARMClient.Authentication.Contracts
{
public class CustomTokenCache : TokenCache
{
private Dictionary<string, TokenCacheInfo> _caches;

public CustomTokenCache(string state = null)
{
if (state == null)
{
_caches = new Dictionary<string, TokenCacheInfo>();
}
else
{
_caches = JsonConvert.DeserializeObject<Dictionary<string, TokenCacheInfo>>(state);
}
}

public IEnumerable<TokenCacheInfo> GetValues(string resource)
{
return _caches.Values.Where(c => c.Resource == resource);
}

public string GetState()
{
return JObject.FromObject(_caches).ToString();
}

public bool TryGetValue(string tenantId, string resource, out TokenCacheInfo cacheInfo)
{
return _caches.TryGetValue(GetKey(tenantId, resource), out cacheInfo);
}

public TokenCacheInfo Get(string tenantId, string resource)
{
return _caches[GetKey(tenantId, resource)];
}

public void Add(TokenCacheInfo cacheInfo)
{
_caches[GetKey(cacheInfo.TenantId, cacheInfo.Resource)] = cacheInfo;
}

private string GetKey(string tenantId, string resource)
{
return String.Format("{0}::{1}", tenantId, resource);
}
}
}
44 changes: 44 additions & 0 deletions ARMClient.Authentication/Contracts/TokenCacheInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ARMClient.Authentication.Contracts
{
public class TokenCacheInfo
{
public TokenCacheInfo()
{
}

public TokenCacheInfo(string tenantId, string appId, string appKey, string resource, AuthenticationResult result)
: this(resource, result)
{
AppId = appId;
AppKey = appKey;
TenantId = tenantId;
}

public TokenCacheInfo(string resource, AuthenticationResult result)
{
AccessToken = result.AccessToken;
DisplayableId = result.UserInfo == null ? null : result.UserInfo.DisplayableId;
ExpiresOn = result.ExpiresOn;
RefreshToken = result.RefreshToken;
Resource = resource;
TenantId = result.TenantId;
}

public string AppId { get; set; }
public string AppKey { get; set; }
public string AccessToken { get; set; }
public string DisplayableId { get; set; }
public DateTimeOffset ExpiresOn { get; set; }
public string RefreshToken { get; set; }
public string Resource { get; set; }
public string TenantId { get; set; }

public string CreateAuthorizationHeader()
{
return String.Format("Bearer {0}", AccessToken);
}
}
}
12 changes: 5 additions & 7 deletions ARMClient.Authentication/IAuthHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ARMClient.Authentication.Contracts;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ARMClient.Authentication
{
public interface IAuthHelper
{
AzureEnvironments AzureEnvironments { get; set; }
Task AcquireTokens();
Task<AuthenticationResult> GetTokenByTenant(string tenantId);
Task<AuthenticationResult> GetTokenBySubscription(string subscriptionId);
Task<AuthenticationResult> GetTokenBySpn(string tenantId, string appId, string appKey);
Task<AuthenticationResult> GetRecentToken();
Task<string> GetAuthorizationHeader(string subscriptionId);
Task<TokenCacheInfo> GetToken(string id, string resource);
Task<TokenCacheInfo> GetTokenBySpn(string tenantId, string appId, string appKey);
Task<TokenCacheInfo> GetTokenByUpn(string tenantId, string username, string password);
bool IsCacheValid();
void ClearTokenCache();
IEnumerable<string> DumpTokenCache();
Expand Down
8 changes: 3 additions & 5 deletions ARMClient.Authentication/TenantStorage/FileTenantStorage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using ARMClient.Authentication.Contracts;
using ARMClient.Authentication.Utilities;
Expand Down Expand Up @@ -32,14 +31,13 @@ public Dictionary<string, TenantCacheInfo> GetCache()

public bool IsCacheValid()
{
var cache = GetCache();
return cache != null && cache.Count > 0;
var file = ProtectedFile.GetCacheFile(_fileName);
return File.Exists(file);
}

public void ClearCache()
{
var filePath = ProtectedFile.GetCacheFile(_fileName);
if (File.Exists(filePath))
foreach (var filePath in Directory.GetFiles(Path.GetDirectoryName(ProtectedFile.GetCacheFile(_fileName)), "cache_tenants*", SearchOption.TopDirectoryOnly))
{
File.Delete(filePath);
}
Expand Down
46 changes: 25 additions & 21 deletions ARMClient.Authentication/TokenStorage/FileTokenStorage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using ARMClient.Authentication.Contracts;
using ARMClient.Authentication.Utilities;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -13,47 +10,54 @@ namespace ARMClient.Authentication.TokenStorage
internal class FileTokenStorage : ITokenStorage
{
private const string _cacheFileName = "cache_tokens.dat";
private const string _recentFileName = "recent_token.dat";
private const string _recentARMFileName = "recent_token_arm.dat";
private const string _recentAADFileName = "recent_token_aad.dat";

public Dictionary<TokenCacheKey, string> GetCache()
public CustomTokenCache GetCache()
{
var file = ProtectedFile.GetCacheFile(_cacheFileName);
if (!File.Exists(file))
{
return new Dictionary<TokenCacheKey, string>();
return new CustomTokenCache();
}

var dict = JsonConvert.DeserializeObject<Dictionary<string, TokenCacheKey>>(ProtectedFile.ReadAllText(file));
return dict.ToDictionary(p => p.Value, p => p.Key);
var state = ProtectedFile.ReadAllText(file);
return new CustomTokenCache(state);
}

public void SaveCache(Dictionary<TokenCacheKey, string> tokens)
public void SaveCache(CustomTokenCache cache)
{
var dict = tokens.ToDictionary(p => p.Value, p => p.Key);
var json = JObject.FromObject(dict);
ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(_cacheFileName), json.ToString());
var state = cache.GetState();
ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(_cacheFileName), state);
}

public AuthenticationResult GetRecentToken()
public TokenCacheInfo GetRecentToken(string resource)
{
return AuthenticationResult.Deserialize(ProtectedFile.ReadAllText(ProtectedFile.GetCacheFile(_recentFileName)));
var file = ProtectedFile.GetCacheFile(resource == Constants.CSMResource ? _recentARMFileName : _recentAADFileName);
if (!File.Exists(file))
{
return null;
}

return JsonConvert.DeserializeObject<TokenCacheInfo>(ProtectedFile.ReadAllText(file));
}

public void SaveRecentToken(AuthenticationResult authResult)
public void SaveRecentToken(TokenCacheInfo cacheInfo, string resource)
{
ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(_recentFileName), authResult.Serialize());
var file = ProtectedFile.GetCacheFile(resource == Constants.CSMResource ? _recentARMFileName : _recentAADFileName);
var json = JObject.FromObject(cacheInfo);
ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(file), json.ToString());
}

public bool IsCacheValid()
{
var cache = GetCache();
return cache != null && cache.Count > 0;
var file = ProtectedFile.GetCacheFile(_cacheFileName);
return File.Exists(file);
}

public void ClearCache()
{
var filePaths = new[] { ProtectedFile.GetCacheFile(_cacheFileName), ProtectedFile.GetCacheFile(_recentFileName) };
foreach (var filePath in filePaths.Where(File.Exists))
foreach (var filePath in Directory.GetFiles(ProtectedFile.GetCachePath(), "*token*", SearchOption.TopDirectoryOnly))
{
File.Delete(filePath);
}
Expand Down
11 changes: 7 additions & 4 deletions ARMClient.Authentication/TokenStorage/ITokenStorage.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.Collections.Generic;
using ARMClient.Authentication.Contracts;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ARMClient.Authentication.TokenStorage
{
public interface ITokenStorage
{
Dictionary<TokenCacheKey, string> GetCache();
void SaveCache(Dictionary<TokenCacheKey, string> tokens);
AuthenticationResult GetRecentToken();
void SaveRecentToken(AuthenticationResult authResult);
CustomTokenCache GetCache();
void SaveCache(CustomTokenCache tokenCache);

TokenCacheInfo GetRecentToken(string resource);
void SaveRecentToken(TokenCacheInfo cacheInfo, string resource);

bool IsCacheValid();
void ClearCache();
}
Expand Down
17 changes: 9 additions & 8 deletions ARMClient.Authentication/TokenStorage/MemoryTokenStorage.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
using System;
using System.Collections.Generic;
using ARMClient.Authentication.Contracts;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ARMClient.Authentication.TokenStorage
{
internal class MemoryTokenStorage : ITokenStorage
{
private Dictionary<TokenCacheKey, string> _cache;
private AuthenticationResult _recentToken;
public Dictionary<TokenCacheKey, string> GetCache()
private CustomTokenCache _cache;
private TokenCacheInfo _recentToken;
public CustomTokenCache GetCache()
{
return this._cache ?? new Dictionary<TokenCacheKey, string>();
return this._cache ?? new CustomTokenCache();
}

public void SaveCache(Dictionary<TokenCacheKey, string> cache)
public void SaveCache(CustomTokenCache cache)
{
this._cache = cache;
}

public AuthenticationResult GetRecentToken()
public TokenCacheInfo GetRecentToken(string resource)
{
return this._recentToken;
}

public void SaveRecentToken(AuthenticationResult authResult)
public void SaveRecentToken(TokenCacheInfo cacheInfo, string resource)
{
this._recentToken = authResult;
this._recentToken = cacheInfo;
}

public bool IsCacheValid()
Expand Down
7 changes: 6 additions & 1 deletion ARMClient.Authentication/Utilities/ProtectedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ namespace ARMClient.Authentication.Utilities
{
internal static class ProtectedFile
{
public static string GetCachePath()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".arm");
}

public static string GetCacheFile(string file)
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".arm");
var path = GetCachePath();
Directory.CreateDirectory(path);
return Path.Combine(path, file);
}
Expand Down
35 changes: 35 additions & 0 deletions ARMClient.Authentication/Utilities/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;

namespace ARMClient.Authentication.Utilities
{
public static class Utils
{
static TraceListener _traceListener;

public static TraceListener Trace
{
get { return _traceListener ?? DefaultTraceListener.Default; }
}

public static void SetTraceListener(TraceListener listener)
{
_traceListener = listener;
}

class DefaultTraceListener : TraceListener
{
public readonly static TraceListener Default = new DefaultTraceListener();

public override void Write(string message)
{
System.Diagnostics.Trace.Write(message);
}

public override void WriteLine(string message)
{
System.Diagnostics.Trace.WriteLine(message);
}
}
}
}
2 changes: 1 addition & 1 deletion ARMClient.Authentication/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net451" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="1.0.5" targetFramework="net451" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.14.201151115" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net451" />
</packages>
Loading

0 comments on commit 5563bbe

Please sign in to comment.