Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DefaultClaimUidExtractorTest.cs « Internal « Microsoft.AspNetCore.Antiforgery.Test « test « Antiforgery « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1852b910dadbd78f7f2499008583051fb11b62d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.Extensions.ObjectPool;
using Moq;
using Xunit;

namespace Microsoft.AspNetCore.Antiforgery.Internal
{
    public class DefaultClaimUidExtractorTest
    {
        private static readonly ObjectPool<AntiforgerySerializationContext> _pool =
            new DefaultObjectPoolProvider().Create(new AntiforgerySerializationContextPooledObjectPolicy());

        [Fact]
        public void ExtractClaimUid_Unauthenticated()
        {
            // Arrange
            var extractor = new DefaultClaimUidExtractor(_pool);

            var mockIdentity = new Mock<ClaimsIdentity>();
            mockIdentity.Setup(o => o.IsAuthenticated)
                        .Returns(false);

            // Act
            var claimUid = extractor.ExtractClaimUid(new ClaimsPrincipal(mockIdentity.Object));

            // Assert
            Assert.Null(claimUid);
        }

        [Fact]
        public void ExtractClaimUid_ClaimsIdentity()
        {
            // Arrange
            var mockIdentity = new Mock<ClaimsIdentity>();
            mockIdentity.Setup(o => o.IsAuthenticated)
                        .Returns(true);
            mockIdentity.Setup(o => o.Claims).Returns(new Claim[] { new Claim(ClaimTypes.Name, "someName") });

            var extractor = new DefaultClaimUidExtractor(_pool);

            // Act
            var claimUid = extractor.ExtractClaimUid(new ClaimsPrincipal(mockIdentity.Object ));

            // Assert
            Assert.NotNull(claimUid);
            Assert.Equal("yhXE+2v4zSXHtRHmzm4cmrhZca2J0g7yTUwtUerdeF4=", claimUid);
        }

        [Fact]
        public void DefaultUniqueClaimTypes_NotPresent_SerializesAllClaimTypes()
        {
            var identity = new ClaimsIdentity("someAuthentication");
            identity.AddClaim(new Claim(ClaimTypes.Email, "someone@antifrogery.com"));
            identity.AddClaim(new Claim(ClaimTypes.GivenName, "some"));
            identity.AddClaim(new Claim(ClaimTypes.Surname, "one"));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, string.Empty));

            // Arrange
            var claimsIdentity = (ClaimsIdentity)identity;

            // Act
            var identiferParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { claimsIdentity })
                                                              .ToArray();
            var claims = claimsIdentity.Claims.ToList();
            claims.Sort((a, b) => string.Compare(a.Type, b.Type, StringComparison.Ordinal));

            // Assert
            int index = 0;
            foreach (var claim in claims)
            {
                Assert.Equal(identiferParameters[index++], claim.Type);
                Assert.Equal(identiferParameters[index++], claim.Value);
                Assert.Equal(identiferParameters[index++], claim.Issuer);
            }
        }

        [Fact]
        public void DefaultUniqueClaimTypes_Present()
        {
            // Arrange
            var identity = new ClaimsIdentity("someAuthentication");
            identity.AddClaim(new Claim("fooClaim", "fooClaimValue"));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.NameIdentifier,
                "nameIdentifierValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_PrefersSubClaimOverNameIdentifierAndUpn()
        {
            // Arrange
            var identity = new ClaimsIdentity("someAuthentication");
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));
            identity.AddClaim(new Claim("sub", "subClaimValue"));
            identity.AddClaim(new Claim(ClaimTypes.Upn, "upnClaimValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity });

            // Assert
            Assert.Equal(new string[]
            {
                "sub",
                "subClaimValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_PrefersNameIdentifierOverUpn()
        {
            // Arrange
            var identity = new ClaimsIdentity("someAuthentication");
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));
            identity.AddClaim(new Claim(ClaimTypes.Upn, "upnClaimValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.NameIdentifier,
                "nameIdentifierValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_UsesUpnIfPresent()
        {
            // Arrange
            var identity = new ClaimsIdentity("someAuthentication");
            identity.AddClaim(new Claim("fooClaim", "fooClaimValue"));
            identity.AddClaim(new Claim(ClaimTypes.Upn, "upnClaimValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.Upn,
                "upnClaimValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_MultipleIdentities_UsesOnlyAuthenticatedIdentities()
        {
            // Arrange
            var identity1 = new ClaimsIdentity(); // no authentication
            identity1.AddClaim(new Claim("sub", "subClaimValue"));
            var identity2 = new ClaimsIdentity("someAuthentication");
            identity2.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity1, identity2 });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.NameIdentifier,
                "nameIdentifierValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_NoKnownClaimTypesFound_SortsAndReturnsAllClaimsFromAuthenticatedIdentities()
        {
            // Arrange
            var identity1 = new ClaimsIdentity(); // no authentication
            identity1.AddClaim(new Claim("sub", "subClaimValue"));
            var identity2 = new ClaimsIdentity("someAuthentication");
            identity2.AddClaim(new Claim(ClaimTypes.Email, "email@domain.com"));
            var identity3 = new ClaimsIdentity("someAuthentication");
            identity3.AddClaim(new Claim(ClaimTypes.Country, "countryValue"));
            var identity4 = new ClaimsIdentity("someAuthentication");
            identity4.AddClaim(new Claim(ClaimTypes.Name, "claimName"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(
                new ClaimsIdentity[] { identity1, identity2, identity3, identity4 });

            // Assert
            Assert.Equal(new List<string>
            {
                ClaimTypes.Country,
                "countryValue",
                "LOCAL AUTHORITY",
                ClaimTypes.Email,
                "email@domain.com",
                "LOCAL AUTHORITY",
                ClaimTypes.Name,
                "claimName",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_PrefersNameFromFirstIdentity_OverSubFromSecondIdentity()
        {
            // Arrange
            var identity1 = new ClaimsIdentity("someAuthentication");
            identity1.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));
            var identity2 = new ClaimsIdentity("someAuthentication");
            identity2.AddClaim(new Claim("sub", "subClaimValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(
                new ClaimsIdentity[] { identity1, identity2 });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.NameIdentifier,
                "nameIdentifierValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }

        [Fact]
        public void GetUniqueIdentifierParameters_PrefersUpnFromFirstIdentity_OverNameFromSecondIdentity()
        {
            // Arrange
            var identity1 = new ClaimsIdentity("someAuthentication");
            identity1.AddClaim(new Claim(ClaimTypes.Upn, "upnValue"));
            var identity2 = new ClaimsIdentity("someAuthentication");
            identity2.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue"));

            // Act
            var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(
                new ClaimsIdentity[] { identity1, identity2 });

            // Assert
            Assert.Equal(new string[]
            {
                ClaimTypes.Upn,
                "upnValue",
                "LOCAL AUTHORITY",
            }, uniqueIdentifierParameters);
        }
    }
}