import {isOAuth2RedemptionCode, OAuth2Client, OAuth2RedemptionCode, RedemptionCodeRepository} from '../types' import {Inject, Injectable} from '../../../di' import {Cache, Maybe, uuid4} from '../../../util' import {Authenticatable} from '../../types' @Injectable() export class CacheRedemptionCodeRepository extends RedemptionCodeRepository { @Inject() protected readonly cache!: Cache async find(codeString: string): Promise> { const cacheKey = `@extollo:oauth2:redemption:${codeString}` if ( await this.cache.has(cacheKey) ) { const code = await this.cache.safe(cacheKey).then(x => x.json()) if ( isOAuth2RedemptionCode(code) ) { return code } } } async issue(user: Authenticatable, client: OAuth2Client, scope?: string): Promise { const code = { scope, clientId: client.id, userId: user.getUniqueIdentifier(), code: uuid4(), } const cacheKey = `@extollo:oauth2:redemption:${code.code}` await this.cache.put(cacheKey, JSON.stringify(code)) return code } }