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

refacto(auth): improve type + remove complexity #9949

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

AMoreaux
Copy link
Contributor

@AMoreaux AMoreaux commented Jan 31, 2025

  • Improve type
  • Remove unnecessary code
  • Fix the issue that prevents the usage of invitations when a user signs in with social media.
  • Add Microsoft icon for sso list page

Introduce `forceSubdomainUrl` for SSO login flow to handle subdomain-specific redirections. Updated guards, controllers, and services to incorporate the new parameter while removing redundant dependencies like `EnvironmentService`. Also added utility hooks and modified UI components to support the feature seamlessly.
@AMoreaux AMoreaux requested a review from Weiko January 31, 2025 13:55
@AMoreaux AMoreaux self-assigned this Jan 31, 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 refactors the authentication system to improve type safety and simplify the social media sign-in flow, particularly focusing on invitation handling and state management.

  • Standardized invitation handling across auth strategies by moving from req.params to req.query for invite-related parameters in /packages/twenty-server/src/engine/core-modules/auth/strategies/*.strategy.ts
  • Added proper type definitions and interfaces for SAML, OIDC, Google, and Microsoft requests in respective strategy files
  • Simplified state management by removing unnecessary conditional spreading of optional parameters in auth strategies
  • Improved error handling with custom AuthException and proper state validation in SSO controllers
  • Consolidated invitation check logic to only depend on currentWorkspace and email in auth controllers

9 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +162 to 163
payload: { email: string },
identityProvider: WorkspaceSSOIdentityProvider,
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: The payload type is too restrictive - OIDC and SAML responses typically include firstName and lastName which are used in formatUserDataPayload()

options = {
...options,
state: JSON.stringify({
workspaceInviteHash: req.params.workspaceInviteHash,
workspaceInviteHash: req.query.workspaceInviteHash,
workspaceId: req.params.workspaceId,
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: workspaceId is still being read from req.params while other parameters were moved to req.query. This inconsistency could cause issues with workspace ID retrieval.

@@ -30,7 +45,7 @@ export class OIDCAuthStrategy extends PassportStrategy(
});
}

async authenticate(req: any, options: any) {
async authenticate(req: Request, options: any) {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: options parameter should be properly typed instead of 'any'

async validate(
req: Request,
tokenset: TokenSet,
done: (err: any, user?: OIDCRequest['user']) => void,
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 properly typed instead of 'any'

Comment on lines +89 to +91
if (!userinfo.email || !isEmail(userinfo.email)) {
return done(new Error('Invalid email'));
}
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 message should use AuthException with proper error code for consistency with extractState method

Suggested change
if (!userinfo.email || !isEmail(userinfo.email)) {
return done(new Error('Invalid email'));
}
if (!userinfo.email || !isEmail(userinfo.email)) {
return done(new AuthException('Invalid email', AuthExceptionCode.INVALID_INPUT));
}

};
}

throw new 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: throwing empty Error here is redundant since it's immediately caught and transformed

Updated the icon mapping logic to recognize Microsoft SSO URLs and return the appropriate icon. Also corrected the import and usage of the Microsoft icon asset to ensure consistency.
Copy link
Member

@Weiko Weiko left a comment

Choose a reason for hiding this comment

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

LGTM

@AMoreaux AMoreaux merged commit b801307 into main Jan 31, 2025
47 checks passed
@AMoreaux AMoreaux deleted the refacto/remove-unecessary-complexity-in-auth branch January 31, 2025 14:39
Copy link
Contributor

Fails
🚫

node failed.

Log

�[31mError: �[39m SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
�[90m    at parseJSONFromBytes (node:internal/deps/undici/undici:5591:19)�[39m
�[90m    at successSteps (node:internal/deps/undici/undici:5562:27)�[39m
�[90m    at fullyReadBody (node:internal/deps/undici/undici:1665:9)�[39m
�[90m    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)�[39m
�[90m    at async specConsumeBody (node:internal/deps/undici/undici:5571:7)�[39m
danger-results://tmp/danger-results-eaf3b6b1.json

Generated by 🚫 dangerJS against 5b9c9c5

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