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

            sut.GetNameIdentifier();

            _principalResolverMock.Verify(m => m.GetCurrentPrincipal(), Times.Once);
        }
        public KeyGenerator(IHashKeyGenerator hashKeyGenerator, IClaimResolver claimResolver)
        {
            NullGuard.NotNull(hashKeyGenerator, nameof(hashKeyGenerator))
            .NotNull(claimResolver, nameof(claimResolver));

            _hashKeyGenerator = hashKeyGenerator;
            _claimResolver    = claimResolver;
        }
        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);
        }
Example #5
0
        public void GetAccountingNumber_WhenCalledAndPrincipalDoesNotHaveAccountingClaim_ReturnsNull()
        {
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()) });
            IClaimResolver sut       = CreateSut(principal);

            int?result = sut.GetAccountingNumber();

            Assert.That(result, Is.Null);
        }
        public void GetNameIdentifier_WhenCalledAndPrincipalDoesNotHaveNameIdentifierClaim_ReturnsNull()
        {
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()) });
            IClaimResolver sut       = CreateSut(principal);

            string result = sut.GetNameIdentifier();

            Assert.That(result, Is.Null);
        }
Example #7
0
        /// <summary>
        /// Creates a new instance of <see cref="LogHandler"/> with the specified parameters.
        /// </summary>
        /// <param name="logStore">The <see cref="ILogStore"/></param>
        /// <param name="claimResolver">the <see cref="IClaimResolver"/></param>
        /// <param name="requestHeaderFilter">The <see cref="IHeaderFilter"/> for the request headers</param>
        /// <param name="assembly">The calling assembly</param>
        /// <exception cref="ArgumentNullException"/>
        public LogHandler(ILogStore logStore, IClaimResolver claimResolver, IHeaderFilter requestHeaderFilter, Assembly assembly)
        {
            _logStore            = logStore ?? throw new ArgumentNullException(nameof(logStore));
            _claimResolver       = claimResolver ?? throw new ArgumentNullException(nameof(claimResolver));
            _requestHeaderFilter = requestHeaderFilter ?? throw new ArgumentNullException(nameof(requestHeaderFilter));

            _applicationName    = assembly.GetName().Name;
            _applicationVersion = FileVersionInfo.GetVersionInfo(assembly.Location)?.FileVersion;
        }
        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>());
        }
Example #9
0
        public void GetMailAddress_WhenCalledAndPrincipalHasEmailClaim_ReturnsEmail()
        {
            string         email     = _fixture.Create <string>();
            IPrincipal     principal = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateEmailClaim(email) });
            IClaimResolver sut       = CreateSut(principal);

            string result = sut.GetMailAddress();

            Assert.That(result, Is.EqualTo(email));
        }
        public SecurityController(IClaimResolver claimResolver, IDataProtectionProvider dataProtectionProvider, IAcmeChallengeResolver acmeChallengeResolver)
        {
            NullGuard.NotNull(claimResolver, nameof(claimResolver))
            .NotNull(dataProtectionProvider, nameof(dataProtectionProvider))
            .NotNull(acmeChallengeResolver, nameof(acmeChallengeResolver));

            _claimResolver          = claimResolver;
            _dataProtectionProvider = dataProtectionProvider;
            _acmeChallengeResolver  = acmeChallengeResolver;
        }
Example #11
0
        public void GetCountryCode_WhenCalledAndPrincipalHasCountryCodeClaim_ReturnsCountryCode()
        {
            string         countryCode = _fixture.Create <string>();
            IPrincipal     principal   = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateCountryCodeClaim(countryCode) });
            IClaimResolver sut         = CreateSut(principal);

            string result = sut.GetCountryCode();

            Assert.That(result, Is.EqualTo(countryCode));
        }
        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
        }
        public void GetNameIdentifier_WhenCalledAndPrincipalHasNameIdentifierClaim_ReturnsNameIdentifier()
        {
            string         nameIdentifier = _fixture.Create <string>();
            IPrincipal     principal      = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateNameIdentifierClaim(nameIdentifier) });
            IClaimResolver sut            = CreateSut(principal);

            string result = sut.GetNameIdentifier();

            Assert.That(result, Is.EqualTo(nameIdentifier));
        }
Example #14
0
        public void GetAccountingNumber_WhenCalledAndPrincipalHasAccountingClaim_ReturnsAccountingNumber()
        {
            int            accountingNumber = _fixture.Create <int>();
            IPrincipal     principal        = CreateClaimsPrincipal(new[] { new Claim(_fixture.Create <string>(), _fixture.Create <string>()), ClaimHelper.CreateAccountingClaim(accountingNumber) });
            IClaimResolver sut = CreateSut(principal);

            int?result = sut.GetAccountingNumber();

            Assert.That(result, Is.EqualTo(accountingNumber));
        }
Example #15
0
        public ContactController(ICommandBus commandBus, IQueryBus queryBus, IClaimResolver claimResolver, ITokenHelperFactory tokenHelperFactory)
        {
            NullGuard.NotNull(commandBus, nameof(commandBus))
            .NotNull(queryBus, nameof(queryBus))
            .NotNull(claimResolver, nameof(claimResolver))
            .NotNull(tokenHelperFactory, nameof(tokenHelperFactory));

            _commandBus         = commandBus;
            _queryBus           = queryBus;
            _claimResolver      = claimResolver;
            _tokenHelperFactory = tokenHelperFactory;
        }
Example #16
0
        public HomeController(IQueryBus queryBus, IClaimResolver claimResolver, ITokenHelperFactory tokenHelperFactory, IAcmeChallengeResolver acmeChallengeResolver)
        {
            NullGuard.NotNull(queryBus, nameof(queryBus))
            .NotNull(claimResolver, nameof(claimResolver))
            .NotNull(tokenHelperFactory, nameof(tokenHelperFactory))
            .NotNull(acmeChallengeResolver, nameof(acmeChallengeResolver));

            _queryBus              = queryBus;
            _claimResolver         = claimResolver;
            _tokenHelperFactory    = tokenHelperFactory;
            _acmeChallengeResolver = acmeChallengeResolver;
        }
        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_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 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);
        }
Example #20
0
 /// <summary>
 /// Creates a new instance of <see cref="Request"/> with the specified <see cref="IOwinContext"/> and <see cref="IClaimResolver"/>.
 /// </summary>
 /// <param name="context">The <see cref="<see cref="IOwinContext"/>"/></param>
 /// <param name="claimResolver">The <see cref="IClaimResolver"/></param>
 public Request(IOwinContext context, IClaimResolver claimResolver)
     : this(context)
 {
     _claimResolver = claimResolver ?? throw new ArgumentNullException(nameof(claimResolver));
 }
Example #21
0
 /// <summary>
 /// Determines if the current User has the claim specified by the given resolver with respect to the given context.
 /// </summary>
 /// <typeparam name="T">The context type for the claim.</typeparam>
 /// <param name="resolver">The resolver to use to verify the claim.</param>
 /// <param name="context">The context for the claim resolver to use.</param>
 /// <returns>`true` if the Claim is resolved with respect to the context.</returns>
 /// <exception cref="UnauthenticatedException">Thrown if the user provider cannot provide an authenticated user.</exception>
 public bool HasClaim <T> (IClaimResolver <T> resolver, T context)
 {
     return(UserProvider.IsLoggedIn ? resolver.HasValidClaim(UserProvider.Account, context) : throw new UnauthenticatedException());
 }
Example #22
0
        public AccountingHelper(IClaimResolver claimResolver)
        {
            NullGuard.NotNull(claimResolver, nameof(claimResolver));

            _claimResolver = claimResolver;
        }
Example #23
0
        public CountryHelper(IClaimResolver claimResolver)
        {
            NullGuard.NotNull(claimResolver, nameof(claimResolver));

            _claimResolver = claimResolver;
        }