// This method handles the refreshing of the true layer api access token private static async Task HandleOnRefreshTokenAsync(TrueLayerTokenRefreshContext context) { var refreshToken = context.Properties.GetTokenValue("refresh_token"); var httpContext = context.HttpContext; var options = context.Options; var properties = context.Properties; var pairs = new Dictionary <string, string> { { "client_id", options.ClientId }, { "client_secret", options.ClientSecret }, { "grant_type", "refresh_token" }, { "refresh_token", refreshToken } }; var content = new FormUrlEncodedContent(pairs); var refreshResponse = await options.Backchannel.PostAsync(options.TokenEndpoint, content, httpContext.RequestAborted).ConfigureAwait(false); refreshResponse.EnsureSuccessStatusCode(); var payload = JObject.Parse(await refreshResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); // Persist the new acess token properties.UpdateTokenValue("access_token", payload.Value <string>("access_token")); refreshToken = payload.Value <string>("refresh_token"); if (!string.IsNullOrEmpty(refreshToken)) { properties.UpdateTokenValue("refresh_token", refreshToken); } if (int.TryParse(payload.Value <string>("expires_in"), NumberStyles.Integer, CultureInfo.InvariantCulture, out var seconds)) { var expiresAt = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(seconds); properties.UpdateTokenValue("expires_at", expiresAt.ToString("o", CultureInfo.InvariantCulture)); } await httpContext.SignInAsync(context.Principal, properties).ConfigureAwait(false); }
public Task RefreshToken(TrueLayerTokenRefreshContext context) => OnRefreshToken(context);