Store traewelling refresh token in database

This commit is contained in:
Birte Kristina Friesel 2023-08-09 19:28:36 +02:00
parent 3ec9970f01
commit 8ec002f041
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
4 changed files with 41 additions and 18 deletions

View file

@ -1881,6 +1881,28 @@ my @migrations = (
} }
); );
}, },
# v47 -> v48
# Store Traewelling refresh tokens; store expiry as explicit column.
sub {
my ($db) = @_;
$db->query(
qq{
alter table traewelling
add column refresh_token text,
add column expiry timestamptz;
drop view traewelling_str;
create view traewelling_str as select
user_id, push_sync, pull_sync, errored,
token, refresh_token, data,
extract(epoch from latest_run) as latest_run_ts,
extract(epoch from expiry) as expiry_ts
from traewelling
;
update schema_version set version = 48;
}
);
},
); );
# TODO add 'hafas' column to in_transit (and maybe journeys? undo/redo needs something to work with...) # TODO add 'hafas' column to in_transit (and maybe journeys? undo/redo needs something to work with...)

View file

@ -51,6 +51,7 @@ sub oauth {
$self->traewelling->link( $self->traewelling->link(
uid => $self->current_user->{id}, uid => $self->current_user->{id},
token => $provider->{access_token}, token => $provider->{access_token},
refresh_token => $provider->{refresh_token},
expires_in => $provider->{expires_in}, expires_in => $provider->{expires_in},
); );
return $self->traewelling_api->get_user_p( $uid, $token )->then( return $self->traewelling_api->get_user_p( $uid, $token )->then(

View file

@ -40,16 +40,15 @@ sub link {
my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ]; my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ];
my $data = { my $data = { log => $log };
log => $log,
expires => $self->now->epoch + $opt{expires_in},
};
my $user_entry = { my $user_entry = {
user_id => $opt{uid}, user_id => $opt{uid},
push_sync => 0, push_sync => 0,
pull_sync => 0, pull_sync => 0,
token => $opt{token}, token => $opt{token},
refresh_token => $opt{refresh_token},
expiry => epoch_to_dt( $self->now->epoch + $opt{expires_in} ),
data => JSON->new->encode($data), data => JSON->new->encode($data),
}; };
@ -58,7 +57,7 @@ sub link {
$user_entry, $user_entry,
{ {
on_conflict => \ on_conflict => \
'(user_id) do update set token = EXCLUDED.token, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null' '(user_id) do update set token = EXCLUDED.token, refresh_token = EXCLUDED.refresh_token, expiry = EXCLUDED.expiry, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null'
} }
); );
@ -106,9 +105,11 @@ 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} ); $res_h->{expires_on}
= epoch_to_dt( $res_h->{expiry_ts} // $res_h->{data}{expires} );
my $expires_in = ( $res_h->{data}{expires} // 0 ) - $self->now->epoch; my $expires_in = ( $res_h->{expiry_ts} // $res_h->{data}{expires} // 0 )
- $self->now->epoch;
if ( $expires_in < 0 ) { if ( $expires_in < 0 ) {
$res_h->{expired} = 1; $res_h->{expired} = 1;

View file

@ -12,8 +12,7 @@
<div class="card-content white-text"> <div class="card-content white-text">
<span class="card-title">Träwelling verknüpft</span> <span class="card-title">Träwelling verknüpft</span>
% my $user = $traewelling->{data}{user_name} // '???'; % my $user = $traewelling->{data}{user_name} // '???';
<p>Dein travelynx-Account hat nun ein Jahr lang Zugriff auf <p>Dein travelynx-Account ist nun mit dem Träwelling-Account <b>@<%= $user %></b> verbunden.</p>
den Träwelling-Account <b>@<%= $user %></b>.</p>
</div> </div>
</div> </div>
% } % }
@ -136,7 +135,7 @@
% else { % else {
%= $traewelling->{email} %= $traewelling->{email}
% } % }
verknüpft. Der Token läuft <%= $traewelling->{expires_on}->strftime('am %d.%m.%Y um %H:%M Uhr') %> ab. verknüpft. Der aktuelle Token läuft <%= $traewelling->{expires_on}->strftime('am %d.%m.%Y um %H:%M Uhr') %> ab.
</p> </p>
</div> </div>
</div> </div>