lib/AnyEvent/MCP/IMAP/OAuth2.pm

filter: Statementstmt 98.6% 151 / 153 Branchbran 60.9% 50 / 82 Conditioncond 44.4% 32 / 72 MC/DCmcdc 10.0% 4 / 40 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 11 totaltotal 68.2% 260 / 381 CC 74 SCAR 30.7
CC
74
Cov
76%
validate
55.3
token
20.8
callback_code
13.9
CRAP 21.6
« bin/anyevent-mcp ↑ Summary bin/mcp-fs »
1 package AnyEvent::MCP::IMAP::OAuth2;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use AnyEvent::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use AnyEvent::MCP::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 68 ▶sub encode {
Subroutine encode: called CC=2 SCAR=7.0
Pod undocumented
14 68 my ($str) = @_;
15 68 ▶ $str = Encode::encode_utf8 ($str) if utf8::is_utf8 ($str);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($str) 0 68 68
16 68 $str =~ s/([^A-Za-z0-9._~-])/sprintf "%%%02X", ord $1/ge;
17 68 $str
18 }
19  
20 7 ▶sub form {
Subroutine form: called CC=1 SCAR=0.0
Pod undocumented
21 7 my (%pairs) = @_;
22 7 join "&", map +(encode $_) . "=" . (encode $pairs{$_}), sort keys %pairs
23 }
24  
25 15 ▶sub decode {
Subroutine decode: called CC=2 SCAR=7.0
Pod undocumented
26 15 my ($str) = @_;
27 15 ▶ $str =~ /%(?![a-fA-F0-9]{2})/ and die "Invalid OAuth redirect URL encoding\n";
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /%(?![a-fA-F0-9]{2})/u and die "Invalid OAuth redirect URL encoding\n" 0 15 15
28 15 $str =~ tr/+/ /;
29 15 $str =~ s/%([a-fA-F0-9]{2})/chr hex $1/ge;
30 15 $str
31 }
32  
33 3 ▶sub b64url {
Subroutine b64url: called CC=1 SCAR=0.0
Pod undocumented
34 3 my ($bytes) = @_;
35 3 my $s = MIME::Base64::encode_base64 ($bytes, "");
36 3 $s =~ tr{+/}{-_};
37 3 $s =~ s/=+\z//;
38 3 $s
39 }
40  
41 14 ▶sub https {
Subroutine https: called CC=4 SCAR=17.1
Pod undocumented
42 14 my ($url) = @_;
43 14 ▶ defined $url && $url =~ m{\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)}i
Branch: 1 branch
BranchTrueFalseTotal
defined $url and $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu and not $url =~ /[\s\x00-\x1f\x7f#@]/u or die "OAuth endpoint must be an HTTPS URL with a hostname\n" 14 0 14
Condition33%2/6
defined $url and $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu and not $url =~ /[\s\x00-\x1f\x7f#@]/u
!ll&&!rl&&r
0014
defined $url and $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu
!ll&&!rl&&r
0014
MC/DC0%0/3
defined $url and $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu and not $url =~ /[\s\x00-\x1f\x7f#@]/u
defined $url $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu not $url =~ /[\s\x00-\x1f\x7f#@]/u
Truth table25%1/4
A and B and not(C)
A= defined $urlB= $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iuC= not $url =~ /[\s\x00-\x1f\x7f#@]/u
ABCresult
0XX0
10X0
1100
1111
44 && $url !~ /[\s\x00-\x1f\x7f#@]/
45 or die "OAuth endpoint must be an HTTPS URL with a hostname\n";
46 14 $url
47 }
48  
49 7 ▶sub validate {
Subroutine validate: called CC=24 SCAR=40.1
Pod undocumented
50 7 my ($kv) = @_;
51 7 for my $key (qw(oauth-client-id oauth-authorization-endpoint oauth-token-endpoint oauth-scope oauth-redirect-uri)) {
52 35 ▶ defined $kv->{$key} && !ref $kv->{$key} && length $kv->{$key}
Branch: 1 branch
BranchTrueFalseTotal
defined $kv->{$key} and not ref $kv->{$key} and length $kv->{$key} or die "Missing $key for OAuth2 login\n" 35 0 35
Condition33%2/6
defined $kv->{$key} and not ref $kv->{$key} and length $kv->{$key}
!ll&&!rl&&r
0035
defined $kv->{$key} and not ref $kv->{$key}
!ll&&!rl&&r
0035
MC/DC0%0/3
defined $kv->{$key} and not ref $kv->{$key} and length $kv->{$key}
defined $kv->{$key} not ref $kv->{$key} length $kv->{$key}
Truth table25%1/4
A and not(B) and C
A= defined $kv->{$key}B= not ref $kv->{$key}C= length $kv->{$key}
ABCresult
0XX0
10X0
1100
1111
53 or die "Missing $key for OAuth2 login\n";
54 }
55 7 https $kv->{"oauth-authorization-endpoint"};
56 7 https $kv->{"oauth-token-endpoint"};
57 7 ▶ $kv->{"oauth-redirect-uri"} =~ m{\A(?:https://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/[^\s?#]*)?|http://(?:127\.0\.0\.1|localhost)(?::[0-9]+)?(?:/[^\s?#]*)?)\z}i
Branch: 1 branch
BranchTrueFalseTotal
$kv->{"oauth-redirect-uri"} =~ m[\A(?:https://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/[^\s?#]*)?|http://(?:127\.0\.0\.1|localhost)(?::[0-9]+)?(?:/[^\s?#]*)?)\z]iu or die "OAuth redirect URI must be HTTPS or HTTP loopback, without a query or fragment\n" 7 0 7
58 or die "OAuth redirect URI must be HTTPS or HTTP loopback, without a query or fragment\n";
59 7 for my $key (qw(oauth-client-id oauth-scope oauth-client-secret)) {
60 21 ▶ next unless defined $kv->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kv->{$key} 0 21 21
61 21 ▶ $kv->{$key} !~ /[\r\n\x00]/ or die "Invalid $key\n";
Branch: 1 branch
BranchTrueFalseTotal
$kv->{$key} =~ /[\r\n\x00]/u and die "Invalid $key\n" 0 21 21
62 }
63 7 ▶ $kv->{"oauth-pkce"} //= 1;
Condition50%1/2
$kv->{"oauth-pkce"} //= 1
l!l
70
MC/DC0%0/1
$kv->{"oauth-pkce"} //= 1
$kv->{"oauth-pkce"}
Truth table50%1/2
A
A= $kv->{"oauth-pkce"}
Aresult
11
00
64 7 ▶ $kv->{"oauth-pkce"} =~ /\A[01]\z/ or die "oauth-pkce must be 0 or 1\n";
Branch: 1 branch
BranchTrueFalseTotal
$kv->{"oauth-pkce"} =~ /\A[01]\z/u or die "oauth-pkce must be 0 or 1\n" 7 0 7
65 7 ▶ $kv->{"oauth-client-auth"} //= defined $kv->{"oauth-client-secret"} ? "client_secret_post" : "none";
Branch: 1 branch
BranchTrueFalseTotal
defined $kv->{'oauth-client-secret'} ? : 0 0 0
Condition50%1/2
$kv->{"oauth-client-auth"} //= defined $kv->{"oauth-client-secret"} ? "client_secret_post" : "none"
l!l
70
MC/DC0%0/1
$kv->{"oauth-client-auth"} //= defined $kv->{"oauth-client-secret"} ? "client_secret_post" : "none"
$kv->{"oauth-client-auth"}
Truth table50%1/2
A
A= $kv->{"oauth-client-auth"}
Aresult
11
00
66 7 ▶ $kv->{"oauth-client-auth"} =~ /\A(?:none|client_secret_post|client_secret_basic)\z/
Branch: 1 branch
BranchTrueFalseTotal
$kv->{"oauth-client-auth"} =~ /\A(?:none|client_secret_post|client_secret_basic)\z/u or die "Invalid oauth-client-auth\n" 7 0 7
67 or die "Invalid oauth-client-auth\n";
68 7 ▶ $kv->{"oauth-client-auth"} eq "none" || defined $kv->{"oauth-client-secret"}
Branch: 1 branch
BranchTrueFalseTotal
$kv->{"oauth-client-auth"} eq "none" or defined $kv->{"oauth-client-secret"} or die "OAuth client-secret required for chosen client authentication\n" 7 0 7
Condition33%1/3
$kv->{"oauth-client-auth"} eq "none" or defined $kv->{"oauth-client-secret"}
l!l&&r!l&&!r
070
MC/DC0%0/2
$kv->{"oauth-client-auth"} eq "none" or defined $kv->{"oauth-client-secret"}
$kv->{"oauth-client-auth"} eq "none" defined $kv->{"oauth-client-secret"}
Truth table33%1/3
A or B
A= $kv->{"oauth-client-auth"} eq "none"B= defined $kv->{"oauth-client-secret"}
ABresult
1X1
011
000
69 or die "OAuth client-secret required for chosen client authentication\n";
70 7 ▶ $kv->{"oauth-sasl"} //= "xoauth2";
Condition100%2/2
$kv->{"oauth-sasl"} //= "xoauth2"
l!l
61
MC/DC100%1/1
$kv->{"oauth-sasl"} //= "xoauth2"
$kv->{"oauth-sasl"}
Truth table100%2/2
A
A= $kv->{"oauth-sasl"}
Aresult
11
00
71 7 ▶ $kv->{"oauth-sasl"} =~ /\A(?:xoauth2|oauthbearer)\z/i or die "Invalid oauth-sasl\n";
Branch: 1 branch
BranchTrueFalseTotal
$kv->{"oauth-sasl"} =~ /\A(?:xoauth2|oauthbearer)\z/iu or die "Invalid oauth-sasl\n" 7 0 7
72 7 for my $key (grep /^oauth-auth-/, keys %$kv) {
73 7 ▶ my ($param) = $key =~ /\Aoauth-auth-([A-Za-z0-9_]+)\z/
Branch: 1 branch
BranchTrueFalseTotal
my($param) = $key =~ /\Aoauth-auth-([A-Za-z0-9_]+)\z/u or die "Invalid OAuth authorization parameter $key\n" 7 0 7
74 or die "Invalid OAuth authorization parameter $key\n";
75 7 ▶ $param !~ /\A(?:client_id|redirect_uri|response_type|scope|state|code_challenge|code_challenge_method)\z/
Branch: 1 branch
BranchTrueFalseTotal
$param =~ /\A(?:client_id|redirect_uri|response_type|scope|state|code_challenge|code_challenge_method)\z/u and die "OAuth parameter $param is reserved\n" 0 7 7
76 or die "OAuth parameter $param is reserved\n";
77 7 ▶ defined $kv->{$key} && !ref $kv->{$key} && $kv->{$key} !~ /[\r\n\x00]/
Branch: 1 branch
BranchTrueFalseTotal
defined $kv->{$key} and not ref $kv->{$key} and not $kv->{$key} =~ /[\r\n\x00]/u or die "Invalid $key\n" 7 0 7
Condition33%2/6
defined $kv->{$key} and not ref $kv->{$key} and not $kv->{$key} =~ /[\r\n\x00]/u
!ll&&!rl&&r
007
defined $kv->{$key} and not ref $kv->{$key}
!ll&&!rl&&r
007
MC/DC0%0/3
defined $kv->{$key} and not ref $kv->{$key} and not $kv->{$key} =~ /[\r\n\x00]/u
defined $kv->{$key} not ref $kv->{$key} not $kv->{$key} =~ /[\r\n\x00]/u
Truth table25%1/4
A and not(B) and not(C)
A= defined $kv->{$key}B= not ref $kv->{$key}C= not $kv->{$key} =~ /[\r\n\x00]/u
ABCresult
0XX0
10X0
1100
1111
78 or die "Invalid $key\n";
79 }
80 $kv
81 7 }
82  
83 1 ▶sub authorize {
Subroutine authorize: called CC=5 SCAR=16.4
Pod undocumented
84 1 my ($kv) = @_;
85 1 validate $kv;
86 1 my $state = b64url (AnyEvent::MCP::random_bytes 32);
87 1 ▶ my $verifier = $kv->{"oauth-pkce"} ? b64url (AnyEvent::MCP::random_bytes 32) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$kv->{'oauth-pkce'} ? : 1 0 1
88 my %params = (
89 response_type => "code",
90 client_id => $kv->{"oauth-client-id"},
91 redirect_uri => $kv->{"oauth-redirect-uri"},
92 1 ▶ scope => $kv->{"oauth-scope"},
Branch: 1 branch
BranchTrueFalseTotal
defined $verifier ? : 1 0 1
93 state => $state,
94 (defined $verifier ? (
95 code_challenge => b64url (Digest::SHA::sha256 ($verifier)),
96 code_challenge_method => "S256",
97 ) : ()),
98 );
99 1 for my $key (grep /^oauth-auth-/, keys %$kv) {
100 1 (my $param = $key) =~ s/^oauth-auth-//;
101 1 $params{$param} = $kv->{$key};
102 }
103 1 my $url = $kv->{"oauth-authorization-endpoint"};
104 1 ▶ $url .= ($url =~ /\?/ ? "&" : "?") . form (%params);
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/u ? : 0 1 1
105 1 ($url, $state, $verifier)
106 }
107  
108 6 ▶sub callback_code {
Subroutine callback_code: called CC=12 SCAR=26.4
Pod undocumented
109 6 my ($kv, $state, $url) = @_;
110 6 my $base = $kv->{"oauth-redirect-uri"};
111 6 my $query;
112 6 ▶ if ($url =~ /^\Q$base\E\?([^#]*)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ /^\Q$base\E\?([^#]*)\z/u) { } 0 6 6
elsif ($base =~ m[://[^/]+\z]u and $url =~ m[^\Q$base\E/\?([^#]*)\z]u) { } 4 2 6
113 0 $query = $1;
114 ▶ } elsif ($base =~ m{://[^/]+\z} && $url =~ /^\Q$base\E\/\?([^#]*)\z/) {
Condition66%2/3
$base =~ m[://[^/]+\z]u and $url =~ m[^\Q$base\E/\?([^#]*)\z]u
!ll&&!rl&&r
024
MC/DC50%1/2
$base =~ m[://[^/]+\z]u and $url =~ m[^\Q$base\E/\?([^#]*)\z]u
$base =~ m[://[^/]+\z]u $url =~ m[^\Q$base\E/\?([^#]*)\z]u
Truth table66%2/3
A and B
A= $base =~ m[://[^/]+\z]uB= $url =~ m[^\Q$base\E/\?([^#]*)\z]u
ABresult
0X0
100
111
115 4 $query = $1; # browsers may add / to an empty HTTP path
116 } else {
117 2 die "OAuth redirect URI does not match configured URI\n";
118 }
119 4 my %params;
120 4 for my $pair (split /&/, $query) {
121 8 my ($key, $value) = split /=/, $pair, 2;
122 8 $key = decode ($key);
123 8 ▶ defined $value or die "Malformed OAuth redirect query\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or die "Malformed OAuth redirect query\n" 8 0 8
124 8 ▶ exists $params{$key} and die "Duplicate OAuth redirect parameter $key\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $params{$key} and die "Duplicate OAuth redirect parameter $key\n" 1 7 8
125 7 $params{$key} = decode ($value);
126 }
127 3 ▶ defined $params{state} && $params{state} eq $state
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"state"} and $params{"state"} eq $state or die "OAuth state mismatch\n" 2 1 3
Condition66%2/3
defined $params{"state"} and $params{"state"} eq $state
!ll&&!rl&&r
012
MC/DC50%1/2
defined $params{"state"} and $params{"state"} eq $state
defined $params{"state"} $params{"state"} eq $state
Truth table66%2/3
A and B
A= defined $params{"state"}B= $params{"state"} eq $state
ABresult
0X0
100
111
128 or die "OAuth state mismatch\n";
129 2 ▶ if (defined $params{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"error"}) 0 2 2
130 0 die "OAuth authorization failed: $params{error}\n";
131 }
132 defined $params{code} && length $params{code}
133 2 ▶ or die "OAuth redirect lacks authorization code\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"code"} and length $params{"code"} or die "OAuth redirect lacks authorization code\n" 2 0 2
Condition33%1/3
defined $params{"code"} and length $params{"code"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $params{"code"} and length $params{"code"}
defined $params{"code"} length $params{"code"}
Truth table33%1/3
A and B
A= defined $params{"code"}B= length $params{"code"}
ABresult
0X0
100
111
134 $params{code}
135 2 }
136  
137 6 ▶sub token {
Subroutine token: called CC=17 SCAR=30.3
Pod undocumented
138 6 my ($kv, %grant) = @_;
139 6 validate $kv;
140 my %data = (
141 %grant,
142 6 client_id => $kv->{"oauth-client-id"},
143 );
144 $data{client_secret} = $kv->{"oauth-client-secret"}
145 6 ▶ if $kv->{"oauth-client-auth"} eq "client_secret_post";
Branch: 1 branch
BranchTrueFalseTotal
if $kv->{"oauth-client-auth"} eq "client_secret_post" 5 1 6
146 6 my %headers = (
147 "Content-Type" => "application/x-www-form-urlencoded",
148 "Accept" => "application/json",
149 );
150 6 ▶ if ($kv->{"oauth-client-auth"} eq "client_secret_basic") {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv->{"oauth-client-auth"} eq "client_secret_basic") 1 5 6
151 $headers{Authorization} = "Basic " . MIME::Base64::encode_base64 (
152 1 encode ($kv->{"oauth-client-id"}) . ":" . encode ($kv->{"oauth-client-secret"}), ""
153 );
154 }
155  
156 6 my $body = "";
157 6 my $oversize;
158 6 my $cv = AE::cv;
159 my $guard = AnyEvent::HTTP::http_request
160 POST => $kv->{"oauth-token-endpoint"},
161 body => form (%data),
162 headers => \%headers,
163 recurse => 0, persistent => 0, proxy => undef, tls_ctx => "high", timeout => 30,
164 6 ▶ on_body => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
165 6 ▶ $oversize = 1, return 0 if length ($body) + length ($_[0]) > 65536;
Branch: 1 branch
BranchTrueFalseTotal
if length($body) + length($_[0]) > 65536 1 5 6
166 5 $body .= $_[0];
167 5 1
168 },
169 6 ▶ sub { $cv->send ($_[1]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
170 6 my $hdr = $cv->recv;
171 6 undef $guard;
172 6 ▶ $oversize and die "OAuth token response too large\n";
Branch: 1 branch
BranchTrueFalseTotal
$oversize and die "OAuth token response too large\n" 1 5 6
173 5 ▶ my $status = $hdr->{Status} || 0;
Condition50%1/2
$hdr->{'Status'} || 0
l!l
50
MC/DC0%0/1
$hdr->{'Status'} || 0
$hdr->{'Status'}
Truth table50%1/2
A
A= $hdr->{'Status'}
Aresult
11
00
174 5 ▶ $status >= 200 && $status < 300 or die "OAuth token endpoint returned HTTP $status\n";
Branch: 1 branch
BranchTrueFalseTotal
$status >= 200 and $status < 300 or die "OAuth token endpoint returned HTTP $status\n" 4 1 5
Condition66%2/3
$status >= 200 and $status < 300
!ll&&!rl&&r
014
MC/DC50%1/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
Truth table66%2/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
175 4 my $result = eval { JSON::XS::decode_json ($body) };
176 ref $result eq "HASH" && defined $result->{access_token} && !ref $result->{access_token}
177 && length $result->{access_token}
178 4 ▶ or die "OAuth token endpoint returned no valid access token\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"} and length $result->{"access_token"} or die "OAuth token endpoint returned no valid access token\n" 4 0 4
Condition33%3/9
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"} and length $result->{"access_token"}
!ll&&!rl&&r
004
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"}
!ll&&!rl&&r
004
ref $result eq "HASH" and defined $result->{"access_token"}
!ll&&!rl&&r
004
MC/DC0%0/4
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"} and length $result->{"access_token"}
ref $result eq "HASH" defined $result->{"access_token"} not ref $result->{"access_token"} length $result->{"access_token"}
Truth table20%1/5
A and B and not(C) and D
A= ref $result eq "HASH"B= defined $result->{"access_token"}C= not ref $result->{"access_token"}D= length $result->{"access_token"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
179 4 ▶ !defined $result->{token_type} || lc $result->{token_type} eq "bearer"
Branch: 1 branch
BranchTrueFalseTotal
not defined $result->{"token_type"} or lc $result->{"token_type"} eq "bearer" or die "OAuth token endpoint returned a non-Bearer token\n" 4 0 4
Condition66%2/3
not defined $result->{"token_type"} or lc $result->{"token_type"} eq "bearer"
l!l&&r!l&&!r
130
MC/DC0%0/2
not defined $result->{"token_type"} or lc $result->{"token_type"} eq "bearer"
not defined $result->{"token_type"} lc $result->{"token_type"} eq "bearer"
Truth table66%2/3
not(A) or B
A= not defined $result->{"token_type"}B= lc $result->{"token_type"} eq "bearer"
ABresult
1X1
011
000
180 or die "OAuth token endpoint returned a non-Bearer token\n";
181 4 ▶ if (exists $result->{refresh_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"refresh_token"}) 3 1 4
182 defined $result->{refresh_token} && !ref $result->{refresh_token}
183 && length $result->{refresh_token}
184 3 ▶ or die "OAuth token endpoint returned invalid refresh token\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"} and length $result->{"refresh_token"} or die "OAuth token endpoint returned invalid refresh token\n" 3 0 3
Condition33%2/6
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"} and length $result->{"refresh_token"}
!ll&&!rl&&r
003
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"}
!ll&&!rl&&r
003
MC/DC0%0/3
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"} and length $result->{"refresh_token"}
defined $result->{"refresh_token"} not ref $result->{"refresh_token"} length $result->{"refresh_token"}
Truth table25%1/4
A and not(B) and C
A= defined $result->{"refresh_token"}B= not ref $result->{"refresh_token"}C= length $result->{"refresh_token"}
ABCresult
0XX0
10X0
1100
1111
185 }
186 $result
187 4 }
188  
189 1 ▶sub finish {
Subroutine finish: called CC=2 SCAR=7.1
Pod undocumented
190 1 my ($kv, $state, $verifier, $url) = @_;
191 1 my $code = callback_code ($kv, $state, $url);
192 token ($kv,
193 grant_type => "authorization_code",
194 code => $code,
195 1 ▶ redirect_uri => $kv->{"oauth-redirect-uri"},
Branch: 1 branch
BranchTrueFalseTotal
defined $verifier ? : 1 0 1
196 (defined $verifier ? (code_verifier => $verifier) : ()),
197 )
198 }
199  
200 3 ▶sub refresh {
Subroutine refresh: called CC=11 SCAR=25.6
Pod undocumented
201 3 my ($file, $kv) = @_;
202 3 my $lock = AnyEvent::MCP::File::lock $file;
203 3 ▶ open my $fh, "<:raw", $file or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "${file}: $!\n" 3 0 3
204 3 local $/;
205 3 my $data = <$fh>;
206 3 close $fh;
207 3 my %current;
208 6 ▶ AnyEvent::MCP::parse_ini $data, sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
209 6 my ($section, $values) = @_;
210 6 ▶ $section eq "" || $section eq "account" or die "$file: invalid account section\n";
Branch: 1 branch
BranchTrueFalseTotal
$section eq "" or $section eq "account" or die "${file}: invalid account section\n" 6 0 6
Condition66%2/3
$section eq "" or $section eq "account"
l!l&&r!l&&!r
330
MC/DC0%0/2
$section eq "" or $section eq "account"
$section eq "" $section eq "account"
Truth table66%2/3
A or B
A= $section eq ""B= $section eq "account"
ABresult
1X1
011
000
211 6 %current = (%current, %$values);
212 3 }, $file;
213 3 for my $key (qw(method server user tls port oauth-client-id oauth-client-secret
214 oauth-client-auth oauth-token-endpoint oauth-scope oauth-sasl)) {
215 28 ▶ ($current{$key} // "") eq ($kv->{$key} // "")
Branch: 1 branch
BranchTrueFalseTotal
($current{$key} // "") eq ($kv->{$key} // "") or die "IMAP OAuth account changed; restart mcp-imap\n" 27 1 28
Condition50%2/4
$current{$key} // ""
l!l
280
$kv->{$key} // ""
l!l
280
MC/DC0%0/1
$current{$key} // ""
$current{$key}
MC/DC0%0/1
$kv->{$key} // ""
$kv->{$key}
Truth table50%1/2
A
A= $current{$key}
Aresult
11
00
Truth table50%1/2
A
A= $kv->{$key}
Aresult
11
00
216 or die "IMAP OAuth account changed; restart mcp-imap\n";
217 }
218 2 my $encoded = $current{"oauth-refresh-token-base64"};
219 2 ▶ defined $encoded && $encoded =~ /\A[A-Za-z0-9+\/]*={0,2}\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $encoded and $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u or die "Missing OAuth refresh token; run mcp-imap login again\n" 2 0 2
Condition33%1/3
defined $encoded and $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
!ll&&!rl&&r
002
MC/DC0%0/2
defined $encoded and $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
defined $encoded $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
Truth table33%1/3
A and B
A= defined $encodedB= $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
ABresult
0X0
100
111
220 or die "Missing OAuth refresh token; run mcp-imap login again\n";
221 2 my $old = MIME::Base64::decode_base64 ($encoded);
222 2 ▶ length $old or die "Empty OAuth refresh token\n";
Branch: 1 branch
BranchTrueFalseTotal
length $old or die "Empty OAuth refresh token\n" 2 0 2
223 2 my $new = token ($kv, grant_type => "refresh_token", refresh_token => $old);
224 2 ▶ if (defined $new->{refresh_token} && $new->{refresh_token} ne $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new->{"refresh_token"} and $new->{"refresh_token"} ne $old) 2 0 2
Condition33%1/3
defined $new->{"refresh_token"} and $new->{"refresh_token"} ne $old
!ll&&!rl&&r
002
MC/DC0%0/2
defined $new->{"refresh_token"} and $new->{"refresh_token"} ne $old
defined $new->{"refresh_token"} $new->{"refresh_token"} ne $old
Truth table33%1/3
A and B
A= defined $new->{"refresh_token"}B= $new->{"refresh_token"} ne $old
ABresult
0X0
100
111
225 2 $current{"oauth-refresh-token-base64"} = MIME::Base64::encode_base64 ($new->{refresh_token}, "");
226 2 AnyEvent::MCP::File::replace $file, "[account]\n"
227 . (join "", map "$_ = $current{$_}\n", sort keys %current);
228 }
229 2 close $lock;
230 $new->{access_token}
231 2 }
232  
233 1 1;
« bin/anyevent-mcp ↑ Summary bin/mcp-fs »