Skip to content

Commit

Permalink
quick allowedDomain fix
Browse files Browse the repository at this point in the history
  • Loading branch information
berry-13 committed Feb 5, 2024
1 parent b151cd9 commit a715f99
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 104 deletions.
23 changes: 1 addition & 22 deletions api/server/services/AuthService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const crypto = require('crypto');
const bcrypt = require('bcryptjs');
const { registerSchema, errorsToString } = require('~/strategies/validators');
const getCustomConfig = require('~/cache/getCustomConfig');
const isDomainAllowed = require('./isDomainAllowed');
const Token = require('~/models/schema/tokenSchema');
const { sendEmail } = require('~/server/utils');
const Session = require('~/models/Session');
Expand All @@ -13,27 +13,6 @@ const domains = {
server: process.env.DOMAIN_SERVER,
};

async function isDomainAllowed(email) {
if (!email) {
return false;
}

const domain = email.split('@')[1];

if (!domain) {
return false;
}

const customConfig = await getCustomConfig();
if (!customConfig) {
return true;
} else if (!customConfig?.registration?.allowedDomains) {
return true;
}

return customConfig.registration.allowedDomains.includes(domain);
}

const isProduction = process.env.NODE_ENV === 'production';

/**
Expand Down
24 changes: 24 additions & 0 deletions api/server/services/isDomainAllowed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getCustomConfig = require('~/cache/getCustomConfig');

async function isDomainAllowed(email) {
if (!email) {
return false;
}

const domain = email.split('@')[1];

if (!domain) {
return false;
}

const customConfig = await getCustomConfig();
if (!customConfig) {
return true;
} else if (!customConfig?.registration?.allowedDomains) {
return true;
}

return customConfig.registration.allowedDomains.includes(domain);
}

module.exports = isDomainAllowed;
9 changes: 8 additions & 1 deletion api/strategies/discordStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Strategy: DiscordStrategy } = require('passport-discord');
const { createNewUser, handleExistingUser } = require('./process');
const { logger } = require('~/config');
const User = require('~/models/User');
const isDomainAllowed = require('~/server/services/isDomainAllowed');

const discordLogin = async (accessToken, refreshToken, profile, cb) => {
try {
Expand All @@ -27,7 +28,13 @@ const discordLogin = async (accessToken, refreshToken, profile, cb) => {
return cb(null, oldUser);
}

if (ALLOW_SOCIAL_REGISTRATION) {
if (!(await isDomainAllowed(email))) {
const errorMessage = 'Registration from this domain is not allowed.';
logger.error(`[registerUser] [Registration not allowed] [Email: ${email}]`);
return { status: 403, message: errorMessage };
}

if (ALLOW_SOCIAL_REGISTRATION && isDomainAllowed(email)) {
const newUser = await createNewUser({
email,
avatarUrl,
Expand Down
9 changes: 8 additions & 1 deletion api/strategies/facebookStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const FacebookStrategy = require('passport-facebook').Strategy;
const { createNewUser, handleExistingUser } = require('./process');
const { logger } = require('~/config');
const User = require('~/models/User');
const isDomainAllowed = require('~/server/services/isDomainAllowed');

const facebookLogin = async (accessToken, refreshToken, profile, cb) => {
try {
Expand All @@ -17,7 +18,13 @@ const facebookLogin = async (accessToken, refreshToken, profile, cb) => {
return cb(null, oldUser);
}

if (ALLOW_SOCIAL_REGISTRATION) {
if (!(await isDomainAllowed(email))) {
const errorMessage = 'Registration from this domain is not allowed.';
logger.error(`[registerUser] [Registration not allowed] [Email: ${email}]`);
return { status: 403, message: errorMessage };
}

if (ALLOW_SOCIAL_REGISTRATION && isDomainAllowed(email)) {
const newUser = await createNewUser({
email,
avatarUrl,
Expand Down
9 changes: 8 additions & 1 deletion api/strategies/githubStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Strategy: GitHubStrategy } = require('passport-github2');
const { createNewUser, handleExistingUser } = require('./process');
const { logger } = require('~/config');
const User = require('~/models/User');
const isDomainAllowed = require('~/server/services/isDomainAllowed');

const githubLogin = async (accessToken, refreshToken, profile, cb) => {
try {
Expand All @@ -17,7 +18,13 @@ const githubLogin = async (accessToken, refreshToken, profile, cb) => {
return cb(null, oldUser);
}

if (ALLOW_SOCIAL_REGISTRATION) {
if (!(await isDomainAllowed(email))) {
const errorMessage = 'Registration from this domain is not allowed.';
logger.error(`[registerUser] [Registration not allowed] [Email: ${email}]`);
return { status: 403, message: errorMessage };
}

if (ALLOW_SOCIAL_REGISTRATION && isDomainAllowed(email)) {
const newUser = await createNewUser({
email,
avatarUrl,
Expand Down
7 changes: 7 additions & 0 deletions api/strategies/googleStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Strategy: GoogleStrategy } = require('passport-google-oauth20');
const { createNewUser, handleExistingUser } = require('./process');
const { logger } = require('~/config');
const User = require('~/models/User');
const isDomainAllowed = require('~/server/services/isDomainAllowed');

const googleLogin = async (accessToken, refreshToken, profile, cb) => {
try {
Expand All @@ -17,6 +18,12 @@ const googleLogin = async (accessToken, refreshToken, profile, cb) => {
return cb(null, oldUser);
}

if (!(await isDomainAllowed(email))) {
const errorMessage = 'Registration from this domain is not allowed.';
logger.error(`[registerUser] [Registration not allowed] [Email: ${email}]`);
return { status: 403, message: errorMessage };
}

if (ALLOW_SOCIAL_REGISTRATION) {
const newUser = await createNewUser({
email,
Expand Down
79 changes: 0 additions & 79 deletions librechat.example.yaml

This file was deleted.

0 comments on commit a715f99

Please sign in to comment.