diff options
author | Daniel Friesel <derf@finalrewind.org> | 2020-10-04 10:38:09 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2020-10-04 10:38:09 +0200 |
commit | 80b02a47b443280a7b8876c45c2bad0b605a7968 (patch) | |
tree | f1871d61c6044c326de1a4724ebbd737d0fb3431 /lib | |
parent | 5e2813e36eae43372842b85b0424309b7147678b (diff) |
Traewelling: Warn when bearer token is about to expire
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Travelynx/Helper/Traewelling.pm | 14 | ||||
-rw-r--r-- | lib/Travelynx/Model/Traewelling.pm | 21 |
2 files changed, 29 insertions, 6 deletions
diff --git a/lib/Travelynx/Helper/Traewelling.pm b/lib/Travelynx/Helper/Traewelling.pm index 1b94aa0..6d7c367 100644 --- a/lib/Travelynx/Helper/Traewelling.pm +++ b/lib/Travelynx/Helper/Traewelling.pm @@ -4,6 +4,8 @@ use strict; use warnings; use 5.020; +use DateTime; +use DateTime::Format::Strptime; use Mojo::Promise; sub new { @@ -182,10 +184,16 @@ sub login_p { } else { $token = $tx->result->json->{token}; + + # As of 2020-10-04, Traewelling tokens expire one year after they + # are generated + my $expiry_dt = DateTime->now( time_zone => 'Europe/Berlin' ) + ->add( years => 1 ); $self->{model}->link( - uid => $uid, - email => $email, - token => $token + uid => $uid, + email => $email, + token => $token, + expires => $expiry_dt ); return $self->get_user_p( $uid, $token ); } diff --git a/lib/Travelynx/Model/Traewelling.pm b/lib/Travelynx/Model/Traewelling.pm index 1a2f4ec..c9b2181 100644 --- a/lib/Travelynx/Model/Traewelling.pm +++ b/lib/Travelynx/Model/Traewelling.pm @@ -37,24 +37,29 @@ sub link { my $log = [ [ $self->now->epoch, "Erfolgreich angemeldet" ] ]; my $data = { + log => $log, + expires => $opt{expires}->epoch, + }; + + my $user_entry = { user_id => $opt{uid}, email => $opt{email}, push_sync => 0, pull_sync => 0, token => $opt{token}, - data => JSON->new->encode( { log => $log } ), + data => JSON->new->encode($data), }; $self->{pg}->db->insert( 'traewelling', - $data, + $user_entry, { on_conflict => \ '(user_id) do update set email = EXCLUDED.email, token = EXCLUDED.token, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null' } ); - return $data; + return $user_entry; } sub set_user { @@ -96,6 +101,16 @@ sub get { for my $log_entry ( @{ $res_h->{data}{log} // [] } ) { $log_entry->[0] = epoch_to_dt( $log_entry->[0] ); } + $res_h->{expires_on} = epoch_to_dt( $res_h->{data}{expires} ); + + my $expires_in = ( $res_h->{data}{expires} // 0 ) - $self->now->epoch; + + if ( $expires_in < 0 ) { + $res_h->{expired} = 1; + } + elsif ( $expires_in < 14 * 24 * 3600 ) { + $res_h->{expiring} = 1; + } return $res_h; } |