'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { Domains } from '@Entities/Domains' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { domainFromParams , domainAPIkeyFromMultipart , verifyDomainAccess } from '@Route-Tools/middleware' ; import multer from 'multer' ; import { Logger } from '@Tools/Logging' ; import { createSimplifiedPublicKey , convertBinKeyToPEM } from '@Route-Tools/Util' ; import { HTTPStatusCode } from '@Route-Tools/RESTResponse' ; const procGetDomainsPublicKey = async ( req , resp , next ) => { Logger . debug ( 'procGetDomainsPublicKey' ) ; if ( req . vDomain ) { req . vRestResp . Data = { 'public_key' : createSimplifiedPublicKey ( req . vDomain . publicKey ) } ; } else { req . vRestResp . HTTPStatus = HTTPStatusCode . Unauthorized ; req . vRestResp . respondFailure ( 'No domain' ) ; } ; next ( ) ; } ; const procPutDomainsPublicKey = async ( req , resp , next ) => { Logger . debug ( 'procPutDomainsPublicKey' ) ; if ( req . vDomain ) { if ( req . files ) { try { const publicKeyBin = ( req . files as any ) . public_key [ 0 ] . buffer ; const fieldsToUpdate = { 'publicKey' : convertBinKeyToPEM ( publicKeyBin ) } ; await Domains . updateEntityFields ( req . vDomain , fieldsToUpdate ) ; } catch ( e ) { Logger . error ( 'procPutDomainsPublicKey: exception converting: ' + e ) ; req . vRestResp . respondFailure ( 'exception converting public key' ) ; } } else { Logger . error ( 'procPutDomainsPublicKey: no files part of body' ) ; req . vRestResp . respondFailure ( 'no public key supplied' ) ; } ; } else { req . vRestResp . HTTPStatus = HTTPStatusCode . Unauthorized ; req . vRestResp . respondFailure ( req . vDomainError ?? 'no such domain' ) ; } ; next ( ) ; } ; export const name = '/api/v1/domains/public_key' ; export const router = Router ( ) ; router . get ( '/api/v1/domains/:domainId/public_key' , [ setupMetaverseAPI , domainFromParams , procGetDomainsPublicKey , finishMetaverseAPI ] ) ; const multiStorage = multer . memoryStorage ( ) ; const uploader = multer ( { storage : multiStorage , } ) ; const multiParser = uploader . fields ( [ { name : 'api_key' } , { name : 'public_key' } ] ) ; router . put ( '/api/v1/domains/:domainId/public_key' , [ setupMetaverseAPI , domainFromParams , multiParser , domainAPIkeyFromMultipart , verifyDomainAccess , procPutDomainsPublicKey , finishMetaverseAPI ] ) ;