2019-12-11 22:13:20 +01:00
|
|
|
import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
|
2018-03-23 14:26:20 +01:00
|
|
|
import { UserRight } from '../../../../../shared/models/users/user-right.enum'
|
2018-12-18 11:32:37 +01:00
|
|
|
import { User as ServerUserModel } from '../../../../../shared/models/users/user.model'
|
2017-03-04 11:45:47 +01:00
|
|
|
// Do not use the barrel (dependency loop)
|
2017-10-27 16:55:03 +02:00
|
|
|
import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role'
|
2018-12-18 11:32:37 +01:00
|
|
|
import { User } from '../../shared/users/user.model'
|
2018-04-19 11:01:34 +02:00
|
|
|
import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type'
|
2020-01-02 13:07:18 +01:00
|
|
|
import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
|
2017-06-16 14:32:15 +02:00
|
|
|
|
|
|
|
export type TokenOptions = {
|
|
|
|
accessToken: string
|
|
|
|
refreshToken: string
|
|
|
|
tokenType: string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private class only used by User
|
|
|
|
class Tokens {
|
|
|
|
private static KEYS = {
|
|
|
|
ACCESS_TOKEN: 'access_token',
|
|
|
|
REFRESH_TOKEN: 'refresh_token',
|
|
|
|
TOKEN_TYPE: 'token_type'
|
|
|
|
}
|
|
|
|
|
|
|
|
accessToken: string
|
|
|
|
refreshToken: string
|
|
|
|
tokenType: string
|
|
|
|
|
|
|
|
static load () {
|
2018-03-23 14:26:20 +01:00
|
|
|
const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
|
|
|
|
const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
|
|
|
|
const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
|
2017-06-16 14:32:15 +02:00
|
|
|
|
|
|
|
if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
|
|
|
|
return new Tokens({
|
|
|
|
accessToken: accessTokenLocalStorage,
|
|
|
|
refreshToken: refreshTokenLocalStorage,
|
|
|
|
tokenType: tokenTypeLocalStorage
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
static flush () {
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor (hash?: TokenOptions) {
|
|
|
|
if (hash) {
|
|
|
|
this.accessToken = hash.accessToken
|
|
|
|
this.refreshToken = hash.refreshToken
|
|
|
|
|
|
|
|
if (hash.tokenType === 'bearer') {
|
|
|
|
this.tokenType = 'Bearer'
|
|
|
|
} else {
|
|
|
|
this.tokenType = hash.tokenType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save () {
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
|
|
|
|
peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
|
|
|
|
peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-09 21:45:21 +02:00
|
|
|
|
|
|
|
export class AuthUser extends User {
|
2016-07-20 16:24:18 +02:00
|
|
|
private static KEYS = {
|
2016-08-05 18:04:08 +02:00
|
|
|
ID: 'id',
|
|
|
|
ROLE: 'role',
|
2017-06-11 11:29:03 +02:00
|
|
|
EMAIL: 'email',
|
2018-12-18 11:32:37 +01:00
|
|
|
VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',
|
2017-04-04 21:37:03 +02:00
|
|
|
USERNAME: 'username',
|
2018-04-19 14:52:10 +02:00
|
|
|
NSFW_POLICY: 'nsfw_policy',
|
2018-10-12 18:12:39 +02:00
|
|
|
WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',
|
2017-12-19 10:45:49 +01:00
|
|
|
AUTO_PLAY_VIDEO: 'auto_play_video'
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
2016-07-20 16:24:18 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
tokens: Tokens
|
2020-01-02 13:07:18 +01:00
|
|
|
specialPlaylists: Partial<VideoPlaylist>[]
|
2016-07-20 16:24:18 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
static load () {
|
2018-03-23 14:26:20 +01:00
|
|
|
const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
|
2016-07-20 16:24:18 +02:00
|
|
|
if (usernameLocalStorage) {
|
2016-08-09 21:45:21 +02:00
|
|
|
return new AuthUser(
|
|
|
|
{
|
2018-03-23 14:26:20 +01:00
|
|
|
id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10),
|
|
|
|
username: peertubeLocalStorage.getItem(this.KEYS.USERNAME),
|
|
|
|
email: peertubeLocalStorage.getItem(this.KEYS.EMAIL),
|
|
|
|
role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole,
|
2018-04-19 14:52:10 +02:00
|
|
|
nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType,
|
2018-10-12 18:12:39 +02:00
|
|
|
webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true',
|
2018-12-18 11:32:37 +01:00
|
|
|
autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true',
|
|
|
|
videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true'
|
2016-08-09 21:45:21 +02:00
|
|
|
},
|
2016-08-05 18:04:08 +02:00
|
|
|
Tokens.load()
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
return null
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
static flush () {
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.ID)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.ROLE)
|
2018-04-19 14:52:10 +02:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY)
|
2018-10-12 18:12:39 +02:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED)
|
2018-12-18 11:32:37 +01:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.VIDEOS_HISTORY_ENABLED)
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
|
2017-06-16 14:32:15 +02:00
|
|
|
Tokens.flush()
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 11:32:37 +01:00
|
|
|
constructor (userHash: Partial<ServerUserModel>, hashTokens: TokenOptions) {
|
2017-06-16 14:32:15 +02:00
|
|
|
super(userHash)
|
|
|
|
this.tokens = new Tokens(hashTokens)
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getAccessToken () {
|
|
|
|
return this.tokens.accessToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getRefreshToken () {
|
|
|
|
return this.tokens.refreshToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getTokenType () {
|
|
|
|
return this.tokens.tokenType
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
refreshTokens (accessToken: string, refreshToken: string) {
|
|
|
|
this.tokens.accessToken = accessToken
|
|
|
|
this.tokens.refreshToken = refreshToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:27:06 +02:00
|
|
|
hasRight (right: UserRight) {
|
2017-10-27 16:55:03 +02:00
|
|
|
return hasUserRight(this.role, right)
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:59:19 +02:00
|
|
|
canManage (user: ServerUserModel) {
|
|
|
|
const myRole = this.role
|
|
|
|
|
|
|
|
if (myRole === UserRole.ADMINISTRATOR) return true
|
|
|
|
|
|
|
|
// I'm a moderator: I can only manage users
|
|
|
|
return user.role === UserRole.USER
|
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
save () {
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
|
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
|
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
|
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString())
|
2018-04-19 14:52:10 +02:00
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString())
|
2018-10-12 18:12:39 +02:00
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled))
|
2018-03-23 14:26:20 +01:00
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
|
2017-06-16 14:32:15 +02:00
|
|
|
this.tokens.save()
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
}
|