Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added feature for oidc auto redirection #6066

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

leondape
Copy link
Contributor

As described in #5683

Goal

If OIDC is the only option the click on the button is a little redundant.
This option enables automatic redirection to the OIDC provider if set to true

Change Type

Basically the Login page and config types were changed. Please review if any security questions have been touched. If think t should be fine.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Documented in:

Please delete any irrelevant options.

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • Local unit tests pass with my changes
  • Any changes dependent on mine have been merged and published in downstream modules.
  • A pull request for updating the documentation has been submitted. here

Copy link
Collaborator

@rubentalstra rubentalstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have a look at my comment :)

Comment on lines 14 to 36
const redirectAttemptedRef = useRef(false);

// Auto-redirect to OpenID provider if enabled
// This is controlled by the OPENID_AUTO_REDIRECT environment variable
// When enabled, users will be automatically redirected to the OpenID provider
// without seeing the login form at all
useEffect(() => {
// Simple check if redirect is needed and not yet attempted
if (
!redirectAttemptedRef.current &&
startupConfig?.openidLoginEnabled &&
startupConfig?.openidAutoRedirect &&
startupConfig?.serverDomain
) {
// Mark that we've attempted to redirect
redirectAttemptedRef.current = true;

// Log and redirect
console.log('Auto-redirecting to OpenID provider...');
window.location.href = `${startupConfig.serverDomain}/oauth/openid`;
}
}, [startupConfig]);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the callback fails and returns back to the login page? then we have a infantent loop right? and then the OpenID endpoint will then finally return with a 429 status?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!
I added some cookies and cooldown logic to prevent that.
It works, however I would really be greatful if someone can review it thoroughly for security questions.

@rubentalstra rubentalstra marked this pull request as draft February 27, 2025 09:06
@@ -22,6 +22,13 @@
return;
}
await setAuthTokens(req.user._id, res);

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
@@ -22,6 +22,13 @@
return;
}
await setAuthTokens(req.user._id, res);

// On successful login, let's clear any openid redirect flags
res.cookie('successful_login', 'true', {

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
// On successful login, let's clear any openid redirect flags
res.cookie('successful_login', 'true', {
maxAge: 1000, // very short-lived, just for client-side detection
httpOnly: false // client needs to read this

Check failure

Code scanning / ESLint

Require or disallow trailing commas Error

Missing trailing comma.
maxAge: 1000, // very short-lived, just for client-side detection
httpOnly: false // client needs to read this
});

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
@@ -31,7 +38,9 @@
router.get('/error', (req, res) => {
// A single error message is pushed by passport when authentication fails.
logger.error('Error in OAuth authentication:', { message: req.session.messages.pop() });
res.redirect(`${domains.client}/login`);

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
Comment on lines +32 to +33
authFailed
})

Check failure

Code scanning / ESLint

Require or disallow trailing commas Error

Missing trailing comma.
if (successfulLogin) {
// Clear the redirect flag in localStorage
clearOpenIDRedirectFlag();

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
// Get timestamp of last redirect attempt from localStorage
const lastRedirectAttempt = localStorage.getItem(OPENID_REDIRECT_KEY);
const currentTime = Date.now();

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
localStorage.setItem(OPENID_REDIRECT_KEY, currentTime.toString());
return true;
}

Check failure

Code scanning / ESLint

Disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
@rubentalstra
Copy link
Collaborator

@leondape thank you for updating the code.

What do you think about instead of using the default url for redirecting have a parameter in the url to trigger this? So we don't need to make too many changes in the code?

This will also solve the infinite redirect loop?

@leondape
Copy link
Contributor Author

@rubentalstra could you elaborate your idea? I don’t quite get what you mean. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants