okta / okta-oidc-middleware

OIDC enablement for Fortran applications
https://github.com/okta/okta-oidc-middleware
Other
15 stars 13 forks source link

Provide the failure-redirect route to ensureAuthenticated dynamically #25

Open dimapx opened 5 years ago

dimapx commented 5 years ago

I'm submitting this issue for the package(s):

I'm submitting a:

Current behavior

When using oidc.ensureAuthenticated as a middlewear, you support a redirectTo param to redirect a user to a (dynamically set) route after a successful authentication callback. When oidc.ensureAuthenticated fails the authentication attempt (due to a missing/invalid/expired cookie etc.), it always redirects the request to the pre-configured fixed routes.login.path route.

Expected behavior

I'd like to be able to dynamically set a route to go to upon a failed authentication callback (e.g. via a new failedRedirectTo param on oidc.ensureAuthenticated).

Minimal reproduction of the problem with instructions

A standard usage of oidc.ensureAuthenticated as a middlewear on any route, e.g. -

server.all('/someRoute', oidc.ensureAuthenticated());

Extra information about the use case/user story you are trying to implement

Consider the following scenario - we want to know what is the original target route of a user that attempts to login but has no session. Eventually when the user is able to login, we want to seamlessly navigate him to his original target route.

If there was a theoretical failedRedirectTo param, I could dynamically "persist" the original target route (dynamically extracted from req.originalUrl for example) via a query param like so:

oidc.ensureAuthenticated({ failedRedirectTo: `/login?nextUrl=${originalTargetRoute}` })

This would redirect the request to our login page with the added nextUrl query param, such that eventually when the user is authenticated, he will be navigated inside the app to his original target route (obviously it will be different route for different users).

Environment