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: add restricted access on github auth #353

Merged
merged 8 commits into from
Feb 1, 2025

Conversation

olzhik11
Copy link
Contributor

@olzhik11 olzhik11 commented Jan 28, 2025

Added access control on github auth through json file


Important

Added restricted access for GitHub authentication using a JSON file to control allowed emails.

  • Authentication:
    • Added signIn callback in auth.ts to restrict GitHub access based on emails from allowed-emails.json.
    • Introduced getEmailsConfig in server-utils.ts to read allowed emails from JSON.
  • Components:
    • Updated GitHubSignInButton in github-signin.tsx to handle sign-in errors with toast notifications.
    • Simplified EmailSignInButton in email-signin.tsx by removing unused props.
  • Features:
    • Added check for GITHUB_AUTH in features.ts to ensure GitHub credentials are set.
  • Misc:
    • Standardized import quotes across modified files.

This description was created by Ellipsis for 855bacc. It will automatically update as commits are pushed.

Copy link
Member

@dinmukhamedm dinmukhamedm left a comment

Choose a reason for hiding this comment

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

Good job. I left a couple comments

import { signIn } from 'next-auth/react';
import * as React from 'react';
import { signIn } from "next-auth/react";
import * as React from "react";
Copy link
Member

Choose a reason for hiding this comment

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

nit: redundant import

setIsLoading(true);
await signIn("github", { callbackUrl });
} catch (error) {
console.error(error);
Copy link
Member

Choose a reason for hiding this comment

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

should we maybe do toast in addition to/instead of console.log?

const filePath = path.join(process.cwd(), "allowed-emails.json");

if (!fs.existsSync(filePath)) {
console.log("file not found :(");
Copy link
Member

Choose a reason for hiding this comment

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

nit: do we really need this? If this is not debug log I suggest we either remove it or make it more informative

const jsonData = JSON.parse(fileContent) as { emails: string[] };
return jsonData.emails;
} catch (e) {
console.error(e);
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's make this a little more informative, e.g. Failed to read allowed-emails.json ${e}

Comment on lines 15 to 16
const jsonData = JSON.parse(fileContent) as { emails: string[] };
return jsonData.emails;
Copy link
Member

Choose a reason for hiding this comment

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

Even though this may throw TS errors, I think it will silently return undefined for valid JSON files with invalid format. Please handle, by at least returning jsonData.emails ?? [] or better throw an error if jsonData.emails === undefined

Comment on lines 15 to 16
const jsonData = JSON.parse(fileContent) as { emails: string[] };
return jsonData.emails;
Copy link
Member

Choose a reason for hiding this comment

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

Even though this may throw TS errors, I think it will silently return undefined for valid JSON files with invalid format. Please handle, by at least returning jsonData.emails ?? [] or better throw an error if jsonData.emails === undefined

Copy link
Member

Choose a reason for hiding this comment

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

  • we might also want to validate every email, but that might be overkill – up to you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think email validation might be too much

@olzhik11
Copy link
Contributor Author

@dinmukhamedm yes agree with all comments, should have fixed them before, should be good now, for the emails part I agree we need fallback or error throw, I added error throw though if needed we can make fallback for empty list, meaning no user will be able to pass github signin

dinmukhamedm
dinmukhamedm previously approved these changes Jan 31, 2025
Copy link
Member

@dinmukhamedm dinmukhamedm left a comment

Choose a reason for hiding this comment

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

LGTM

@olzhik11 olzhik11 marked this pull request as ready for review January 31, 2025 10:08
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

❌ Changes requested. Reviewed everything up to 855bacc in 1 minute and 57 seconds

More details
  • Looked at 511 lines of code in 6 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. frontend/components/sign-in/email-signin.tsx:43
  • Draft comment:
    The handleEnter prop is not defined in the Button component's props. Consider removing it or implementing it in the Button component.
  • Reason this comment was not posted:
    Comment was on unchanged code.

Workflow ID: wflow_KfkpCB94KpD8oF5l


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

return false;
}

const fileContent = fs.readFileSync(filePath, "utf-8");
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using fs.promises.readFile instead of fs.readFileSync to avoid blocking the event loop in an async function.

Copy link
Member

Choose a reason for hiding this comment

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

@olzhik11 please have a look if that makes sense


const fileContent = fs.readFileSync(filePath, "utf-8");

const jsonData = JSON.parse(fileContent) as { emails?: string[] };
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a try-catch block around JSON.parse to handle potential JSON parsing errors gracefully.

Copy link
Member

Choose a reason for hiding this comment

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

@olzhik11 please address

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dinmukhamedm for this one then we won't be able to throw error if file doesn't match structure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dinmukhamedm I added fallback instead, and throw on file parsing error

@dinmukhamedm dinmukhamedm merged commit 74bc7f8 into lmnr-ai:dev Feb 1, 2025
2 checks passed
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