X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Foauth-model.ts;h=3f8b8e6183e645354607125bc7517a9e86e4ebc2;hb=fae6e4da8f516a9d6c3bad9bf6f35811ccacbad8;hp=8d8a6d85e60bb9a9e92c93d88f089e02f2f7b513;hpb=9107d791e2eef9a1b24b0499dac8b9dbba8a792f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts index 8d8a6d85e..3f8b8e618 100644 --- a/server/lib/oauth-model.ts +++ b/server/lib/oauth-model.ts @@ -8,12 +8,13 @@ import { LRU_CACHE } from '../initializers/constants' import { Transaction } from 'sequelize' import { CONFIG } from '../initializers/config' import * as LRUCache from 'lru-cache' -import { MOAuthTokenUser } from '@server/typings/models/oauth/oauth-token' -import { MUser } from '@server/typings/models/user/user' +import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' +import { MUser } from '@server/types/models/user/user' import { UserAdminFlag } from '@shared/models/users/user-flag.model' import { createUserAccountAndChannelAndPlaylist } from './user' import { UserRole } from '@shared/models/users/user-role' import { PluginManager } from '@server/lib/plugins/plugin-manager' +import { ActorModel } from '@server/models/activitypub/actor' type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } @@ -109,22 +110,31 @@ async function getUser (usernameOrEmail?: string, password?: string) { let user = await UserModel.loadByEmail(obj.user.email) if (!user) user = await createUserFromExternal(obj.pluginName, obj.user) - // This user does not belong to this plugin, skip it - if (user.pluginAuth !== obj.pluginName) return null + // Cannot create a user + if (!user) throw new AccessDeniedError('Cannot create such user: an actor with that name already exists.') - return user + // If the user does not belongs to a plugin, it was created before its installation + // Then we just go through a regular login process + if (user.pluginAuth !== null) { + // This user does not belong to this plugin, skip it + if (user.pluginAuth !== obj.pluginName) return null + + checkUserValidityOrThrow(user) + + return user + } } logger.debug('Getting User (username/email: ' + usernameOrEmail + ', password: ******).') const user = await UserModel.loadByUsernameOrEmail(usernameOrEmail) // If we don't find the user, or if the user belongs to a plugin - if (!user || user.pluginAuth !== null) return null + if (!user || user.pluginAuth !== null || !password) return null const passwordMatch = await user.isPasswordMatch(password) - if (passwordMatch === false) return null + if (passwordMatch !== true) return null - if (user.blocked) throw new AccessDeniedError('User is blocked.') + checkUserValidityOrThrow(user) if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION && user.emailVerified === false) { throw new AccessDeniedError('User email is not verified.') @@ -133,13 +143,15 @@ async function getUser (usernameOrEmail?: string, password?: string) { return user } -async function revokeToken (tokenInfo: { refreshToken: string }) { +async function revokeToken (tokenInfo: { refreshToken: string }): Promise<{ success: boolean, redirectUrl?: string }> { const res: express.Response = this.request.res const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) if (token) { + let redirectUrl: string + if (res.locals.explicitLogout === true && token.User.pluginAuth && token.authName) { - PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName, token.User) + redirectUrl = await PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName, token.User, this.request) } clearCacheByToken(token.accessToken) @@ -147,10 +159,10 @@ async function revokeToken (tokenInfo: { refreshToken: string }) { token.destroy() .catch(err => logger.error('Cannot destroy token when revoking token.', { err })) - return true + return { success: true, redirectUrl } } - return false + return { success: false } } async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) { @@ -176,6 +188,10 @@ async function saveToken (token: TokenInfo, client: OAuthClientModel, user: User } const tokenCreated = await OAuthTokenModel.create(tokenToCreate) + + user.lastLoginDate = new Date() + await user.save() + return Object.assign(tokenCreated, { client, user }) } @@ -200,6 +216,10 @@ async function createUserFromExternal (pluginAuth: string, options: { role: UserRole displayName: string }) { + // Check an actor does not already exists with that name (removed user) + const actor = await ActorModel.loadLocalByName(options.username) + if (actor) return null + const userToCreate = new UserModel({ username: options.username, password: null, @@ -220,3 +240,7 @@ async function createUserFromExternal (pluginAuth: string, options: { return user } + +function checkUserValidityOrThrow (user: MUser) { + if (user.blocked) throw new AccessDeniedError('User is blocked.') +}