File Coverage

blib/lib/WebService/DS/SOP/Auth/V1_1.pm
Criterion Covered Total %
statement 51 51 100.0
branch 5 6 83.3
condition 2 2 100.0
subroutine 18 18 100.0
pod 6 6 100.0
total 82 83 98.8


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