1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
import {
isUserAdminFlagsValid,
isUserDisplayNameValid,
isUserRoleValid,
isUserUsernameValid,
isUserVideoQuotaDailyValid,
isUserVideoQuotaValid
} from '@server/helpers/custom-validators/users'
import { logger } from '@server/helpers/logger'
import { generateRandomString } from '@server/helpers/utils'
import { PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME } from '@server/initializers/constants'
import { PluginManager } from '@server/lib/plugins/plugin-manager'
import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
import { MUser } from '@server/types/models'
import {
RegisterServerAuthenticatedResult,
RegisterServerAuthPassOptions,
RegisterServerExternalAuthenticatedResult
} from '@server/types/plugins/register-server-auth.model'
import { UserAdminFlag, UserRole } from '@shared/models'
import { BypassLogin } from './oauth-model'
export type ExternalUser =
Pick<MUser, 'username' | 'email' | 'role' | 'adminFlags' | 'videoQuotaDaily' | 'videoQuota'> &
{ displayName: string }
// Token is the key, expiration date is the value
const authBypassTokens = new Map<string, {
expires: Date
user: ExternalUser
userUpdater: RegisterServerAuthenticatedResult['userUpdater']
authName: string
npmName: string
}>()
async function onExternalUserAuthenticated (options: {
npmName: string
authName: string
authResult: RegisterServerExternalAuthenticatedResult
}) {
const { npmName, authName, authResult } = options
if (!authResult.req || !authResult.res) {
logger.error('Cannot authenticate external user for auth %s of plugin %s: no req or res are provided.', authName, npmName)
return
}
const { res } = authResult
if (!isAuthResultValid(npmName, authName, authResult)) {
res.redirect('/login?externalAuthError=true')
return
}
logger.info('Generating auth bypass token for %s in auth %s of plugin %s.', authResult.username, authName, npmName)
const bypassToken = await generateRandomString(32)
const expires = new Date()
expires.setTime(expires.getTime() + PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME)
const user = buildUserResult(authResult)
authBypassTokens.set(bypassToken, {
expires,
user,
npmName,
authName,
userUpdater: authResult.userUpdater
})
// Cleanup expired tokens
const now = new Date()
for (const [ key, value ] of authBypassTokens) {
if (value.expires.getTime() < now.getTime()) {
authBypassTokens.delete(key)
}
}
res.redirect(`/login?externalAuthToken=${bypassToken}&username=${user.username}`)
}
async function getAuthNameFromRefreshGrant (refreshToken?: string) {
if (!refreshToken) return undefined
const tokenModel = await OAuthTokenModel.loadByRefreshToken(refreshToken)
return tokenModel?.authName
}
async function getBypassFromPasswordGrant (username: string, password: string): Promise<BypassLogin> {
const plugins = PluginManager.Instance.getIdAndPassAuths()
const pluginAuths: { npmName?: string, registerAuthOptions: RegisterServerAuthPassOptions }[] = []
for (const plugin of plugins) {
const auths = plugin.idAndPassAuths
for (const auth of auths) {
pluginAuths.push({
npmName: plugin.npmName,
registerAuthOptions: auth
})
}
}
pluginAuths.sort((a, b) => {
const aWeight = a.registerAuthOptions.getWeight()
const bWeight = b.registerAuthOptions.getWeight()
// DESC weight order
if (aWeight === bWeight) return 0
if (aWeight < bWeight) return 1
return -1
})
const loginOptions = {
id: username,
password
}
for (const pluginAuth of pluginAuths) {
const authOptions = pluginAuth.registerAuthOptions
const authName = authOptions.authName
const npmName = pluginAuth.npmName
logger.debug(
'Using auth method %s of plugin %s to login %s with weight %d.',
authName, npmName, loginOptions.id, authOptions.getWeight()
)
try {
const loginResult = await authOptions.login(loginOptions)
if (!loginResult) continue
if (!isAuthResultValid(pluginAuth.npmName, authOptions.authName, loginResult)) continue
logger.info(
'Login success with auth method %s of plugin %s for %s.',
authName, npmName, loginOptions.id
)
return {
bypass: true,
pluginName: pluginAuth.npmName,
authName: authOptions.authName,
user: buildUserResult(loginResult),
userUpdater: loginResult.userUpdater
}
} catch (err) {
logger.error('Error in auth method %s of plugin %s', authOptions.authName, pluginAuth.npmName, { err })
}
}
return undefined
}
function getBypassFromExternalAuth (username: string, externalAuthToken: string): BypassLogin {
const obj = authBypassTokens.get(externalAuthToken)
if (!obj) throw new Error('Cannot authenticate user with unknown bypass token')
const { expires, user, authName, npmName } = obj
const now = new Date()
if (now.getTime() > expires.getTime()) {
throw new Error('Cannot authenticate user with an expired external auth token')
}
if (user.username !== username) {
throw new Error(`Cannot authenticate user ${user.username} with invalid username ${username}`)
}
logger.info(
'Auth success with external auth method %s of plugin %s for %s.',
authName, npmName, user.email
)
return {
bypass: true,
pluginName: npmName,
authName,
userUpdater: obj.userUpdater,
user
}
}
function isAuthResultValid (npmName: string, authName: string, result: RegisterServerAuthenticatedResult) {
const returnError = (field: string) => {
logger.error('Auth method %s of plugin %s did not provide a valid %s.', authName, npmName, field, { [field]: result[field] })
return false
}
if (!isUserUsernameValid(result.username)) return returnError('username')
if (!result.email) return returnError('email')
// Following fields are optional
if (result.role && !isUserRoleValid(result.role)) return returnError('role')
if (result.displayName && !isUserDisplayNameValid(result.displayName)) return returnError('displayName')
if (result.adminFlags && !isUserAdminFlagsValid(result.adminFlags)) return returnError('adminFlags')
if (result.videoQuota && !isUserVideoQuotaValid(result.videoQuota + '')) return returnError('videoQuota')
if (result.videoQuotaDaily && !isUserVideoQuotaDailyValid(result.videoQuotaDaily + '')) return returnError('videoQuotaDaily')
if (result.userUpdater && typeof result.userUpdater !== 'function') {
logger.error('Auth method %s of plugin %s did not provide a valid user updater function.', authName, npmName)
return false
}
return true
}
function buildUserResult (pluginResult: RegisterServerAuthenticatedResult) {
return {
username: pluginResult.username,
email: pluginResult.email,
role: pluginResult.role ?? UserRole.USER,
displayName: pluginResult.displayName || pluginResult.username,
adminFlags: pluginResult.adminFlags ?? UserAdminFlag.NONE,
videoQuota: pluginResult.videoQuota,
videoQuotaDaily: pluginResult.videoQuotaDaily
}
}
// ---------------------------------------------------------------------------
export {
onExternalUserAuthenticated,
getBypassFromExternalAuth,
getAuthNameFromRefreshGrant,
getBypassFromPasswordGrant
}
|