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,EACP,EACA,SAAU,EAAS,GACf,GAAwC,IAApC,SAAS,EAAQ,OAAO,QACxB,OAAO,EAAK,IAAI,MAAM,QAAU,EAAW,iBAE/C,EAAa,EAAQ,MACV,mBAAqB,SAAS,EAAQ,OAAO,oBACxD,EAAa,QAAQ,EAAU,EAAQ,OAAO,SAAU,EAC5D,EACA,SAAU,EAAe,GACrB,IAAK,EACD,OAAO,EAAK,IAAI,MAAM,qBAE1B,EAAK,KAAM,EACf,IACD,SAAU,EAAO,GAChB,GAAI,EACA,OAAO,EAAI,OAAO,KAAK,KAAK,CACxB,QAAS,EAAM,UAIvB,EAAI,MAAM,OAAM,WACZ,EAAQ,IAAI,UAAW,wDAAyD,GAChF,EAAI,KAAK,EACb,GACJ,IArDW,EAAI,OAAO,KAAK,KAAK,CACxB,QAAS,sBAPN,EAAI,OAAO,KAAK,KAAK,CACxB,QAAS,uEA2DrB,IAEJ,GACJ,GAIX,CAzHD,CAyHG,OAAO","file":"/npm/nodebb-plugin-ns-login@3.0.0/plugin/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/nodebb-plugin-ns-login@3.0.0/plugin/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","(function (Plugin) {\n    'use strict';\n\n    var ExpressBrute = require('express-brute'),\n        moment       = require('moment'),\n        async        = require('async'),\n\n        nodebb       = require('./nodebb'),\n        utils        = nodebb.utils,\n        user         = nodebb.user,\n        db           = nodebb.db,\n        passwordUtil = nodebb.password,\n        winston      = nodebb.winston,\n\n        store        = new ExpressBrute.MemoryStore(),\n        settings     = {\n            freeRetries : 5,\n            proxyDepth  : 1,\n            minWait     : 5 * 60 * 1000, // 5 minutes\n            maxWait     : 60 * 60 * 1000, // 1 hour,\n            failCallback: failCallback\n        },\n        userDefence  = new ExpressBrute(store, settings);\n\n    function failCallback(req, res, next, nextValidRequestDate) {\n        res.status(403).json({\n            message: 'You have made too many failed attempts in a short period of time, please try again ' + moment(nextValidRequestDate).fromNow()\n        });\n    }\n\n    //NodeBB list of Hooks: https://github.com/NodeBB/NodeBB/wiki/Hooks\n    Plugin.hooks = {\n        statics: {\n            load: function (params, callback) {\n                var router      = params.router,\n                    middleware  = params.middleware,\n                    controllers = params.controllers,\n                    apiUri      = '/api/ns/login';\n\n                router.post(\n                    apiUri,\n                    userDefence.getMiddleware({\n                        key: function (req, res, next) {\n                            // prevent too many attempts for the same username\n                            next(req.body.username);\n                        }\n                    }),\n                    function (req, res, next) {\n                        var username   = req.body.username,\n                            password   = req.body.password,\n                            uid        = null,\n                            userObject = null;\n\n                        if (!username) {\n                            return res.status(400).json({\n                                message: 'Username is not provided, username and password are required fields'\n                            });\n                        }\n\n                        if (!password) {\n                            return res.status(400).json({\n                                message: 'Password is empty'\n                            });\n                        }\n\n                        async.waterfall([\n                            function (next) {\n                                if (utils.isEmailValid(username)) {\n                                    user.getUidByEmail(username, next);\n                                } else {\n                                    user.getUidByUserslug(utils.slugify(username), next);\n                                }\n                            },\n                            function (_uid, next) {\n                                if (!_uid) {\n                                    return next(new Error('User ' + username + ' does not exist'));\n                                }\n\n                                uid = _uid;\n                                next();\n                            },\n                            function (next) {\n                                async.parallel({\n                                    user   : async.apply(user.getUserData, uid),\n                                    secure : async.apply(db.getObjectFields, 'user:' + uid, ['password', 'banned', 'passwordExpiry', 'email:confirmed']),\n                                    isAdmin: async.apply(user.isAdministrator, uid)\n                                }, next);\n                            },\n                            function (payload, next) {\n                                if (parseInt(payload.secure.banned) === 1) {\n                                    return next(new Error('User ' + username + ' is banned.'));\n                                }\n                                userObject = payload.user;\n                                userObject['email:confirmed'] = parseInt(payload.secure['email:confirmed']);\n                                passwordUtil.compare(password, payload.secure.password, next);\n                            },\n                            function (passwordMatch, next) {\n                                if (!passwordMatch) {\n                                    return next(new Error('Invalid Password'));\n                                }\n                                next(null, userObject);\n                            }\n                        ], function (error, user) {\n                            if (error) {\n                                return res.status(403).json({\n                                    message: error.message\n                                });\n                            }\n                            // Reset the failure counter\n                            req.brute.reset(function () {\n                                winston.log('verbose', '[plugins/ns-login] Successful external login, uid: %d', uid);\n                                res.json(user);\n                            });\n                        });\n                    });\n\n                callback();\n            }\n        }\n    };\n\n})(module.exports);\n"]}