{"version":3,"file":"Memory.mjs","names":[],"sources":["../src/Memory.ts"],"sourcesContent":["import buildDebug from 'debug';\n\nimport { API_ERROR, errorUtils, pluginUtils } from '@verdaccio/core';\nimport type { Config, Logger, PackageAccess, RemoteUser } from '@verdaccio/types';\n\nimport type { Users, VerdaccioMemoryConfig } from './types';\n\nconst debug = buildDebug('verdaccio:plugin:auth:memory:user');\n\nconst { Plugin } = pluginUtils;\n\nexport default class Memory\n  extends Plugin<VerdaccioMemoryConfig>\n  implements pluginUtils.Auth<VerdaccioMemoryConfig>\n{\n  public _logger: Logger;\n  public _users: Users;\n  public _config: {};\n  public _app_config: Config;\n\n  public constructor(config: VerdaccioMemoryConfig, appOptions: pluginUtils.PluginOptions) {\n    super(config, appOptions);\n    this._users = config.users || {};\n    this._config = config;\n    this._logger = appOptions.logger;\n    this._app_config = appOptions.config;\n  }\n\n  public authenticate(user: string, password: string, cb: pluginUtils.AuthCallback): void {\n    debug('authenticate %o:%o', user, password);\n    const userCredentials = this._users[user];\n\n    if (!userCredentials) {\n      debug('user %o does not exist', user);\n      return cb(null, false);\n    }\n\n    if (password !== userCredentials.password) {\n      const err = errorUtils.getUnauthorized(API_ERROR.BAD_USERNAME_PASSWORD);\n      debug('password invalid for: %o', user);\n\n      return cb(err);\n    }\n\n    // authentication succeeded!\n    // return all usergroups this user has access to;\n    debug('authentication succeed for %o', user);\n    return cb(null, [user]);\n  }\n\n  public adduser(user: string, password: string, cb: pluginUtils.AuthUserCallback): void {\n    if (this._users[user]) {\n      debug('user %o already exist', user);\n      return cb(null, true);\n    }\n\n    if (this._app_config.max_users) {\n      if (Object.keys(this._users).length >= this._app_config.max_users) {\n        const err = errorUtils.getConflict(API_ERROR.MAX_USERS_REACHED);\n        debug(API_ERROR.MAX_USERS_REACHED);\n        return cb(err);\n      }\n    }\n\n    this._users[user] = { name: user, password: password };\n\n    debug('user added succeeded for %o', user);\n    cb(null, user);\n  }\n\n  public changePassword(\n    username: string,\n    password: string,\n    newPassword: string,\n    cb: pluginUtils.AuthChangePasswordCallback\n  ): void {\n    const user = this._users[username];\n    debug('init change password for %o', user?.name);\n\n    if (user && user.password === password) {\n      user.password = newPassword;\n      this._users[username] = user;\n      debug('user changed password succeeded for %o', user?.name);\n      cb(null, true);\n    } else {\n      const err = errorUtils.getNotFound('user not found');\n      this._logger.debug({ user: username }, 'change password user  @{user} not found');\n      debug('change password user for %o not found', user?.name);\n      return cb(err);\n    }\n  }\n\n  public allow_access(user: RemoteUser, pkg: PackageAccess, cb: pluginUtils.AccessCallback): void {\n    debug('allow access for %o', user);\n    if (pkg?.access?.includes('$all') || pkg?.access?.includes('$anonymous')) {\n      debug('%o has been granted access', user?.name);\n\n      return cb(null, true);\n    }\n\n    if (!user?.name) {\n      const err = errorUtils.getForbidden('not allowed to access package');\n      this._logger.debug({ user: user.name }, 'user: @{user} not allowed to access package');\n      debug('%o not allowed to access package err', user?.name, err.message);\n      return cb(err);\n    }\n\n    if (pkg?.access?.includes(user?.name) || pkg?.access?.includes('$authenticated')) {\n      debug('%o has been granted access', user?.name);\n      return cb(null, true);\n    }\n\n    const err = errorUtils.getForbidden('not allowed to access package');\n    debug('%o not allowed to access package err', user?.name, err?.message);\n    return cb(err);\n  }\n\n  public allow_publish(\n    user: RemoteUser,\n    pkg: PackageAccess,\n    cb: pluginUtils.AuthAccessCallback\n  ): void {\n    if (pkg?.publish?.includes('$all') || pkg?.publish?.includes('$anonymous')) {\n      debug('%o has been granted to publish', user?.name);\n      return cb(null, true);\n    }\n\n    if (!user?.name) {\n      const err = errorUtils.getForbidden('not allowed to publish package');\n      debug('%o not allowed to publish package err %o', user?.name, err.message);\n      return cb(err);\n    }\n\n    if (pkg?.publish?.includes(user.name) || pkg?.publish?.includes('$authenticated')) {\n      return cb(null, true);\n    }\n\n    const err = errorUtils.getForbidden('not allowed to publish package');\n    debug('%o not allowed to publish package err %o', user?.name, err.message);\n\n    return cb(err);\n  }\n}\n"],"mappings":";;;AAOA,IAAM,QAAQ,WAAW,m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