diff options
author | Yorhel <git@yorhel.nl> | 2019-07-29 16:48:48 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-07-29 16:53:04 +0200 |
commit | 6cda16f862283b4cb502af8ebdddea25bde29816 (patch) | |
tree | 4a143aade9cd01d9ee7880a4696fd7ed4bdbb959 /lib/VN3/User/Login.pm | |
parent | e1e1d4e9b85d4ff0b26832fe95374f6f9718511e (diff) |
v3: Define & Generate API responses from Perl
This allows Perl modules to define new API responses and the
corresponding Elm type and parser will be generated automatically. The
primary reason to do this is to ensure that Elm and Perl stay
synchronized and to make sure that errors in generating API responses
can easily be found on the server side.
Also moded json_api() from VN3::Validate to VN3::Prelude, as that seemed
more fitting.
Diffstat (limited to 'lib/VN3/User/Login.pm')
-rw-r--r-- | lib/VN3/User/Login.pm | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/lib/VN3/User/Login.pm b/lib/VN3/User/Login.pm index 050d7130..7660762a 100644 --- a/lib/VN3/User/Login.pm +++ b/lib/VN3/User/Login.pm @@ -12,6 +12,9 @@ TUWF::get '/u/login' => sub { }; +my $elm_Throttled = elm_api 'Throttled'; +my $elm_BadLogin = elm_api 'BadLogin'; + json_api '/u/login', { username => { username => 1 }, password => { password => 1 } @@ -25,21 +28,16 @@ json_api '/u/login', { 'SELECT', sql_totime('greatest(timeout, now())'), 'FROM login_throttle WHERE ip =', \$ip ) || time; - my $status - = $tm-time() > $conf->[1] ? 'Throttled' - : auth->login($data->{username}, $data->{password}) ? 'Success' - : 'BadLogin'; + return $elm_Throttled->() if $tm-time() > $conf->[1]; + return $elm_Success->() if auth->login($data->{username}, $data->{password}); # Failed login, update throttle. - if($status eq 'BadLogin') { - my $upd = { - ip => \$ip, - timeout => sql_fromtime $tm+$conf->[0] - }; - tuwf->dbExeci('INSERT INTO login_throttle', $upd, 'ON CONFLICT (ip) DO UPDATE SET', $upd); - } - - tuwf->resJSON({$status => 1}); + my $upd = { + ip => \$ip, + timeout => sql_fromtime $tm+$conf->[0] + }; + tuwf->dbExeci('INSERT INTO login_throttle', $upd, 'ON CONFLICT (ip) DO UPDATE SET', $upd); + $elm_BadLogin->() }; |