feat(rebase): rebase

This commit is contained in:
Aiden Vigue
2021-02-15 20:29:55 -05:00
parent 9d61092f37
commit 3357343d98
14 changed files with 420 additions and 195 deletions

View File

@@ -43,7 +43,7 @@ authRoutes.post('/plex', async (req, res, next) => {
settings.main.mediaServerType != MediaServerType.PLEX &&
settings.main.mediaServerType != MediaServerType.NOT_CONFIGURED
) {
return res.status(500).json({ error: 'Plex login disabled' });
return res.status(500).json({ error: 'Plex login is disabled' });
}
try {
// First we need to use this auth token to get the users email from plex.tv
@@ -154,40 +154,52 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
username?: string;
password?: string;
hostname?: string;
email?: string;
};
//Make sure jellyfin login is enabled, but only if jellyfin is not already configured
if (
settings.main.mediaServerType != MediaServerType.JELLYFIN &&
settings.jellyfin.hostname != ''
settings.main.mediaServerType !== MediaServerType.JELLYFIN &&
settings.jellyfin.hostname !== ''
) {
return res.status(500).json({ error: 'Jellyfin login is disabled' });
} else if (!body.username || !body.password) {
return res
.status(500)
.json({ error: 'You must provide an username and a password' });
} else if (settings.jellyfin.hostname != '' && body.hostname) {
} else if (settings.jellyfin.hostname !== '' && body.hostname) {
return res
.status(500)
.json({ error: 'Jellyfin hostname already configured' });
} else if (settings.jellyfin.hostname == '' && !body.hostname) {
} else if (settings.jellyfin.hostname === '' && !body.hostname) {
return res.status(500).json({ error: 'No hostname provided.' });
}
try {
const hostname =
settings.jellyfin.hostname != ''
settings.jellyfin.hostname !== ''
? settings.jellyfin.hostname
: body.hostname;
// Try to find deviceId that corresponds to jellyfin user, else generate a new one
let user = await userRepository.findOne({
where: { jellyfinUsername: body.username },
});
let deviceId = '';
if (user) {
deviceId = user.jellyfinDeviceId ?? '';
} else {
deviceId = Buffer.from(
`Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0|${Date.now()}`
).toString('base64');
}
// First we need to attempt to log the user in to jellyfin
const jellyfinserver = new JellyfinAPI(hostname ?? '');
settings.jellyfin.name = await jellyfinserver.getServerName();
const jellyfinserver = new JellyfinAPI(hostname ?? '', undefined, deviceId);
const account = await jellyfinserver.login(body.username, body.password);
// Next let's see if the user already exists
let user = await userRepository.findOne({
where: { jellyfinId: account.User.Id },
user = await userRepository.findOne({
where: { jellyfinUserId: account.User.Id },
});
if (user) {
@@ -200,9 +212,9 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
if (typeof account.User.PrimaryImageTag !== undefined) {
user.avatar = `${hostname}/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90`;
} else {
user.avatar = '/images/os_logo_square.png';
user.avatar = '/os_logo_square.png';
}
user.email = account.User.Name;
user.jellyfinUsername = account.User.Name;
if (user.username === account.User.Name) {
@@ -213,30 +225,52 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
// Here we check if it's the first user. If it is, we create the user with no check
// and give them admin permissions
const totalUsers = await userRepository.count();
if (totalUsers === 0) {
user = new User({
email: account.User.Name,
email: body.email,
jellyfinUsername: account.User.Name,
jellyfinId: account.User.Id,
jellyfinUserId: account.User.Id,
jellyfinDeviceId: deviceId,
jellyfinAuthToken: account.AccessToken,
permissions: Permission.ADMIN,
avatar:
typeof account.User.PrimaryImageTag !== undefined
? `${hostname}/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90`
: '/images/os_logo_square.png',
: '/os_logo_square.png',
userType: UserType.JELLYFIN,
});
await userRepository.save(user);
//Update hostname in settings if it doesn't exist (initial configuration)
//Also set mediaservertype to JELLYFIN
if (settings.jellyfin.hostname == '') {
if (settings.jellyfin.hostname === '') {
settings.main.mediaServerType = MediaServerType.JELLYFIN;
settings.jellyfin.hostname = body.hostname ?? '';
settings.jellyfin.serverId = account.User.ServerId;
settings.save();
}
}
if (!user) {
if (!body.email) {
throw new Error('add_email');
}
user = new User({
email: body.email,
jellyfinUsername: account.User.Name,
jellyfinUserId: account.User.Id,
jellyfinDeviceId: deviceId,
jellyfinAuthToken: account.AccessToken,
permissions: settings.main.defaultPermissions,
avatar:
typeof account.User.PrimaryImageTag !== undefined
? `${hostname}/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90`
: '/os_logo_square.png',
userType: UserType.JELLYFIN,
});
await userRepository.save(user);
}
}
// Set logged in session
@@ -246,16 +280,32 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
return res.status(200).json(user?.filter() ?? {});
} catch (e) {
if (e.message != 'Unauthorized') {
if (e.message === 'Unauthorized') {
logger.info(
'Failed login attempt from user with incorrect Jellyfin credentials',
{
label: 'Auth',
account: {
ip: req.ip,
email: body.username,
password: '__REDACTED__',
},
}
);
return next({
status: 401,
message: 'Unauthorized',
});
} else if (e.message === 'add_email') {
return next({
status: 406,
message: 'CREDENTIAL_ERROR_ADD_EMAIL',
});
} else {
logger.error(e.message, { label: 'Auth' });
return next({
status: 500,
message: 'Something went wrong. Is your auth token valid?',
});
} else {
return next({
status: 401,
message: 'CREDENTIAL_ERROR',
message: 'Something went wrong.',
});
}
}