import fs from 'fs' ; import path from 'path' ; import { getDataPath } from '@kbn/utils' ; import type { Logger } from 'src/core/server' ; import { getDetailedErrorMessage } from './errors' ; import { VerificationCode } from './verification_code' ; export class VerificationService { private fileName : string ; constructor ( private readonly logger ) { this . fileName = path . join ( getDataPath ( ) , 'verification_code' ) ; } public setup ( ) { const verificationCode = new VerificationCode ( this . logger ) ; try { fs . writeFileSync ( this . fileName , verificationCode . code ) ; this . logger . debug ( `Successfully wrote verification code to ${ this . fileName }` ) ; return verificationCode ; } catch ( error ) { this . logger . error ( `Failed to write verification code to ${ this . fileName }: ${ getDetailedErrorMessage ( error ) }.` ) ; } } public stop ( ) { try { fs . unlinkSync ( this . fileName ) ; this . logger . debug ( `Successfully removed ${ this . fileName }` ) ; } catch ( error ) { if ( ( error as NodeJS . ErrnoException ) . code !== 'ENOENT' ) { this . logger . error ( `Failed to remove ${ this . fileName }: ${ getDetailedErrorMessage ( error ) }.` ) ; } } } }