2018-03-23 14:26:20 +01:00
|
|
|
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
|
|
|
|
import { UserRight } from '../../../../../shared/models/users/user-right.enum'
|
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'
|
2017-10-25 17:31:11 +02:00
|
|
|
import { User, UserConstructorHash } from '../../shared/users/user.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',
|
2017-04-04 21:37:03 +02:00
|
|
|
USERNAME: 'username',
|
2017-12-19 10:45:49 +01:00
|
|
|
DISPLAY_NSFW: 'display_nsfw',
|
|
|
|
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
|
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,
|
|
|
|
displayNSFW: peertubeLocalStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true',
|
|
|
|
autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === '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)
|
|
|
|
peertubeLocalStorage.removeItem(this.KEYS.DISPLAY_NSFW)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-25 17:31:11 +02:00
|
|
|
constructor (userHash: UserConstructorHash, 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)
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
peertubeLocalStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|