Skip to content

Commit

Permalink
https://github.com/danny-avila/LibreChat/issues/2812
Browse files Browse the repository at this point in the history
  • Loading branch information
rubentalstra committed Feb 3, 2025
1 parent d93f5c9 commit 31925af
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_CALLBACK_URL=/oauth/github/callback

# GitHub Enterprise
GITHUB_ENTERPRISE_BASE_URL=
GITHUB_ENTERPRISE_CLIENT_ID=
GITHUB_ENTERPRISE_CLIENT_SECRET=
GITHUB_ENTERPRISE_CALLBACK_URL=/oauth/github-enterprise/callback
# optional
GITHUB_ENTERPRISE_USER_AGENT=

# Google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
Expand Down
6 changes: 6 additions & 0 deletions api/models/schema/userSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { SystemRoles } = require('librechat-data-provider');
* @property {string} [openidId] - Optional OpenID ID for the user
* @property {string} [ldapId] - Optional LDAP ID for the user
* @property {string} [githubId] - Optional GitHub ID for the user
* @property {string} [githubEnterpriseId] - Optional GitHub Enterprise ID for the user
* @property {string} [discordId] - Optional Discord ID for the user
* @property {string} [appleId] - Optional Apple ID for the user
* @property {Array} [plugins=[]] - List of plugins used by the user
Expand Down Expand Up @@ -107,6 +108,11 @@ const userSchema = mongoose.Schema(
unique: true,
sparse: true,
},
githubEnterpriseId: {
type: String,
unique: true,
sparse: true,
},
discordId: {
type: String,
unique: true,
Expand Down
5 changes: 5 additions & 0 deletions api/server/routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ router.get('/', async function (req, res) {
facebookLoginEnabled:
!!process.env.FACEBOOK_CLIENT_ID && !!process.env.FACEBOOK_CLIENT_SECRET,
githubLoginEnabled: !!process.env.GITHUB_CLIENT_ID && !!process.env.GITHUB_CLIENT_SECRET,
githubEnterpriseLoginEnabled:
!!process.env.GITHUB_ENTERPRISE_BASE_URL &&
!!process.env.GITHUB_ENTERPRISE_CLIENT_ID &&
!!process.env.GITHUB_ENTERPRISE_CLIENT_SECRET &&
!!process.env.GITHUB_ENTERPRISE_CALLBACK_URL,
googleLoginEnabled: !!process.env.GOOGLE_CLIENT_ID && !!process.env.GOOGLE_CLIENT_SECRET,
appleLoginEnabled:
!!process.env.APPLE_CLIENT_ID &&
Expand Down
24 changes: 23 additions & 1 deletion api/server/routes/oauth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// file deepcode ignore NoRateLimitingForLogin: Rate limiting is handled by the `loginLimiter` middleware
// routes/oauth.js
const express = require('express');
const passport = require('passport');
const { loginLimiter, checkBan, checkDomainAllowed } = require('~/server/middleware');
Expand Down Expand Up @@ -122,6 +122,28 @@ router.get(
oauthHandler,
);

/**
* GitHub Enterprise Routes
*/
router.get(
'/github-enterprise',
passport.authenticate('githubEnterprise', {
scope: ['user:email', 'read:user'],
session: false,
}),
);

router.get(
'/github-enterprise/callback',
passport.authenticate('githubEnterprise', {
failureRedirect: `${domains.client}/oauth/error`,
failureMessage: true,
session: false,
scope: ['user:email', 'read:user'],
}),
oauthHandler,
);

/**
* Discord Routes
*/
Expand Down
6 changes: 5 additions & 1 deletion api/server/socialLogins.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
setupOpenId,
googleLogin,
githubLogin,
githubEnterpriseLogin,
discordLogin,
facebookLogin,
appleLogin,
Expand All @@ -28,6 +29,9 @@ const configureSocialLogins = (app) => {
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
passport.use(githubLogin());
}
if (process.env.GHE_CLIENT_ID && process.env.GHE_CLIENT_SECRET) {
passport.use( githubEnterpriseLogin());
}
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
passport.use(discordLogin());
}
Expand Down Expand Up @@ -64,4 +68,4 @@ const configureSocialLogins = (app) => {
}
};

module.exports = configureSocialLogins;
module.exports = configureSocialLogins;
31 changes: 31 additions & 0 deletions api/strategies/githubEnterpriseStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const GitHubStrategy = require('passport-github2').Strategy;
const socialLogin = require('./socialLogin');

const getProfileDetails = ({ profile }) => ({
email: profile.emails[0].value,
id: profile.id,
avatarUrl: profile.photos[0].value,
username: profile.username,
name: profile.displayName,
emailVerified: profile.emails[0].verified,
});

const githubEnterpriseLogin = socialLogin('githubEnterprise', getProfileDetails);

module.exports = () =>
new GitHubStrategy(
{
name: 'githubEnterprise',
clientID: process.env.GITHUB_ENTERPRISE_CLIENT_ID,
clientSecret: process.env.GITHUB_ENTERPRISE_CLIENT_SECRET,
callbackURL: `${process.env.DOMAIN_SERVER}${process.env.GITHUB_ENTERPRISE_CALLBACK_URL}`,
authorizationURL: `${process.env.GITHUB_ENTERPRISE_BASE_URL}/login/oauth/authorize`,
tokenURL: `${process.env.GITHUB_ENTERPRISE_BASE_URL}/login/oauth/access_token`,
userProfileURL: `${process.env.GITHUB_ENTERPRISE_BASE_URL}/api/v3/user`,
userEmailURL: `${process.env.GITHUB_ENTERPRISE_BASE_URL}/api/v3/user/emails`,
userAgent: process.env.GITHUB_ENTERPRISE_USER_AGENT || 'passport-github',
scope: ['user:email', 'read:user'],
proxy: false,
},
githubEnterpriseLogin,
);
4 changes: 3 additions & 1 deletion api/strategies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const appleLogin = require('./appleStrategy');
const passportLogin = require('./localStrategy');
const googleLogin = require('./googleStrategy');
const githubLogin = require('./githubStrategy');
const githubEnterpriseLogin = require('./githubEnterpriseStrategy');
const discordLogin = require('./discordStrategy');
const facebookLogin = require('./facebookStrategy');
const setupOpenId = require('./openidStrategy');
Expand All @@ -13,9 +14,10 @@ module.exports = {
passportLogin,
googleLogin,
githubLogin,
githubEnterpriseLogin,
discordLogin,
jwtLogin,
facebookLogin,
setupOpenId,
ldapLogin,
};
};
11 changes: 11 additions & 0 deletions client/src/components/Auth/SocialLoginRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function SocialLoginRender({
id="github"
/>
),
githubEnterprise: startupConfig.githubEnterpriseLoginEnabled && (
<SocialButton
key="github-enterprise"
enabled={startupConfig.githubEnterpriseLoginEnabled}
serverDomain={startupConfig.serverDomain}
oauthPath="github-enterprise"
Icon={GithubIcon}
label={localize('com_auth_github_enterprise_login')}
id="github-enterprise"
/>
),
google: startupConfig.googleLoginEnabled && (
<SocialButton
key="google"
Expand Down
1 change: 1 addition & 0 deletions client/src/localization/languages/Eng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ export default {
com_auth_google_login: 'Continue with Google',
com_auth_facebook_login: 'Continue with Facebook',
com_auth_github_login: 'Continue with Github',
com_auth_github_enterprise_login: 'Continue with Github Enterprise',
com_auth_discord_login: 'Continue with Discord',
com_auth_apple_login: 'Sign in with Apple',
com_auth_email: 'Email',
Expand Down
2 changes: 1 addition & 1 deletion librechat.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface:

# Example Registration Object Structure (optional)
registration:
socialLogins: ['github', 'google', 'discord', 'openid', 'facebook', 'apple']
socialLogins: ['github', 'githubEnterprise', 'google', 'discord', 'openid', 'facebook', 'apple']
# allowedDomains:
# - "gmail.com"

Expand Down
1 change: 1 addition & 0 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ export type TStartupConfig = {
discordLoginEnabled: boolean;
facebookLoginEnabled: boolean;
githubLoginEnabled: boolean;
githubEnterpriseLoginEnabled: boolean;
googleLoginEnabled: boolean;
openidLoginEnabled: boolean;
appleLoginEnabled: boolean;
Expand Down

0 comments on commit 31925af

Please sign in to comment.