import uniq from "lodash/uniq" ; import { stringify } from "querystring" ; import { authenticatedUserGroups } from "../../constants" ; import { logger } from "../../logger" ; import { User , Verdaccio } from "../verdaccio" ; import { Config , getConfig } from "./Config" ; export class AuthCore { private readonly org = getConfig ( this . config , "org" ) ; private readonly requiredGroup = this . org ? "github/owner/" + this . org : null ; private readonly configuredGroups = this . getConfiguredGroups ( ) ; constructor ( private readonly verdaccio , private readonly config ) { } /**
     * Returns all permission groups used in the Verdacio config.
     */ getConfiguredGroups ( ) { const configuredGroups = { } ; Object . values ( this . config . packages || { } ) . forEach ( ( packageConfig ) => { ; [ "access" , "publish" , "unpublish" ] . flatMap ( ( key ) => packageConfig [ key ] ) . filter ( Boolean ) . forEach ( ( group ) => { configuredGroups [ group ] = true ; } ) ; } ) ; return configuredGroups ; } async createAuthenticatedUser ( username , groups ) { const relevantGroups = groups . filter ( ( group ) => group in this . configuredGroups ) ; relevantGroups . push ( username ) ; if ( this . requiredGroup ) { relevantGroups . push ( this . requiredGroup ) ; } const user = { name : username , groups : [ ... authenticatedUserGroups ] , real_groups : uniq ( relevantGroups . filter ( Boolean ) . sort ( ) ) , } ; logger . log ( "Created authenticated user" , user ) ; return user ; } async createUiCallbackUrl ( username , token , groups ) { const user = await this . createAuthenticatedUser ( username , groups ) ; const uiToken = await this . verdaccio . issueUiToken ( user ) ; const npmToken = await this . verdaccio . issueNpmToken ( token , user ) ; const query = { username , uiToken , npmToken } ; const url = "/?" + stringify ( query ) ; return url ; } authenticate ( username , groups ) { if ( this . requiredGroup && ! groups . includes ( this . requiredGroup ) ) { logger . error ( `Access denied: User "${ username }" is not a member of "${ this . requiredGroup }"` ) ; return false ; } if ( ! groups . length ) { logger . error ( `Access denied: User "${ username }" does not have any groups` ) ; return false ; } return true ; } }