1
0
Fork 0

Support logout and add id and pass tests

This commit is contained in:
Chocobozzz 2020-04-23 11:36:50 +02:00 committed by Chocobozzz
parent 8dc8a34ee8
commit e1c5503114
25 changed files with 273 additions and 101 deletions

View File

@ -11,15 +11,15 @@ import {
getVideoAnnounceActivityPubUrl, getVideoAnnounceActivityPubUrl,
getVideoChannelActivityPubUrl, getVideoChannelActivityPubUrl,
getVideoCommentActivityPubUrl getVideoCommentActivityPubUrl
} from '../server/lib/activitypub' } from '../server/lib/activitypub/url'
import { VideoShareModel } from '../server/models/video/video-share' import { VideoShareModel } from '../server/models/video/video-share'
import { VideoCommentModel } from '../server/models/video/video-comment' import { VideoCommentModel } from '../server/models/video/video-comment'
import { getServerActor } from '../server/helpers/utils'
import { AccountModel } from '../server/models/account/account' import { AccountModel } from '../server/models/account/account'
import { VideoChannelModel } from '../server/models/video/video-channel' import { VideoChannelModel } from '../server/models/video/video-channel'
import { VideoStreamingPlaylistModel } from '../server/models/video/video-streaming-playlist' import { VideoStreamingPlaylistModel } from '../server/models/video/video-streaming-playlist'
import { initDatabaseModels } from '../server/initializers' import { initDatabaseModels } from '../server/initializers'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { getServerActor } from '@server/models/application/application'
run() run()
.then(() => process.exit(0)) .then(() => process.exit(0))

View File

@ -1,5 +1,5 @@
import * as express from 'express' import * as express from 'express'
import { getFormattedObjects} from '../../helpers/utils' import { getFormattedObjects } from '../../helpers/utils'
import { import {
asyncMiddleware, asyncMiddleware,
authenticate, authenticate,

View File

@ -1,7 +1,7 @@
import * as express from 'express' import * as express from 'express'
import { UserRight } from '../../../../shared/models/users' import { UserRight } from '../../../../shared/models/users'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { getFormattedObjects} from '../../../helpers/utils' import { getFormattedObjects } from '../../../helpers/utils'
import { SERVER_ACTOR_NAME } from '../../../initializers/constants' import { SERVER_ACTOR_NAME } from '../../../initializers/constants'
import { sendAccept, sendReject, sendUndoFollow } from '../../../lib/activitypub/send' import { sendAccept, sendReject, sendUndoFollow } from '../../../lib/activitypub/send'
import { import {

View File

@ -1,6 +1,6 @@
import * as express from 'express' import * as express from 'express'
import 'multer' import 'multer'
import { getFormattedObjects} from '../../../helpers/utils' import { getFormattedObjects } from '../../../helpers/utils'
import { import {
asyncMiddleware, asyncMiddleware,
asyncRetryTransactionMiddleware, asyncRetryTransactionMiddleware,

View File

@ -26,12 +26,12 @@ import {
usersUpdateValidator usersUpdateValidator
} from '../../../middlewares' } from '../../../middlewares'
import { import {
ensureCanManageUser,
usersAskResetPasswordValidator, usersAskResetPasswordValidator,
usersAskSendVerifyEmailValidator, usersAskSendVerifyEmailValidator,
usersBlockingValidator, usersBlockingValidator,
usersResetPasswordValidator, usersResetPasswordValidator,
usersVerifyEmailValidator, usersVerifyEmailValidator
ensureCanManageUser
} from '../../../middlewares/validators' } from '../../../middlewares/validators'
import { UserModel } from '../../../models/account/user' import { UserModel } from '../../../models/account/user'
import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
@ -49,15 +49,10 @@ import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
import { UserRegister } from '../../../../shared/models/users/user-register.model' import { UserRegister } from '../../../../shared/models/users/user-register.model'
import { MUser, MUserAccountDefault } from '@server/typings/models' import { MUser, MUserAccountDefault } from '@server/typings/models'
import { Hooks } from '@server/lib/plugins/hooks' import { Hooks } from '@server/lib/plugins/hooks'
import { handleIdAndPassLogin } from '@server/lib/auth' import { tokensRouter } from '@server/controllers/api/users/token'
const auditLogger = auditLoggerFactory('users') const auditLogger = auditLoggerFactory('users')
const loginRateLimiter = RateLimit({
windowMs: CONFIG.RATES_LIMIT.LOGIN.WINDOW_MS,
max: CONFIG.RATES_LIMIT.LOGIN.MAX
})
// @ts-ignore // @ts-ignore
const signupRateLimiter = RateLimit({ const signupRateLimiter = RateLimit({
windowMs: CONFIG.RATES_LIMIT.SIGNUP.WINDOW_MS, windowMs: CONFIG.RATES_LIMIT.SIGNUP.WINDOW_MS,
@ -72,6 +67,7 @@ const askSendEmailLimiter = new RateLimit({
}) })
const usersRouter = express.Router() const usersRouter = express.Router()
usersRouter.use('/', tokensRouter)
usersRouter.use('/', myNotificationsRouter) usersRouter.use('/', myNotificationsRouter)
usersRouter.use('/', mySubscriptionsRouter) usersRouter.use('/', mySubscriptionsRouter)
usersRouter.use('/', myBlocklistRouter) usersRouter.use('/', myBlocklistRouter)
@ -168,23 +164,6 @@ usersRouter.post('/:id/verify-email',
asyncMiddleware(verifyUserEmail) asyncMiddleware(verifyUserEmail)
) )
usersRouter.post('/token',
loginRateLimiter,
handleIdAndPassLogin,
tokenSuccess
)
usersRouter.post('/token',
loginRateLimiter,
handleIdAndPassLogin,
tokenSuccess
)
usersRouter.post('/revoke-token',
loginRateLimiter,
handleIdAndPassLogin,
tokenSuccess
)
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
@ -391,12 +370,6 @@ async function verifyUserEmail (req: express.Request, res: express.Response) {
return res.status(204).end() return res.status(204).end()
} }
function tokenSuccess (req: express.Request) {
const username = req.body.username
Hooks.runAction('action:api.user.oauth2-got-token', { username, ip: req.ip })
}
async function changeUserBlock (res: express.Response, user: MUserAccountDefault, block: boolean, reason?: string) { async function changeUserBlock (res: express.Response, user: MUserAccountDefault, block: boolean, reason?: string) {
const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) const oldUserAuditView = new UserAuditView(user.toFormattedJSON())

View File

@ -0,0 +1,38 @@
import { handleIdAndPassLogin, handleTokenRevocation } from '@server/lib/auth'
import * as RateLimit from 'express-rate-limit'
import { CONFIG } from '@server/initializers/config'
import * as express from 'express'
import { Hooks } from '@server/lib/plugins/hooks'
import { asyncMiddleware, authenticate } from '@server/middlewares'
const tokensRouter = express.Router()
const loginRateLimiter = RateLimit({
windowMs: CONFIG.RATES_LIMIT.LOGIN.WINDOW_MS,
max: CONFIG.RATES_LIMIT.LOGIN.MAX
})
tokensRouter.post('/token',
loginRateLimiter,
handleIdAndPassLogin,
tokenSuccess
)
tokensRouter.post('/revoke-token',
authenticate,
asyncMiddleware(handleTokenRevocation),
tokenSuccess
)
// ---------------------------------------------------------------------------
export {
tokensRouter
}
// ---------------------------------------------------------------------------
function tokenSuccess (req: express.Request) {
const username = req.body.username
Hooks.runAction('action:api.user.oauth2-got-token', { username, ip: req.ip })
}

View File

@ -1,5 +1,5 @@
import * as express from 'express' import * as express from 'express'
import { getFormattedObjects} from '../../helpers/utils' import { getFormattedObjects } from '../../helpers/utils'
import { import {
asyncMiddleware, asyncMiddleware,
asyncRetryTransactionMiddleware, asyncRetryTransactionMiddleware,

View File

@ -1,5 +1,5 @@
import * as express from 'express' import * as express from 'express'
import { getFormattedObjects} from '../../helpers/utils' import { getFormattedObjects } from '../../helpers/utils'
import { import {
asyncMiddleware, asyncMiddleware,
asyncRetryTransactionMiddleware, asyncRetryTransactionMiddleware,

View File

@ -5,6 +5,7 @@ import { PluginManager } from '@server/lib/plugins/plugin-manager'
import { RegisterServerAuthPassOptions } from '@shared/models/plugins/register-server-auth.model' import { RegisterServerAuthPassOptions } from '@shared/models/plugins/register-server-auth.model'
import { logger } from '@server/helpers/logger' import { logger } from '@server/helpers/logger'
import { UserRole } from '@shared/models' import { UserRole } from '@shared/models'
import { revokeToken } from '@server/lib/oauth-model'
const oAuthServer = new OAuthServer({ const oAuthServer = new OAuthServer({
useErrorHandler: true, useErrorHandler: true,
@ -37,8 +38,9 @@ async function handleIdAndPassLogin (req: express.Request, res: express.Response
const aWeight = a.registerAuthOptions.getWeight() const aWeight = a.registerAuthOptions.getWeight()
const bWeight = b.registerAuthOptions.getWeight() const bWeight = b.registerAuthOptions.getWeight()
// DESC weight order
if (aWeight === bWeight) return 0 if (aWeight === bWeight) return 0
if (aWeight > bWeight) return 1 if (aWeight < bWeight) return 1
return -1 return -1
}) })
@ -48,18 +50,24 @@ async function handleIdAndPassLogin (req: express.Request, res: express.Response
} }
for (const pluginAuth of pluginAuths) { for (const pluginAuth of pluginAuths) {
const authOptions = pluginAuth.registerAuthOptions
logger.debug( logger.debug(
'Using auth method of %s to login %s with weight %d.', 'Using auth method %s of plugin %s to login %s with weight %d.',
pluginAuth.npmName, loginOptions.id, pluginAuth.registerAuthOptions.getWeight() authOptions.authName, pluginAuth.npmName, loginOptions.id, authOptions.getWeight()
) )
const loginResult = await pluginAuth.registerAuthOptions.login(loginOptions) const loginResult = await authOptions.login(loginOptions)
if (loginResult) { if (loginResult) {
logger.info('Login success with plugin %s for %s.', pluginAuth.npmName, loginOptions.id) logger.info(
'Login success with auth method %s of plugin %s for %s.',
authOptions.authName, pluginAuth.npmName, loginOptions.id
)
res.locals.bypassLogin = { res.locals.bypassLogin = {
bypass: true, bypass: true,
pluginName: pluginAuth.npmName, pluginName: pluginAuth.npmName,
authName: authOptions.authName,
user: { user: {
username: loginResult.username, username: loginResult.username,
email: loginResult.email, email: loginResult.email,
@ -75,12 +83,40 @@ async function handleIdAndPassLogin (req: express.Request, res: express.Response
return localLogin(req, res, next) return localLogin(req, res, next)
} }
async function handleTokenRevocation (req: express.Request, res: express.Response) {
const token = res.locals.oauth.token
PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName)
await revokeToken(token)
.catch(err => {
logger.error('Cannot revoke token.', err)
})
// FIXME: uncomment when https://github.com/oauthjs/node-oauth2-server/pull/289 is released
// oAuthServer.revoke(req, res, err => {
// if (err) {
// logger.warn('Error in revoke token handler.', { err })
//
// return res.status(err.status)
// .json({
// error: err.message,
// code: err.name
// })
// .end()
// }
// })
return res.sendStatus(200)
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
oAuthServer, oAuthServer,
handleIdAndPassLogin, handleIdAndPassLogin,
onExternalAuthPlugin onExternalAuthPlugin,
handleTokenRevocation
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -88,6 +124,8 @@ export {
function localLogin (req: express.Request, res: express.Response, next: express.NextFunction) { function localLogin (req: express.Request, res: express.Response, next: express.NextFunction) {
return oAuthServer.token()(req, res, err => { return oAuthServer.token()(req, res, err => {
if (err) { if (err) {
logger.warn('Login error.', { err })
return res.status(err.status) return res.status(err.status)
.json({ .json({
error: err.message, error: err.message,

View File

@ -2,9 +2,16 @@ import * as Bull from 'bull'
import { import {
ActivitypubFollowPayload, ActivitypubFollowPayload,
ActivitypubHttpBroadcastPayload, ActivitypubHttpBroadcastPayload,
ActivitypubHttpFetcherPayload, ActivitypubHttpUnicastPayload, EmailPayload, ActivitypubHttpFetcherPayload,
ActivitypubHttpUnicastPayload,
EmailPayload,
JobState, JobState,
JobType, RefreshPayload, VideoFileImportPayload, VideoImportPayload, VideoRedundancyPayload, VideoTranscodingPayload JobType,
RefreshPayload,
VideoFileImportPayload,
VideoImportPayload,
VideoRedundancyPayload,
VideoTranscodingPayload
} from '../../../shared/models' } from '../../../shared/models'
import { logger } from '../../helpers/logger' import { logger } from '../../helpers/logger'
import { Redis } from '../redis' import { Redis } from '../redis'
@ -13,13 +20,13 @@ import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-bro
import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
import { processEmail } from './handlers/email' import { processEmail } from './handlers/email'
import { processVideoTranscoding} from './handlers/video-transcoding' import { processVideoTranscoding } from './handlers/video-transcoding'
import { processActivityPubFollow } from './handlers/activitypub-follow' import { processActivityPubFollow } from './handlers/activitypub-follow'
import { processVideoImport} from './handlers/video-import' import { processVideoImport } from './handlers/video-import'
import { processVideosViews } from './handlers/video-views' import { processVideosViews } from './handlers/video-views'
import { refreshAPObject} from './handlers/activitypub-refresher' import { refreshAPObject } from './handlers/activitypub-refresher'
import { processVideoFileImport} from './handlers/video-file-import' import { processVideoFileImport } from './handlers/video-file-import'
import { processVideoRedundancy} from '@server/lib/job-queue/handlers/video-redundancy' import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy'
type CreateJobArgument = type CreateJobArgument =
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
@ -117,7 +124,7 @@ class JobQueue {
createJob (obj: CreateJobArgument): void { createJob (obj: CreateJobArgument): void {
this.createJobWithPromise(obj) this.createJobWithPromise(obj)
.catch(err => logger.error('Cannot create job.', { err, obj })) .catch(err => logger.error('Cannot create job.', { err, obj }))
} }
createJobWithPromise (obj: CreateJobArgument) { createJobWithPromise (obj: CreateJobArgument) {

View File

@ -14,6 +14,7 @@ import { MUser } from '@server/typings/models/user/user'
import { UserAdminFlag } from '@shared/models/users/user-flag.model' import { UserAdminFlag } from '@shared/models/users/user-flag.model'
import { createUserAccountAndChannelAndPlaylist } from './user' import { createUserAccountAndChannelAndPlaylist } from './user'
import { UserRole } from '@shared/models/users/user-role' import { UserRole } from '@shared/models/users/user-role'
import { PluginManager } from '@server/lib/plugins/plugin-manager'
type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
@ -82,7 +83,7 @@ async function getUser (usernameOrEmail: string, password: string) {
const obj = res.locals.bypassLogin const obj = res.locals.bypassLogin
logger.info('Bypassing oauth login by plugin %s.', obj.pluginName) logger.info('Bypassing oauth login by plugin %s.', obj.pluginName)
let user = await UserModel.loadByEmail(obj.user.username) let user = await UserModel.loadByEmail(obj.user.email)
if (!user) user = await createUserFromExternal(obj.pluginName, obj.user) if (!user) user = await createUserFromExternal(obj.pluginName, obj.user)
// This user does not belong to this plugin, skip it // This user does not belong to this plugin, skip it
@ -94,7 +95,8 @@ async function getUser (usernameOrEmail: string, password: string) {
logger.debug('Getting User (username/email: ' + usernameOrEmail + ', password: ******).') logger.debug('Getting User (username/email: ' + usernameOrEmail + ', password: ******).')
const user = await UserModel.loadByUsernameOrEmail(usernameOrEmail) const user = await UserModel.loadByUsernameOrEmail(usernameOrEmail)
if (!user) return null // If we don't find the user, or if the user belongs to a plugin
if (!user || user.pluginAuth !== null) return null
const passwordMatch = await user.isPasswordMatch(password) const passwordMatch = await user.isPasswordMatch(password)
if (passwordMatch === false) return null if (passwordMatch === false) return null
@ -109,8 +111,14 @@ async function getUser (usernameOrEmail: string, password: string) {
} }
async function revokeToken (tokenInfo: TokenInfo) { async function revokeToken (tokenInfo: TokenInfo) {
const res: express.Response = this.request.res
const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken)
if (token) { if (token) {
if (res.locals.explicitLogout === true && token.User.pluginAuth && token.authName) {
PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName)
}
clearCacheByToken(token.accessToken) clearCacheByToken(token.accessToken)
token.destroy() token.destroy()
@ -123,6 +131,12 @@ async function revokeToken (tokenInfo: TokenInfo) {
} }
async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) { async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) {
const res: express.Response = this.request.res
const authName = res.locals.bypassLogin?.bypass === true
? res.locals.bypassLogin.authName
: null
logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
const tokenToCreate = { const tokenToCreate = {
@ -130,6 +144,7 @@ async function saveToken (token: TokenInfo, client: OAuthClientModel, user: User
accessTokenExpiresAt: token.accessTokenExpiresAt, accessTokenExpiresAt: token.accessTokenExpiresAt,
refreshToken: token.refreshToken, refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt, refreshTokenExpiresAt: token.refreshTokenExpiresAt,
authName,
oAuthClientId: client.id, oAuthClientId: client.id,
userId: user.id userId: user.id
} }

View File

@ -76,7 +76,7 @@ export class PluginManager implements ServerHook {
return this.registeredPlugins[npmName] return this.registeredPlugins[npmName]
} }
getRegisteredPlugin (name: string) { getRegisteredPluginByShortName (name: string) {
const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN) const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN)
const registered = this.getRegisteredPluginOrTheme(npmName) const registered = this.getRegisteredPluginOrTheme(npmName)
@ -85,7 +85,7 @@ export class PluginManager implements ServerHook {
return registered return registered
} }
getRegisteredTheme (name: string) { getRegisteredThemeByShortName (name: string) {
const npmName = PluginModel.buildNpmName(name, PluginType.THEME) const npmName = PluginModel.buildNpmName(name, PluginType.THEME)
const registered = this.getRegisteredPluginOrTheme(npmName) const registered = this.getRegisteredPluginOrTheme(npmName)
@ -132,6 +132,22 @@ export class PluginManager implements ServerHook {
return this.translations[locale] || {} return this.translations[locale] || {}
} }
onLogout (npmName: string, authName: string) {
const plugin = this.getRegisteredPluginOrTheme(npmName)
if (!plugin || plugin.type !== PluginType.PLUGIN) return
const auth = plugin.registerHelpersStore.getIdAndPassAuths()
.find(a => a.authName === authName)
if (auth.onLogout) {
try {
auth.onLogout()
} catch (err) {
logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
}
}
}
// ###################### Hooks ###################### // ###################### Hooks ######################
async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {

View File

@ -171,6 +171,11 @@ export class RegisterHelpersStore {
private buildRegisterIdAndPassAuth () { private buildRegisterIdAndPassAuth () {
return (options: RegisterServerAuthPassOptions) => { return (options: RegisterServerAuthPassOptions) => {
if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
return
}
this.idAndPassAuths.push(options) this.idAndPassAuths.push(options)
} }
} }

View File

@ -2,7 +2,7 @@ import * as express from 'express'
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
import { Socket } from 'socket.io' import { Socket } from 'socket.io'
import { getAccessToken } from '../lib/oauth-model' import { getAccessToken } from '../lib/oauth-model'
import { handleIdAndPassLogin, oAuthServer } from '@server/lib/auth' import { oAuthServer } from '@server/lib/auth'
function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {} const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {}

View File

@ -16,7 +16,7 @@ const serveThemeCSSValidator = [
if (areValidationErrors(req, res)) return if (areValidationErrors(req, res)) return
const theme = PluginManager.Instance.getRegisteredTheme(req.params.themeName) const theme = PluginManager.Instance.getRegisteredThemeByShortName(req.params.themeName)
if (!theme || theme.version !== req.params.themeVersion) { if (!theme || theme.version !== req.params.themeVersion) {
return res.sendStatus(404) return res.sendStatus(404)

View File

@ -222,7 +222,7 @@ enum ScopeNames {
export class UserModel extends Model<UserModel> { export class UserModel extends Model<UserModel> {
@AllowNull(true) @AllowNull(true)
@Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password')) @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password', true))
@Column @Column
password: string password: string
@ -388,7 +388,7 @@ export class UserModel extends Model<UserModel> {
@BeforeCreate @BeforeCreate
@BeforeUpdate @BeforeUpdate
static cryptPasswordIfNeeded (instance: UserModel) { static cryptPasswordIfNeeded (instance: UserModel) {
if (instance.changed('password')) { if (instance.changed('password') && instance.password) {
return cryptPassword(instance.password) return cryptPassword(instance.password)
.then(hash => { .then(hash => {
instance.password = hash instance.password = hash

View File

@ -97,6 +97,9 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
@Column @Column
refreshTokenExpiresAt: Date refreshTokenExpiresAt: Date
@Column
authName: string
@CreatedAt @CreatedAt
createdAt: Date createdAt: Date

View File

@ -2,8 +2,9 @@
import * as chai from 'chai' import * as chai from 'chai'
import 'mocha' import 'mocha'
import { MyUser, User, UserRole, Video, VideoPlaylistType, VideoAbuseState, VideoAbuseUpdate } from '../../../../shared/index' import { MyUser, User, UserRole, Video, VideoAbuseState, VideoAbuseUpdate, VideoPlaylistType } from '../../../../shared/index'
import { import {
addVideoCommentThread,
blockUser, blockUser,
cleanupTests, cleanupTests,
createUser, createUser,
@ -11,12 +12,14 @@ import {
flushAndRunServer, flushAndRunServer,
getAccountRatings, getAccountRatings,
getBlacklistedVideosList, getBlacklistedVideosList,
getCustomConfig,
getMyUserInformation, getMyUserInformation,
getMyUserVideoQuotaUsed, getMyUserVideoQuotaUsed,
getMyUserVideoRating, getMyUserVideoRating,
getUserInformation, getUserInformation,
getUsersList, getUsersList,
getUsersListPaginationAndSort, getUsersListPaginationAndSort,
getVideoAbusesList,
getVideoChannel, getVideoChannel,
getVideosList, getVideosList,
installPlugin, installPlugin,
@ -26,21 +29,21 @@ import {
registerUserWithChannel, registerUserWithChannel,
removeUser, removeUser,
removeVideo, removeVideo,
reportVideoAbuse,
ServerInfo, ServerInfo,
testImage, testImage,
unblockUser, unblockUser,
updateCustomSubConfig,
updateMyAvatar, updateMyAvatar,
updateMyUser, updateMyUser,
updateUser, updateUser,
updateVideoAbuse,
uploadVideo, uploadVideo,
userLogin, userLogin,
reportVideoAbuse, waitJobs
addVideoCommentThread,
updateVideoAbuse,
getVideoAbusesList, updateCustomSubConfig, getCustomConfig, waitJobs
} from '../../../../shared/extra-utils' } from '../../../../shared/extra-utils'
import { follow } from '../../../../shared/extra-utils/server/follows' import { follow } from '../../../../shared/extra-utils/server/follows'
import { setAccessTokensToServers, logout } from '../../../../shared/extra-utils/users/login' import { logout, serverLogin, setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
import { getMyVideos } from '../../../../shared/extra-utils/videos/videos' import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model' import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
import { CustomConfig } from '@shared/models/server' import { CustomConfig } from '@shared/models/server'
@ -60,7 +63,14 @@ describe('Test users', function () {
before(async function () { before(async function () {
this.timeout(30000) this.timeout(30000)
server = await flushAndRunServer(1)
server = await flushAndRunServer(1, {
rates_limit: {
login: {
max: 30
}
}
})
await setAccessTokensToServers([ server ]) await setAccessTokensToServers([ server ])
@ -217,8 +227,6 @@ describe('Test users', function () {
await uploadVideo(server.url, server.accessToken, { name: 'video' }, 401) await uploadVideo(server.url, server.accessToken, { name: 'video' }, 401)
}) })
it('Should not be able to remove a video')
it('Should not be able to rate a video', async function () { it('Should not be able to rate a video', async function () {
const path = '/api/v1/videos/' const path = '/api/v1/videos/'
const data = { const data = {
@ -235,13 +243,17 @@ describe('Test users', function () {
await makePutBodyRequest(options) await makePutBodyRequest(options)
}) })
it('Should be able to login again') it('Should be able to login again', async function () {
server.accessToken = await serverLogin(server)
})
it('Should have an expired access token') it('Should have an expired access token')
it('Should refresh the token') it('Should refresh the token')
it('Should be able to upload a video again') it('Should be able to get my user information again', async function () {
await getMyUserInformation(server.url, server.accessToken)
})
}) })
describe('Creating a user', function () { describe('Creating a user', function () {

View File

@ -3,7 +3,7 @@ async function register ({
peertubeHelpers peertubeHelpers
}) { }) {
registerIdAndPassAuth({ registerIdAndPassAuth({
type: 'id-and-pass', authName: 'spyro-auth',
onLogout: () => { onLogout: () => {
peertubeHelpers.logger.info('On logout for auth 1 - 1') peertubeHelpers.logger.info('On logout for auth 1 - 1')
@ -16,7 +16,7 @@ async function register ({
return Promise.resolve({ return Promise.resolve({
username: 'spyro', username: 'spyro',
email: 'spyro@example.com', email: 'spyro@example.com',
role: 0, role: 2,
displayName: 'Spyro the Dragon' displayName: 'Spyro the Dragon'
}) })
} }
@ -26,7 +26,7 @@ async function register ({
}) })
registerIdAndPassAuth({ registerIdAndPassAuth({
type: 'id-and-pass', authName: 'crash-auth',
onLogout: () => { onLogout: () => {
peertubeHelpers.logger.info('On logout for auth 1 - 2') peertubeHelpers.logger.info('On logout for auth 1 - 2')
@ -39,7 +39,7 @@ async function register ({
return Promise.resolve({ return Promise.resolve({
username: 'crash', username: 'crash',
email: 'crash@example.com', email: 'crash@example.com',
role: 2, role: 1,
displayName: 'Crash Bandicoot' displayName: 'Crash Bandicoot'
}) })
} }

View File

@ -3,7 +3,7 @@ async function register ({
peertubeHelpers peertubeHelpers
}) { }) {
registerIdAndPassAuth({ registerIdAndPassAuth({
type: 'id-and-pass', authName: 'laguna-bad-auth',
onLogout: () => { onLogout: () => {
peertubeHelpers.logger.info('On logout for auth 3 - 1') peertubeHelpers.logger.info('On logout for auth 3 - 1')

View File

@ -3,7 +3,7 @@ async function register ({
peertubeHelpers peertubeHelpers
}) { }) {
registerIdAndPassAuth({ registerIdAndPassAuth({
type: 'id-and-pass', authName: 'laguna-auth',
onLogout: () => { onLogout: () => {
peertubeHelpers.logger.info('On logout for auth 2 - 1') peertubeHelpers.logger.info('On logout for auth 2 - 1')

View File

@ -1,11 +1,23 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha' import 'mocha'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' import {
getMyUserInformation,
getPluginTestPath,
installPlugin,
logout,
setAccessTokensToServers,
uninstallPlugin,
updateMyUser,
userLogin
} from '../../../shared/extra-utils'
import { User, UserRole } from '@shared/models'
import { expect } from 'chai'
describe('Test id and pass auth plugins', function () { describe('Test id and pass auth plugins', function () {
let server: ServerInfo let server: ServerInfo
let crashToken: string
before(async function () { before(async function () {
this.timeout(30000) this.timeout(30000)
@ -13,54 +25,97 @@ describe('Test id and pass auth plugins', function () {
server = await flushAndRunServer(1) server = await flushAndRunServer(1)
await setAccessTokensToServers([ server ]) await setAccessTokensToServers([ server ])
await installPlugin({ for (const suffix of [ 'one', 'two', 'three' ]) {
url: server.url, await installPlugin({
accessToken: server.accessToken, url: server.url,
path: getPluginTestPath('-id-pass-auth-one') accessToken: server.accessToken,
}) path: getPluginTestPath('-id-pass-auth-' + suffix)
})
await installPlugin({ }
url: server.url,
accessToken: server.accessToken,
path: getPluginTestPath('-id-pass-auth-two')
})
}) })
it('Should not login', async function() { it('Should not login', async function () {
await userLogin(server, { username: 'toto', password: 'password' }, 400)
}) })
it('Should login Spyro, create the user and use the token', async function() { it('Should login Spyro, create the user and use the token', async function () {
const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' })
const res = await getMyUserInformation(server.url, accessToken)
const body: User = res.body
expect(body.username).to.equal('spyro')
expect(body.account.displayName).to.equal('Spyro the Dragon')
expect(body.role).to.equal(UserRole.USER)
}) })
it('Should login Crash, create the user and use the token', async function() { it('Should login Crash, create the user and use the token', async function () {
crashToken = await userLogin(server, { username: 'crash', password: 'crash password' })
const res = await getMyUserInformation(server.url, crashToken)
const body: User = res.body
expect(body.username).to.equal('crash')
expect(body.account.displayName).to.equal('Crash Bandicoot')
expect(body.role).to.equal(UserRole.MODERATOR)
}) })
it('Should login the first Laguna, create the user and use the token', async function() { it('Should login the first Laguna, create the user and use the token', async function () {
const accessToken = await userLogin(server, { username: 'laguna', password: 'laguna password' })
const res = await getMyUserInformation(server.url, accessToken)
const body: User = res.body
expect(body.username).to.equal('laguna')
expect(body.account.displayName).to.equal('laguna')
expect(body.role).to.equal(UserRole.USER)
}) })
it('Should update Crash profile', async function () { it('Should update Crash profile', async function () {
await updateMyUser({
url: server.url,
accessToken: crashToken,
displayName: 'Beautiful Crash',
description: 'Mutant eastern barred bandicoot'
})
const res = await getMyUserInformation(server.url, crashToken)
const body: User = res.body
expect(body.account.displayName).to.equal('Beautiful Crash')
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
}) })
it('Should logout Crash', async function () { it('Should logout Crash', async function () {
await logout(server.url, crashToken)
// test token
}) })
it('Should have logged the Crash logout', async function () { it('Should have logged out Crash', async function () {
await getMyUserInformation(server.url, crashToken, 401)
await waitUntilLog(server, 'On logout for auth 1 - 2')
}) })
it('Should login Crash and keep the old existing profile', async function () { it('Should login Crash and keep the old existing profile', async function () {
crashToken = await userLogin(server, { username: 'crash', password: 'crash password' })
const res = await getMyUserInformation(server.url, crashToken)
const body: User = res.body
expect(body.username).to.equal('crash')
expect(body.account.displayName).to.equal('Beautiful Crash')
expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
expect(body.role).to.equal(UserRole.MODERATOR)
}) })
it('Should uninstall the plugin one and do not login existing Crash', async function () { it('Should uninstall the plugin one and do not login existing Crash', async function () {
await uninstallPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-test-id-pass-auth-one'
})
await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
}) })
after(async function () { after(async function () {

View File

@ -37,6 +37,7 @@ declare module 'express' {
bypassLogin?: { bypassLogin?: {
bypass: boolean bypass: boolean
pluginName: string pluginName: string
authName?: string
user: { user: {
username: string username: string
email: string email: string
@ -45,6 +46,8 @@ declare module 'express' {
} }
} }
explicitLogout: boolean
videoAll?: MVideoFullLight videoAll?: MVideoFullLight
onlyImmutableVideo?: MVideoImmutable onlyImmutableVideo?: MVideoImmutable
onlyVideo?: MVideoThumbnail onlyVideo?: MVideoThumbnail

View File

@ -9,7 +9,11 @@ import { Logger } from 'winston'
import { Router } from 'express' import { Router } from 'express'
import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model' import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model' import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
import { RegisterServerAuthPassOptions, RegisterServerAuthExternalOptions, RegisterServerAuthExternalResult } from '@shared/models/plugins/register-server-auth.model' import {
RegisterServerAuthExternalOptions,
RegisterServerAuthExternalResult,
RegisterServerAuthPassOptions
} from '@shared/models/plugins/register-server-auth.model'
export type PeerTubeHelpers = { export type PeerTubeHelpers = {
logger: Logger logger: Logger

View File

@ -3,10 +3,12 @@ import { UserRole } from '@shared/models'
export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions
export interface RegisterServerAuthPassOptions { export interface RegisterServerAuthPassOptions {
type: 'id-and-pass' // Authentication name (a plugin can register multiple auth strategies)
authName: string
onLogout?: Function onLogout?: Function
// Weight of this authentication so PeerTube tries the auth methods in DESC weight order
getWeight(): number getWeight(): number
// Used by PeerTube to login a user // Used by PeerTube to login a user
@ -23,7 +25,8 @@ export interface RegisterServerAuthPassOptions {
} }
export interface RegisterServerAuthExternalOptions { export interface RegisterServerAuthExternalOptions {
type: 'external' // Authentication name (a plugin can register multiple auth strategies)
authName: string
onLogout?: Function onLogout?: Function
} }