SSO.pm

filter: Statementstmt 52.2% 47 / 90 Branchbran 27.2% 6 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.6% 13 / 15 Podpod 100% 3 / 3 totaltotal 52.5% 71 / 135 CC 14 SCAR 23.8
CC
14
Cov
48%
_webserver
30.0
get_token
15.9
get_token_through_webserver
12.0
CRAP 10.8
↑ Summary SSO/Client.pm »
1  
2 =encoding utf-8
3  
4 =head1 NAME
5  
6 EveOnline::SSO - Module for Single Sign On in EveOnline API-services.
7  
8 =head1 SYNOPSIS
9  
10 use EveOnline::SSO;
11  
12 my $sso = EveOnline::SSO->new(client_id => '03ed7324fe4f455', client_secret => 'bgHejXdYo0YJf9NnYs');
13
14 # return url for open in browser
15 print $sso->get_code();
16 # or
17 print $sso->get_code(state => 'some_ids_or_flags');
18 # or
19 print $sso->get_code(state => 'some_ids_or_flags', scope=>'esi-calendar.respond_calendar_events.v1 esi-location.read_location.v1');
20  
21 # return hash with access and refresh tokens by auth code
22 print Dumper $sso->get_token(code=>'tCaVozogf45ttk-Fb71DeEFcSYJXnCHjhGy');
23 # or hash with access and refresh tokens by refresh_token
24 print Dumper $sso->get_token(refresh_token=>'berF1ZVu_bkt2ud1JzuqmjFkpafSkobqdso');
25
26 # return hash with access and refresh tokens through listening light web-server
27 print Dumper $sso->get_token_through_webserver(
28 scope=>'esi-calendar.respond_calendar_events.v1 esi-location.read_location.v1',
29 state=> 'Awesome'
30 );
31  
32  
33 =head1 DESCRIPTION
34  
35 EveOnline::SSO is a perl module for get auth in https://eveonline.com through Single Sign-On (OAuth) interface.
36  
37 =cut
38  
39 package EveOnline::SSO;
40 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use LWP::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 our $VERSION = "0.03";
54  
55  
56 has 'ua' => (
57 is => 'ro',
58 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 3 my $ua = LWP::UserAgent->new();
60 3 $ua->agent( 'EveOnline::SSO Perl Client' );
61 3 $ua->timeout( 120 );
62 3 return $ua;
63 }
64 2 );
65  
66 2 has 'auth_url' => (
67 is => 'ro',
68 default => 'https://login.eveonline.com/oauth/authorize/',
69  
70 );
71  
72 2 has 'token_url' => (
73 is => 'ro',
74 default => 'https://login.eveonline.com/oauth/token',
75 );
76  
77 2 has 'callback_url' => (
78 is => 'rw',
79 default => 'http://localhost:10707/',
80 );
81  
82 2 has 'client_id' => (
83 is => 'rw',
84 required => 1,
85 );
86  
87 2 has 'client_secret' => (
88 is => 'rw',
89 required => 1,
90 );
91  
92 2 has 'demo' => (
93 is => 'rw'
94 );
95  
96 =head1 CONSTRUCTOR
97  
98 =over
99  
100 =item B<new()>
101  
102 Require two arguments: client_id and client_secret.
103 Optional arguments: callback_url. Default is http://localhost:10707/
104  
105 Get your client_id and client_secret on EveOnline developers page:
106 L<https://developers.eveonline.com/>
107  
108 =back
109  
110 =head1 METHODS
111  
112 =over
113  
114 =item B<get_code()>
115  
116 Return URL for open in browser.
117  
118 Optional params: state, scope
119  
120 See available scopes on L<https://developers.eveonline.com/>
121  
122 # return url for open in browser
123 print $sso->get_code();
124
125 # or
126 print $sso->get_code(state => 'some_ids_or_flags');
127
128 # or
129 print $sso->get_code(scope=>'esi-calendar.respond_calendar_events.v1 esi-location.read_location.v1');
130  
131 =back
132 =cut
133  
134 4 ▶sub get_code {
Subroutine get_code: called CC=3 SCAR=11.0
Pod documented
135 4 my ( $self, %params ) = @_;
136  
137 return $self->auth_url .
138 "?response_type=code&client_id=".$self->client_id .
139 "&redirect_uri=".uri_escape( $self->callback_url ) .
140 ( ( defined $params{scope} ) ? "&scope=" . uri_escape( $params{scope} ) : '' ) .
141 4 ▶ ( ( defined $params{state} ) ? "&state=" . uri_escape( $params{state} ) : '' );
Branch: 2 branches
BranchTrueFalseTotal
defined $params{'scope'} ? : 1 3 4
defined $params{'state'} ? : 2 2 4
142 }
143  
144 =over
145  
146 =item B<get_token()>
147  
148 Return hashref with access and refresh tokens.
149 refresh_token is undef if code was received without scopes.
150  
151 Need "code" or "refresh_token" in arguments.
152
153 # return hash with access and refresh tokens by auth code
154 print Dumper $sso->get_token(code=>'tCaVozogf45ttk-Fb71DeEFcSYJXnCHjhGy');
155
156 # or hash with access and refresh tokens by refresh_token
157 print Dumper $sso->get_token(refresh_token=>'berF1ZVu_bkt2ud1JzuqmjFkpafSkobqdso');
158  
159 =back
160 =cut
161  
162 2 ▶sub get_token {
Subroutine get_token: called CC=6 SCAR=27.7
Pod documented
163 2 my ( $self, %params ) = @_;
164  
165 2 ▶ return unless $params{code} || $params{refresh_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"code"} or $params{"refresh_token"} 0 2 2
Condition66%2/3
$params{"code"} or $params{"refresh_token"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{"code"} or $params{"refresh_token"}
$params{"code"} $params{"refresh_token"}
Truth table66%2/3
A or B
A= $params{"code"}B= $params{"refresh_token"}
ABresult
1X1
011
000
166  
167 2 ▶ return JSON::XS::decode_json( $self->demo ) if $self->demo;
Branch: 1 branch
BranchTrueFalseTotal
if $self->demo 2 0 2
168  
169 0 my $base64 = encode_base64($self->client_id.':'.$self->client_secret,"");
170 0 $self->ua->default_header('Authorization' => "Basic " . $base64 );
171 0 $self->ua->default_header('Content-Type' => "application/x-www-form-urlencoded");
172  
173 0 my $post_params = {};
174 0 foreach my $key ( keys %params ) {
175 0 $post_params->{$key} = $params{$key};
176 }
177  
178 my $res = $self->ua->post($self->token_url, {
179 %$post_params,
180 0 ▶ grant_type => $params{code} ? 'authorization_code' : 'refresh_token',
Branch: 1 branch
BranchTrueFalseTotal
$params{'code'} ? : 0 0 0
181 });
182  
183 0 return JSON::XS::decode_json( $res->content );
184 }
185  
186 =over
187  
188 =item B<get_token_through_webserver()>
189  
190 Return hashref with access and refresh tokens by using local webserver for get code.
191 Use callback_url parameter for start private web server on host and port in callback url.
192  
193 Default url: http://localhost:10707/
194  
195 # return hash with access and refresh tokens
196 print Dumper $sso->get_token_through_webserver(scope=>'esi-location.read_location.v1');
197  
198 =back
199 =cut
200  
201 0 ▶sub get_token_through_webserver {
Subroutine get_token_through_webserver: not called CC=3 SCAR=24.8
Pod documented
202 0 my ( $self, %params ) = @_;
203  
204 0 my $url = $self->get_code( %params );
205  
206 0 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 0 0
207  
208 0 say "Go to url: " . $url;
209 0 my $code = $self->_webserver();
210  
211 0 ▶ if ( $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 0 0
212 0 say $code;
213 0 return $self->get_token(code=>$code);
214 }
215 }
216 0 return;
217 }
218  
219 0 ▶sub _webserver {
Subroutine _webserver: not called CC=5 SCAR=34.0
220 0 my ( $self, %params ) = @_;
221  
222 0 my $headers = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
223
224 0 my $conn = new URI::URL $self->callback_url;
225  
226 0 my $sock = new LWP::Socket();
227 0 ▶ die "Can't bind a socket" unless $sock->bind($conn->host, $conn->port);
Branch: 1 branch
BranchTrueFalseTotal
unless $sock->bind($conn->host, $conn->port) 0 0 0
228 0 $sock->listen(1);
229  
230 0 my $code;
231 0 ▶ while ( my $socket = $sock->accept(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $socket = $sock->accept(1)) 0 0 0
232 0 my $content = "<b>EveOnline::SSO code receiver</b><br />";
233 0 my $request = '';
234 0 $socket->read( \$request );
235 0 ▶ if ( $request =~ /code=/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ /code=/gu) 0 0 0
236 0 ▶ if ( $request =~ /code=([\w-]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ /code=([\w-]+)/u) 0 0 0
237 0 $code = $1;
238 0 $request =~ s/GET \/\?([^ ]*) HTTP.+/$1/s;
239 0 $request =~ s/&/<br \/>/g;
240 0 $request =~ s/=/:/g;
241  
242 0 $content .= $request;
243 0 $content .= "<br />Now you can close this page<br />Fly safe!";
244 0 $socket->write( $headers . $content );
245 0 $socket->shutdown();
246 0 $socket = undef;
247 0 last;
248 }
249 }
250 }
251
252 0 $sock->shutdown();
253 0 $sock = undef;
254 0 return $code;
255 }
256  
257 2 1;
258 __END__
↑ Summary SSO/Client.pm »