public void NameClaimTypeRetrieverShouldUseSubIfSubNotPresent()
        {
            var authOptions = new AuthOptions()
            {
                EnableServiceAccountAuthorization = false
            };
            var devPermissionsOptions     = new DevPermissionsOptions();
            var jwtSigningKeyProviderMock = new Mock <IJwtSigningKeyResolver>();
            var hostingEnvironmentMock    = new Mock <IHostingEnvironment>();

            var tokenValidationParametersFactory = new TokenValidationParametersFactory(Options.Create(authOptions),
                                                                                        jwtSigningKeyProviderMock.Object,
                                                                                        Options.Create(devPermissionsOptions),
                                                                                        hostingEnvironmentMock.Object);

            SecurityToken securityToken = new JwtSecurityToken();

            var result = tokenValidationParametersFactory.NameClaimTypeRetriever(securityToken, String.Empty);

            Assert.Equal(Claims.Sub, result);
        }
        public void NameClaimTypeRetriever_Should_Use_XAuthenticatedUserId_If_Present()
        {
            var authOptions = new AuthOptions()
            {
                EnableServiceAccountAuthorization = false
            };
            var devPermissionsOptions     = new DevPermissionsOptions();
            var jwtSigningKeyProviderMock = new Mock <IJwtSigningKeyResolver>();
            var hostingEnvironmentMock    = new Mock <IHostingEnvironment>();

            var tokenValidationParametersFactory = new TokenValidationParametersFactory(Options.Create(authOptions),
                                                                                        jwtSigningKeyProviderMock.Object,
                                                                                        Options.Create(devPermissionsOptions),
                                                                                        hostingEnvironmentMock.Object);

            var           token         = "eyJ4NXUiOiJodHRwczpcL1wvYXBpLWd3LW8uYW50d2VycGVuLmJlXC9rZXlzXC9wdWIiLCJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTE5MTA5ODMsIlgtQ3JlZGVudGlhbC1Vc2VybmFtZSI6Im5vbmUiLCJYLUNvbnN1bWVyLUdyb3VwcyI6InBla2UubW9ja2Jpbi52MSIsImlzcyI6IjcwOTU5NGMyYjAwZDRkMjA5MDJlZTc3YWZkMDJiYjBjIiwiYXVkIjoibW9ja2Jpbi5vcmciLCJYLUpXVC1Jc3N1ZXIiOiJodHRwczpcL1wvYXBpLWd3LW8uYW50d2VycGVuLmJlI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Q6MeQg4ZfwW7Oto9ce_Oy2fnxOanMPgAyG3GKfLRrm8Brg7i6w";
            SecurityToken securityToken = new JwtSecurityToken(token);

            var result = tokenValidationParametersFactory.NameClaimTypeRetriever(securityToken, String.Empty);

            Assert.Equal(Claims.XAuthenticatedUserId, result);
        }
        public void NameClaimTypeRetrieverShouldUseXConsumerUsernameClaimIfSubNotPresent()
        {
            var authOptions = new AuthOptions()
            {
                EnableServiceAccountAuthorization = true
            };
            var devPermissionsOptions     = new DevPermissionsOptions();
            var jwtSigningKeyProviderMock = new Mock <IJwtSigningKeyResolver>();
            var hostingEnvironmentMock    = new Mock <IHostingEnvironment>();

            var tokenValidationParametersFactory = new TokenValidationParametersFactory(Options.Create(authOptions),
                                                                                        jwtSigningKeyProviderMock.Object,
                                                                                        Options.Create(devPermissionsOptions),
                                                                                        hostingEnvironmentMock.Object);

            var           token         = "eyJ4NXUiOiJodHRwczpcL1wvYXBpLWd3LWEuYW50d2VycGVuLmJlXC9rZXlzXC9wdWIiLCJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1MjA1MTI1MTEsIlgtQ3JlZGVudGlhbC1Vc2VybmFtZSI6Im5vbmUiLCJYLUNvbnN1bWVyLUdyb3VwcyI6ImFzdGFkLmFwcm9maWVsLnYxLCBhYnMuYWJzYmlqbGFnZWFwaS52MSwgYWJzLmFic2Rvc3NpZXJhcGkudjEsIGFjcGFhcy53Y21jb250ZW50bWFuYWdlci52MywgamltbXloYW5ub24udGVzdC52MSwgYWJzLmFic3Byb2Nlc3NhcGkudjEsIGFjcGFhcy53Y21wcm94eS52MyIsImlzcyI6Imh0dHBzOlwvXC9hcGktZ3ctYS5hbnR3ZXJwZW4uYmUiLCJhdWQiOiJyYXN1MTE1NS5ydGUuYW50d2VycGVuLmxvY2FsOjUwMTAyIiwiWC1KV1QtSXNzdWVyIjoiaHR0cHM6XC9cL2FwaS1ndy1hLmFudHdlcnBlbi5iZSIsIlgtQ29uc3VtZXIt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kL7PLAe2nPCErxd7SYA2dg";
            SecurityToken securityToken = new JwtSecurityToken(token);

            var result = tokenValidationParametersFactory.NameClaimTypeRetriever(securityToken, String.Empty);

            Assert.Equal(Claims.XConsumerUsername, result);
        }
        public void NameClaimTypeRetrieverShouldUseSubClaimIfPresent()
        {
            var authOptions = new AuthOptions()
            {
                EnableServiceAccountAuthorization = true
            };
            var devPermissionsOptions     = new DevPermissionsOptions();
            var jwtSigningKeyProviderMock = new Mock <IJwtSigningKeyResolver>();
            var hostingEnvironmentMock    = new Mock <IHostingEnvironment>();

            var tokenValidationParametersFactory = new TokenValidationParametersFactory(Options.Create(authOptions),
                                                                                        jwtSigningKeyProviderMock.Object,
                                                                                        Options.Create(devPermissionsOptions),
                                                                                        hostingEnvironmentMock.Object);

            var           token         = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsIng1dSI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC94NXUifQ.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE0NzI1NDk1NDgsImV4cCI6MTUwNDA4NTU0OCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.jKg9l0cuTapEFcx9v1pLtBiigK_7EXlCqvKZBoS24XE";
            SecurityToken securityToken = new JwtSecurityToken(token);

            var result = tokenValidationParametersFactory.NameClaimTypeRetriever(securityToken, String.Empty);

            Assert.Equal(Claims.Sub, result);
        }