/// <inheritdoc/> protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { this.Logger.LogInformation("starting authentication handler for app service authentication"); if (this.canUseHeaderAuth(this.Context.Request.Headers, this.Context.User)) { return(EasyAuthWithHeaderService.AuthUser(this.Logger, this.Context, this.Options)); } else if (CanUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options)) { return(await EasyAuthWithAuthMeService.AuthUser(this.Logger, this.Context, this.Options)); } else { if (IsContextUserNotAuthenticated(this.Context.User)) { this.Logger.LogInformation("The identity isn't set by easy auth."); } else { this.Logger.LogInformation("identity already set, skipping middleware"); } return(AuthenticateResult.NoResult()); } }
public void IfTheAuthorizationHeaderIsNotSetTheCanUseMethodMustReturnFalse() { // Arrange var handler = new EasyAuthWithHeaderService(this.loggerFactory.CreateLogger <EasyAuthWithHeaderService>()); var httpcontext = new DefaultHttpContext(); // Act var result = handler.CanHandleAuthentification(httpcontext); // Arrange Assert.False(result); }
public void IfTheAADIdTokenHeaderIsSetTheCanUseMethodMustReturnTrue() { // Arrange var handler = new EasyAuthWithHeaderService(this.loggerFactory.CreateLogger <EasyAuthWithHeaderService>()); var httpcontext = new DefaultHttpContext(); httpcontext.Request.Headers.Add("X-MS-TOKEN-AAD-ID-TOKEN", "blup"); // Act var result = handler.CanHandleAuthentification(httpcontext); // Arrange Assert.True(result); }
public void IfAValidJwtTokenIsInTheHeaderTheResultIsSuccsess() { // Arrange var handler = new EasyAuthWithHeaderService(this.loggerFactory.CreateLogger <EasyAuthWithHeaderService>()); var httpcontext = new DefaultHttpContext(); var inputObject = new InputJson() { Claims = new List <InputClaims>() { new InputClaims() { Typ = "x", Value = "y" }, new InputClaims() { Typ = ClaimTypes.Email, Value = "PrincipalName" }, new InputClaims() { Typ = ClaimTypes.Role, Value = "Admin" } } }; var json = JsonConvert.SerializeObject(inputObject); httpcontext.Request.Headers.Add("X-MS-TOKEN-AAD-ID-TOKEN", "Blup"); httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL-IDP", "providername"); httpcontext.Request.Headers.Add("X-MS-CLIENT-PRINCIPAL", Base64Encode(json)); // Act var result = handler.AuthUser(httpcontext); // Arrange Assert.True(result.Succeeded); Assert.Equal("PrincipalName", result.Principal.Identity.Name); Assert.True(result.Principal.IsInRole("Admin")); }