lib/Mailru/Cloud/Auth.pm

filter: Statementstmt 43.2% 48 / 111 Branchbran 4.1% 2 / 48 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 12 / 16 Podpod 33.3% 1 / 3 totaltotal 34.1% 67 / 196 CC 29 SCAR 37.5
CC
29
Cov
32%
__getToken
110.0
__parseInfo
42.0
login
35.1
CRAP 42.6
↑ Summary blib/lib/Mailru/Cloud/Auth.pm »
1 package Mailru::Cloud::Auth;
2  
3 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 our $VERSION = '0.06';
15  
16 sub new {
17 1 ▶ my ($class, %opt) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 1 my $self = {};
19 1 $self->{debug} = $opt{-debug};
20 1 my $ua = LWP::UserAgent->new (
21 agent => 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
22 cookie_jar => {},
23 );
24 1 $self->{ua} = $ua;
25 1 return bless $self, $class;
26 }
27  
28 sub login {
29 1 ▶ my ($self, %opt) = @_;
Subroutine login: called CC=10 SCAR=35.6
Pod documented
30 1 ▶ $self->{login} = $opt{-login} || $self->{login} || croak "You must specify -login opt for 'login' method";
Condition33%2/6
$opt{'-login'} || $self->{'login'} || croak(q[You must specify -login opt for 'login' method])
l!l&&r!l&&!r
100
$opt{"-login"} or $self->{"login"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$opt{'-login'} || $self->{'login'} || croak(q[You must specify -login opt for 'login' method])
$opt{"-login"} $self->{"login"} croak(q[You must specify -login opt for 'login' method])
Truth table25%1/4
A or B || C
A= $opt{"-login"}B= $self->{"login"}C= croak(q[You must specify -login opt for 'login' method])
ABCresult
1XX1
01X1
0011
0000
31 1 ▶ $self->{password} = $opt{-password} || $self->{password} || croak "You must specify -password opt for 'login' method";
Condition33%2/6
$opt{'-password'} || $self->{'password'} || croak(q[You must specify -password opt for 'login' method])
l!l&&r!l&&!r
100
$opt{"-password"} or $self->{"password"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$opt{'-password'} || $self->{'password'} || croak(q[You must specify -password opt for 'login' method])
$opt{"-password"} $self->{"password"} croak(q[You must specify -password opt for 'login' method])
Truth table25%1/4
A or B || C
A= $opt{"-password"}B= $self->{"password"}C= croak(q[You must specify -password opt for 'login' method])
ABCresult
1XX1
01X1
0011
0000
32 1 my $ua = $self->{ua};
33 1 my ($res, $code);
34  
35 #Get login token
36 1 $res = $ua->get('https://mail.ru');
37 1 $code = $res->code;
38 1 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 1 1
39 0 croak "Can't get start mail.ru page. Code: $code";
40 }
41 1 my ($login_token) = $res->decoded_content =~ /CSRF\s*[:=]\s*"([0-9A-Za-z]+?)"/;
42 1 ▶ if (not $login_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login_token) 1 0 1
43 1 croak "Can't found login token";
44 }
45  
46 #Login
47 my %param = (
48 'login' => $self->{login},
49 'password' => $self->{password},
50 0 'saveauth' => 1,
51 'project' => 'e.mail.ru',
52 'token' => $login_token,
53 );
54 0 my %headers = (
55 'Content-type' => 'application/x-www-form-urlencoded',
56 'Accept' => '*/*',
57 'Accept-Encoding' => 'gzip, deflate, br',
58 'Accept-Language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
59 'Referer' => 'https://mail.ru/?from=logout',
60 'Origin' => 'https://mail.ru',
61 );
62  
63 0 $res = $ua->post('https://auth.mail.ru/jsapi/auth', \%param, %headers);
64 0 $code = $res->code;
65 0 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 0 0
66 0 croak "Wrong response code from login form: $code";
67 }
68  
69 0 my $json = decode_json($res->decoded_content);
70 0 ▶ if ($json->{status} eq 'fail') {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"status"} eq "fail") 0 0 0
71 0 croak "Fail login: $json->{code}";
72 }
73  
74 0 ▶ $self->__getToken() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->__getToken or return 0 0 0
75 0 return $self->{authToken};
76 0 return;
77 }
78  
79 sub __getToken {
80 0 ▶ my $self = shift;
Subroutine __getToken: not called CC=10 SCAR=47.0
81  
82 0 my $ua = $self->{ua};
83 0 my $res = $ua->get('https://cloud.mail.ru/?from=promo&from=authpopup');
84  
85 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
86 0 my $content = $res->decoded_content;
87 0 ▶ if ($content =~ /"csrf"\s*:\s*"([a-zA-Z0-9]+?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /"csrf"\s*:\s*"([a-zA-Z0-9]+?)"/) 0 0 0
88 0 $self->{authToken} = $1;
89 0 ▶ carp "Found authToken: $self->{authToken}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
90  
91 0 ▶ if ($content =~ /"email"\s*:\s*"(.+?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /"email"\s*:\s*"(.+?)"/) 0 0 0
92 0 $self->{email} = $1;
93 0 ▶ carp "Found email: $self->{email}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
94
95 #Get BUILD
96 0 $self->{build} = 'hotfix_CLOUDWEB-7726_50-0-3.201710311503';
97 0 ▶ if ($content =~ /"BUILD"\s*:\s*"(.+?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /"BUILD"\s*:\s*"(.+?)"/) 0 0 0
98 0 $self->{build} = $1;
99 0 ▶ carp "Found and use new build $self->{build}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
100 }
101
102 #Get x-page-id
103 0 $self->{'x-page-id'} = 'f9jfLFeHA5';
104 0 ▶ if ($content =~ /"x-page-id"\s*:\s*"(.+?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /"x-page-id"\s*:\s*"(.+?)"/) 0 0 0
105 0 $self->{'x-page-id'} = $1;
106 0 ▶ carp "Found and use new x-page_id $self->{build}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
107 }
108  
109 #Parse free space info
110 0 $self->{info} = __parseInfo(\$content);
111 0 return 1;
112 }
113  
114 }
115 }
116 0 return;
117 }
118  
119 sub info {
120 0 ▶ my $self = shift;
Subroutine info: not called CC=2 SCAR=17.9
Pod undocumented
121 0 ▶ if ($self->{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 0 0
122 0 my %info = map {$_, $self->{info}->{$_}} keys %{$self->{info}};
123 0 return \%info;
124 }
125 0 return;
126 }
127  
128 sub __parseInfo {
129 0 ▶ my $content = shift;
Subroutine __parseInfo: not called CC=6 SCAR=37.4
130 0 my %info = (
131 'used_space' => '',
132 'total_space' => '',
133 'file_size_limit' => '',
134 );
135
136 0 ▶ if (my ($size_block) = $$content =~ /"space":\s*{([^}]*)}/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($size_block) = $$content =~ /"space":\s*{([^}]*)}/s) 0 0 0
137 0 ▶ while ($size_block =~ /"([^"]+)":\s*(\w+?)\b/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_block =~ /"([^"]+)":\s*(\w+?)\b/gm) 0 0 0
138 0 ▶ if ($1 eq 'bytes_total') {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'bytes_total') { } 0 0 0
elsif ($1 eq 'bytes_used') { } 0 0 0
139 0 $info{total_space} = $2;
140 }
141 elsif ($1 eq 'bytes_used') {
142 0 $info{used_space} = $2;
143 }
144 }
145 }
146  
147 0 ▶ if ($$content =~ /"file_size_limit":\s*(.+?)[,\s]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content =~ /"file_size_limit":\s*(.+?)[,\s]/) 0 0 0
148 0 $info{file_size_limit} = $1;
149 }
150 0 return \%info;
151 }
152  
153 sub __isLogin {
154 0 ▶ my $self = shift;
Subroutine __isLogin: not called CC=5 SCAR=34.0
155 0 ▶ if ($self->{authToken}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"authToken"}) 0 0 0
156 0 my $ua = $self->{ua};
157 0 my $res = $ua->get('https://auth.mail.ru/cgi-bin/auth?mac=1&Login=' . uri_escape($self->{login}));
158 0 my $code = $res->code;
159 0 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 0 0
160 0 croak "Can't get status about login";
161 }
162 0 my $json_res = decode_json($res->content);
163 0 ▶ $json_res->{status} eq 'ok' and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$json_res->{"status"} eq "ok" and return 1 0 0 0
164 0 ▶ $self->login() and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->login and return 1 0 0 0
165 }
166  
167 0 croak "Not logined";
168 }
169  
170 1 1;
171  
172  
173 __END__
↑ Summary blib/lib/Mailru/Cloud/Auth.pm »