UNPKG

verdaccio

Version:

A lightweight private npm proxy registry

76 lines (66 loc) 8.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _lodash = _interopRequireDefault(require("lodash")); var _constants = require("../../../lib/constants"); var _utils = require("../../../lib/utils"); var _authUtils = require("../../../lib/auth-utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @prettier * @flow */ function addUserAuthApi(route, auth, config) { route.post('/login', function (req, res, next) { const { username, password } = req.body; auth.authenticate(username, password, async (err, user) => { if (err) { const errorCode = err.message ? _constants.HTTP_STATUS.UNAUTHORIZED : _constants.HTTP_STATUS.INTERNAL_ERROR; next(_utils.ErrorCode.getCode(errorCode, err.message)); } else { req.remote_user = user; const jWTSignOptions = (0, _authUtils.getSecurity)(config).web.sign; next({ token: await auth.jwtEncrypt(user, jWTSignOptions), username: req.remote_user.name }); } }); }); route.put('/reset_password', function (req, res, next) { if (_lodash.default.isNil(req.remote_user.name)) { res.status(_constants.HTTP_STATUS.UNAUTHORIZED); return next({ // FUTURE: update to a more meaningful message message: _constants.API_ERROR.MUST_BE_LOGGED }); } const { password } = req.body; const { name } = req.remote_user; if ((0, _authUtils.validatePassword)(password.new) === false) { auth.changePassword(name, password.old, password.new, (err, isUpdated) => { if (_lodash.default.isNil(err) && isUpdated) { next({ ok: true }); } else { return next(_utils.ErrorCode.getInternalError(_constants.API_ERROR.INTERNAL_SERVER_ERROR)); } }); } else { return next(_utils.ErrorCode.getCode(_constants.HTTP_STATUS.BAD_REQUEST, _constants.APP_ERROR.PASSWORD_VALIDATION)); } }); } var _default = addUserAuthApi; exports.default = _default; //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9hcGkvd2ViL2VuZHBvaW50L3VzZXIudHMiXSwibmFtZXMiOlsiYWRkVXNlckF1dGhBcGkiLCJyb3V0ZSIsImF1dGgiLCJjb25ma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