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); }
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")); }
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); }
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); }
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); }
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 }, })); }
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"); } }
public void Generate_WhenClientSecretIdentityIsNull_ThrowsArgumentNullException() { ITokenHelper sut = CreateSut(); ArgumentNullException result = Assert.Throws <ArgumentNullException>(() => sut.Generate(null)); Assert.That(result.ParamName, Is.EqualTo("clientSecretIdentity")); }