feat(oidc): create user if it doesn't exist

pull/2792/head
Jakob Ankarhem 3 years ago committed by Jakob Ankarhem
parent 7de7e1f7bd
commit 52fd0b7dd8
No known key found for this signature in database
GPG Key ID: 149CBB661002B3BE

@ -14,6 +14,7 @@ import {
type WellKnownConfiguration,
} from '@server/utils/oidc';
import { randomBytes } from 'crypto';
import gravatarUrl from 'gravatar-url';
import decodeJwt from 'jwt-decode';
import type { InferType } from 'yup';
@ -523,20 +524,39 @@ authRoutes.get('/oidc-callback', async (req, res, next) => {
return res.redirect('/login');
}
// Map email to user
// Check that email is verified and map email to user
const decoded: InferType<ReturnType<typeof createJwtSchema>> =
decodeJwt(idToken);
if (!decoded.email_verified) {
logger.info('Failed OIDC login attempt', {
cause: 'Email not verified',
ip: req.ip,
email: decoded.email,
});
}
const userRepository = getRepository(User);
const user = await userRepository.findOne({
let user = await userRepository.findOne({
where: { email: decoded.email },
});
// Create user if it doesn't exist
if (!user) {
logger.info('Failed OIDC login attempt', {
cause: 'User not found',
logger.info(`Creating user for ${decoded.email}`, {
ip: req.ip,
email: decoded.email,
});
return res.redirect('/login');
const avatar = gravatarUrl(decoded.email, { default: 'mm', size: 200 });
user = new User({
avatar: avatar,
username: decoded.email,
email: decoded.email,
permissions: settings.main.defaultPermissions,
plexToken: '',
userType: UserType.LOCAL,
});
await userRepository.save(user);
}
// Set logged in session and return

Loading…
Cancel
Save