Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

WIP Fix/277 #278

Merged
merged 4 commits into from
Feb 12, 2021
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
23 changes: 16 additions & 7 deletions src/OidcClient/OidcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using IdentityModel.Client;
using IdentityModel.OidcClient.Infrastructure;
using IdentityModel.OidcClient.Results;

using Microsoft.Extensions.Logging;

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,6 +22,7 @@ namespace IdentityModel.OidcClient
/// </summary>
public class OidcClient
{
private const long TOKEN_START_TIME = 621355968000000000;// 1970-01-01T00:00:00Z UTCTicks
private readonly ILogger _logger;
private readonly AuthorizeClient _authorizeClient;

Expand Down Expand Up @@ -76,7 +79,7 @@ public virtual async Task<LoginResult> LoginAsync(LoginRequest request = null, C
Timeout = request.BrowserTimeout,
ExtraParameters = request.FrontChannelExtraParameters
}, cancellationToken);

if (authorizeResult.IsError)
{
return new LoginResult(authorizeResult.Error, authorizeResult.ErrorDescription);
Expand Down Expand Up @@ -229,14 +232,20 @@ public virtual async Task<LoginResult> ProcessResponseAsync(string data, Authori

var user = ProcessClaims(result.User, userInfoClaims);

long seconds = 0;
var authTimeValue = result.TokenResponse.TryGet(JwtClaimTypes.AuthenticationTime);
DateTimeOffset? authTime = null;
if (authTimeValue.IsPresent() && long.TryParse(authTimeValue, out seconds))
authTime = new DateTimeOffset(TOKEN_START_TIME, TimeSpan.Zero).AddSeconds(seconds);

var loginResult = new LoginResult
{
User = user,
AccessToken = result.TokenResponse.AccessToken,
RefreshToken = result.TokenResponse.RefreshToken,
AccessTokenExpiration = DateTimeOffset.Now.AddSeconds(result.TokenResponse.ExpiresIn),
IdentityToken = result.TokenResponse.IdentityToken,
AuthenticationTime = DateTimeOffset.Now,
AuthenticationTime = authTime,
TokenResponse = result.TokenResponse // In some cases there is additional custom response data that clients need access to
};

Expand Down Expand Up @@ -307,7 +316,7 @@ public virtual async Task<RefreshTokenResult> RefreshTokenAsync(string refreshTo

await EnsureConfigurationAsync(cancellationToken);
var client = Options.CreateClient();

var response = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
{
Address = Options.ProviderInformation.TokenEndpoint,
Expand Down Expand Up @@ -341,8 +350,8 @@ public virtual async Task<RefreshTokenResult> RefreshTokenAsync(string refreshTo
IdentityToken = response.IdentityToken,
AccessToken = response.AccessToken,
RefreshToken = response.RefreshToken,
ExpiresIn = (int)response.ExpiresIn,
AccessTokenExpiration = DateTime.Now.AddSeconds(response.ExpiresIn)
ExpiresIn = response.ExpiresIn,
AccessTokenExpiration = DateTimeOffset.Now.AddSeconds(response.ExpiresIn)
};
}

Expand Down Expand Up @@ -377,11 +386,11 @@ internal async Task EnsureProviderInformationAsync(CancellationToken cancellatio
Address = Options.Authority,
Policy = Options.Policy.Discovery
}, cancellationToken).ConfigureAwait(false);

if (disco.IsError)
{
_logger.LogError("Error loading discovery document: {errorType} - {error}", disco.ErrorType.ToString(), disco.Error);

if (disco.ErrorType == ResponseErrorType.Exception)
{
throw new InvalidOperationException("Error loading discovery document: " + disco.Error, disco.Exception);
Expand Down
2 changes: 1 addition & 1 deletion src/OidcClient/Results/LoginResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public LoginResult(string error, string errorDescription)
/// <value>
/// The authentication time.
/// </value>
public virtual DateTimeOffset AuthenticationTime { get; internal set; }
public virtual DateTimeOffset? AuthenticationTime { get; internal set; }

/// <summary>
/// Gets or sets the refresh token handler.
Expand Down
2 changes: 1 addition & 1 deletion src/OidcClient/Results/RefreshTokenResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class RefreshTokenResult : Result
/// <value>
/// The access token expiration.
/// </value>
public virtual DateTime AccessTokenExpiration { get; internal set; }
public virtual DateTimeOffset AccessTokenExpiration { get; internal set; }

}
}