| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mslm::OTP; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20
|
use 5.006; |
|
|
1
|
|
|
|
|
4
|
|
|
4
|
1
|
|
|
1
|
|
8
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
28
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
57
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Mslm::Common qw($default_base_url $default_user_agent $default_api_key DEFAULT_TIMEOUT); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
198
|
|
|
7
|
1
|
|
|
1
|
|
9
|
use URI; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use LWP::UserAgent; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
7
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $send_api_path = '/api/otp/v1/send'; |
|
12
|
|
|
|
|
|
|
our $verify_api_path = '/api/otp/v1/token_verify'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
|
15
|
0
|
|
|
0
|
1
|
|
my ( $class, $api_key, %opts ) = @_; |
|
16
|
0
|
|
|
|
|
|
my $self = {}; |
|
17
|
0
|
|
0
|
|
|
|
my $timeout = $opts{timeout} || DEFAULT_TIMEOUT; |
|
18
|
0
|
|
0
|
|
|
|
my $user_agent = $opts{user_agent} || $default_user_agent; |
|
19
|
0
|
|
0
|
|
|
|
my $base_url = $opts{base_url} || $default_base_url; |
|
20
|
0
|
|
0
|
|
|
|
my $access_key = $api_key || $default_api_key; |
|
21
|
0
|
|
|
|
|
|
$self->{base_url} = URI->new($base_url); |
|
22
|
0
|
|
|
|
|
|
$self->{api_key} = $access_key; |
|
23
|
0
|
|
|
|
|
|
$self->{user_agent} = $user_agent; |
|
24
|
0
|
|
|
|
|
|
my $default_http_client = LWP::UserAgent->new; |
|
25
|
0
|
|
|
|
|
|
$default_http_client->ssl_opts( 'verify_hostname' => 0 ); |
|
26
|
0
|
|
|
|
|
|
$default_http_client->default_headers( |
|
27
|
|
|
|
|
|
|
HTTP::Headers->new( |
|
28
|
|
|
|
|
|
|
Accept => 'application/json' |
|
29
|
|
|
|
|
|
|
) |
|
30
|
|
|
|
|
|
|
); |
|
31
|
0
|
|
|
|
|
|
$default_http_client->agent($user_agent); |
|
32
|
0
|
|
|
|
|
|
$default_http_client->timeout($timeout); |
|
33
|
0
|
|
0
|
|
|
|
$self->{http_client} = $opts{http_client} || $default_http_client; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$self->{common_client} = Mslm::Common->new( |
|
36
|
|
|
|
|
|
|
http_client => $self->{http_client}, |
|
37
|
|
|
|
|
|
|
base_url => $self->{base_url}, |
|
38
|
|
|
|
|
|
|
user_agent => $self->{user_agent}, |
|
39
|
|
|
|
|
|
|
api_key => $self->{api_key} |
|
40
|
0
|
|
|
|
|
|
); |
|
41
|
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
bless $self, $class; |
|
43
|
0
|
|
|
|
|
|
return $self; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub error_msg { |
|
47
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return $self->{message}; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub set_base_url { |
|
53
|
0
|
|
|
0
|
1
|
|
my ( $self, $base_url_str ) = @_; |
|
54
|
0
|
|
|
|
|
|
my $base_url = URI->new($base_url_str); |
|
55
|
0
|
|
|
|
|
|
$self->{base_url} = $base_url; |
|
56
|
0
|
|
|
|
|
|
$self->{common_client}->set_base_url($base_url); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub set_http_client { |
|
60
|
0
|
|
|
0
|
1
|
|
my ( $self, $http_client ) = @_; |
|
61
|
0
|
|
|
|
|
|
$self->{http_client} = $http_client; |
|
62
|
0
|
|
|
|
|
|
$self->{common_client}->set_http_client($http_client); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub set_user_agent { |
|
66
|
0
|
|
|
0
|
1
|
|
my ( $self, $user_agent ) = @_; |
|
67
|
0
|
|
|
|
|
|
$self->{user_agent} = $user_agent; |
|
68
|
0
|
|
|
|
|
|
$self->{common_client}->set_user_agent($user_agent); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub set_api_key { |
|
72
|
0
|
|
|
0
|
1
|
|
my ( $self, $api_key ) = @_; |
|
73
|
0
|
|
|
|
|
|
$self->{api_key} = $api_key; |
|
74
|
0
|
|
|
|
|
|
$self->{common_client}->set_api_key($api_key); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub send { |
|
78
|
0
|
|
|
0
|
1
|
|
my ( $self, $otp_send_req, %opts ) = @_; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Check if $otp_send_req is a hash reference |
|
81
|
0
|
0
|
|
|
|
|
if ( ref($otp_send_req) ne 'HASH' ) { |
|
82
|
|
|
|
|
|
|
$self->{message} = |
|
83
|
0
|
|
|
|
|
|
"Invalid input format: \$otp_send_req must be a hash reference."; |
|
84
|
0
|
|
|
|
|
|
return undef; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Validate keys in $otp_send_req |
|
88
|
0
|
|
|
|
|
|
my @required_keys = qw(phone tmpl_sms token_len expire_seconds); |
|
89
|
0
|
|
|
|
|
|
foreach my $key (@required_keys) { |
|
90
|
0
|
0
|
|
|
|
|
unless ( exists $otp_send_req->{$key} ) { |
|
91
|
|
|
|
|
|
|
$self->{message} = |
|
92
|
0
|
|
|
|
|
|
"Missing required key: $key in \$otp_send_req."; |
|
93
|
0
|
|
|
|
|
|
return undef; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Setting options just for this request |
|
98
|
0
|
|
|
|
|
|
my $common_client = $self->{common_client}; |
|
99
|
0
|
|
0
|
|
|
|
my $base_url = URI->new( $opts{base_url} ) || $self->{base_url}; |
|
100
|
0
|
|
0
|
|
|
|
my $user_agent = $opts{user_agent} || $self->{user_agent}; |
|
101
|
0
|
|
0
|
|
|
|
my $api_key = $opts{api_key} || $self->{api_key}; |
|
102
|
0
|
|
0
|
|
|
|
my $http_client = $opts{http_client} || $self->{http_client}; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Prepare and send request |
|
105
|
0
|
|
|
|
|
|
my $qp = {}; |
|
106
|
0
|
|
|
|
|
|
my $reqUrl = $common_client->prepare_url( |
|
107
|
|
|
|
|
|
|
$send_api_path, |
|
108
|
|
|
|
|
|
|
$qp, |
|
109
|
|
|
|
|
|
|
base_url => $base_url, |
|
110
|
|
|
|
|
|
|
api_key => $api_key, |
|
111
|
|
|
|
|
|
|
); |
|
112
|
0
|
|
|
|
|
|
my $encoded_json_data = encode_json($otp_send_req); |
|
113
|
0
|
|
|
|
|
|
my ( $response, $msg ) = |
|
114
|
|
|
|
|
|
|
$common_client->req_and_resp( $http_client, 'POST', $reqUrl, |
|
115
|
|
|
|
|
|
|
$encoded_json_data, user_agent => $user_agent ); |
|
116
|
0
|
|
|
|
|
|
$self->{message} = $msg; |
|
117
|
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
return defined $response ? $response : undef; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub verify { |
|
122
|
0
|
|
|
0
|
1
|
|
my ( $self, $otp_token_verify_req, %opts ) = @_; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Check if $otp_token_verify_req is a hash reference |
|
125
|
0
|
0
|
|
|
|
|
if ( ref($otp_token_verify_req) ne 'HASH' ) { |
|
126
|
|
|
|
|
|
|
$self->{message} = |
|
127
|
0
|
|
|
|
|
|
"Invalid input format: \$otp_token_verify_req must be a hash reference."; |
|
128
|
0
|
|
|
|
|
|
return undef; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Validate keys in $otp_token_verify_req |
|
132
|
0
|
|
|
|
|
|
my @required_keys = qw(phone token consume); |
|
133
|
0
|
|
|
|
|
|
foreach my $key (@required_keys) { |
|
134
|
0
|
0
|
|
|
|
|
unless ( exists $otp_token_verify_req->{$key} ) { |
|
135
|
|
|
|
|
|
|
$self->{message} = |
|
136
|
0
|
|
|
|
|
|
"Missing required key: $key in \$otp_token_verify_req."; |
|
137
|
0
|
|
|
|
|
|
return undef; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Setting options just for this request |
|
142
|
0
|
|
|
|
|
|
my $common_client = $self->{common_client}; |
|
143
|
0
|
|
0
|
|
|
|
my $base_url = URI->new( $opts{base_url} ) || $self->{base_url}; |
|
144
|
0
|
|
0
|
|
|
|
my $user_agent = $opts{user_agent} || $self->{user_agent}; |
|
145
|
0
|
|
0
|
|
|
|
my $api_key = $opts{api_key} || $self->{api_key}; |
|
146
|
0
|
|
0
|
|
|
|
my $http_client = $opts{http_client} || $self->{http_client}; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Prepare and send request |
|
149
|
0
|
|
|
|
|
|
my $qp = {}; |
|
150
|
0
|
|
|
|
|
|
my $reqUrl = $common_client->prepare_url( |
|
151
|
|
|
|
|
|
|
$verify_api_path, |
|
152
|
|
|
|
|
|
|
$qp, |
|
153
|
|
|
|
|
|
|
base_url => $base_url, |
|
154
|
|
|
|
|
|
|
api_key => $api_key, |
|
155
|
|
|
|
|
|
|
); |
|
156
|
0
|
|
|
|
|
|
my $encoded_json_data = encode_json($otp_token_verify_req); |
|
157
|
0
|
|
|
|
|
|
my ( $response, $msg ) = |
|
158
|
|
|
|
|
|
|
$common_client->req_and_resp( $http_client, 'POST', $reqUrl, |
|
159
|
|
|
|
|
|
|
$encoded_json_data, user_agent => $user_agent ); |
|
160
|
0
|
|
|
|
|
|
$self->{message} = $msg; |
|
161
|
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
return defined $response ? $response : undef; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
|
166
|
|
|
|
|
|
|
__END__ |