LibreChat/api/strategies/openidStrategy.js

124 lines
3.4 KiB
JavaScript
Raw Normal View History

OpenID Authentication (#495) * Squashed commit of the following: commit 26ab03fb36fcc7fcee63fdf3ae8c2dfb29027eff Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:23:23 2023 -0500 Update Registration.spec.tsx commit e908dd82fe9ef1b43c75ee64c183d2f654bdac1c Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:23:01 2023 -0500 Update Login.spec.tsx commit 223734820fb77d7fb5af4802af642d1c1fd7c1f5 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:22:39 2023 -0500 Update Registration.tsx commit 7036d3dd0538979ee397d958ebc113bb0ea32411 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:21:55 2023 -0500 Update Login.tsx commit 76bb78221db3195fd930fe9cfd6a5da7194fa759 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:21:03 2023 -0500 Update envConstants.js commit ee2f69f33d75fbb57022afbcd9564bca38a46bee Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:20:08 2023 -0500 Update docker-compose.yml commit 5ac72d789b3446884c6e2f4f595cbf67d731d43c Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:18:41 2023 -0500 Update Dockerfile commit d24341db2bd5b17eb89ab01e171a5f51f3beab0a Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:16:38 2023 -0500 Update .env.example commit 22154f4a09c5fcdfee95d43609fb01a5a883b7a9 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:07:48 2023 -0500 Update Registration.spec.tsx commit 5163f7d372a6a03c94f4357b358211a03369456e Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:07:30 2023 -0500 Update Login.spec.tsx commit 61da49e330a9376e130b24dc944854f97ab58d80 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:07:00 2023 -0500 Update Registration.tsx commit 0e45d3f0dbde34388ff2f0b2dc51b983b472eb05 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:06:18 2023 -0500 Update Login.tsx commit dca1e5367e5f3b468c7964218cc5914ca53095af Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:05:07 2023 -0500 Update envConstants.js commit f48c058465d82b03716ba85224e9f97007e014d2 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Tue Jun 13 00:04:05 2023 -0500 Update .env.example commit 818226c9cb079acae4fcbfe5997e4aa9e3c6d2cc Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:59:08 2023 -0500 Update .env.example commit 9a805439189b352a38ac7654d7a31bb28f0f58dd Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:58:31 2023 -0500 Update env.d.ts commit 3f37ce54758b017c9281b7fad9b040a47630ec66 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:57:04 2023 -0500 Update .env.example commit 1026036f4dd529e9531c53084450ce768cfca4c1 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:50:36 2023 -0500 Update docker-compose.yml commit a61cf7b8c51d4a9bd73a20bd67abc29891c11463 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:50:00 2023 -0500 Update Dockerfile commit 79610d6648755cd5ec45215b9fdbe04ba8242fcf Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:35:34 2023 -0500 Update package-lock.json commit e40853fd2b77f2db5be1c3dfd8b170d650e23271 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:30:17 2023 -0500 Update envConstants.js commit 5529bc61b43f279fb4418c3851be2f9011b6454d Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:25:58 2023 -0500 Update docker-compose.yml commit 07848cc464a64f7cad484e24a1310dc61aa03b18 Merge: ec628a3 72e9828 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:24:03 2023 -0500 Merge branch 'danny-avila:main' into openid-client commit ec628a3044ba963b4e733c72229400074e7c2bc4 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:23:16 2023 -0500 Update envConstants.js commit 21272221db0f58c244f08335482d45b177d338ab Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:21:59 2023 -0500 Update Registration.spec.tsx commit d3f2949c0484d5760e7b689501852f86209992a3 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:21:12 2023 -0500 Update Login.spec.tsx commit f2cf23ddd6708a3bb8d032dde5f1ce300dbe8cad Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:20:15 2023 -0500 Update Registration.tsx commit 482c346b2a7baf958665c9474223d2557504dee5 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:17:53 2023 -0500 Update Login.tsx commit 2f017aa5bf4ef91b73fe027fb346132e1a5d8b87 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:14:17 2023 -0500 Update env.d.ts commit addfd95cf93ef19cae05bab652d634af64313e6a Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:13:16 2023 -0500 Create openidStrategy.js commit 84c3b5c2f078494d8380f3a02e3ba2d935d8d79f Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:09:02 2023 -0500 Update oauth.js commit 63225cdf33b7f42005b4a446797acbd91b7ee4a7 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:07:35 2023 -0500 Update index.js commit 6efe4dafd4359ed1c3139468bf9d43f70bbaf6aa Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:04:55 2023 -0500 Update package.json commit 201badbbb5a5c8d48f5c4cba3a1349d4cfc7a070 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:03:37 2023 -0500 Update User.js commit 7d13d5c303465be9b1268e5f6d9bdf7bb8dfb2e4 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:02:29 2023 -0500 Update Dockerfile commit 2ef7f84ea77f281c3dce61211d9fd841a6424e65 Author: bsu3338 <bsu3338@users.noreply.github.com> Date: Mon Jun 12 23:00:42 2023 -0500 Update .env.example * Update openidStrategy.js * Update .env.example * Update .env.example * Update docker-compose.yml * Update env.d.ts * Update .env.example * Update .env.example * Update config.js * Update Login.tsx * Update config.js * Update Login.tsx * Update Registration.tsx * Update docker-compose.yml * Update openidStrategy.js * Update docker-compose.yml * Update config.spec.js * Update Login.spec.tsx * Update Registration.spec.tsx * Update types.ts * Update .env.example * Update package-lock.json * Update openidStrategy.js * Update openidStrategy.js * Update config.js * Update config.js * Update Login.tsx * Update Registration.tsx * Update oauth.js * Update openidStrategy.js * Update openidStrategy.js * Update Registration.tsx * Update Login.tsx * Update Login.tsx * Update Registration.tsx * Update Registration.tsx * Update index.js * Update index.js * Update .env.example * Update user_auth_system.md updated instruction that includes OpenID set up * Update package.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update openidStrategy.js * Update openidStrategy.js Lookup user based on openID instead of email. This is because not all AzureAD users may have an email tied to their account * Update openidStrategy.js First try to match an email, then try openIdID * Update openidStrategy.js * Update openidStrategy.js Consider a family name or given name is not provided --------- Co-authored-by: Fuegovic <32828263+fuegovic@users.noreply.github.com>
2023-06-24 21:45:52 -05:00
const passport = require('passport');
const jwt = require('jsonwebtoken');
const { Issuer, Strategy: OpenIDStrategy } = require('openid-client');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const config = require('../../config/loader');
const domains = config.domains;
const User = require('../models/User');
let crypto;
try {
crypto = require('node:crypto');
} catch (err) {
console.error('crypto support is disabled!');
}
const downloadImage = async (url, imagePath, accessToken) => {
try {
const response = await axios.get(url, {
headers: {
'Authorization': `Bearer ${accessToken}`
},
responseType: 'arraybuffer'
});
fs.mkdirSync(path.dirname(imagePath), { recursive: true });
fs.writeFileSync(imagePath, response.data);
const fileName = path.basename(imagePath);
return `/images/openid/${fileName}`;
} catch (error) {
console.error(`Error downloading image at URL "${url}": ${error}`);
return '';
}
};
Issuer.discover(process.env.OPENID_ISSUER)
.then(issuer => {
const client = new issuer.Client({
client_id: process.env.OPENID_CLIENT_ID,
client_secret: process.env.OPENID_CLIENT_SECRET,
redirect_uris: [domains.server + process.env.OPENID_CALLBACK_URL]
});
const openidLogin = new OpenIDStrategy(
{
client,
params: {
scope: process.env.OPENID_SCOPE
}
},
async (tokenset, userinfo, done) => {
try {
let user = await User.findOne({ openidId: userinfo.sub });
if (!user) {
user = await User.findOne({ email: userinfo.email });
}
let fullName = '';
if (userinfo.given_name && userinfo.family_name) {
fullName = userinfo.given_name + ' ' + userinfo.family_name;
} else if (userinfo.given_name) {
fullName = userinfo.given_name;
} else if (userinfo.family_name) {
fullName = userinfo.family_name;
}
if (!user) {
user = new User({
provider: 'openid',
openidId: userinfo.sub,
username: userinfo.given_name || '',
email: userinfo.email || '',
emailVerified: userinfo.email_verified || false,
name: fullName
});
} else {
user.provider = 'openid';
user.openidId = userinfo.sub;
user.username = userinfo.given_name || '';
user.name = fullName;
}
if (userinfo.picture) {
const imageUrl = userinfo.picture;
let fileName;
if (crypto) {
const hash = crypto.createHash('sha256');
hash.update(userinfo.sub);
fileName = hash.digest('hex') + '.png';
} else {
fileName = userinfo.sub + '.png';
}
const imagePath = path.join(__dirname, '..', '..', 'client', 'public', 'images', 'openid', fileName);
const imagePathOrEmpty = await downloadImage(imageUrl, imagePath, tokenset.access_token);
user.avatar = imagePathOrEmpty;
} else {
user.avatar = '';
}
await user.save();
done(null, user);
} catch (err) {
done(err);
}
}
);
passport.use('openid', openidLogin);
})
.catch(err => {
console.error(err);
});