aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-08-03 10:10:26 +0200
committerChocobozzz <me@florianbigard.com>2022-08-03 10:10:26 +0200
commit0b6f531653a7a24f82ad65564479a70a9326301a (patch)
treef65d9c80e0e8ced86a8a9f7b00952bb04413a5b7 /server/lib
parent35a0a924830d84f9ec28c129ec85cb1f45011fb8 (diff)
downloadPeerTube-0b6f531653a7a24f82ad65564479a70a9326301a.tar.gz
PeerTube-0b6f531653a7a24f82ad65564479a70a9326301a.tar.zst
PeerTube-0b6f531653a7a24f82ad65564479a70a9326301a.zip
Suffix external auth username on conflict
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/auth/oauth-model.ts9
-rw-r--r--server/lib/local-actor.ts18
-rw-r--r--server/lib/user.ts17
3 files changed, 30 insertions, 14 deletions
diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.ts
index d9cf32827..322b69e3a 100644
--- a/server/lib/auth/oauth-model.ts
+++ b/server/lib/auth/oauth-model.ts
@@ -1,7 +1,6 @@
1import express from 'express' 1import express from 'express'
2import { AccessDeniedError } from '@node-oauth/oauth2-server' 2import { AccessDeniedError } from '@node-oauth/oauth2-server'
3import { PluginManager } from '@server/lib/plugins/plugin-manager' 3import { PluginManager } from '@server/lib/plugins/plugin-manager'
4import { ActorModel } from '@server/models/actor/actor'
5import { MOAuthClient } from '@server/types/models' 4import { MOAuthClient } from '@server/types/models'
6import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' 5import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
7import { MUser } from '@server/types/models/user/user' 6import { MUser } from '@server/types/models/user/user'
@@ -12,6 +11,7 @@ import { CONFIG } from '../../initializers/config'
12import { OAuthClientModel } from '../../models/oauth/oauth-client' 11import { OAuthClientModel } from '../../models/oauth/oauth-client'
13import { OAuthTokenModel } from '../../models/oauth/oauth-token' 12import { OAuthTokenModel } from '../../models/oauth/oauth-token'
14import { UserModel } from '../../models/user/user' 13import { UserModel } from '../../models/user/user'
14import { findAvailableLocalActorName } from '../local-actor'
15import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user' 15import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user'
16import { TokensCache } from './tokens-cache' 16import { TokensCache } from './tokens-cache'
17 17
@@ -225,13 +225,12 @@ async function createUserFromExternal (pluginAuth: string, options: {
225 role: UserRole 225 role: UserRole
226 displayName: string 226 displayName: string
227}) { 227}) {
228 // Check an actor does not already exists with that name (removed user) 228 const username = await findAvailableLocalActorName(options.username)
229 const actor = await ActorModel.loadLocalByName(options.username)
230 if (actor) return null
231 229
232 const userToCreate = buildUser({ 230 const userToCreate = buildUser({
233 ...pick(options, [ 'username', 'email', 'role' ]), 231 ...pick(options, [ 'email', 'role' ]),
234 232
233 username,
235 emailVerified: null, 234 emailVerified: null,
236 password: null, 235 password: null,
237 pluginAuth 236 pluginAuth
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts
index 1d9be76e2..8c10ed700 100644
--- a/server/lib/local-actor.ts
+++ b/server/lib/local-actor.ts
@@ -1,6 +1,7 @@
1import { remove } from 'fs-extra' 1import { remove } from 'fs-extra'
2import LRUCache from 'lru-cache' 2import LRUCache from 'lru-cache'
3import { join } from 'path' 3import { join } from 'path'
4import { Transaction } from 'sequelize/types'
4import { ActorModel } from '@server/models/actor/actor' 5import { ActorModel } from '@server/models/actor/actor'
5import { getLowercaseExtension } from '@shared/core-utils' 6import { getLowercaseExtension } from '@shared/core-utils'
6import { buildUUID } from '@shared/extra-utils' 7import { buildUUID } from '@shared/extra-utils'
@@ -87,6 +88,22 @@ async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MC
87 88
88// --------------------------------------------------------------------------- 89// ---------------------------------------------------------------------------
89 90
91async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) {
92 let actor = await ActorModel.loadLocalByName(baseActorName, transaction)
93 if (!actor) return baseActorName
94
95 for (let i = 1; i < 30; i++) {
96 const name = `${baseActorName}-${i}`
97
98 actor = await ActorModel.loadLocalByName(name, transaction)
99 if (!actor) return name
100 }
101
102 throw new Error('Cannot find available actor local name (too much iterations).')
103}
104
105// ---------------------------------------------------------------------------
106
90function downloadActorImageFromWorker (options: { 107function downloadActorImageFromWorker (options: {
91 fileUrl: string 108 fileUrl: string
92 filename: string 109 filename: string
@@ -109,6 +126,7 @@ const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.
109export { 126export {
110 actorImagePathUnsafeCache, 127 actorImagePathUnsafeCache,
111 updateLocalActorImageFiles, 128 updateLocalActorImageFiles,
129 findAvailableLocalActorName,
112 downloadActorImageFromWorker, 130 downloadActorImageFromWorker,
113 deleteLocalActorImageFile, 131 deleteLocalActorImageFile,
114 downloadImageFromWorker, 132 downloadImageFromWorker,
diff --git a/server/lib/user.ts b/server/lib/user.ts
index f4ffae0e4..2e433da04 100644
--- a/server/lib/user.ts
+++ b/server/lib/user.ts
@@ -3,13 +3,11 @@ import { logger } from '@server/helpers/logger'
3import { CONFIG } from '@server/initializers/config' 3import { CONFIG } from '@server/initializers/config'
4import { UserModel } from '@server/models/user/user' 4import { UserModel } from '@server/models/user/user'
5import { MActorDefault } from '@server/types/models/actor' 5import { MActorDefault } from '@server/types/models/actor'
6import { buildUUID } from '@shared/extra-utils'
7import { ActivityPubActorType } from '../../shared/models/activitypub' 6import { ActivityPubActorType } from '../../shared/models/activitypub'
8import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users' 7import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users'
9import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' 8import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
10import { sequelizeTypescript } from '../initializers/database' 9import { sequelizeTypescript } from '../initializers/database'
11import { AccountModel } from '../models/account/account' 10import { AccountModel } from '../models/account/account'
12import { ActorModel } from '../models/actor/actor'
13import { UserNotificationSettingModel } from '../models/user/user-notification-setting' 11import { UserNotificationSettingModel } from '../models/user/user-notification-setting'
14import { MAccountDefault, MChannelActor } from '../types/models' 12import { MAccountDefault, MChannelActor } from '../types/models'
15import { MUser, MUserDefault, MUserId } from '../types/models/user' 13import { MUser, MUserDefault, MUserId } from '../types/models/user'
@@ -17,7 +15,7 @@ import { generateAndSaveActorKeys } from './activitypub/actors'
17import { getLocalAccountActivityPubUrl } from './activitypub/url' 15import { getLocalAccountActivityPubUrl } from './activitypub/url'
18import { Emailer } from './emailer' 16import { Emailer } from './emailer'
19import { LiveQuotaStore } from './live/live-quota-store' 17import { LiveQuotaStore } from './live/live-quota-store'
20import { buildActorInstance } from './local-actor' 18import { buildActorInstance, findAvailableLocalActorName } from './local-actor'
21import { Redis } from './redis' 19import { Redis } from './redis'
22import { createLocalVideoChannel } from './video-channel' 20import { createLocalVideoChannel } from './video-channel'
23import { createWatchLaterPlaylist } from './video-playlist' 21import { createWatchLaterPlaylist } from './video-playlist'
@@ -71,6 +69,8 @@ function buildUser (options: {
71 }) 69 })
72} 70}
73 71
72// ---------------------------------------------------------------------------
73
74async function createUserAccountAndChannelAndPlaylist (parameters: { 74async function createUserAccountAndChannelAndPlaylist (parameters: {
75 userToCreate: MUser 75 userToCreate: MUser
76 userDisplayName?: string 76 userDisplayName?: string
@@ -157,6 +157,8 @@ async function createApplicationActor (applicationId: number) {
157 return accountCreated 157 return accountCreated
158} 158}
159 159
160// ---------------------------------------------------------------------------
161
160async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { 162async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
161 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) 163 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id)
162 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString 164 let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString
@@ -169,6 +171,8 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
169 Emailer.Instance.addVerifyEmailJob(username, email, url) 171 Emailer.Instance.addVerifyEmailJob(username, email, url)
170} 172}
171 173
174// ---------------------------------------------------------------------------
175
172async function getOriginalVideoFileTotalFromUser (user: MUserId) { 176async function getOriginalVideoFileTotalFromUser (user: MUserId) {
173 // Don't use sequelize because we need to use a sub query 177 // Don't use sequelize because we need to use a sub query
174 const query = UserModel.generateUserQuotaBaseSQL({ 178 const query = UserModel.generateUserQuotaBaseSQL({
@@ -263,12 +267,7 @@ function createDefaultUserNotificationSettings (user: MUserId, t: Transaction |
263async function buildChannelAttributes (user: MUser, transaction?: Transaction, channelNames?: ChannelNames) { 267async function buildChannelAttributes (user: MUser, transaction?: Transaction, channelNames?: ChannelNames) {
264 if (channelNames) return channelNames 268 if (channelNames) return channelNames
265 269
266 let channelName = user.username + '_channel' 270 const channelName = await findAvailableLocalActorName(user.username + '_channel', transaction)
267
268 // Conflict, generate uuid instead
269 const actor = await ActorModel.loadLocalByName(channelName, transaction)
270 if (actor) channelName = buildUUID()
271
272 const videoChannelDisplayName = `Main ${user.username} channel` 271 const videoChannelDisplayName = `Main ${user.username} channel`
273 272
274 return { 273 return {