'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import { Tokens , TokenScope } from '@Entities/Tokens' ; import { IsNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; const procPostTokenNew = async ( req , resp , next ) => { if ( req . vAuthAccount ) { let scope = TokenScope . OWNER ; if ( req . query && req . query . scope && typeof ( req . query . scope ) === 'string' ) { scope = req . query . scope as string ; } ; if ( TokenScope . KnownScope ( scope ) ) { const tokenInfo = await Tokens . createToken ( req . vAuthAccount . id , [ scope ] ) ; await Tokens . addToken ( tokenInfo ) ; req . vRestResp . Data = { 'token' : tokenInfo . token , 'token_id' : tokenInfo . id , 'refresh_token' : tokenInfo . refreshToken , 'token_expiration_seconds' : ( tokenInfo . expirationTime . valueOf ( ) - tokenInfo . whenCreated . valueOf ( ) ) / 1000 , 'account_name' : req . vAuthAccount . username , 'account_roles' : req . vAuthAccount . roles , 'account_id' : req . vAuthAccount . id } ; } else { req . vRestResp . respondFailure ( 'incorrect scope' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/token/new' ; export const router = Router ( ) ; router . post ( '/api/v1/token/new' , setupMetaverseAPI , accountFromAuthToken , procPostTokenNew , finishMetaverseAPI ) ;