blib/lib/Mailru/Cloud/Auth.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 165 CC 12 SCAR 26.2
CC
12
Cov
0%
__getToken
42.0
__parseInfo
12.0
__isLogin
12.0
CRAP 13.7
1 package Mailru::Cloud::Auth;
2  
3 use 5.008001;
4 use strict;
5 use warnings;
6 use utf8;
7 use open qw(:std :utf8);
8 use LWP::UserAgent;
9 use HTTP::Request;
10 use JSON::XS;
11 use URI::Escape;
12 use Carp 'croak';
13
14 our $VERSION = '0.02';
15  
16 sub new {
17 my ($class) = @_;
18 my $self = {};
19 my $ua = LWP::UserAgent->new (
20 agent => 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
21 cookie_jar => {},
22 );
23 $self->{ua} = $ua;
24 return bless $self, $class;
25 }
26  
27 sub login {
28 my ($self, %opt) = @_;
29 $self->{login} = $opt{-login} || $self->{login} || croak "You must specify -login opt for 'login' method";
30 $self->{password} = $opt{-password} || $self->{password} || croak "You must specify -password opt for 'login' method";
31 my $ua = $self->{ua};
32  
33 my %param;
34 $param{Domain} = 'mail.ru';
35 $param{FailPage} = '';
36 $param{Login} = $self->{login};
37 $param{Password} = $self->{password};
38 $param{new_auth_form} = 1;
39 $param{page} ='https://cloud.mail.ru/?from=promo';
40 $param{saveauth} = 1;
41  
42 my $res = $ua->post('https://auth.mail.ru/cgi-bin/auth?lang=ru_RU&from=authpopup', \%param);
43  
44 my $code = $res->code;
45 if ($code eq '302' || $code eq '301' || $code eq '200') {
46 $self->__getToken() or return;
47 return $self->{authToken};
48 }
49 return;
50 }
51  
52 sub __getToken {
53 my $self = shift;
54  
55 my $ua = $self->{ua};
56 my $res = $ua->get('https://cloud.mail.ru/?from=promo&from=authpopup');
57  
58 if ($res->is_success) {
59 my $content = $res->decoded_content;
60 if ($content =~ /"csrf":"(.+?)"/) {
61 $self->{authToken} = $1;
62 if ($content =~ /"email":"(.+?)"/) {
63 $self->{email} = $1;
64
65 #Get BUILD
66 $self->{build} = 'hotfix_CLOUDWEB-7726_50-0-3.201710311503';
67 if ($content =~ /"BUILD":"(.+?)"/) {
68 $self->{build} = $1;
69 }
70
71 #Get x-page-id
72 $self->{'x-page-id'} = 'f9jfLFeHA5';
73 if ($content =~ /"x-page-id":"(.+?)"/) {
74 $self->{'x-page-id'} = $1;
75 }
76  
77 #Parse free space info
78 $self->{info} = __parseInfo(\$content);
79 return 1;
80 }
81  
82 }
83 }
84 return;
85 }
86  
87 sub info {
88 my $self = shift;
89 if ($self->{info}) {
90 my %info = map {$_, $self->{info}->{$_}} keys %{$self->{info}};
91 return \%info;
92 }
93 return;
94 }
95  
96 sub __parseInfo {
97 my $content = shift;
98 my %info = (
99 'used_space' => '',
100 'total_space' => '',
101 'file_size_limit' => '',
102 );
103 if ($$content =~ /"space":[{].+?"used":(.+?),"total":(.+?)[}]/) {
104 $info{used_space} = $1 * 1024 * 1024; # To bytes from Mbytes
105 $info{total_space} = $2 * 1024 * 1024; # To bytes from Mbytes
106 }
107 if ($$content =~ /"file_size_limit":(.+?),/) {
108 $info{file_size_limit} = $1;
109 }
110 return \%info;
111 }
112  
113 sub __isLogin {
114 my $self = shift;
115 if ($self->{authToken}) {
116 my $ua = $self->{ua};
117 my $res = $ua->get('https://auth.mail.ru/cgi-bin/auth?mac=1&Login=' . uri_escape($self->{login}));
118 my $code = $res->code;
119 if ($code ne '200') {
120 croak "Can't get status about login";
121 }
122 my $json_res = decode_json($res->content);
123 $json_res->{status} eq 'ok' and return 1;
124 $self->login() and return 1;
125 }
126  
127 croak "Not logined";
128 }
129  
130 1;
131  
132  
133 __END__