public void CanUpdateTokens() { var props = new AuthenticationProperties(); var tokens = new List <AuthenticationToken>(); var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; tokens.Add(tok1); tokens.Add(tok2); tokens.Add(tok3); props.StoreTokens(tokens); tok1.Value = ".1"; tok2.Value = ".2"; tok3.Value = ".3"; props.StoreTokens(tokens); Assert.Equal(".1", props.GetTokenValue("One")); Assert.Equal(".2", props.GetTokenValue("Two")); Assert.Equal(".3", props.GetTokenValue("Three")); Assert.Equal(3, props.GetTokens().Count()); }
public void SubsequentStoreTokenDeletesPreviousTokens() { var props = new AuthenticationProperties(); var tokens = new List <AuthenticationToken>(); var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; tokens.Add(tok1); tokens.Add(tok2); tokens.Add(tok3); props.StoreTokens(tokens); props.StoreTokens(new[] { new AuthenticationToken { Name = "Zero", Value = "0" } }); Assert.Equal("0", props.GetTokenValue("Zero")); Assert.Null(props.GetTokenValue("One")); Assert.Null(props.GetTokenValue("Two")); Assert.Null(props.GetTokenValue("Three")); Assert.Single(props.GetTokens()); }
public void UpdateTokenValueReturnsFalseForUnknownToken() { var props = new AuthenticationProperties(); var tokens = new List <AuthenticationToken>(); var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; tokens.Add(tok1); tokens.Add(tok2); tokens.Add(tok3); props.StoreTokens(tokens); Assert.False(props.UpdateTokenValue("ONE", ".11")); Assert.False(props.UpdateTokenValue("Jigglypuff", ".11")); Assert.Null(props.GetTokenValue("ONE")); Assert.Null(props.GetTokenValue("Jigglypuff")); Assert.Equal(3, props.GetTokens().Count()); }
public void CanStoreMultipleTokens() { var props = new AuthenticationProperties(); var tokens = new List<AuthenticationToken>(); var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; tokens.Add(tok1); tokens.Add(tok2); tokens.Add(tok3); props.StoreTokens(tokens); Assert.Equal("1", props.GetTokenValue("One")); Assert.Equal("2", props.GetTokenValue("Two")); Assert.Equal("3", props.GetTokenValue("Three")); Assert.Equal(3, props.GetTokens().Count()); }
private async Task <bool> RefreshApiToken(AuthenticationProperties properties) { string webToken = properties.GetTokenValue("access_token"); string newApiToken = await GetApiToken(webToken); if (!string.IsNullOrEmpty(newApiToken)) { var tokens = properties.GetTokens().Append(new AuthenticationToken() { Name = "api_token", Value = newApiToken }); properties.StoreTokens(tokens); return(true); } return(false); }
/// <summary> /// Store id_token in <paramref name="properties"/> token collection. /// </summary> /// <param name="properties">Authentication properties.</param> /// <param name="idToken">The id_token JWT.</param> private static void SaveIdToken( [NotNull] AuthenticationProperties properties, [NotNull] string?idToken) { if (!string.IsNullOrWhiteSpace(idToken)) { // Get the currently available tokens var tokens = properties.GetTokens().ToList(); // Add the extra token tokens.Add(new AuthenticationToken() { Name = "id_token", Value = idToken }); // Overwrite store with original tokens with the new additional token properties.StoreTokens(tokens); } }
public void UpdateStravaToken(long athleteId, AuthenticationProperties properties) { var authenticationTokens = properties.GetTokens(); var stravaToken = new StravaToken { AccessToken = authenticationTokens.FirstOrDefault(p => p.Name == "access_token")?.Value, RefreshToken = authenticationTokens.FirstOrDefault(p => p.Name == "refresh_token")?.Value, TokenType = authenticationTokens.FirstOrDefault(p => p.Name == "token_type")?.Value }; var expiresAt = authenticationTokens.FirstOrDefault(p => p.Name == "expires_at")?.Value; stravaToken.ExpirationDate = DateTimeOffset.TryParse(expiresAt, CultureInfo.InvariantCulture, DateTimeStyles.None, out var expiration) ? expiration : DateTimeOffset.UtcNow; this.UpdateStravaToken(athleteId, stravaToken); }
public void SubsequentStoreTokenDeletesPreviousTokens() { var props = new AuthenticationProperties(); var tokens = new List<AuthenticationToken>(); var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; tokens.Add(tok1); tokens.Add(tok2); tokens.Add(tok3); props.StoreTokens(tokens); props.StoreTokens(new[] { new AuthenticationToken { Name = "Zero", Value = "0" } }); Assert.Equal("0", props.GetTokenValue("Zero")); Assert.Equal(null, props.GetTokenValue("One")); Assert.Equal(null, props.GetTokenValue("Two")); Assert.Equal(null, props.GetTokenValue("Three")); Assert.Equal(1, props.GetTokens().Count()); }
/// <summary> /// Stores a set of authentication tokens, after removing any old tokens. /// </summary> /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> /// <param name="tokens">The tokens to store.</param> public static void StoreTokens(this AuthenticationProperties properties, IEnumerable <AuthenticationToken> tokens) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (tokens == null) { throw new ArgumentNullException(nameof(tokens)); } // Clear old tokens first var oldTokens = properties.GetTokens(); foreach (var t in oldTokens) { properties.Items.Remove(TokenKeyPrefix + t.Name); } properties.Items.Remove(TokenNamesKey); var tokenNames = new List <string>(); foreach (var token in tokens) { if (token.Name is null) { throw new ArgumentNullException(nameof(tokens), "Token name cannot be null."); } // REVIEW: should probably check that there are no ; in the token name and throw or encode tokenNames.Add(token.Name); properties.Items[TokenKeyPrefix + token.Name] = token.Value; } if (tokenNames.Count > 0) { properties.Items[TokenNamesKey] = string.Join(";", tokenNames.ToArray()); } }
public static void StoreToken(this AuthenticationProperties properties, AuthToken token) { if (properties is null) { throw new ArgumentNullException(nameof(properties)); } if (token is null) { throw new ArgumentNullException(nameof(token)); } var oldTokens = properties.GetTokens(); foreach (var t in oldTokens) { properties.Items.Remove(TokenPropertyName(token.Name)); } var tokenPropertyName = TokenPropertyName(token.Name); properties.Items.Remove(tokenPropertyName); properties.Parameters[tokenPropertyName] = token; }