blib/lib/Net/Linkedin/OAuth2.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 101 CC 7 SCAR 20.9
CC
7
Cov
0%
request
20.0
get_access_token
12.0
authorization_code_url
6.0
CRAP 8.1
1 package Net::Linkedin::OAuth2;
2  
3 use strict;
4 use warnings;
5 use JSON::Any;
6 use LWP::UserAgent;
7 use Carp 'confess';
8 use XML::Hash;
9 use Digest::MD5 'md5_hex';
10  
11 require Exporter;
12  
13 our @ISA = qw(Exporter);
14  
15 our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
16  
17 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18  
19 our @EXPORT = qw(
20
21 );
22  
23 our $VERSION = '0.31';
24  
25  
26 =head1 NAME
27  
28 Net::Linkedin::OAuth2 - An easy way to authenticate users via LinkedIn.
29  
30 =head1 VERSION
31  
32 version 0.31
33  
34 =head1 SYNOPSIS
35  
36 my $linkedin = Net::Linkedin::OAuth2->new( key => 'your-app-key', secret => 'your-app-secret');
37
38 =head2 get authorization url({ redirect_uri => 'http://localhost:3000/user/linkedin', scope => ['r_basicprofile','rw_groups','r_emailaddress']})
39  
40 # scope is an array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional
41  
42 my $authorization_code_url = $linkedin->authorization_code_url(
43 redirect_uri => 'url_of_your_app_to_intercept_success',
44 scope => ['r_basicprofile','rw_groups','r_emailaddress']
45 );
46  
47 # convert code response to an access token
48 # redirect_uri is the url where you will check for the parameter code.
49 # param('code') is the parameter 'code' that you will get after the user authorizes your app and gets redirected to the redirect_uri (callback) page.
50
51 my $token_object = $linkedin->get_access_token(
52 authorization_code => param('code'),
53 redirect_uri => 'your-app-redirect-url-or-callback'
54 );
55
56 # use the new token to request user information
57  
58 my $result = $linkedin->request(
59 url => 'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address)?format=json',
60 token => $token_object->{access_token}
61 );
62
63 # we have the email address
64 if ($result->{emailAddress}) {
65 # ...
66 }
67  
68 # Or here is an entire login logic or recipe:
69  
70 my $linkedin = Net::Linkedin::OAuth2->new( key => 'your-app-key',
71 secret => 'your-app-secret');
72
73
74 # catch the code param and try to convert it into an access_token and get the email address
75 if (param('code')) {
76  
77 my $token_object = $linkedin->get_access_token(
78 authorization_code => param('code'),
79 # has to be the same redirect_uri you specified in the code before
80 redirect_uri => 'your-app-redirect-uri-or-callback-url'
81 );
82
83 my $result = $linkedin->request(
84 url => 'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address)?format=json',
85 token => $token_object->{access_token} );
86  
87 if ($result->{emailAddress}) {
88 # we have the email address, authenticate the user and redirect somewhere..
89 # ....
90
91 return;
92 } else {
93 # we did not get an email address
94 # redirect to try again?
95
96 return;
97 }
98
99 }
100
101 # get the url for permissions
102
103 my $authorization_code_url = $linkedin->authorization_code_url(
104 # this field is required
105 redirect_uri => 'your-app-redirect',
106 #array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional
107 scope => ['r_basicprofile','rw_groups','r_emailaddress']
108 );
109
110 #redirect the user to get their permission
111 redirect($authorization_code_url);
112  
113 # and catch an error back from linked in
114 if (param('error')) {
115 # handle the error
116 # if the user denied, redirect to try again...
117 }
118
119 =head1 SEE ALSO
120  
121  
122 http://developer.linkedin.com/documents
123  
124 =head1 AUTHOR
125  
126 Asaf Klibansky
127  
128 discobeta@gmail.com
129  
130 =head1 METHODS
131  
132  
133 =head2 authorization_code_url( { redirect_uri => '...', scope => '...'} )
134  
135 =over
136  
137 B<Definition:> This method is used to get the url required to authenticate the
138 user via LinkedIn OAuth2.
139 I<It assumes that you have a linkedin api key and secret which you may obtain
140 here https://www.linkedin.com/secure/developer.> Basically this builds the
141 url where you should redirect the user to obtain their permission to access
142 certain information (scope) on linkedin.
143  
144 B<Accepts:> a hash or hashref of arguments. They must include the
145 necessary information to build the url.
146 I<redirect_url> is a url where the user should be redirect to after successfuly
147 authorizing (or not) and should be a method ready to capture the 'code' or
148 'error' parameters.'
149 I<scope> scope is an array of permissions that your app requires,
150 see http://developer.linkedin.com/documents/authentication#granting for
151 more details, this field is optional
152  
153 B<Returns:> This will return a string containing the url where the user should
154 be redirect to in order to obtain their linkedin permissions.
155 =back
156  
157 =cut
158  
159 sub authorization_code_url {
160 my ($self, %args) = @_;
161 foreach(qw( redirect_uri )) {
162 confess "Required '$_' was not specified" unless $args{$_};
163 }
164 return "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=$self->{key}&scope=".join('+',$self->{scope})."&state=".rand()."&redirect_uri=$args{redirect_uri}";
165 }
166 =back
167  
168 =head2 get_access_token( { authorization_code => '...', redirect_uri => '...'} )
169  
170 =over
171  
172 B<Definition:> This method is used to convert the parameter 'code' that we
173 got from facebook after obtaining the user permissions into an access token
174 that we can later use to access the LinkedIn API.
175  
176 B<Accepts:> a hash or hashref of arguments. They must include the
177 necessary information to convert the code.
178 I<authorization_code> is the parameter linkedin provided you with after
179 successfully obtaining a user's permission.
180 I<redirect_uri> is a url where the user should be redirect to after successfuly
181 authorizing (or not) and should be a method ready to capture the 'code' or
182 'error' parameters.'
183  
184 B<Returns:> This will return a hash containing an access_token and an expires_in keys and values
185  
186 =back
187  
188 =cut
189  
190 sub get_access_token {
191 my ($self, %args) = @_;
192 foreach(qw( authorization_code redirect_uri )) {
193 confess "Required '$_' was not specified" unless $args{$_};
194 }
195 my $r = $self->{class}->get("https://api.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=$args{authorization_code}&redirect_uri=$args{redirect_uri}&client_id=$self->{key}&client_secret=$self->{secret}");
196 if (!$r->is_success){
197 my $j = JSON::Any->new;
198 my $error = $j->jsonToObj($r->content());
199 }
200 my $file = $r->content();
201 my $j = JSON::Any->new;
202 my $res = $j->jsonToObj($r->content());
203 return $res;
204 }
205  
206  
207  
208 =head2 new( { key => '...', secret => '...', scope => ['...'] } )
209  
210 =over
211  
212 B<Definition:> This method is used to convert the parameter 'code' that we
213 got from facebook after obtaining the user permissions into an access token
214 that we can later use to access the LinkedIn API.
215  
216 B<Accepts:> a hash or hashref of arguments. They must include the
217 necessary information to convert the code.
218 I<key> is the key linkedin provided you with when you create an app. See
219 https://www.linkedin.com/secure/developer for more details.
220 I<secret> is tha app secret that linkedin provided you with when you create
221 an app.
222 I<scope> scope is an array of permissions that your app requires,
223 see http://developer.linkedin.com/documents/authentication#granting for
224 more details, this field is optional
225  
226 B<Returns:> This will create an interface to the linked in API
227  
228 =back
229  
230 =cut
231  
232 sub new {
233 my ($class, %args) = @_;
234 my $self = bless {}, $class;
235 $self->{class} = LWP::UserAgent->new(
236 params => %args,
237 );
238 $self->{ params } = %args;
239 $self->{ key } = %args->{'key'};
240 $self->{ secret } = %args->{'secret'};
241 my @e;
242 for (my $n=0; $n <= 2; $n++) {
243 push @e, %args->{'scope'}[$n];
244 }
245 $self->{ scope } = join('+',@e);
246 return $self;
247 }
248  
249  
250  
251 =head2 request( { url => '...', token => '...' } )
252  
253 =over
254  
255 B<Definition:> This method is used to access the linkedin api.
256  
257 B<Accepts:> a hash or hashref of arguments. They must include the
258 necessary information to convert the code.
259 I<url> is the linkedin API url to access. See
260 https://developer.linkedin.com/docs for more details.
261 I<token> is a valid token that you retrieved from a successful
262 linkedin authentication.
263  
264 B<Returns:> This will return a scalar with the results from a given url
265  
266 =back
267  
268 =cut
269  
270 sub request {
271 my ($self, %args) = @_;
272 foreach(qw( url token )) {
273 confess "Required '$_' was not specified" unless $args{$_};
274 }
275 my $url;
276 if ($args{url} =~ /\?/) {
277 $url = "$args{url}&oauth2_access_token=$args{token}";
278 } else {
279 $url = "$args{url}?oauth2_access_token=$args{token}";
280 }
281 my $r = $self->{class}->get($url);
282 if (!$r->is_success){
283 my $j = XML::Hash->new();
284 my $error = $j->fromXMLStringtoHash($r->content());
285 }
286 my $j = JSON::Any->new;
287 return $j->jsonToObj( $r->content() );
288 }
289  
290 1;
291