aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Auth/Phone.pm14
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm14
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/Update.pm14
-rw-r--r--perllib/FixMyStreet/SMS.pm24
4 files changed, 29 insertions, 37 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm
index e4ffc2205..8387b9d64 100644
--- a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm
+++ b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm
@@ -23,17 +23,19 @@ Handle the submission of a code sent by text to a mobile number.
=cut
sub code : Path('') {
- my ( $self, $c ) = @_;
+ my ( $self, $c, $scope, $success_action ) = @_;
$c->stash->{template} = 'auth/smsform.html';
+ $scope ||= 'phone_sign_in';
+ $success_action ||= '/auth/process_login';
my $token = $c->stash->{token} = $c->get_param('token');
my $code = $c->get_param('code') || '';
- my $data = $c->forward('/auth/get_token', [ $token, 'phone_sign_in' ]) || return;
+ my $data = $c->stash->{token_data} = $c->forward('/auth/get_token', [ $token, $scope ]) || return;
$c->stash->{incorrect_code} = 1, return if $data->{code} ne $code;
- $c->detach( '/auth/process_login', [ $data, 'phone' ] );
+ $c->detach( $success_action, [ $data, 'phone' ] );
}
=head2 sign_in
@@ -90,6 +92,12 @@ sub send_token : Private {
my ( $self, $c, $token_data, $token_scope, $to ) = @_;
my $result = FixMyStreet::SMS->send_token($token_data, $token_scope, $to);
+ if ($result->{error}) {
+ $c->log->debug("Failure sending text containing code *$result->{random}*");
+ $c->stash->{sms_error} = $result->{error};
+ $c->stash->{username_error} = 'sms_failed';
+ return;
+ }
$c->stash->{token} = $result->{token};
$c->log->debug("Sending text containing code *$result->{random}*");
$c->stash->{template} = 'auth/smsform.html';
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index fa3967bf3..5f36443c0 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -1114,18 +1114,8 @@ sub send_problem_confirm_text : Private {
sub confirm_by_text : Path('text') {
my ( $self, $c ) = @_;
- my $token = $c->stash->{token} = $c->get_param('token');
- my $code = $c->get_param('code') || '';
-
- my $data = $c->stash->{token_data} = $c->forward('/auth/get_token', [ $token, 'problem' ]) || return;
- if ($data->{code} ne $code) {
- $c->stash->{template} = 'auth/smsform.html';
- $c->stash->{submit_url} = '/report/new/text';
- $c->stash->{incorrect_code} = 1;
- return;
- }
-
- $c->detach('process_confirmation');
+ $c->stash->{submit_url} = '/report/new/text';
+ $c->forward('/auth/phone/code', [ 'problem', '/report/new/process_confirmation' ]);
}
sub process_confirmation : Private {
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm
index 66724f2d1..c28039808 100644
--- a/perllib/FixMyStreet/App/Controller/Report/Update.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm
@@ -521,18 +521,8 @@ sub send_confirmation_text : Private {
sub confirm_by_text : Path('text') {
my ( $self, $c ) = @_;
- my $token = $c->stash->{token} = $c->get_param('token');
- my $code = $c->get_param('code') || '';
-
- my $data = $c->stash->{token_data} = $c->forward('/auth/get_token', [ $token, 'comment' ]) || return;
- if ($data->{code} ne $code) {
- $c->stash->{template} = 'auth/smsform.html';
- $c->stash->{submit_url} = '/report/update/text';
- $c->stash->{incorrect_code} = 1;
- return;
- }
-
- $c->detach('process_confirmation');
+ $c->stash->{submit_url} = '/report/update/text';
+ $c->forward('/auth/phone/code', [ 'comment', '/report/update/process_confirmation' ]);
}
sub process_confirmation : Private {
diff --git a/perllib/FixMyStreet/SMS.pm b/perllib/FixMyStreet/SMS.pm
index c71cceadc..874108706 100644
--- a/perllib/FixMyStreet/SMS.pm
+++ b/perllib/FixMyStreet/SMS.pm
@@ -3,7 +3,7 @@ package FixMyStreet::SMS;
use strict;
use warnings;
-# use JSON::MaybeXS;
+use JSON::MaybeXS;
use Moo;
use Number::Phone::Lib;
use WWW::Twilio::API;
@@ -28,6 +28,11 @@ has from => (
default => sub { FixMyStreet->config('TWILIO_FROM_PARAMETER') },
);
+has messaging_service => (
+ is => 'lazy',
+ default => sub { FixMyStreet->config('TWILIO_MESSAGING_SERVICE_SID') },
+);
+
sub send_token {
my ($class, $token_data, $token_scope, $to) = @_;
@@ -44,24 +49,23 @@ sub send_token {
return {
random => $random,
token => $token_obj->token,
- result => $result,
+ %$result,
};
}
sub send {
my ($self, %params) = @_;
my $output = $self->twilio->POST('Messages.json',
- From => $self->from,
+ $self->from ? (From => $self->from) : (),
+ $self->messaging_service ? (MessagingServiceSid => $self->messaging_service) : (),
To => $params{to},
Body => $params{body},
);
- # At present, we do nothing and assume sent okay.
- # TODO add error checking
- # my $data = decode_json($output->{content});
- # if ($output->{code} != 200) {
- # return { error => "$data->{message} ($data->{code})" };
- # }
- # return { success => $data->{sid} };
+ my $data = decode_json($output->{content});
+ if ($output->{code} >= 400) {
+ return { error => "$data->{message} ($data->{code})" };
+ }
+ return { success => $data->{sid} };
}
=head2 parse_username