Cloud/Auth.pm

filter: Statementstmt 35.4% 39 / 110 Branchbran 0.0% 0 / 50 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 68.7% 11 / 16 Podpod 33.3% 1 / 3 totaltotal 26.5% 52 / 196 CC 31 SCAR 41.7
CC
31
Cov
23%
login
132.0
__getToken
110.0
__parseInfo
42.0
CRAP 64.8
↑ Summary Cloud.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.10';
15  
16 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
17 1 my ($class, %opt) = @_;
18 1 ▶ my $max_redirect = $opt{'-max_redirect'} // 30;
Condition50%1/2
$opt{"-max_redirect"} // 30
l!l
01
MC/DC0%0/1
$opt{"-max_redirect"} // 30
$opt{"-max_redirect"}
Truth table50%1/2
A
A= $opt{"-max_redirect"}
Aresult
11
00
19 1 my $self = {};
20 1 $self->{debug} = $opt{-debug};
21 1 my $ua = LWP::UserAgent->new (
22 agent => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
23 cookie_jar => {},
24 );
25 1 $self->{ua} = $ua;
26 1 $self->{ua}->max_redirect($max_redirect);
27 1 return bless $self, $class;
28 }
29  
30 0 ▶sub login {
Subroutine login: not called CC=11 SCAR=48.8
Pod documented
31 0 my ($self, %opt) = @_;
32 0 ▶ $self->{login} = $opt{-login} || $self->{login} || croak "You must specify -login opt for 'login' method";
Condition0%0/5
$opt{'-login'} || $self->{'login'} || croak(q[You must specify -login opt for 'login' method])
l!l
00
$opt{"-login"} or $self->{"login"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-login'} || $self->{'login'} || croak(q[You must specify -login opt for 'login' method])
$opt{"-login"} $self->{"login"}
Truth table0%0/3
A or B
A= $opt{"-login"}B= $self->{"login"}
ABresult
1X1
011
000
33 0 ▶ $self->{password} = $opt{-password} || $self->{password} || croak "You must specify -password opt for 'login' method";
Condition0%0/5
$opt{'-password'} || $self->{'password'} || croak(q[You must specify -password opt for 'login' method])
l!l
00
$opt{"-password"} or $self->{"password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-password'} || $self->{'password'} || croak(q[You must specify -password opt for 'login' method])
$opt{"-password"} $self->{"password"}
Truth table0%0/3
A or B
A= $opt{"-password"}B= $self->{"password"}
ABresult
1X1
011
000
34 0 my $res;
35  
36 #Get login token
37 0 $res = $self->{ua}->get('https://mail.ru');
38 0 ▶ if ($res->code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code ne 200) 0 0 0
39 0 croak "Can't get start mail.ru page. Code: " . $res->code;
40 }
41  
42 0 my $login_token;
43  
44 # В бровзере ловится вот этой регулярочкой
45 0 ▶ if ($res->decoded_content =~ /CSRF\s*=\s*\"([a-f|A-F|0-9]+)\"/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->decoded_content =~ /CSRF\s*=\s*\"([a-f|A-F|0-9]+)\"/) { } 0 0 0
elsif ($res->decoded_content =~ /CSRF:\s+\"([a-f|A-F|0-9]+)\"/) { } 0 0 0
46 0 $login_token = $1;
47 # В этом скрипте - почему-то вот этой
48 } elsif ($res->decoded_content =~ /CSRF:\s+\"([a-f|A-F|0-9]+)\"/) {
49 0 $login_token = $1;
50 } else {
51 0 croak "Can't found login token";
52 }
53  
54 #Login
55 my $param = {
56 'login' => $self->{login},
57 'password' => $self->{password},
58 0 'saveauth' => 1,
59 'project' => 'e.mail.ru',
60 'token' => $login_token,
61 };
62  
63 0 my %headers = (
64 'Referer' => 'https://mail.ru/?from=logout',
65 'Origin' => 'https://mail.ru',
66 );
67  
68 0 $res = $self->{ua}->post('https://auth.mail.ru/jsapi/auth', $param, %headers);
69 0 ▶ if ($res->code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code ne 200) 0 0 0
70 0 croak "Wrong response code from login form: " . $res->code;
71 }
72  
73 0 my $json = decode_json($res->decoded_content);
74 0 ▶ if ($json->{status} eq 'fail') {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"status"} eq "fail") 0 0 0
75 0 croak "Fail login: $json->{code}";
76 }
77  
78 0 ▶ $self->__getToken() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->__getToken or return 0 0 0
79 0 return $self->{authToken};
80 }
81  
82 0 ▶sub __getToken {
Subroutine __getToken: not called CC=10 SCAR=47.0
83 0 my $self = shift;
84  
85 0 my $res = $self->{ua}->get('https://auth.mail.ru/sdc?from=' . uri_escape('https://cloud.mail.ru/?from=promo&from=authpopup') );
86  
87 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
88 0 my $content = $res->decoded_content;
89 0 ▶ if ($content =~ /\"csrf\"\s*\:\s*\"([0-9|a-z|A-Z|_]+)\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\"csrf\"\s*\:\s*\"([0-9|a-z|A-Z|_]+)\"/) 0 0 0
90 0 $self->{authToken} = $1;
91 0 ▶ carp "Found authToken: $self->{authToken}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
92  
93 0 ▶ if ($content =~ /\"email\"\s*:\s*\"(.+?)\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\"email\"\s*:\s*\"(.+?)\"/) 0 0 0
94 0 $self->{email} = $1;
95 0 ▶ carp "Found email: $self->{email}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
96  
97 #Get BUILD
98 0 $self->{build} = 'hotfix_CLOUDWEB-7726_50-0-3.201710311503';
99 0 ▶ if ($content =~ /\"BUILD\"\s*:\s*\"(.+?)\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\"BUILD\"\s*:\s*\"(.+?)\"/) 0 0 0
100 0 $self->{build} = $1;
101 0 ▶ carp "Found and use new build $self->{build}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
102 }
103  
104 #Get x-page-id
105 0 $self->{'x-page-id'} = 'f9jfLFeHA5';
106 0 ▶ if ($content =~ /\"x\-page\-id\"\s*:\s*\"(.+?)\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\"x\-page\-id\"\s*:\s*\"(.+?)\"/) 0 0 0
107 0 $self->{'x-page-id'} = $1;
108 0 ▶ carp "Found and use new x-page_id $self->{build}" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
109 }
110  
111 #Parse free space info
112 0 $self->{info} = __parseInfo(\$content);
113 0 return 1;
114 }
115  
116 }
117 }
118 0 return;
119 }
120  
121 0 ▶sub info {
Subroutine info: not called CC=2 SCAR=17.9
Pod undocumented
122 0 my $self = shift;
123 0 ▶ if ($self->{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 0 0
124 0 my %info = map {$_, $self->{info}->{$_}} keys %{$self->{info}};
125 0 return \%info;
126 }
127 0 return;
128 }
129  
130 0 ▶sub __parseInfo {
Subroutine __parseInfo: not called CC=6 SCAR=37.4
131 0 my $content = shift;
132 0 my %info = (
133 'used_space' => '',
134 'total_space' => '',
135 'file_size_limit' => '',
136 );
137  
138 0 ▶ if (my ($size_block) = $$content =~ /"space":\s*{([^}]*)}/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($size_block) = $$content =~ /"space":\s*{([^}]*)}/s) 0 0 0
139 0 ▶ while ($size_block =~ /"([^"]+)":\s*(\w+?)\b/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_block =~ /"([^"]+)":\s*(\w+?)\b/gm) 0 0 0
140 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
141 0 $info{total_space} = $2;
142 }
143 elsif ($1 eq 'bytes_used') {
144 0 $info{used_space} = $2;
145 }
146 }
147 }
148  
149 0 ▶ if ($$content =~ /"file_size_limit":\s*(.+?)[,\s]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content =~ /"file_size_limit":\s*(.+?)[,\s]/) 0 0 0
150 0 $info{file_size_limit} = $1;
151 }
152 0 return \%info;
153 }
154  
155 0 ▶sub __isLogin {
Subroutine __isLogin: not called CC=5 SCAR=34.0
156 0 my $self = shift;
157 0 ▶ if ($self->{authToken}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"authToken"}) 0 0 0
158 0 my $ua = $self->{ua};
159 0 my $res = $ua->get('https://auth.mail.ru/cgi-bin/auth?mac=1&Login=' . uri_escape($self->{login}));
160 0 my $code = $res->code;
161 0 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 0 0
162 0 croak "Can't get status about login";
163 }
164 0 my $json_res = decode_json($res->content);
165 0 ▶ $json_res->{status} eq 'ok' and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$json_res->{"status"} eq "ok" and return 1 0 0 0
166 0 ▶ $self->login() and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->login and return 1 0 0 0
167 }
168  
169 0 croak "Not logined";
170 }
171  
172 1 1;
173  
174  
175 __END__
↑ Summary Cloud.pm »