IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Additional client validation and a proper response just like "UnauthorizedClient"

Open jmatheti opened this issue 8 years ago • 3 comments

Scenario, Client passed acr_values tenant, validate the tenant and client. If validation fails return a error message "UnauthorizedClient".

on token request, validate user and client and return appropriate message through.

CustomTokenRequestValidationContext .Result.Error/ErrorDescription is not been handled to the end of the request.

CustomTokenRequestValidationContext .Result.IsError=true , results and exception, I assume given the error it will handled in the pipeline instead.

.AddCustomAuthorizeRequestValidator<TenantAuthorizeRequestValidator>() .AddCustomTokenRequestValidator<TenantTokenRequestValidator>()

Implementation is like

public class TenantTokenRequestValidator : ICustomTokenRequestValidator
   {
       HttpContext _context;
       IClientService _clientService;
       public TenantTokenRequestValidator(IHttpContextAccessor contextAccessor, IClientService clientService)
       {
           _context = contextAccessor.HttpContext;
           _clientService = clientService;
       }

       public Task ValidateAsync(CustomTokenRequestValidationContext context)
       {
           if (!context.Result.IsError)
           {
               if (!_clientService.IsValidUser())
                   context.Result.Error = "UnauthorizedUser";
           }

           return Task.CompletedTask;
       }
   }

public class TenantAuthorizeRequestValidator : ICustomAuthorizeRequestValidator
   {
       HttpContext _context;
       ITenantService _tenantService;
       public TenantAuthorizeRequestValidator(IHttpContextAccessor contextAccessor,ITenantService tenantService)
       {
           _context = contextAccessor.HttpContext;
           _tenantService = tenantService;
       }
       public Task ValidateAsync(CustomAuthorizeRequestValidationContext context)
       {
           if (!context.Result.IsError)
           {
               if(!_tenantService.IsValidClient())
                   context.Result.ErrorDescription ="UnauthorizedClient";
           }

           return Task.CompletedTask;
       }
   }

ResourceOwnerClient

The token response is ignoring the error passed in ICustomTokenRequestValidator

var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret"); object optional = new { acr_values = "tenant:resourceownerclient" }; var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice", "Pass123$", "api1",optional);

jmatheti avatar Apr 12 '18 02:04 jmatheti

one of your earlier response was not to set the error=true. Recommended to leave the message the it is handled well. Seems like it is not correct now. I have crawl through the code and fixed it with the below code.

context.Result.IsError = true; context.Result.Error = OidcConstants.AuthorizeErrors.UnauthorizedClient;

However when using password grant type with resourceowner flow, noticed the flow is first validates the client (ICustomAuthorizevalidator doesn't call), then on token validation after issuing the token then customtokenvalidation is taking place.

ValidateRequestAsync() --Start token request validation ValidateResourceOwnerCredentialRequestAsync --Start resource owner password token request validation missing customauthorizerequestvalidator, if not the right context, then something similar is required here Expected customtokenvalidationhere --Resource owner password token request validation success RunValidationAsync(ValidateResourceOwnerCredentialRequestAsync, parameters) --Custom token request validator error {error} Return type return customValidationContext.Result; instead of return Invalid(stomValidationContext.Result.Error);

jmatheti avatar Apr 12 '18 04:04 jmatheti

All set on this issue -- can we close?

brockallen avatar May 03 '18 13:05 brockallen

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 09:01 lock[bot]