Skip to content

Commit

Permalink
updated auth assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
jformacek committed Aug 6, 2022
1 parent 6285d38 commit 2ddbb4a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Authentication/Authentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>GreyCorbel.Identity.Authentication</AssemblyName>
<RootNamespace>GreyCorbel.Identity.Authentication</RootNamespace>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<Authors>Jiri Formacek</Authors>
<Company>GreyCorbel Solutions</Company>
<Product>Unified AAD Authentication client library for Public, Confidential and ManagedIdentity client authentication</Product>
Expand Down
1 change: 1 addition & 0 deletions Authentication/ManagedIdentityAuthenticationResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class ManagedIdentityAuthenticationResponse
public string access_token { get; set; }
public string client_id { get; set; }
public string expires_in { get; set; }
public string ext_expires_in { get; set; }
public string expires_on { get; set; }
public string not_before { get; set; }
public string resource { get; set; }
Expand Down
24 changes: 17 additions & 7 deletions Authentication/TokenProviders/TokenProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Identity.Client;
using System;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -23,29 +25,37 @@ public TokenProvider(IMsalHttpClientFactory factory, string clientId = null)
{
_httpClientFactory = factory;
_clientId = clientId;

}
public abstract Task<AuthenticationResult> AcquireTokenForClientAsync(string[] scopes, CancellationToken cancellationToken);

protected AuthenticationResult CreateAuthenticationResult(ManagedIdentityAuthenticationResponse authResponse)
{
long tokenExpiresOn = long.Parse(authResponse.expires_on);
DateTimeOffset tokenExpires = new DateTimeOffset(DateTime.UtcNow.AddSeconds(tokenExpiresOn));
long.TryParse(authResponse.expires_on, out long expiresOn);
DateTimeOffset tokenExpiresOn = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expiresOn);
ClaimsPrincipal principal = null;
if(!string.IsNullOrEmpty(authResponse.client_id))
{
principal = new();
GenericIdentity identity = new(authResponse.client_id, "aad");
principal.AddIdentity(new ClaimsIdentity(identity));
}

Guid tokenId = Guid.NewGuid();
return new AuthenticationResult(
authResponse.access_token,
false,
tokenId.ToString(),
tokenExpires,
tokenExpires,
tokenExpiresOn,
tokenExpiresOn,
null,
null,
null,
ScopeHelper.ResourceToScope(authResponse.resource),
tokenId,
authResponse.token_type
authResponse.token_type,
null,
principal
);

}
}
}
Binary file modified Module/AadAuthenticationFactory/AadAuthenticationFactory.psd1
Binary file not shown.
Binary file not shown.

0 comments on commit 2ddbb4a

Please sign in to comment.