File Coverage

blib/lib/WebService/SOP/Auth/V1_1.pm
Criterion Covered Total %
statement 42 42 100.0
branch 5 6 83.3
condition 2 2 100.0
subroutine 15 15 100.0
pod 6 6 100.0
total 70 71 98.5


line stmt bran cond sub pod time code
1             package WebService::SOP::Auth::V1_1;
2 2     2   63520 use 5.008001;
  2         5  
  2         70  
3 2     2   9 use strict;
  2         2  
  2         48  
4 2     2   17 use warnings;
  2         4  
  2         70  
5              
6             our $VERSION = "0.02";
7              
8 2     2   8 use Carp ();
  2         7  
  2         28  
9 2     2   4790 use URI;
  2         7388  
  2         129  
10 2     2   1017 use WebService::SOP::Auth::V1_1::Request::GET;
  2         7  
  2         83  
11 2     2   1045 use WebService::SOP::Auth::V1_1::Request::POST;
  2         4  
  2         59  
12 2     2   812 use WebService::SOP::Auth::V1_1::Request::POST_JSON;
  2         3  
  2         56  
13 2     2   9 use WebService::SOP::Auth::V1_1::Util qw(is_signature_valid);
  2         2  
  2         498  
14              
15             sub new {
16 10     10 1 7766 my ($class, $args) = @_;
17 10   100     26 $args ||= +{ };
18              
19             do {
20 18 100       84 Carp::croak("Missing required parameter: ${_}") if not $args->{$_};
21 10         22 } for qw( app_id app_secret );
22              
23 7 100       26 $args->{time} = time if not $args->{time};
24              
25 7         22 bless $args, $class;
26             }
27              
28 1     1 1 11 sub app_id { $_[0]->{app_id} }
29 11     11 1 78 sub app_secret { $_[0]->{app_secret} }
30 12     12 1 52 sub time { $_[0]->{time} }
31              
32             sub create_request {
33 6     6 1 472 my ($self, $type, $uri, $params) = @_;
34 6 50       30 $uri = URI->new($uri) if not ref $uri;
35 6         4365 my $request_maker = "WebService::SOP::Auth::V1_1::Request::${type}";
36 6         20 $request_maker->create_request(
37             $uri,
38             { %$params, time => $self->time },
39             $self->app_secret,
40             );
41             }
42              
43             sub verify_signature {
44 4     4 1 158 my ($self, $sig, $params) = @_;
45 4         7 is_signature_valid($sig, $params, $self->app_secret, $self->time);
46             }
47              
48             1;
49             __END__
50              
51             =encoding utf-8
52              
53             =head1 NAME
54              
55             WebService::SOP::Auth::V1_1 - SOP version 1.1 authentication module
56              
57             =head1 SYNOPSIS
58              
59             use WebService::SOP::Auth::V1_1;
60              
61             To create an instance:
62              
63             my $auth = WebService::SOP::Auth::V1_1->new({
64             app_id => '1',
65             app_secret => 'hogehoge',
66             });
67              
68              
69             When making a GET request to API:
70              
71             my $req = $auth->create_request(
72             GET => 'https://<API_HOST>/path/to/endpoint' => {
73             hoge => 'hoge',
74             fuga => 'fuga',
75             },
76             );
77              
78             my $res = LWP::UserAgent->new->request($req);
79              
80             When making a POST request with JSON data to API:
81              
82             my $req = $auth->create_request(
83             POST_JSON => 'http://<API_HOST>/path/to/endpoint' => {
84             hoge => 'hoge',
85             fuga => 'fuga',
86             },
87             );
88              
89             my $res = LWP::UserAgent->new->request($req);
90              
91             When embedding JavaScript URL in page:
92              
93             <script src="<: $req.uri.as_string :>"></script>
94              
95             =head1 DESCRIPTION
96              
97             WebService::SOP::Auth::V1_1 is an authentication module
98             for L<SOP|http://console.partners.surveyon.com/> version 1.1
99             by L<Research Panel Asia, Inc|http://www.researchpanelasia.com/>.
100              
101             =head1 METHODS
102              
103             =head2 new( \%options )
104              
105             Creates a new instance.
106              
107             Possible options:
108              
109             =over 4
110              
111             =item C<app_id>
112              
113             (Required) Your C<app_id>.
114              
115             =item C<app_secret>
116              
117             (Required) Your C<app_secret>.
118              
119             =item C<time>
120              
121             (Optional) POSIX time.
122              
123             =back
124              
125             =head2 app_id
126              
127             Gets C<app_id> configured to instance.
128              
129             =head2 app_secret
130              
131             Gets C<app_secret> configured to instance.
132              
133             =head2 time
134              
135             Gets C<time> configured to instance.
136              
137             =head2 create_request( $type, $uri, $params )
138              
139             Creates a new L<HTTP::Request> object for API request.
140              
141             I<$type> can be one of followings:
142              
143             =over 4
144              
145             =item C<GET>
146              
147             For HTTP GET request to SOP endpoint with signature in query string as parameter
148             B<sig>.
149              
150             =item C<POST>
151              
152             For HTTP POST request to SOP endpoint with signature in query string as
153             parameter B<sig> of request content type C<application/x-www-form-urlencoded>.
154              
155             =item C<POST_JSON>
156              
157             For HTTP POST request to SOP endpoint with signature as request header
158             C<X-Sop-Sig> of request content type C<application/json>.
159              
160             =back
161              
162             =head2 verify_signature( $sig, $params )
163              
164             Verifies if request signature is valid.
165              
166             =head1 SEE ALSO
167              
168             L<WebService::SOP::Auth::V1_1::Request::GET>,
169             L<WebService::SOP::Auth::V1_1::Request::POST>,
170             L<WebService::SOP::Auth::V1_1::Request::POST_JSON>,
171             L<WebService::SOP::Auth::V1_1::Util>
172              
173             Research Panel Asia, Inc. website L<http://www.researchpanelasia.com/>
174              
175             =head1 LICENSE
176              
177             Copyright (C) Research Panel Asia, Inc.
178              
179             This library is free software; you can redistribute it and/or modify
180             it under the same terms as Perl itself.
181              
182             =head1 AUTHOR
183              
184             yowcowvg E<lt>yoko_ohyama [ at ] voyagegroup.comE<gt>
185              
186             =cut
187