예제 #1
0
        public void Generate_WhenCalled_AssertSecurityJwtKeyWasCalledOnConfiguration()
        {
            ITokenHelper sut = CreateSut();

            sut.Generate(_fixture.BuildClientSecretIdentityMock().Object);

            _configurationMock.Verify(m => m[It.Is <string>(value => string.Compare(value, "Security:JWT:Key", StringComparison.Ordinal) == 0)], Times.Once);
        }
예제 #2
0
        public void Generate_WhenCalled_ReturnsTokenWithTokenType()
        {
            ITokenHelper sut = CreateSut();

            Mock <IClientSecretIdentity> clientSecretIdentityMock = _fixture.BuildClientSecretIdentityMock();
            string result = sut.Generate(clientSecretIdentityMock.Object).TokenType;

            Assert.That(result, Is.EqualTo("Bearer"));
        }
예제 #3
0
        public void Generate_WhenCalled_ReturnsToken()
        {
            ITokenHelper sut = CreateSut();

            Mock <IClientSecretIdentity> clientSecretIdentityMock = _fixture.BuildClientSecretIdentityMock();
            IToken result = sut.Generate(clientSecretIdentityMock.Object);

            Assert.That(result, Is.Not.Null);
        }
예제 #4
0
        public void Generate_WhenCalled_ReturnsTokenWithExpireTime()
        {
            ITokenHelper sut = CreateSut();

            Mock <IClientSecretIdentity> clientSecretIdentityMock = _fixture.BuildClientSecretIdentityMock();
            DateTime result = sut.Generate(clientSecretIdentityMock.Object).Expires;

            Assert.That(result, Is.EqualTo(DateTime.UtcNow.AddHours(1)).Within(1).Seconds);
        }
예제 #5
0
        public void Generate_WhenCalled_AssertToClaimsIdentityWasCalledOnClientSecretIdentity()
        {
            ITokenHelper sut = CreateSut();

            Mock <IClientSecretIdentity> clientSecretIdentityMock = _fixture.BuildClientSecretIdentityMock();

            sut.Generate(clientSecretIdentityMock.Object);

            clientSecretIdentityMock.Verify(m => m.ToClaimsIdentity(), Times.Once);
        }
        protected override IClientSecretIdentity CreateAuthenticatedIdentity(IAuthenticateClientSecretCommand command, IIdentity identity)
        {
            NullGuard.NotNull(command, nameof(command))
            .NotNull(identity, nameof(identity));

            IClientSecretIdentity clientSecretIdentity = (IClientSecretIdentity)identity;

            clientSecretIdentity.AddClaims(command.Claims);
            clientSecretIdentity.ClearSensitiveData();

            clientSecretIdentity.AddToken(_tokenHelper.Generate(clientSecretIdentity));

            return(clientSecretIdentity);
        }
예제 #7
0
        public async Task <string> Login(LoginModel model)
        {
            var user = await _accountRepository.GetUserByEmail(model.Email);

            if (user == null)
            {
                throw new ApiErrorException(HttpStatusCode.BadRequest, "Invalid Login details");
            }

            if (!PasswordHelper.VerifyPassword(user.PasswordHash, model.Password))
            {
                throw new ApiErrorException(HttpStatusCode.BadRequest, "Invalid Login details");
            }

            return(_tokenHelper.Generate(new Dictionary <string, object>()
            {
                { "accId", user.Id },
            }));
        }
예제 #8
0
        public async Task <ValidableSession> RegisterAsync(ValidableCredentials credentials)
        {
            var user       = credentials.ToUser <TUser>();
            var resultFlag = await _userService.CreateAsync(user, credentials.Password, credentials.Parameters);

            if (resultFlag)
            {
                var roles = await _userService.GetRolesAsync(user);

                var claims = await _userService.GetClaimsAsync(user);

                var tokenInfo = _tokenHelper.Generate(user, roles, claims, _securitySettings.PrivateKey);
                var session   = new ValidableSession(user, tokenInfo, credentials.Parameters);

                return(session);
            }
            else
            {
                throw new AuthenticationException($"User {user.UserName} could not be created");
            }
        }
예제 #9
0
        public void Generate_WhenClientSecretIdentityIsNull_ThrowsArgumentNullException()
        {
            ITokenHelper sut = CreateSut();

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

            Assert.That(result.ParamName, Is.EqualTo("clientSecretIdentity"));
        }