This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserManager.ts
71 lines (57 loc) · 2.49 KB
/
UserManager.ts
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
import {ITestableUserManager} from "webdav-server/lib/user/v2/userManager/ITestableUserManager";
import {IListUserManager} from "webdav-server/lib/user/v2/userManager/IListUserManager";
import {IUser} from "webdav-server/lib/user/v2/IUser";
import User from "./User";
import {v2 as webdav} from "webdav-server";
import api from './api'
import logger from './logger';
export default class UserManager implements ITestableUserManager, IListUserManager {
users: Map<string, User>
constructor() {
// TODO: Clear list regularly (sometimes there are 'ghost' files when changed in web client)
this.users = new Map()
}
getDefaultUser(callback: (user: IUser) => void): any {
logger.info('Retrieving default user...')
callback(null)
}
getUserByName(name: string, callback: (error: Error, user?: IUser) => void): any {
logger.info('Retrieving user by name...')
// relevant for HTTPDigestAuthentication
}
async getUserByNamePassword(name: string, password: string, callback: (error: Error, user?: IUser) => void): Promise<any> {
// Currently this method isn't called due to a missing authorisation header, probably because MacOS doesn't send an Authorization header to unsecured sites
logger.info('Retrieving user \'' + name + '\'...')
// relevant for HTTPBasicAuthentication
if (this.users.has(name)) {
if (this.users.get(name).password === password) {
callback(null, this.users.get(name))
return
} else {
logger.info('Access denied!')
callback(webdav.Errors.BadAuthentication)
return
}
}
const res = await api({json: true}).post('/authentication', {
strategy: 'local',
username: name,
password,
privateDevice: true
})
const data = res.data
if (data.accessToken) {
const user = new User(data.account.userId, name, password, data.accessToken)
await user.loadRoles()
this.users.set(name, user)
callback(null, user)
} else {
logger.warn(`UserManager.getUserByName.data.accessToken.false : Access denied! username: ${name}`)
callback(webdav.Errors.BadAuthentication)
}
}
getUsers(callback: (error: Error, users?: IUser[]) => void): any {
logger.info('Retrieving users...')
callback(null, [...this.users.values()])
}
}