mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
Refactor auth flow
Reimplement some node-oauth2-server methods to remove hacky code needed by our external login workflow
This commit is contained in:
parent
cae2df6bdc
commit
f43db2f46e
24 changed files with 487 additions and 255 deletions
52
server/lib/auth/tokens-cache.ts
Normal file
52
server/lib/auth/tokens-cache.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import * as LRUCache from 'lru-cache'
|
||||
import { MOAuthTokenUser } from '@server/types/models'
|
||||
import { LRU_CACHE } from '../../initializers/constants'
|
||||
|
||||
export class TokensCache {
|
||||
|
||||
private static instance: TokensCache
|
||||
|
||||
private readonly accessTokenCache = new LRUCache<string, MOAuthTokenUser>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE })
|
||||
private readonly userHavingToken = new LRUCache<number, string>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE })
|
||||
|
||||
private constructor () { }
|
||||
|
||||
static get Instance () {
|
||||
return this.instance || (this.instance = new this())
|
||||
}
|
||||
|
||||
hasToken (token: string) {
|
||||
return this.accessTokenCache.has(token)
|
||||
}
|
||||
|
||||
getByToken (token: string) {
|
||||
return this.accessTokenCache.get(token)
|
||||
}
|
||||
|
||||
setToken (token: MOAuthTokenUser) {
|
||||
this.accessTokenCache.set(token.accessToken, token)
|
||||
this.userHavingToken.set(token.userId, token.accessToken)
|
||||
}
|
||||
|
||||
deleteUserToken (userId: number) {
|
||||
this.clearCacheByUserId(userId)
|
||||
}
|
||||
|
||||
clearCacheByUserId (userId: number) {
|
||||
const token = this.userHavingToken.get(userId)
|
||||
|
||||
if (token !== undefined) {
|
||||
this.accessTokenCache.del(token)
|
||||
this.userHavingToken.del(userId)
|
||||
}
|
||||
}
|
||||
|
||||
clearCacheByToken (token: string) {
|
||||
const tokenModel = this.accessTokenCache.get(token)
|
||||
|
||||
if (tokenModel !== undefined) {
|
||||
this.userHavingToken.del(tokenModel.userId)
|
||||
this.accessTokenCache.del(token)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue