| 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::Config (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # OAuth form parameters are Unicode text; escape the UTF-8 bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 80 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 80 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 80 | my $bytes = AnyEvent::MCP::utf8enc $str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 80 | $bytes =~ s/([^A-Za-z0-9._~-])/sprintf "%%%02X", ord $1/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 80 | $bytes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | sub form { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
form: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | my (%pairs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | join "&", map +(encode $_) . "=" . (encode $pairs{$_}), sort keys %pairs | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | ▶ | sub decode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 23 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 23 | ▶ | $str =~ /%(?![a-fA-F0-9]{2})/ and die "Invalid OAuth redirect URL encoding\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 23 | $str =~ tr/+/ /; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 23 | $str =~ s/%([a-fA-F0-9]{2})/chr hex $1/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 23 | AnyEvent::MCP::utf8dec $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 | 16 | ▶ | sub https { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
https: called CC=4 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 16 | my ($url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 16 | ▶ | defined $url && $url =~ m{\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)}i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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
defined $url and $url =~ m[\Ahttps://[A-Za-z0-9.-]+(?::[0-9]+)?(?:/|\?|\z)]iu
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | && $url !~ /[\s\x00-\x1f\x7f#@]/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | or die "OAuth endpoint must be an HTTPS URL with a hostname\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 16 | $url | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=24 SCAR=40.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | my ($kv) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 8 | for my $key (qw(oauth-client-id oauth-authorization-endpoint oauth-token-endpoint oauth-scope oauth-redirect-uri)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 40 | ▶ | defined $kv->{$key} && !ref $kv->{$key} && length $kv->{$key} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $kv->{$key} and not ref $kv->{$key} and length $kv->{$key}
defined $kv->{$key} and not ref $kv->{$key}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | or die "Missing $key for OAuth2 login\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | https $kv->{"oauth-authorization-endpoint"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | https $kv->{"oauth-token-endpoint"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | or die "OAuth redirect URI must be HTTPS or HTTP loopback, without a query or fragment\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 8 | for my $key (qw(oauth-client-id oauth-scope oauth-client-secret)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 24 | ▶ | next unless defined $kv->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | ▶ | $kv->{$key} !~ /[\r\n\x00]/ or die "Invalid $key\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | ▶ | $kv->{"oauth-pkce"} //= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$kv->{"oauth-pkce"} //= 1
MC/DC0%0/1
$kv->{"oauth-pkce"} //= 1
$kv->{"oauth-pkce"}
Truth table50%1/2
A
A= $kv->{"oauth-pkce"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | ▶ | $kv->{"oauth-pkce"} =~ /\A[01]\z/ or die "oauth-pkce must be 0 or 1\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | $kv->{"oauth-client-auth"} //= defined $kv->{"oauth-client-secret"} ? "client_secret_post" : "none"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$kv->{"oauth-client-auth"} //= defined $kv->{"oauth-client-secret"} ? "client_secret_post" : "none"
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | $kv->{"oauth-client-auth"} =~ /\A(?:none|client_secret_post|client_secret_basic)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | or die "Invalid oauth-client-auth\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | $kv->{"oauth-client-auth"} eq "none" || defined $kv->{"oauth-client-secret"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kv->{"oauth-client-auth"} eq "none" or defined $kv->{"oauth-client-secret"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | or die "OAuth client-secret required for chosen client authentication\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | $kv->{"oauth-sasl"} //= "xoauth2"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kv->{"oauth-sasl"} //= "xoauth2"
MC/DC100%1/1
$kv->{"oauth-sasl"} //= "xoauth2"
$kv->{"oauth-sasl"}
Truth table100%2/2
A
A= $kv->{"oauth-sasl"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | $kv->{"oauth-sasl"} =~ /\A(?:xoauth2|oauthbearer)\z/i or die "Invalid oauth-sasl\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | for my $key (grep /^oauth-auth-/, keys %$kv) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | my ($param) = $key =~ /\Aoauth-auth-([A-Za-z0-9_]+)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | or die "Invalid OAuth authorization parameter $key\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | $param !~ /\A(?:client_id|redirect_uri|response_type|scope|state|code_challenge|code_challenge_method)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | or die "OAuth parameter $param is reserved\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | defined $kv->{$key} && !ref $kv->{$key} && $kv->{$key} !~ /[\r\n\x00]/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $kv->{$key} and not ref $kv->{$key} and not $kv->{$key} =~ /[\r\n\x00]/u
defined $kv->{$key} and not ref $kv->{$key}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | or die "Invalid $key\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | $kv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ($url, $state, $verifier) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 8 | ▶ | sub callback_code { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
callback_code: called CC=12 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | my ($kv, $state, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | my $base = $kv->{"oauth-redirect-uri"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | my $query; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | if ($url =~ /^\Q$base\E\?([^#]*)\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | $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 | 6 | my %params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 6 | for my $pair (split /&/, $query) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 11 | my ($key, $value) = split /=/, $pair, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | $key = decode ($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 11 | ▶ | defined $value or die "Malformed OAuth redirect query\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 11 | ▶ | exists $params{$key} and die "Duplicate OAuth redirect parameter $key\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 10 | $params{$key} = decode ($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | ▶ | defined $params{state} && $params{state} eq $state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params{"state"} and $params{"state"} eq $state
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | or die "OAuth state mismatch\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | ▶ | if (defined $params{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | die "OAuth authorization failed: $params{error}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | defined $params{code} && length $params{code} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | ▶ | or die "OAuth redirect lacks authorization code\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $params{"code"} and length $params{"code"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | $params{code} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | sub token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token: called CC=17 SCAR=30.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 7 | my ($kv, %grant) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | validate $kv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | my %data = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | %grant, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 7 | client_id => $kv->{"oauth-client-id"}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | $data{client_secret} = $kv->{"oauth-client-secret"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | ▶ | if $kv->{"oauth-client-auth"} eq "client_secret_post"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | my %headers = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | "Content-Type" => "application/x-www-form-urlencoded", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | "Accept" => "application/json", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | ▶ | if ($kv->{"oauth-client-auth"} eq "client_secret_basic") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | $headers{Authorization} = "Basic " . MIME::Base64::encode_base64 ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | encode ($kv->{"oauth-client-id"}) . ":" . encode ($kv->{"oauth-client-secret"}), "" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 7 | my $body = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 7 | my $oversize; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | 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 | 7 | ▶ | on_body => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 7 | ▶ | $oversize = 1, return 0 if length ($body) + length ($_[0]) > 65536; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | $body .= $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 7 | ▶ | sub { $cv->send ($_[1]) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | my $hdr = $cv->recv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | undef $guard; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 7 | ▶ | $oversize and die "OAuth token response too large\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | ▶ | my $status = $hdr->{Status} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$hdr->{'Status'} || 0
MC/DC0%0/1
$hdr->{'Status'} || 0
$hdr->{'Status'}
Truth table50%1/2
A
A= $hdr->{'Status'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | ▶ | $status >= 200 && $status < 300 or die "OAuth token endpoint returned HTTP $status\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 200 and $status < 300
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | 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 | 5 | ▶ | or die "OAuth token endpoint returned no valid access token\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"} and length $result->{"access_token"}
ref $result eq "HASH" and defined $result->{"access_token"} and not ref $result->{"access_token"}
ref $result eq "HASH" and defined $result->{"access_token"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | !defined $result->{token_type} || lc $result->{token_type} eq "bearer" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $result->{"token_type"} or lc $result->{"token_type"} eq "bearer"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | or die "OAuth token endpoint returned a non-Bearer token\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 5 | ▶ | if (exists $result->{refresh_token}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | defined $result->{refresh_token} && !ref $result->{refresh_token} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | && length $result->{refresh_token} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | ▶ | or die "OAuth token endpoint returned invalid refresh token\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"} and length $result->{"refresh_token"}
defined $result->{"refresh_token"} and not ref $result->{"refresh_token"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | $result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | (defined $verifier ? (code_verifier => $verifier) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | ▶ | sub refresh { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
refresh: called CC=13 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 4 | my ($file, $kv) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | my $lock = AnyEvent::MCP::file_lock $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 4 | my $sections = AnyEvent::MCP::Config::parse_ini (AnyEvent::MCP::file_load ($file), $file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | for (@$sections) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | ▶ | $_->[0] eq "" || $_->[0] eq "account" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_->[0] eq "" or $_->[0] eq "account"
MC/DC0%0/2
$_->[0] eq "" or $_->[0] eq "account"
$_->[0] eq ""
$_->[0] eq "account"
Truth table66%2/3
A or B
A= $_->[0] eq ""B= $_->[0] eq "account"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | or die "$file: invalid account section\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 4 | my %current = %{AnyEvent::MCP::Config::merge_sections ($sections, "account")->{account}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 4 | for my $key (qw(method server user tls port oauth-client-id oauth-client-secret | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | oauth-client-auth oauth-token-endpoint oauth-scope oauth-sasl)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 39 | ▶ | ($current{$key} // "") eq ($kv->{$key} // "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$current{$key} // ""
$kv->{$key} // ""
MC/DC0%0/1
$current{$key} // ""
$current{$key}
MC/DC0%0/1
$kv->{$key} // ""
$kv->{$key}
Truth table50%1/2
A
A= $current{$key}
Truth table50%1/2
A
A= $kv->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | or die "IMAP OAuth account changed; restart mcp-imap\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 3 | my $encoded = $current{"oauth-refresh-token-base64"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 3 | ▶ | defined $encoded && $encoded =~ /\A[A-Za-z0-9+\/]*={0,2}\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $encoded and $encoded =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | or die "Missing OAuth refresh token; run mcp-imap login again\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 3 | my $old = AnyEvent::MCP::utf8dec (MIME::Base64::decode_base64 $encoded); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 3 | ▶ | length $old or die "Empty OAuth refresh token\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | my $new = token ($kv, grant_type => "refresh_token", refresh_token => $old); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | ▶ | if (defined $new->{refresh_token} && $new->{refresh_token} ne $old) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $new->{"refresh_token"} and $new->{"refresh_token"} ne $old
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | $current{"oauth-refresh-token-base64"} = MIME::Base64::encode_base64 (AnyEvent::MCP::utf8enc $new->{refresh_token}, ""); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | AnyEvent::MCP::file_save $file, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | AnyEvent::MCP::Config::serialize_ini [["account", \%current]]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 3 | close $lock; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | $new->{access_token} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||