From 80b02a47b443280a7b8876c45c2bad0b605a7968 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 4 Oct 2020 10:38:09 +0200 Subject: Traewelling: Warn when bearer token is about to expire --- lib/Travelynx/Model/Traewelling.pm | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib/Travelynx/Model') 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; } -- cgit v1.2.3