travelynx/lib/Travelynx/Controller/Traewelling.pm
Birte Kristina Friesel 52c0da3f46
Traewelling: replace legacy password login with OAuth2
This is a breaking change insofar as that traewelling support is no longer
provided automatically, but must be enabled by providing a traewelling.de
application ID and secret in travelynx.conf. However, as traewelling.de
password login is deprecated and wil soon be disabled, travelynx would break
either way. So we might or might not see travelynx 2.0.0 in the next days.

Automatic token refresh is still todo, but that was the case for password
login as well.

Closes #64
2023-08-07 21:17:10 +02:00

145 lines
3.4 KiB
Perl

package Travelynx::Controller::Traewelling;
# Copyright (C) 2020-2023 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Promise;
sub oauth {
my ($self) = @_;
if ( $self->param('action')
and $self->validation->csrf_protect->has_error('csrf_token') )
{
$self->render(
'bad_request',
csrf => 1,
status => 400
);
return;
}
$self->render_later;
my $oa = $self->config->{traewelling}{oauth};
return $self->oauth2->get_token_p(
traewelling => { scope => 'read-statuses write-statuses' } )->then(
sub {
my ($provider) = @_;
if ( not defined $provider ) {
# OAuth2 plugin performed a redirect, no need to render
return;
}
if ( not $provider or not $provider->{access_token} ) {
$self->flash( new_traewelling => 1 );
$self->flash( login_error => 'no token received' );
$self->redirect_to('/account/traewelling');
return;
}
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},
);
return $self->traewelling_api->get_user_p( $uid, $token )->then(
sub {
$self->flash( new_traewelling => 1 );
$self->redirect_to('/account/traewelling');
}
);
}
)->catch(
sub {
my ($err) = @_;
say "error $err";
$self->flash( new_traewelling => 1 );
$self->flash( login_error => $err );
$self->redirect_to('/account/traewelling');
return;
}
);
}
sub settings {
my ($self) = @_;
my $uid = $self->current_user->{id};
if ( $self->param('action')
and $self->validation->csrf_protect->has_error('csrf_token') )
{
$self->render(
'bad_request',
csrf => 1,
status => 400
);
return;
}
if ( $self->param('action') and $self->param('action') eq 'logout' ) {
$self->render_later;
my $traewelling = $self->traewelling->get( uid => $uid );
$self->traewelling_api->logout_p(
uid => $uid,
token => $traewelling->{token}
)->then(
sub {
$self->flash( success => 'traewelling' );
$self->redirect_to('account');
}
)->catch(
sub {
my ($err) = @_;
$self->render(
'traewelling',
traewelling => {},
new_traewelling => 1,
logout_error => $err,
);
}
)->wait;
return;
}
elsif ( $self->param('action') and $self->param('action') eq 'config' ) {
$self->traewelling->set_sync(
uid => $uid,
push_sync => $self->param('sync_source') eq 'travelynx' ? 1 : 0,
pull_sync => $self->param('sync_source') eq 'traewelling' ? 1 : 0,
toot => $self->param('toot') ? 1 : 0,
tweet => $self->param('tweet') ? 1 : 0,
);
$self->flash( success => 'traewelling' );
$self->redirect_to('account');
return;
}
my $traewelling = $self->traewelling->get( uid => $uid );
if ( $traewelling->{push_sync} ) {
$self->param( sync_source => 'travelynx' );
}
elsif ( $traewelling->{pull_sync} ) {
$self->param( sync_source => 'traewelling' );
}
else {
$self->param( sync_source => 'none' );
}
if ( $traewelling->{data}{toot} ) {
$self->param( toot => 1 );
}
if ( $traewelling->{data}{tweet} ) {
$self->param( tweet => 1 );
}
$self->render(
'traewelling',
traewelling => $traewelling,
);
}
1;