(
domainId: string, uname: string, password: string, rememberme = false, redirect = '',
tfa = '', authnChallenge = '', judge = false,
)
| 70 | @param('authnChallenge', Types.String, true) |
| 71 | @param('judge', Types.Boolean, true) |
| 72 | async post( |
| 73 | domainId: string, uname: string, password: string, rememberme = false, redirect = '', |
| 74 | tfa = '', authnChallenge = '', judge = false, |
| 75 | ) { |
| 76 | if (!judge && !system.get('server.login')) throw new BuiltinLoginError(); |
| 77 | let udoc = await user.getByEmail(domainId, uname); |
| 78 | udoc ||= await user.getByUname(domainId, uname); |
| 79 | if (judge && !system.get('server.login') && !udoc?.hasPriv(PRIV.PRIV_JUDGE)) throw new BuiltinLoginError(); |
| 80 | if (!udoc) throw new UserNotFoundError(uname); |
| 81 | if (system.get('system.contestmode') && !udoc.hasPriv(PRIV.PRIV_EDIT_SYSTEM)) { |
| 82 | if (udoc._loginip && udoc._loginip !== this.request.ip) throw new ValidationError('ip'); |
| 83 | if (system.get('system.contestmode') === 'strict') { |
| 84 | const udocs = await user.getMulti({ loginip: this.request.ip, _id: { $ne: udoc._id } }).toArray(); |
| 85 | if (udocs.length) throw new ValidationError('ip'); |
| 86 | } |
| 87 | } |
| 88 | await Promise.all([ |
| 89 | this.limitRate('user_login', 60, 30), |
| 90 | this.limitRate('user_login_id', 60, 5, uname), |
| 91 | oplog.log(this, 'user.login', { redirect }), |
| 92 | ]); |
| 93 | if (udoc.tfa || udoc.authn) { |
| 94 | if (udoc.tfa && tfa) { |
| 95 | if (!verifyTFA(udoc._tfa, tfa)) throw new InvalidTokenError('2FA'); |
| 96 | } else if (udoc.authn && authnChallenge) { |
| 97 | const challenge = await token.get(authnChallenge, token.TYPE_WEBAUTHN); |
| 98 | if (!challenge || challenge.uid !== udoc._id) throw new InvalidTokenError(token.TYPE_TEXTS[token.TYPE_WEBAUTHN]); |
| 99 | if (!challenge.verified) throw new ValidationError('challenge'); |
| 100 | await token.del(authnChallenge, token.TYPE_WEBAUTHN); |
| 101 | } else throw new ValidationError('2FA', 'Authn'); |
| 102 | } |
| 103 | await udoc.checkPassword(password); |
| 104 | if (!udoc.hasPriv(PRIV.PRIV_USER_PROFILE)) throw new BlacklistedError(uname, udoc.banReason); |
| 105 | await successfulAuth.call(this, udoc); |
| 106 | this.session.save = rememberme; |
| 107 | this.response.redirect = redirect || ((this.request.referer || '/login').endsWith('/login') |
| 108 | ? this.url('homepage') : this.request.referer); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | class UserSudoHandler extends Handler { |
nothing calls this directly
no test coverage detected