import { JWTSignOptions } from "@verdaccio/types" ; import merge from "lodash/merge" ; import { getMajorVersion , VerdaccioConfig } from "../plugin/Config" ; import { Auth , User } from "../verdaccio" ; const TIME_EXPIRATION_7D = "7d" as const ; const defaultSecurity = { api : { legacy : true , } , web : { sign : { expiresIn : TIME_EXPIRATION_7D , } , verify : { } , } , } as const ; function getSecurity ( config ) { return merge ( { } , defaultSecurity , config . security ) ; } /**
 * Abstract Verdaccio version differences and usage of all Verdaccio objects.
 */ export class Verdaccio { readonly majorVersion = getMajorVersion ( this . config ) ; readonly security = getSecurity ( this . config ) ; private auth ! : Auth ; constructor ( private readonly config ) { } setAuth ( auth ) { this . auth = auth ; return this ; } async issueNpmToken ( token , user ) { const jwtSignOptions = this . security ?. api ?. jwt ?. sign ; if ( jwtSignOptions ) { return this . issueVerdaccio4PlusJWT ( user , jwtSignOptions ) ; } else { return this . encrypt ( user . name + ":" + token ) ; } } async issueUiToken ( user ) { const jwtSignOptions = this . security . web . sign ; return this . issueVerdaccio4PlusJWT ( user , jwtSignOptions ) ; } private async issueVerdaccio4PlusJWT ( user , jwtSignOptions ) { return this . auth . jwtEncrypt ( user , jwtSignOptions ) ; } private encrypt ( text ) { return this . auth . aesEncrypt ( Buffer . from ( text ) ) . toString ( "base64" ) ; } }