This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Controller/Api/Sendmessage.pm

130 lines
3.6 KiB
Perl
Raw Normal View History

package Pear::LocalLoop::Controller::Api::Sendmessage;
use Mojo::Base 'Mojolicious::Controller';
2020-11-08 15:05:53 +00:00
use LWP::UserAgent;
use JSON;
2020-11-08 17:35:29 +00:00
use JSON::Parse 'parse_json';
2020-11-08 15:05:53 +00:00
use Mojo::JWT;
use Mojo::File;
use Carp;
has error_messages => sub {
return {
2020-11-08 17:35:29 +00:00
devicetoken => {
required => { message => 'Device token is required', status => 400 },
in_resultset => { message => 'Device token not found', status => 400 },
2020-11-08 17:35:29 +00:00
},
sender => {
required => { message => 'Sender name is required', status => 400 },
in_resultset => { message => 'Sender org not found', status => 400 },
},
messagetext => {
required => { message => 'Message is required', status => 400 },
},
};
};
2020-11-08 15:05:53 +00:00
=begin comment
Credit: Peter Scott/StackOverflow
https://stackoverflow.com/a/53357961/4580273
Credit: jeffez/StackOverflow
https://stackoverflow.com/q/56556438/4580273
=cut
2020-11-08 17:35:29 +00:00
my $jwt = create_jwt_from_path_and_scopes('./localspend-47012.json', 'email https://www.googleapis.com/auth/cloud-platform');
2020-11-08 15:05:53 +00:00
my $ua = LWP::UserAgent->new();
2020-11-08 17:35:29 +00:00
my $response = $ua->post('https://www.googleapis.com/oauth2/v4/token',
2020-11-08 15:05:53 +00:00
{
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
}
);
2020-11-08 17:35:29 +00:00
my $bearer_token = parse_json($response->content);
2020-11-08 15:05:53 +00:00
sub create_jwt_from_path_and_scopes
{
my ( $path, $scope ) = @_;
croak("No path provided") if not defined $path;
croak("$path not available") if not -f $path;
my $json = decode_json( Mojo::File->new($path)->slurp );
croak("No Private key in $path") if not defined $json->{private_key};
croak("Not a service account") if $json->{type} ne 'service_account';
my $jwt = Mojo::JWT->new();
$jwt->algorithm('RS256');
$jwt->secret($json->{private_key});
$jwt->claims( {
iss => $json->{client_email},
scope => $scope,
aud => 'https://www.googleapis.com/oauth2/v4/token',
iat => time(),
exp => time()+3600
} );
$jwt->set_iat( 1 );
return $jwt->encode;
}
sub post_message {
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('devicetoken')->in_resultset('token', $c->schema->resultset('DeviceToken'));
$validation->required('sender')->in_resultset('name', $c->schema->resultset('Organisation'));
$validation->required('messagetext');
return $c->api_validation_error if $validation->has_error;
2020-11-08 15:05:53 +00:00
my $end_point = "https://fcm.googleapis.com/v1/projects/localspend-47012/messages:send";
my $request = HTTP::Request->new('POST', $end_point);
2020-11-08 17:35:29 +00:00
$request->header('Authorization' => "Bearer $bearer_token->{access_token}");
2020-11-08 15:05:53 +00:00
$request->header('Content-Type' => 'application/json');
$request->content(JSON::encode_json ({
message => {
2020-11-08 17:35:29 +00:00
token => $validation->param('devicetoken'),
2020-11-08 15:05:53 +00:00
notification => {
2020-11-08 17:35:29 +00:00
title => $validation->param('sender'),
body => $validation->param('messagetext')
2020-11-08 15:05:53 +00:00
},
webpush => {
headers => {
2020-11-08 17:35:29 +00:00
urgency => 'very-low'
2020-11-08 15:05:53 +00:00
},
notification => {
2020-11-08 17:35:29 +00:00
title => $validation->param('sender'),
body => $validation->param('messagetext'),
2020-11-08 15:05:53 +00:00
}
}
}
}));
2020-11-08 17:35:29 +00:00
my $response = $ua->request($request);
if ($response->is_success) {
return $c->render( json => {
success => Mojo::JSON->true,
message => 'Your message has been sent successfully!',
});
} elsif ($response->is_error) {
return $c->render(
json => {
success => Mojo::JSON->false,
message => [
$response->decoded_content,
$jwt,
$bearer_token
],
error => 'message_error',
},
status => $response->code,
);
}
}
1;