Traewelling: Warn when bearer token is about to expire

This commit is contained in:
Daniel Friesel 2020-10-04 10:38:09 +02:00
parent 5e2813e36e
commit 80b02a47b4
4 changed files with 39 additions and 10 deletions

View file

@ -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 );
}

View file

@ -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;
}

View file

@ -17,7 +17,7 @@
<span class="card-title">Einstellungen zu öffentlichen Account-Daten geändert</span>
% }
% elsif ($success eq 'traewelling') {
<span class="card-title">Traewelling-Verknüpfung aktualisiert</span>
<span class="card-title">Träwelling-Verknüpfung aktualisiert</span>
% }
% elsif ($success eq 'use_history') {
<span class="card-title">Einstellungen zu vorgeschlagenen Verbindungen geändert</span>
@ -123,11 +123,17 @@
<span style="color: #999999;">Nicht verknüpft</span>
% }
% elsif ($traewelling->{errored}) {
Fehlerhaft <i class="material-icons">error</i>
Fehlerhaft <i class="material-icons" aria-hidden="true">error</i>
% }
% else {
Verknüpft mit <%= $traewelling->{data}{user_name} // $traewelling->{email} %>
% if ($traewelling->{pull_sync}) {
% if ($traewelling->{expired}) {
Login-Token abgelaufen <i class="material-icons" aria-hidden="true">error</i>
% }
% elsif ($traewelling->{expiring}) {
Login-Token läuft bald ab <i class="material-icons" aria-hidden="true">warning</i>
% }
% elsif ($traewelling->{pull_sync}) {
Checkins in Träwelling werden von travelynx übernommen
% }
% elsif ($traewelling->{push_sync}) {

View file

@ -117,7 +117,7 @@
% else {
%= $traewelling->{email}
% }
verknüpft.
verknüpft. Der Login-Token läuft <%= $traewelling->{expires_on}->strftime('am %d.%m.%Y um %H:%M Uhr') %> ab.
</p>
</div>
</div>