public void GetToken_WhenUnprotectIsNotNull_AssertGetCurrentPrincipalWasCalledOnPrincipalResolver()
        {
            IClaimResolver sut = CreateSut();

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

            _principalResolverMock.Verify(m => m.GetCurrentPrincipal(), Times.Once);
        }
        public void GetToken_WhenUnprotectIsNotNullAndPrincipalDoesNotHaveTokenClaim_ReturnsNull()
        {
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()) });
            IClaimResolver sut       = CreateSut(principal);

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

            Assert.That(result, Is.Null);
        }
        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_WhenUnprotectIsNotNullAndPrincipalDoesNotHaveTokenClaim_AssertUnprotectWasNotCalled()
        {
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()) });
            IClaimResolver sut       = CreateSut(principal);

            bool unprotectCalled = false;

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

            Assert.That(unprotectCalled, Is.False);
        }
        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()));
        }
        public ActionResult <AccessTokenModel> AcquireToken([FromForm(Name = "grant_type")] string grantType)
        {
            if (string.IsNullOrWhiteSpace(grantType))
            {
                throw new IntranetExceptionBuilder(ErrorCode.ValueCannotBeNullOrWhiteSpace, nameof(grantType))
                      .WithValidatingType(typeof(string))
                      .WithValidatingField(nameof(grantType))
                      .Build();
            }

            IToken token = _claimResolver.GetToken <IToken>(UnprotectBase64Token);

            if (string.CompareOrdinal(grantType, "client_credentials") != 0 || token == null)
            {
                throw new IntranetExceptionBuilder(ErrorCode.CannotRetrieveJwtBearerTokenForAuthenticatedUser).Build();
            }

            return(Ok(_securityModelConverter.Convert <IToken, AccessTokenModel>(token)));
        }
        public void GetToken_WhenUnprotectIsNull_ThrowsArgumentNullException()
        {
            IClaimResolver sut = CreateSut();

            ArgumentNullException result = Assert.Throws <ArgumentNullException>(() => sut.GetToken <IToken>(null));

            // ReSharper disable PossibleNullReferenceException
            Assert.That(result.ParamName, Is.EqualTo("unprotect"));
            // ReSharper restore PossibleNullReferenceException
        }