public async Task <IActionResult> LoginCallback(string returnUrl = null) { Uri returnUri = null; if (string.IsNullOrWhiteSpace(returnUrl) == false) { returnUri = ConvertToAbsoluteUri(returnUrl); if (returnUri == null || _trustedDomainHelper.IsTrustedDomain(returnUri) == false) { return(Unauthorized()); } } AuthenticateResult authenticateResult = await HttpContext.AuthenticateAsync("OSDevGrp.OSIntranet.External"); if (authenticateResult.Succeeded == false || authenticateResult.Ticket == null || authenticateResult.Principal == null) { return(Unauthorized()); } Claim mailClaim = authenticateResult.Principal.FindFirst(ClaimTypes.Email); if (mailClaim == null || string.IsNullOrWhiteSpace(mailClaim.Value)) { return(Unauthorized()); } IAuthenticateUserCommand authenticateUserCommand = new AuthenticateUserCommand(mailClaim.Value, authenticateResult.Principal.Claims); IUserIdentity userIdentity = await _commandBus.PublishAsync <IAuthenticateUserCommand, IUserIdentity>(authenticateUserCommand); if (userIdentity == null) { return(Unauthorized()); } ClaimsIdentity claimsIdentity = new ClaimsIdentity(userIdentity.ToClaimsIdentity().Claims, "OSDevGrp.OSIntranet.Internal"); await HttpContext.SignInAsync("OSDevGrp.OSIntranet.Internal", new ClaimsPrincipal(claimsIdentity)); AuthenticationProperties authenticationProperties = authenticateResult.Ticket.Properties; foreach (TokenType tokenType in Enum.GetValues(typeof(TokenType)).Cast <TokenType>()) { string tokenTypeKey = $".{tokenType}"; if (authenticationProperties.Items.ContainsKey(tokenTypeKey) == false) { continue; } await _tokenHelperFactory.StoreTokenAsync(tokenType, HttpContext, authenticationProperties.Items[tokenTypeKey]); } if (returnUri != null) { return(Redirect(returnUri.AbsoluteUri)); } return(LocalRedirect("/Home/Index")); }
public async Task StoreTokenAsync_WhenCalledWithSupportedTokenType_AssertStoreTokenAsyncWasCalledOnTokenHelperForTokenType() { TokenType tokenType = _fixture.Create <TokenType>(); Mock <ITokenHelper> tokenHelperMock = BuildTokenHelperMock(tokenType); ITokenHelperFactory sut = CreateSut(new[] { tokenHelperMock.Object }); HttpContext httpContext = CreateHttpContext(); string base64Token = _fixture.Create <string>(); await sut.StoreTokenAsync(tokenType, httpContext, base64Token); tokenHelperMock.Verify(m => m.StoreTokenAsync( It.Is <HttpContext>(value => value == httpContext), It.Is <string>(value => string.CompareOrdinal(value, base64Token) == 0)), Times.Once); }
public void StoreTokenAsync_WhenBase64TokenIsWhiteSpace_ThrowsArgumentNullException() { ITokenHelperFactory sut = CreateSut(); ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.StoreTokenAsync(_fixture.Create <TokenType>(), CreateHttpContext(), " ")); Assert.That(result.ParamName, Is.EqualTo("base64Token")); }
public void StoreTokenAsync_WhenCalledWithUnsupportedTokenType_ThrowsNotSupportedException() { ITokenHelperFactory sut = CreateSut(); TokenType tokenType = _fixture.Create <TokenType>(); NotSupportedException result = Assert.ThrowsAsync <NotSupportedException>(async() => await sut.StoreTokenAsync(tokenType, CreateHttpContext(), _fixture.Create <string>())); Assert.That(result.Message, Is.EqualTo($"The token type '{tokenType}' is not supported within the method 'StoreTokenAsync'.")); }
public void StoreTokenAsync_WhenHttpContextIsNull_ThrowsArgumentNullException() { ITokenHelperFactory sut = CreateSut(); ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.StoreTokenAsync(_fixture.Create <TokenType>(), null, _fixture.Create <string>())); Assert.That(result.ParamName, Is.EqualTo("httpContext")); }