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 warnings;
use 5.020; use 5.020;
use DateTime;
use DateTime::Format::Strptime;
use Mojo::Promise; use Mojo::Promise;
sub new { sub new {
@ -182,10 +184,16 @@ sub login_p {
} }
else { else {
$token = $tx->result->json->{token}; $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( $self->{model}->link(
uid => $uid, uid => $uid,
email => $email, email => $email,
token => $token token => $token,
expires => $expiry_dt
); );
return $self->get_user_p( $uid, $token ); return $self->get_user_p( $uid, $token );
} }

View file

@ -37,24 +37,29 @@ sub link {
my $log = [ [ $self->now->epoch, "Erfolgreich angemeldet" ] ]; my $log = [ [ $self->now->epoch, "Erfolgreich angemeldet" ] ];
my $data = { my $data = {
log => $log,
expires => $opt{expires}->epoch,
};
my $user_entry = {
user_id => $opt{uid}, user_id => $opt{uid},
email => $opt{email}, email => $opt{email},
push_sync => 0, push_sync => 0,
pull_sync => 0, pull_sync => 0,
token => $opt{token}, token => $opt{token},
data => JSON->new->encode( { log => $log } ), data => JSON->new->encode($data),
}; };
$self->{pg}->db->insert( $self->{pg}->db->insert(
'traewelling', 'traewelling',
$data, $user_entry,
{ {
on_conflict => \ 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' '(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 { sub set_user {
@ -96,6 +101,16 @@ sub get {
for my $log_entry ( @{ $res_h->{data}{log} // [] } ) { for my $log_entry ( @{ $res_h->{data}{log} // [] } ) {
$log_entry->[0] = epoch_to_dt( $log_entry->[0] ); $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; return $res_h;
} }

View file

@ -17,7 +17,7 @@
<span class="card-title">Einstellungen zu öffentlichen Account-Daten geändert</span> <span class="card-title">Einstellungen zu öffentlichen Account-Daten geändert</span>
% } % }
% elsif ($success eq 'traewelling') { % 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') { % elsif ($success eq 'use_history') {
<span class="card-title">Einstellungen zu vorgeschlagenen Verbindungen geändert</span> <span class="card-title">Einstellungen zu vorgeschlagenen Verbindungen geändert</span>
@ -123,11 +123,17 @@
<span style="color: #999999;">Nicht verknüpft</span> <span style="color: #999999;">Nicht verknüpft</span>
% } % }
% elsif ($traewelling->{errored}) { % elsif ($traewelling->{errored}) {
Fehlerhaft <i class="material-icons">error</i> Fehlerhaft <i class="material-icons" aria-hidden="true">error</i>
% } % }
% else { % else {
Verknüpft mit <%= $traewelling->{data}{user_name} // $traewelling->{email} %> 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 Checkins in Träwelling werden von travelynx übernommen
% } % }
% elsif ($traewelling->{push_sync}) { % elsif ($traewelling->{push_sync}) {

View file

@ -117,7 +117,7 @@
% else { % else {
%= $traewelling->{email} %= $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> </p>
</div> </div>
</div> </div>