|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -using System.Security.Claims; |
5 | 4 | using Microsoft.AspNetCore.Http; |
6 | 5 | using Microsoft.Extensions.DependencyInjection; |
| 6 | +using System.Security.Claims; |
| 7 | +using static Microsoft.AspNetCore.Authentication.Core.Test.AuthenticationPropertiesTests; |
7 | 8 |
|
8 | 9 | namespace Microsoft.AspNetCore.Authentication.Core.Test; |
9 | 10 |
|
@@ -149,6 +150,64 @@ public async Task GetTokenWorksWithExplicitScheme() |
149 | 150 | Assert.Equal("3", await context.GetTokenAsync("simple", "Three")); |
150 | 151 | } |
151 | 152 |
|
| 153 | + [Fact] |
| 154 | + public void StoreTokensStoresTokensWithNullValues() |
| 155 | + { |
| 156 | + var properties = new AuthenticationProperties(); |
| 157 | + var tokens = new List<AuthenticationToken> |
| 158 | + { |
| 159 | + new AuthenticationToken { Name = "access_token", Value = "access_value" }, |
| 160 | + new AuthenticationToken { Name = "refresh_token", Value = null }, |
| 161 | + new AuthenticationToken { Name = "id_token", Value = "id_value" } |
| 162 | + }; |
| 163 | + |
| 164 | + properties.StoreTokens(tokens); |
| 165 | + |
| 166 | + Assert.Equal("access_value", properties.GetTokenValue("access_token")); |
| 167 | + Assert.Null(properties.GetTokenValue("refresh_token")); |
| 168 | + Assert.Equal("id_value", properties.GetTokenValue("id_token")); |
| 169 | + |
| 170 | + var retrievedTokens = properties.GetTokens().ToList(); |
| 171 | + Assert.Equal(2, retrievedTokens.Count); |
| 172 | + Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void UpdateTokenValueWorksWithNullGetTokenValueResult() |
| 177 | + { |
| 178 | + var sourceProperties = new AuthenticationProperties(); |
| 179 | + var targetProperties = new AuthenticationProperties(); |
| 180 | + |
| 181 | + var tokens = new List<AuthenticationToken> |
| 182 | + { |
| 183 | + new AuthenticationToken { Name = "refresh_token", Value = "refresh_value" } |
| 184 | + }; |
| 185 | + |
| 186 | + targetProperties.StoreTokens(tokens); |
| 187 | + targetProperties.UpdateTokenValue("refresh_token", sourceProperties.GetTokenValue("refresh_token")); |
| 188 | + Assert.Null(targetProperties.GetTokenValue("refresh_token")); |
| 189 | + } |
| 190 | + |
| 191 | + [Fact] |
| 192 | + public void GetTokensExcludesTokensWithNullValues() |
| 193 | + { |
| 194 | + var properties = new AuthenticationProperties(); |
| 195 | + var tokens = new List<AuthenticationToken> |
| 196 | + { |
| 197 | + new AuthenticationToken { Name = "access_token", Value = "access_value" }, |
| 198 | + new AuthenticationToken { Name = "refresh_token", Value = null }, |
| 199 | + new AuthenticationToken { Name = "id_token", Value = "id_value" } |
| 200 | + }; |
| 201 | + |
| 202 | + properties.StoreTokens(tokens); |
| 203 | + var retrievedTokens = properties.GetTokens().ToList(); |
| 204 | + |
| 205 | + Assert.Equal(2, retrievedTokens.Count); |
| 206 | + Assert.Contains(retrievedTokens, t => t.Name == "access_token" && t.Value == "access_value"); |
| 207 | + Assert.Contains(retrievedTokens, t => t.Name == "id_token" && t.Value == "id_value"); |
| 208 | + Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); |
| 209 | + } |
| 210 | + |
152 | 211 | private class SimpleAuth : IAuthenticationHandler |
153 | 212 | { |
154 | 213 | public Task<AuthenticateResult> AuthenticateAsync() |
|
0 commit comments