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

feat(auth): centralize SSO error handling logic #9832

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

Conversation

AMoreaux
Copy link
Contributor

Introduce SsoErrorRedirectService to handle SSO error redirection and exception capturing across the authentication controllers. Refactor Microsoft, Google, and SSO authentication controllers to utilize this service, replacing the previous direct calls to DomainManagerService. Added unit tests for the new service to ensure robust error handling.

Introduce `SsoErrorRedirectService` to handle SSO error redirection and exception capturing across the authentication controllers. Refactor Microsoft, Google, and SSO authentication controllers to utilize this service, replacing the previous direct calls to `DomainManagerService`. Added unit tests for the new service to ensure robust error handling.
@AMoreaux AMoreaux self-assigned this Jan 24, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR introduces a new SsoErrorRedirectService to centralize error handling across SSO authentication flows, improving code organization and maintainability.

  • Added SsoErrorRedirectService in /auth/services/sso-error-redirect.service.ts to handle error redirection and exception capturing
  • Refactored Microsoft, Google, and SSO auth controllers to use the new service, removing direct DomainManagerService dependencies
  • Added comprehensive test coverage in sso-error-redirect.spec.ts for various error scenarios
  • Improved error handling consistency by capturing and redirecting all error types uniformly
  • Simplified OIDC issuer discovery logic in sso.service.ts with better error handling patterns

8 file(s) reviewed, 11 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 118 to 127
} catch (err) {
if (err instanceof AuthException) {
return res.redirect(
this.domainManagerService.computeRedirectErrorUrl(
err.message,
currentWorkspace?.subdomain ??
this.environmentService.get('DEFAULT_SUBDOMAIN'),
),
);
}
throw new AuthException(err, AuthExceptionCode.INTERNAL_SERVER_ERROR);
return res.redirect(
this.ssoErrorService.getRedirectErrorUrlAndCaptureExceptions(
err,
currentWorkspace ?? {
subdomain: this.environmentService.get('DEFAULT_SUBDOMAIN'),
},
),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: error handling now captures all types of errors, not just AuthException, which could expose internal server errors to users if not properly sanitized in SsoErrorRedirectService

Comment on lines 26 to 27
err: any,
workspace: { id?: string; subdomain: string },
Copy link
Contributor

Choose a reason for hiding this comment

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

style: err parameter should be typed more specifically than 'any'. Consider creating a union type of expected error types (AuthException | Error | unknown)

Comment on lines 31 to 33
!(err instanceof AuthException) ||
('code' in err && err.code === 'INTERNAL_SERVER_ERROR')
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: this condition will capture exceptions for non-AuthExceptions OR AuthExceptions with INTERNAL_SERVER_ERROR code. The logic may need to be reversed to capture ONLY non-AuthExceptions and INTERNAL_SERVER_ERROR AuthExceptions

const email = '[email protected]';
const mockWorkspace = { id: 'workspace-id-456' } as Workspace;

jest.spyOn(environmentService, 'get').mockReturnValue(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Missing test for environmentService.get() returning false - should test both enabled and disabled multi-workspace scenarios

Comment on lines 40 to 42
} catch (err) {
this.exceptionHandlerService.captureExceptions([err]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: catching and capturing errors from the exception handler itself could create an infinite loop if the exception handler throws

Comment on lines 15 to 23
private getErrorMessage(err: any) {
try {
return err.message && err.message.length !== 0
? err.message
: 'Unknown error';
} catch (err) {
return 'Unknown error';
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: try-catch block here is unnecessary since err.message access won't throw - the ternary is sufficient


import { SsoErrorRedirectService } from './sso-error-redirect.service';

describe('SsoErrorService', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax: test suite name 'SsoErrorService' doesn't match the actual service name 'SsoErrorRedirectService'

Comment on lines 94 to 96
if (err instanceof SSOException) {
return err;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Returning the error directly instead of throwing it creates inconsistent error handling. Consider throwing the error to maintain consistency with other methods

Integrate ExceptionHandlerService into SSOService and refine error capturing in SsoErrorRedirectService. Streamlined error handling logic by consolidating exception capture methods and improving maintainability.
@AMoreaux AMoreaux removed the request for review from charlesBochet January 24, 2025 15:59
@AMoreaux AMoreaux marked this pull request as draft January 24, 2025 15:59
…vice

Removed SsoErrorRedirectService and its related tests, consolidating its functionality into GuardRedirectService. Updated all references across the codebase to use GuardRedirectService for error handling and redirection in guards and controllers. This change improves modularity and simplifies the error handling logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants