summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-08-09 19:28:36 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2023-08-09 19:28:36 +0200
commit8ec002f0415665bb3a575d6c44b6fd7a1b8a1eb8 (patch)
tree9890e5aba842ebfb8c2280136fa28ebdc62f338f
parent3ec9970f01b7641eaf8787905b423aaa41ea71bc (diff)
Store traewelling refresh token in database1.34.3
-rw-r--r--lib/Travelynx/Command/database.pm22
-rw-r--r--lib/Travelynx/Controller/Traewelling.pm7
-rw-r--r--lib/Travelynx/Model/Traewelling.pm25
-rw-r--r--templates/traewelling.html.ep5
4 files changed, 41 insertions, 18 deletions
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;
diff --git a/templates/traewelling.html.ep b/templates/traewelling.html.ep
index 4147140..cffdf8f 100644
--- a/templates/traewelling.html.ep
+++ b/templates/traewelling.html.ep
@@ -12,8 +12,7 @@
<div class="card-content white-text">
<span class="card-title">Träwelling verknüpft</span>
% my $user = $traewelling->{data}{user_name} // '???';
- <p>Dein travelynx-Account hat nun ein Jahr lang Zugriff auf
- den Träwelling-Account <b>@<%= $user %></b>.</p>
+ <p>Dein travelynx-Account ist nun mit dem Träwelling-Account <b>@<%= $user %></b> verbunden.</p>
</div>
</div>
% }
@@ -136,7 +135,7 @@
% else {
%= $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>
</div>
</div>