From 2502e01efdf57ee8ffc9ae4ccf1c316d3d9781bc Mon Sep 17 00:00:00 2001 From: Onur BIYIK Date: Sat, 14 Nov 2020 22:15:08 +0300 Subject: [PATCH] Fix for Nullreference exception when public api authenticate endpoint called with invalid username. (#482) --- src/PublicApi/AuthEndpoints/Authenticate.cs | 6 +++++- .../PublicApi/AuthEndpoints/AuthenticateEndpoint.cs | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PublicApi/AuthEndpoints/Authenticate.cs b/src/PublicApi/AuthEndpoints/Authenticate.cs index d542f609f..f2dfcb812 100644 --- a/src/PublicApi/AuthEndpoints/Authenticate.cs +++ b/src/PublicApi/AuthEndpoints/Authenticate.cs @@ -42,7 +42,11 @@ public override async Task> HandleAsync(Authe response.IsNotAllowed = result.IsNotAllowed; response.RequiresTwoFactor = result.RequiresTwoFactor; response.Username = request.Username; - response.Token = await _tokenClaimsService.GetTokenAsync(request.Username); + + if (result.Succeeded) + { + response.Token = await _tokenClaimsService.GetTokenAsync(request.Username); + } return response; } diff --git a/tests/FunctionalTests/PublicApi/AuthEndpoints/AuthenticateEndpoint.cs b/tests/FunctionalTests/PublicApi/AuthEndpoints/AuthenticateEndpoint.cs index 4232e9c86..19c0de46b 100644 --- a/tests/FunctionalTests/PublicApi/AuthEndpoints/AuthenticateEndpoint.cs +++ b/tests/FunctionalTests/PublicApi/AuthEndpoints/AuthenticateEndpoint.cs @@ -24,6 +24,7 @@ public AuthenticateEndpoint(ApiTestFixture factory) [Theory] [InlineData("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)] [InlineData("demouser@microsoft.com", "badpassword", false)] + [InlineData("baduser@microsoft.com", "badpassword", false)] public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult) { var request = new AuthenticateRequest() @@ -38,6 +39,6 @@ public async Task ReturnsExpectedResultGivenCredentials(string testUsername, str var model = stringResponse.FromJson(); Assert.Equal(expectedResult, model.Result); - } + } } }