From 8ec002f0415665bb3a575d6c44b6fd7a1b8a1eb8 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Wed, 9 Aug 2023 19:28:36 +0200 Subject: Store traewelling refresh token in database --- lib/Travelynx/Command/database.pm | 22 ++++++++++++++++++++++ lib/Travelynx/Controller/Traewelling.pm | 7 ++++--- lib/Travelynx/Model/Traewelling.pm | 25 +++++++++++++------------ 3 files changed, 39 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm index 19a4b57..e875a27 100644 --- a/lib/Travelynx/Command/database.pm +++ b/lib/Travelynx/Command/database.pm @@ -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...) diff --git a/lib/Travelynx/Controller/Traewelling.pm b/lib/Travelynx/Controller/Traewelling.pm index 6df4e52..6c38f7a 100644 --- a/lib/Travelynx/Controller/Traewelling.pm +++ b/lib/Travelynx/Controller/Traewelling.pm @@ -49,9 +49,10 @@ sub oauth { my $uid = $self->current_user->{id}; my $token = $provider->{access_token}; $self->traewelling->link( - uid => $self->current_user->{id}, - token => $provider->{access_token}, - expires_in => $provider->{expires_in}, + uid => $self->current_user->{id}, + token => $provider->{access_token}, + refresh_token => $provider->{refresh_token}, + expires_in => $provider->{expires_in}, ); return $self->traewelling_api->get_user_p( $uid, $token )->then( sub { diff --git a/lib/Travelynx/Model/Traewelling.pm b/lib/Travelynx/Model/Traewelling.pm index 72ee92d..25648cc 100644 --- a/lib/Travelynx/Model/Traewelling.pm +++ b/lib/Travelynx/Model/Traewelling.pm @@ -40,17 +40,16 @@ sub link { my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ]; - my $data = { - log => $log, - expires => $self->now->epoch + $opt{expires_in}, - }; + my $data = { log => $log }; my $user_entry = { - user_id => $opt{uid}, - push_sync => 0, - pull_sync => 0, - token => $opt{token}, - data => JSON->new->encode($data), + user_id => $opt{uid}, + push_sync => 0, + pull_sync => 0, + token => $opt{token}, + refresh_token => $opt{refresh_token}, + expiry => epoch_to_dt( $self->now->epoch + $opt{expires_in} ), + data => JSON->new->encode($data), }; $self->{pg}->db->insert( @@ -58,7 +57,7 @@ sub link { $user_entry, { 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} // [] } ) { $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 ) { $res_h->{expired} = 1; -- cgit v1.2.3