IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Unable to change Login url from ProcessLoginAsync result

Open slaneyrw opened this issue 8 years ago • 17 comments

Issue / Steps to reproduce the problem

Override AuthorizeInteractionResponseGenerator.ProcessLoginAsync to set RedirectUrl for custom page on login.

If you don't set IsLogin to false LoginPageResult doesn't use the RedirectUrl value of the incoming ValidatedAuthorizeRequest in preference to the default system LoginUrl.

If you set IsLogin to false, the default implementation in ProcessInteractionAsync assumes it is a Consent request and calls ProcessConsentAsync. This will have 2 different outcomes depending on whether the client is configured to require consent. If true, exception is thrown as no user is logged in. If false, new InteractionResponse is created and (ultimately) an exception is thrown when trying to generate an AuthorizationCode, as the user is not logged in. Either way it goes boom!

It appears I'm unable to influence the login Url.

slaneyrw avatar Jan 05 '18 02:01 slaneyrw

The login URL is fixed. Why do you want to change it?

leastprivilege avatar Jan 05 '18 07:01 leastprivilege

A few use cases...

  1. Client wants to go directly to Register page instead of login
  2. Once logged in, the client wants to deep link into one of our account management pages. We need to re-authenticate the user because claims might change.
  3. We have multiple types of login pages, depending on client.

Ultimately I'm trying to migrate functionality from the old IdSrvr3 PreAuthenticationUser handler that allowed me to supply a custom redirect URL based on various information supplied in the authorize endpoint request.

Original SO question https://stackoverflow.com/questions/48087042/controlling-target-redirect-page-from-authorize-endpoint/

slaneyrw avatar Jan 05 '18 07:01 slaneyrw

But you can dispatch all that from the login URL. In a sense the LoginUrl is the new PreAuthenticate

leastprivilege avatar Jan 05 '18 08:01 leastprivilege

Ok, then I would have to unpack the returnUrl parameter. So now I have a intermediary controller action whose sole purpose in life is to redirect to the correct target page.

I would prefer to do that in the ProcessLoginAsync method where all the contextual information I need to at hand.

I have a workaround in that I override the ProcessInteractionAsync, wait for the base.InteractionResponseAsync result. If IsLogin and has RedirectUrl, I change the IsLogin to false and allow the CustomRedirectResult to redirect to take over

CustomRedirectResult and LoginPageResult do exactly the same handling with the exception of the url.

https://github.com/IdentityServer/IdentityServer4/blob/ad945f4204f19ce7dca40e29e51eb2b7bee3b918/src/IdentityServer4/Endpoints/AuthorizeEndpointBase.cs#L82-L97

slaneyrw avatar Jan 05 '18 08:01 slaneyrw

Ok, then I would have to unpack the returnUrl parameter.

Which we designed for:

https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/blob/798b3b6ac869e3dd6dc2a497898828a24e0c5164/Quickstart/Account/AccountService.cs#L38

leastprivilege avatar Jan 05 '18 09:01 leastprivilege

Now you're assuming the login url is in the same website and has access to the extension.

I can see that we aren't going to agree on this, can you please ensure lines 94 to 97 of AuthoriseEndpointBase.ProcessAuthorizeRequestAsync are not removed. I cannot find any existing code in AuthorizeInteractionResponseGenerator that will ever cause it to execute a CustomRedirectResult so could be a candidate for dead code removal.

slaneyrw avatar Jan 05 '18 10:01 slaneyrw

... oh and thank you for the fine product

slaneyrw avatar Jan 05 '18 11:01 slaneyrw

Now you're assuming the login url is in the same website and has access to the extension.

Did you ever mention that before?

leastprivilege avatar Jan 05 '18 11:01 leastprivilege

oh and thank you for the fine product

thanks

leastprivilege avatar Jan 05 '18 11:01 leastprivilege

The RedirectUrl on the InteractionResponse was only ever intended for redirecting to a custom page, not for dynamically changing the URL for the login page.

If you want to send the user to a custom page, then override all of ProcessInteractionAsync and set & return your custom RedirectUrl. Does that not work for you?

The other approach would be to add IsRedirect to the if (result.IsLogin || result.IsError) check in the existing ProcessInteractionAsync.

brockallen avatar Jan 07 '18 15:01 brockallen

Which uses cases cannot be done with the current logic?

leastprivilege avatar Jan 08 '18 10:01 leastprivilege

Which uses cases cannot be done with the current logic?

I think all of the use cases can be done with the current logic, unless @slaneyrw can come up with one.

brockallen avatar Jan 08 '18 15:01 brockallen

@slaneyrw any update?

brockallen avatar Jan 09 '18 20:01 brockallen

Closing. If you still have issues, feel free to reopen.

brockallen avatar Jan 12 '18 18:01 brockallen

Sorry, was out of internet range and couldn't comment ( school holidays and camping! )...

There are a number of other workarounds, like the "Login" url being an intermediary that will the further redirect but I loose some information that is dropped when the returnURL param is generated, like prompt.

I would prefer to avoid duplicating all the ProcessInteractionAsync code prior to the IsLogin | IsError check, I will have to keep that in sync if you ever change it's behaviour

A dirty hack it to return a IsLogin interaction response from ProcessLoginAsync but with the RedirectUrl property set. This bypasses the ProcessConsentAsync flow ( and avoiding the error ). Then overriding the default ProcessInteractionAsync return to set the IsLogin to false before the AuthorizeEndpointBase executes the result.

    public async override Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
    {
        var result = await base.ProcessInteractionAsync(request, consent);

        if ( result.IsLogin && result.IsRedirect)
        {
            result.IsLogin = false;
        }

        return result;
    }

    protected async override Task<InteractionResponse> ProcessLoginAsync(ValidatedAuthorizeRequest request)
    {
        var result = await base.ProcessLoginAsync(request);

        if (result.IsLogin)
        {
                // Implementation of GetTargetPage determines where to send the user, like Login, Register, Edit Profile, etc
                var targetPage = GetTargetPage(request.AuthenticationContextReferenceClasses);
                result.RedirectUrl = targetPage;
        }

        return result;
    }

slaneyrw avatar Jan 23 '18 21:01 slaneyrw

I agree with @slaneyrw that an intermediary redirect does not seem to be a good technical solution. Why force the user to go through additional communication with the server when all relevant information is available in the first /authorize call already.

Our use case is a multi-tenant architecture, where the service is deciding on the login URL programmatically, where the various different URLs contain various different hostnames, etc.

Unfortunately using the custom redirect workaround leads to CustomRedirectResult, which in the end passes the /authorize endpoint, not /authorize/callback as the return URL.

mbodecek avatar Jan 31 '19 17:01 mbodecek

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 11 '20 21:01 lock[bot]