@verdaccio/server-fastify
Version:
fastify server api implementation
94 lines (93 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _debug = _interopRequireDefault(require("debug"));
var _lodash = _interopRequireDefault(require("lodash"));
var _core = require("@verdaccio/core");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('verdaccio:fastify:web:login');
const loginBodySchema = {
body: {
type: 'object',
required: ['username', 'password'],
additionalProperties: false,
properties: {
username: {
type: 'string'
},
password: {
type: 'string'
}
}
}
};
const resetPasswordSchema = {
body: {
type: 'object',
required: ['password'],
additionalProperties: false,
properties: {
password: {
type: 'string'
}
}
}
};
async function loginRoute(fastify) {
fastify.post('/login', {
schema: loginBodySchema
}, async (request, reply) => {
// @ts-expect-error
const {
username,
password
} = request.body;
debug('authenticate %o', username);
fastify.auth.authenticate(username, password, async function callbackAuthenticate(err, user) {
if (err) {
const errorCode = err.message ? fastify.statusCode.UNAUTHORIZED : fastify.statusCode.INTERNAL_ERROR;
reply.send(fastify.errorUtils.getCode(errorCode, err.message));
} else {
const jWTSignOptions = fastify.configInstance.security.web.sign;
debug('jwtSignOptions: %o', jWTSignOptions);
const token = await fastify.auth.jwtEncrypt(user, jWTSignOptions);
reply.code(fastify.statusCode.OK).send({
token,
username
});
}
});
});
fastify.put('/reset_password', {
schema: resetPasswordSchema
}, async (request, reply) => {
var _fastify$configInstan, _fastify$configInstan2;
if (_lodash.default.isNil(request.userRemote.name)) {
reply.send(fastify.errorUtils.getCode(fastify.statusCode.UNAUTHORIZED, fastify.errorUtils.API_ERROR.MUST_BE_LOGGED));
}
// @ts-ignore
const {
password
} = request.body;
const {
name
} = request.userRemote;
if (_core.validatioUtils.validatePassword(password.new, (_fastify$configInstan = fastify.configInstance) === null || _fastify$configInstan === void 0 ? void 0 : (_fastify$configInstan2 = _fastify$configInstan.server) === null || _fastify$configInstan2 === void 0 ? void 0 : _fastify$configInstan2.passwordValidationRegex) === false) {
reply.send(fastify.errorUtils.getCode(fastify.statusCode.BAD_REQUEST, fastify.errorUtils.APP_ERROR.PASSWORD_VALIDATION));
return;
}
fastify.auth.changePassword(name, password.old, password.new, (err, isUpdated) => {
if (_lodash.default.isNil(err) && isUpdated) {
reply.code(fastify.statusCode.OK);
} else {
reply.send(fastify.errorUtils.getInternalError(fastify.errorUtils.API_ERROR.INTERNAL_SERVER_ERROR));
}
});
});
// });
}
var _default = loginRoute;
exports.default = _default;
//# sourceMappingURL=login.js.map