private async Task <ClaimsPrincipal> GetAuthenticatedClaimsPrincipalAsync(string clientId, string clientSecret)
        {
            NullGuard.NotNull(clientId, nameof(clientId))
            .NotNull(clientSecret, nameof(clientSecret));

            IAuthenticateClientSecretCommand command = new AuthenticateClientSecretCommand(clientId, clientSecret);
            IClientSecretIdentity            clientSecretIdentity = await _commandBus.PublishAsync <IAuthenticateClientSecretCommand, IClientSecretIdentity>(command);

            if (clientSecretIdentity == null)
            {
                return(null);
            }

            clientSecretIdentity.AddClaims(new[] { ClaimHelper.CreateTokenClaim(clientSecretIdentity.Token, ProtectBase64Token) });

            ClaimsIdentity authenticatedClaimsIdentity = new ClaimsIdentity(clientSecretIdentity.ToClaimsIdentity().Claims, Scheme.Name);

            return(new ClaimsPrincipal(authenticatedClaimsIdentity));
        }
        public void GetToken_WhenUnprotectIsNotNullAndPrincipalHasTokenClaim_ReturnsToken()
        {
            IToken         token     = new Token(_fixture.Create <string>(), _fixture.Create <string>(), DateTime.Now.AddMinutes(5));
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateTokenClaim(token, value => value) });
            IClaimResolver sut       = CreateSut(principal);

            IToken result = sut.GetToken <IToken>(value => value);

            Assert.That(result, Is.TypeOf <Token>());
        }
        public void GetToken_WhenUnprotectIsNotNullAndPrincipalHasTokenClaim_AssertUnprotectWasCalled()
        {
            IToken         token     = new Token(_fixture.Create <string>(), _fixture.Create <string>(), DateTime.Now.AddMinutes(5));
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateTokenClaim(token, value => value) });
            IClaimResolver sut       = CreateSut(principal);

            bool unprotectCalled = false;

            sut.GetToken <IToken>(value =>
            {
                unprotectCalled = true;
                return(value);
            });

            Assert.That(unprotectCalled, Is.True);
        }
        public void GetToken_WhenUnprotectIsNotNullAndPrincipalHasTokenClaim_AssertUnprotectWasCalledWithBase64ForToken()
        {
            IToken         token     = new Token(_fixture.Create <string>(), _fixture.Create <string>(), DateTime.Now.AddMinutes(5));
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateTokenClaim(token, value => value) });
            IClaimResolver sut       = CreateSut(principal);

            string unprotectCalledWithValue = null;

            sut.GetToken <IToken>(value =>
            {
                unprotectCalledWithValue = value;
                return(value);
            });

            Assert.That(unprotectCalledWithValue, Is.EqualTo(token.ToBase64()));
        }