blib/lib/AnyEvent/MCP/Config.pm

filter: Statementstmt 53.0% 209 / 394 Branchbran 42.8% 84 / 196 Conditioncond 30.1% 64 / 212 MC/DCmcdc 13.2% 14 / 106 Subroutinesub 56.8% 29 / 51 Podpod 100% 40 / 40 totaltotal 44.0% 440 / 999 CC 187 SCAR 33.5
CC
187
Cov
45%
http_settings
103.1
http_settings
103.1
serialize_ini
28.4
CRAP 28.4
« bin/mcp-http ↑ Summary blib/lib/AnyEvent/MCP/Extension.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Config - named servers and derived MCP credentials
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::MCP::Config;
8  
9 my $config = AnyEvent::MCP::Config::global_config;
10 my $token = $config->user_credentials ("agni")->[0][2];
11 my $url = $config->http_url ("agni", undef, path => "/");
12  
13 =head1 DESCRIPTION
14  
15 An MCP server and its clients often need matching credentials. Rather than
16 maintaining a separate password for each server and user, this module can
17 derive them from one private master secret. Users are defined by
18 C<user.NAME = GENERATION> in C<[global]>, C<[program PROGRAM]> or
19 C<[instance NAME]>, in that order of precedence. An absent global
20 C<user.default> is implicitly enabled at generation 1.
21  
22 The shared file format is described in L<AnyEvent::MCP/GLOBAL CONFIGURATION>.
23 Loading this module does not read any files. C<global_config> loads the
24 configuration, ensures a persistent master secret exists and caches the
25 initialized object for the process.
26  
27 A missing secret is generated using C<AnyEvent::MCP::random_bytes> and
28 appended under a file lock. Failure to save the secret is fatal, rather
29 than using a temporary secret that other processes cannot reproduce.
30 A supplied invalid secret is an error. Changing or losing the master
31 secret invalidates all credentials derived from it.
32  
33 Derivation uses C<derive_secret> with the random master key.
34 The application namespace, purpose, program, instance name and username are
35 separately length-prefixed, so distinct contexts derive different credentials.
36 Deriving a password does not create an account or configure the server to
37 accept it: authentication still belongs to the server.
38  
39 =head1 METHODS
40  
41 =over
42  
43 =item $config = AnyEvent::MCP::Config::global_config (file => $file)
44  
45 Return the process-global configuration object, loaded and initialized.
46 The first call accepts the same arguments as C<new>, and ensures that the
47 master secret is persistently stored. Later calls return the same object
48 without rereading or reinitializing it.
49  
50 =item $config = AnyEvent::MCP::Config->new (file => $file)
51  
52 Construct an independent configuration object without reading or writing
53 the file. C<$file> defaults to the global configuration path.
54  
55 =item $config->load
56  
57 Reload the file, replacing the object's sections. A missing file yields an
58 empty configuration; other read and parsing errors are fatal. Sections are
59 available as C<< $config->{section}{$section} >>, each a hash reference of
60 string values. Named servers use C<[instance NAME]>. An optional raw file
61 handle can be supplied when the caller already holds the file lock.
62  
63 =item $config->save
64  
65 Write the loaded sections back as canonical UTF-8 INI text, sorting
66 sections and keys. Comments and original formatting are not retained.
67 The caller must hold C<AnyEvent::MCP::file_lock> through the update.
68 Returns the configuration object.
69  
70 =item $config->ensure_secret
71  
72 Ensure that C<[global]> contains a valid persistent master secret. If
73 missing, lock and reread the file, generate a secret only if still absent,
74 and append it without changing other settings. Fail if it cannot be saved
75 or is invalid. Returns the configuration object.
76  
77 =item $bytes = $config->derive_secret ($length, @info)
78  
79 =item $bytes = AnyEvent::MCP::Config->derive_secret ($length, @info)
80  
81 Derive C<$length> secret bytes from the configuration's master secret and
82 an ordered list of context byte strings C<@info>. The class form uses
83 C<global_config>, loading the default configuration on first use and reusing it
84 thereafter. The object form uses that object's configuration, including
85 one loaded from an explicit file.
86  
87 The same master secret and context always produce the same bytes. Use a
88 distinctive application/purpose string as the first context field, followed
89 by names or other values identifying the result:
90  
91 my $secret = AnyEvent::MCP::Config->derive_secret (
92 32, "my application token v1", AnyEvent::MCP::utf8enc ($name)
93 );
94  
95 The result contains raw bytes; encode it yourself if a printable token is
96 needed. This method does not add the result to a server's accepted tokens.
97  
98 The fixed C<anyevent-mcp> context field is prepended automatically.
99 Context fields are separately length-prefixed, so C<("ab", "c")> and
100 C<("a", "bc")> have different contexts. Encode character strings explicitly,
101 for example with C<AnyEvent::MCP::utf8enc>. Context arguments are left
102 unchanged.
103  
104 C<$length> must be an integer in 0..16320; zero returns an empty string.
105 Shorter results are prefixes of longer results for the same master secret
106 and context. Use distinct context fields for different purposes, rather
107 than relying on different lengths to separate them.
108  
109 Derivation uses HKDF-SHA512 expansion, with each context field prefixed by
110 its four-byte big-endian byte length. Changing or losing the master secret
111 changes every result derived from it.
112  
113 =item $credential = $config->credential ($purpose, $name, $username)
114  
115 Derives a 32-byte credential, returned as unpadded URL-safe Base64.
116 C<$purpose> is C<token>. C<$name> is the instance name, and C<$username>
117 is optional. This is the older unmanaged derivation API; its result is not
118 automatically accepted by a server. Managed credentials are enumerated by
119 C<user_credentials>.
120  
121 Credentials use the context C<("anyevent-mcp", "token", PROGRAM, NAME, USER)>.
122 PROGRAM is the invoked basename, with a trailing C<.pl> or C<.perl> removed.
123 Changing the program or instance name changes the credential.
124 A credential derived with C<$username> is B<not> automatically accepted
125 by the server. This method neither requires a server section nor configures
126 authentication.
127  
128 =item $users = $config->users ($instance, $program)
129  
130 Return effective user definitions keyed by username, each with C<scope>
131 (global, program or instance) and signed C<generation>. Inheritance is
132 global, program, instance; a negative generation disables without fallback.
133  
134 =item $credentials = $config->user_credentials ($instance, $program)
135  
136 Return enabled users as C<[[USERNAME, PRINCIPAL, TOKEN]...]> with default
137 first. The principal is C<global:NAME>, C<program:NAME>, or C<instance:NAME>.
138 HKDF context includes program, instance, scope, user and generation. Rotating
139 a generation changes the token but preserves saved user permissions.
140  
141 =item $user = $config->connect_user ($instance, $program)
142  
143 Return the username allowed to register private Vim, browser WebSocket, or
144 multibroker reverse-MCP connections. C<connect_user> inherits through the
145 usual scopes and defaults to C<default>. Revoking that user disables these
146 connections until its generation is renewed or another user is selected.
147  
148 =item $program = AnyEvent::MCP::Config::program_name ()
149  
150 Returns the invoked program basename, stripping C<.pl> or C<.perl>.
151 An inline Perl invocation uses C<perl>. This identity also selects the
152 C<[program PROGRAM]> configuration section; it is independent of routing aliases.
153  
154 =item $user = AnyEvent::MCP::Config::user_name ($user)
155  
156 Validate a managed username (ASCII letter or underscore, followed by
157 letters, digits, underscores or hyphens). Instance names are validated by
158 L<AnyEvent::MCP/SMALL UTILITIES>.
159  
160 =item @options = AnyEvent::MCP::Config::http_getopt (\$name, \$bind, \$print_url, \@tokens)
161  
162 Return standard C<Getopt::Long> options referring to the caller's scalars.
163  
164 =item $port = $config->server_port ($name)
165  
166 Derive a stable default port in 16384..32767 from the master secret and
167 program basename and service-instance name. Collisions produce ordinary listener bind errors.
168  
169 =item %settings = $config->http_settings ($name, $bind, $default_port)
170  
171 Resolve HTTP settings from C<[instance NAME]> and an optional command-line
172 C<bind> override. The default is IPv4 loopback and the name-derived port;
173 an optional C<default_port> replaces the derived port when no bind port
174 is specified.
175 The bind setting accepts C<ADDRESS:PORT>, C<[IPv6]:PORT>, C<ADDRESS>,
176 and C<:PORT> (which keeps the default loopback address).
177 Hostnames are resolved once when settings are obtained; their original
178 spelling is advertised unless C<url_host> overrides it. The returned options
179 include C<name>, C<host>, C<port> and C<tokens>, plus configured C<path>,
180 C<url_host> and C<url_scheme>. This does not start a listener.
181  
182 =item @rules = $config->auto_rules ($name)
183  
184 Return ordered automatic permission rules from C<[program PROGRAM]> and
185 C<[instance NAME]>. C<auto-rule.SUFFIX = allow|deny TOOL[,ACTION[,CAPABILITY]]>
186 entries are sorted by key. The suffix is an arbitrary ordering label; instance
187 entries override matching program entries, and an empty value disables an
188 inherited entry. Command-line rules take precedence over these defaults.
189  
190 =item $url = $config->http_url ($name, $bind, %options)
191  
192 Format a URL containing the first accepted token, without listening.
193 Port zero cannot be used because the kernel has not yet selected a port.
194 When the token is derived, the master secret must be persistently stored,
195 so the next process derives the same URL. Explicit static tokens and
196 C<auth =E<gt> "none"> do not require that persistence check.
197  
198 =back
199  
200 =head1 INI FUNCTIONS
201  
202 These functions work with Unicode character strings, not octets. Use
203 L<AnyEvent::MCP/file_load> and L<AnyEvent::MCP/file_save> for UTF-8
204 file I/O. Binary values belong in Base64 or hexadecimal form.
205  
206 =over
207  
208 =item $sections = AnyEvent::MCP::Config::parse_ini ($text, $filename)
209  
210 Parse Unicode INI text into C<[[NAME, {KEY =E<gt> VALUE}], ...]>.
211 Sections retain file order, including repeated section names and the
212 initial unnamed section C<"">. Duplicate keys within one section use
213 the final value. C<$filename> is optional, for line-numbered errors.
214  
215 =item $merged = AnyEvent::MCP::Config::merge_sections ($sections, $default)
216  
217 Return a hash mapping section names to key/value hashes, with later
218 sections overriding earlier ones. When C<$default> is given, unnamed
219 sections use that name (C<global> for global configuration and
220 C<account> for IMAP accounts). Use the original ordered list instead
221 for files where repeated sections are meaningful, such as permission rules.
222  
223 =item $text = AnyEvent::MCP::Config::serialize_ini ($sections)
224  
225 Serialize an ordered section list to canonical INI text. Section order
226 is preserved; keys within each section are sorted. Values must be
227 single-line character strings. Comments and whitespace formatting
228 are deliberately not preserved on rewrite.
229  
230 =back
231  
232 =cut
233  
234 package AnyEvent::MCP::Config;
235  
236 103 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 103 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
239 103 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
240 103 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
241 103 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 103 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
244 103 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 103 our @ISA = qw(AnyEvent::MCP::Base);
247  
248 386 ▶sub parse_ini {
Subroutine parse_ini: called CC=6 SCAR=23.5
Pod documented
249 0 ▶ my ($text, $file) = @_;
Subroutine parse_ini: not called CC=6 SCAR=23.5
Pod documented
250 0 ▶ $file //= "INI";
Condition50%2/4
$file //= "INI"
l!l
3824
$file //= "INI"
l!l
00
MC/DC100%1/1
$file //= "INI"
$file
MC/DC0%0/1
$file //= "INI"
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
Truth table0%0/2
A
A= $file
Aresult
11
00
251 0 my @sections = (["", {}]);
252 0 my $line = 0;
253  
254 0 for (split /\n/, $text) {
255 0 ++$line;
256 0 s/\r\z//;
257 0 ▶ next if /\A\s*(?:[#;]|\z)/;
Branch: 2 branches
BranchTrueFalseTotal
if /\A\s*(?:[#;]|\z)/u 335 2448 2783
if /\A\s*(?:[#;]|\z)/u 0 0 0
258 0 ▶ if (/\A\s*\[([^\]\r\n]+)\]\s*\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/\A\s*\[([^\]\r\n]+)\]\s*\z/u) { } 646 1802 2448
elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/u) { } 1801 1 1802
if (/\A\s*\[([^\]\r\n]+)\]\s*\z/u) { } 0 0 0
elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/u) { } 0 0 0
259 0 push @sections, [$1, {}];
260 } elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/) {
261 0 $sections[-1][1]{$1} = $2;
262 } else {
263 0 die "$file:$line: invalid INI line\n";
264 }
265 }
266  
267 \@sections
268 0 }
269  
270 122 ▶sub merge_sections {
Subroutine merge_sections: called CC=5 SCAR=21.7
Pod documented
271 0 ▶ my ($sections, $default) = @_;
Subroutine merge_sections: not called CC=5 SCAR=21.7
Pod documented
272 0 my %merged;
273  
274 0 for (@$sections) {
275 0 my ($name, $kv) = @$_;
276 0 ▶ $name = $default if defined $default && $name eq "";
Branch: 2 branches
BranchTrueFalseTotal
if defined $default and $name eq "" 122 229 351
if defined $default and $name eq "" 0 0 0
Condition33%2/6
defined $default and $name eq ""
!ll&&!rl&&r
0229122
defined $default and $name eq ""
!ll&&!rl&&r
000
MC/DC50%1/2
defined $default and $name eq ""
defined $default $name eq ""
MC/DC0%0/2
defined $default and $name eq ""
defined $default $name eq ""
Truth table66%2/3
A and B
A= defined $defaultB= $name eq ""
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $defaultB= $name eq ""
ABresult
0X0
100
111
277 0 ▶ my $values = $merged{$name} ||= {};
Condition50%2/4
$merged{$name} ||= {}
l!l
120231
$merged{$name} ||= {}
l!l
00
MC/DC100%1/1
$merged{$name} ||= {}
$merged{$name}
MC/DC0%0/1
$merged{$name} ||= {}
$merged{$name}
Truth table100%2/2
A
A= $merged{$name}
Aresult
11
00
Truth table0%0/2
A
A= $merged{$name}
Aresult
11
00
278 0 %$values = (%$values, %$kv);
279 }
280  
281 0 \%merged
282 }
283  
284 73 ▶sub serialize_ini {
Subroutine serialize_ini: called CC=9 SCAR=33.5
Pod documented
285 0 ▶ my ($sections) = @_;
Subroutine serialize_ini: not called CC=9 SCAR=33.5
Pod documented
286  
287 join "", map {
288 0 my ($name, $values) = @$_;
289 0 ▶ $name !~ /[\]\r\n]/ or die "Invalid INI section name\n";
Branch: 2 branches
BranchTrueFalseTotal
$name =~ /[\]\r\n]/u and die "Invalid INI section name\n" 0 92 92
$name =~ /[\]\r\n]/u and die "Invalid INI section name\n" 0 0 0
290 (length $name ? "[$name]\n" : "")
291 . (join "", map {
292 0 ▶ length && !/[=\s]/ && !/\A[#;]/
Branch: 4 branches
BranchTrueFalseTotal
length $name ? : 91 1 92
length $_ and not /[=\s]/u and not /\A[#;]/u or die "Invalid INI key\n" 340 1 341
length $name ? : 0 0 0
length $_ and not /[=\s]/u and not /\A[#;]/u or die "Invalid INI key\n" 0 0 0
Condition25%3/12
length $_ and not /[=\s]/u and not /\A[#;]/u
!ll&&!rl&&r
01340
length $_ and not /[=\s]/u
!ll&&!rl&&r
00341
length $_ and not /[=\s]/u and not /\A[#;]/u
!ll&&!rl&&r
000
length $_ and not /[=\s]/u
!ll&&!rl&&r
000
MC/DC33%1/3
length $_ and not /[=\s]/u and not /\A[#;]/u
length $_ not /[=\s]/u not /\A[#;]/u
MC/DC0%0/3
length $_ and not /[=\s]/u and not /\A[#;]/u
length $_ not /[=\s]/u not /\A[#;]/u
Truth table50%2/4
A and not(B) and not(C)
A= length $_B= not /[=\s]/uC= not /\A[#;]/u
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= length $_B= not /[=\s]/uC= not /\A[#;]/u
ABCresult
0XX0
10X0
1100
1111
293 or die "Invalid INI key\n";
294 0 my $value = $values->{$_};
295 0 ▶ defined $value && !ref $value && $value !~ /[\r\n]/
Branch: 2 branches
BranchTrueFalseTotal
defined $value and not ref $value and not $value =~ /[\r\n]/u or die "Invalid INI value for $_\n" 339 1 340
defined $value and not ref $value and not $value =~ /[\r\n]/u or die "Invalid INI value for $_\n" 0 0 0
Condition25%3/12
defined $value and not ref $value and not $value =~ /[\r\n]/u
!ll&&!rl&&r
01339
defined $value and not ref $value
!ll&&!rl&&r
00340
defined $value and not ref $value and not $value =~ /[\r\n]/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC33%1/3
defined $value and not ref $value and not $value =~ /[\r\n]/u
defined $value not ref $value not $value =~ /[\r\n]/u
MC/DC0%0/3
defined $value and not ref $value and not $value =~ /[\r\n]/u
defined $value not ref $value not $value =~ /[\r\n]/u
Truth table50%2/4
A and not(B) and not(C)
A= defined $valueB= not ref $valueC= not $value =~ /[\r\n]/u
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= defined $valueB= not ref $valueC= not $value =~ /[\r\n]/u
ABCresult
0XX0
10X0
1100
1111
296 or die "Invalid INI value for $_\n";
297 0 "$_ = $value\n"
298 } sort keys %$values)
299 . "\n"
300 } @$sections
301 }
302  
303 111 ▶sub _file {
Subroutine _file: called CC=3 SCAR=15.7
304 my $dir = $ENV{XDG_CONFIG_HOME}
305 0 ▶ || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Subroutine _file: not called CC=3 SCAR=15.7
Condition37%3/8
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
1056
$ENV{'HOME'} || die("HOME is not set\n")
l!l
60
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
00
$ENV{'HOME'} || die("HOME is not set\n")
l!l
00
MC/DC100%1/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table100%2/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
306  
307 0 "$dir/anyevent-mcp/global.ini"
308 }
309  
310 103 my $global;
311  
312 85 ▶sub global_config {
Subroutine global_config: called CC=2 SCAR=10.9
Pod documented
313 0 ▶ $global //= __PACKAGE__->new (@_)->load->ensure_secret
Subroutine global_config: not called CC=2 SCAR=10.9
Pod documented
Condition33%2/6
$global //= __PACKAGE__->new(@_)->load->ensure_secret
l!l&&r!l&&!r
29560
$global //= __PACKAGE__->new(@_)->load->ensure_secret
l!l&&r!l&&!r
000
MC/DC0%0/2
$global //= __PACKAGE__->new(@_)->load->ensure_secret
$global __PACKAGE__->new(@_)->load->ensure_secret
MC/DC0%0/2
$global //= __PACKAGE__->new(@_)->load->ensure_secret
$global __PACKAGE__->new(@_)->load->ensure_secret
Truth table66%2/3
A //= B
A= $globalB= __PACKAGE__->new(@_)->load->ensure_secret
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $globalB= __PACKAGE__->new(@_)->load->ensure_secret
ABresult
1X1
011
000
314 }
315  
316 114 ▶sub new {
Subroutine new: called CC=2 SCAR=9.2
Pod documented
317 0 ▶ my ($class, %kv) = @_;
Subroutine new: not called CC=2 SCAR=9.2
Pod documented
318  
319 0 my $self = $class->SUPER::new (section => {}, %kv);
320 0 ▶ $self->{file} = _file unless exists $self->{file};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"file"} 54 60 114
unless exists $self->{"file"} 0 0 0
321 0 $self
322 }
323  
324 131 ▶sub load {
Subroutine load: called CC=7 SCAR=28.1
Pod documented
325 0 ▶ my ($self, $fh) = @_;
Subroutine load: not called CC=7 SCAR=28.1
Pod documented
326 0 $self->{section} = {};
327  
328 0 ▶ unless ($fh) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($fh) 102 29 131
unless ($fh) 0 0 0
329 0 ▶ open $fh, "<:raw", $self->{file} or do {
Branch: 2 branches
BranchTrueFalseTotal
open $fh, "<:raw", $self->{"file"} or do { die qq[$self->{"file"}: $!\n] unless $! == 2; return $self } 87 15 102
open $fh, "<:raw", $self->{"file"} or do { die qq[$self->{"file"}: $!\n] unless $! == 2; return $self } 0 0 0
330 0 ▶ die "$self->{file}: $!\n" unless $! == ENOENT;
Branch: 2 branches
BranchTrueFalseTotal
unless $! == 2 0 15 15
unless $! == 2 0 0 0
331 0 return $self
332 };
333 }
334  
335 0 ▶ seek $fh, 0, 0 or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
seek $fh, 0, 0 or die qq[$self->{"file"}: $!\n] 116 0 116
seek $fh, 0, 0 or die qq[$self->{"file"}: $!\n] 0 0 0
336 0 $self->{section} = merge_sections (parse_ini (AnyEvent::MCP::file_load ($self->{file}, $fh), $self->{file}), "global");
337 0 ▶ close $fh or die "$self->{file}: $!\n" unless @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ > 1 87 29 116
unless @_ > 1 0 0 0
Condition30%3/10
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
l!l&&r!l&&!r
29870
close $fh or die qq[$self->{"file"}: $!\n]
l!l
870
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
l!l&&r!l&&!r
000
close $fh or die qq[$self->{"file"}: $!\n]
l!l
00
MC/DC0%0/2
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
@_ > 1 close $fh
MC/DC0%0/2
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
@_ > 1 close $fh
Truth table66%2/3
A || B
A= @_ > 1B= close $fh
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= @_ > 1B= close $fh
ABresult
1X1
011
000
338  
339 0 $self
340 }
341  
342 9 ▶sub save {
Subroutine save: called CC=1 SCAR=1.2
Pod documented
343 0 ▶ my ($self) = @_;
Subroutine save: not called CC=1 SCAR=1.2
Pod documented
344  
345 AnyEvent::MCP::file_save $self->{file},
346 0 serialize_ini [map [$_, $self->{section}{$_}], sort keys %{$self->{section}}];
347 0 $self
348 }
349  
350 77 ▶sub ensure_secret {
Subroutine ensure_secret: called CC=8 SCAR=29.9
Pod documented
351 0 ▶ my ($self) = @_;
Subroutine ensure_secret: not called CC=8 SCAR=29.9
Pod documented
352  
353 0 ▶ unless (exists $self->{section}{global}{secret}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"section"}{"global"}{"secret"}) 19 58 77
unless (exists $self->{"section"}{"global"}{"secret"}) 0 0 0
354 0 my $lock = AnyEvent::MCP::file_lock $self->{file};
355 0 $self->load ($lock);
356  
357 0 ▶ unless (exists $self->{section}{global}{secret}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"section"}{"global"}{"secret"}) 18 0 18
unless (exists $self->{"section"}{"global"}{"secret"}) 0 0 0
358 0 my $secret = eval { unpack "H*", AnyEvent::MCP::random_bytes 64 };
359 0 ▶ die "$@Supply a securely generated 128-digit hexadecimal secret in $self->{file}\n"
Branch: 2 branches
BranchTrueFalseTotal
if $@ 1 17 18
if $@ 0 0 0
360 if $@;
361 0 ▶ seek $lock, 0, 2 or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
seek $lock, 0, 2 or die qq[$self->{"file"}: $!\n] 17 0 17
seek $lock, 0, 2 or die qq[$self->{"file"}: $!\n] 0 0 0
362 0 ▶ print {$lock} "\n[global]\nsecret = $secret\n" or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
print {$lock;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n] 17 0 17
print {$lock;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n] 0 0 0
363 0 $self->{section}{global}{secret} = $secret;
364 }
365  
366 0 ▶ close $lock or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
close $lock or die qq[$self->{"file"}: $!\n] 17 0 17
close $lock or die qq[$self->{"file"}: $!\n] 0 0 0
367 }
368  
369 0 ▶ $self->{section}{global}{secret} =~ /\A[0-9a-fA-F]{128}\z/
Branch: 2 branches
BranchTrueFalseTotal
$self->{"section"}{"global"}{"secret"} =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: invalid secret; supply 128 hexadecimal digits\n] 74 1 75
$self->{"section"}{"global"}{"secret"} =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: invalid secret; supply 128 hexadecimal digits\n] 0 0 0
370 or die "$self->{file}: invalid secret; supply 128 hexadecimal digits\n";
371 0 $self
372 }
373  
374 145 ▶sub _prk_derive_secret($$@) {
Subroutine _prk_derive_secret: called CC=5 SCAR=21.5
375 0 ▶ my ($prk, $length, @info) = @_;
Subroutine _prk_derive_secret: not called CC=5 SCAR=21.5
376  
377 0 ▶ 64 == length $prk
Branch: 2 branches
BranchTrueFalseTotal
64 == length $prk or die "derive_secret: key length must be 64 octets\n" 145 0 145
64 == length $prk or die "derive_secret: key length must be 64 octets\n" 0 0 0
378 or die "derive_secret: key length must be 64 octets\n";
379 0 ▶ $length =~ /\A[0-9]+\z/ && $length <= 255 * 64
Branch: 2 branches
BranchTrueFalseTotal
$length =~ /\A[0-9]+\z/u and $length <= 16320 or die "derive_secret: length must be an integer in 0..16320\n" 142 3 145
$length =~ /\A[0-9]+\z/u and $length <= 16320 or die "derive_secret: length must be an integer in 0..16320\n" 0 0 0
Condition50%3/6
$length =~ /\A[0-9]+\z/u and $length <= 16320
!ll&&!rl&&r
21142
$length =~ /\A[0-9]+\z/u and $length <= 16320
!ll&&!rl&&r
000
MC/DC100%2/2
$length =~ /\A[0-9]+\z/u and $length <= 16320
$length =~ /\A[0-9]+\z/u $length <= 16320
MC/DC0%0/2
$length =~ /\A[0-9]+\z/u and $length <= 16320
$length =~ /\A[0-9]+\z/u $length <= 16320
Truth table100%3/3
A and B
A= $length =~ /\A[0-9]+\z/uB= $length <= 16320
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $length =~ /\A[0-9]+\z/uB= $length <= 16320
ABresult
0X0
100
111
380 or die "derive_secret: length must be an integer in 0..16320\n";
381  
382 0 my $info = pack "(N/a*)*", "anyevent-mcp", @info;
383 0 my $t = "";
384 0 my $okm = "";
385 0 my $i = 0;
386  
387 0 ▶ while ($length > length $okm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length > length $okm) 397 142 539
if ($length > length $okm) 0 0 0
388 0 $t = Digest::SHA::hmac_sha512 $t . $info . chr ++$i, $prk;
389 0 $okm .= $t;
390 }
391  
392 0 substr $okm, 0, $length
393 }
394  
395 146 ▶sub derive_secret {
Subroutine derive_secret: called CC=3 SCAR=14.2
Pod documented
396 0 ▶ my ($self, $length, @info) = @_;
Subroutine derive_secret: not called CC=3 SCAR=14.2
Pod documented
397  
398 0 ▶ $self = global_config () unless ref $self;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self 2 144 146
unless ref $self 0 0 0
399  
400 0 my $secret = $self->{section}{global}{secret};
401 0 ▶ $secret =~ /\A[0-9a-fA-F]{128}\z/
Branch: 2 branches
BranchTrueFalseTotal
$secret =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: missing or invalid secret\n] 145 1 146
$secret =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: missing or invalid secret\n] 0 0 0
402 or die "$self->{file}: missing or invalid secret\n";
403  
404 0 _prk_derive_secret (pack ("H*", $secret), $length, @info)
405 }
406  
407 355 ▶sub program_name {
Subroutine program_name: called CC=2 SCAR=9.2
Pod documented
408 0 ▶ my $name = File::Basename::basename ($0);
Subroutine program_name: not called CC=2 SCAR=9.2
Pod documented
409 0 $name =~ s/\.(?:pl|perl)\z//;
410 0 ▶ $name eq "-e" ? "perl" : $name
Branch: 2 branches
BranchTrueFalseTotal
$name eq '-e' ? : 25 330 355
$name eq '-e' ? : 0 0 0
411 }
412  
413 51 ▶sub user_name {
Subroutine user_name: called CC=3 SCAR=15.3
Pod documented
414 0 ▶ my ($name) = @_;
Subroutine user_name: not called CC=3 SCAR=15.3
Pod documented
415  
416 0 ▶ defined $name && $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
Branch: 2 branches
BranchTrueFalseTotal
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u or die "Invalid MCP user name\n" 50 1 51
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u or die "Invalid MCP user name\n" 0 0 0
Condition33%2/6
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
!ll&&!rl&&r
0150
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
!ll&&!rl&&r
000
MC/DC50%1/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
defined $name $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
MC/DC0%0/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
defined $name $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
ABresult
0X0
100
111
417 or die "Invalid MCP user name\n";
418  
419 0 $name
420 }
421  
422 14 ▶sub credential {
Subroutine credential: called CC=3 SCAR=14.2
Pod documented
423 0 ▶ my ($self, $purpose, $name, $username) = @_;
Subroutine credential: not called CC=3 SCAR=14.2
Pod documented
424  
425 0 ▶ $username = "" unless defined $username;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $username 5 9 14
unless defined $username 0 0 0
426  
427 0 ▶ $purpose eq "token"
Branch: 2 branches
BranchTrueFalseTotal
$purpose eq "token" or die "Unknown credential purpose '${purpose}'\n" 13 1 14
$purpose eq "token" or die "Unknown credential purpose '${purpose}'\n" 0 0 0
428 or die "Unknown credential purpose '$purpose'\n";
429  
430 0 my $credential = MIME::Base64::encode_base64 (
431 $self->derive_secret (
432 32,
433 $purpose,
434 (AnyEvent::MCP::utf8enc program_name),
435 (AnyEvent::MCP::utf8enc $name),
436 (AnyEvent::MCP::utf8enc $username)
437 ), ""
438 );
439 0 $credential =~ y!+/=!-_!d;
440  
441 0 $credential
442 }
443  
444 64 ▶sub users {
Subroutine users: called CC=8 SCAR=31.5
Pod documented
445 0 ▶ my ($self, $name, $program) = @_;
Subroutine users: not called CC=8 SCAR=31.5
Pod documented
446  
447 0 $name = AnyEvent::MCP::instance_name $name;
448 0 ▶ $program //= program_name;
Condition16%1/6
$program //= program_name()
l!l&&r!l&&!r
6400
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table33%1/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
449 0 my %users = (default => { scope => "global", generation => 1 });
450  
451 0 for my $entry (
452 ["global", $self->{section}{global}],
453 ["program", $self->{section}{"program $program"}],
454 ["instance", $self->{section}{"instance $name"}],
455 ) {
456 0 my ($scope, $section) = @$entry;
457 0 ▶ next unless $section;
Branch: 2 branches
BranchTrueFalseTotal
unless $section 54 138 192
unless $section 0 0 0
458  
459 0 for my $key (grep /\Auser\./, keys %$section) {
460 0 ▶ my ($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/
Branch: 2 branches
BranchTrueFalseTotal
my($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/u or die "Invalid MCP user '${key}'\n" 58 0 58
my($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/u or die "Invalid MCP user '${key}'\n" 0 0 0
461 or die "Invalid MCP user '$key'\n";
462 0 my $generation = $section->{$key};
463 0 ▶ $generation =~ /\A-?[1-9][0-9]*\z/ && length $generation < 18
Branch: 2 branches
BranchTrueFalseTotal
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18 or die "Invalid MCP user generation for '${key}'\n" 58 0 58
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18 or die "Invalid MCP user generation for '${key}'\n" 0 0 0
Condition16%1/6
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
0058
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
000
MC/DC0%0/2
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
$generation =~ /\A-?[1-9][0-9]*\z/u length $generation < 18
MC/DC0%0/2
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
$generation =~ /\A-?[1-9][0-9]*\z/u length $generation < 18
Truth table33%1/3
A and B
A= $generation =~ /\A-?[1-9][0-9]*\z/uB= length $generation < 18
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $generation =~ /\A-?[1-9][0-9]*\z/uB= length $generation < 18
ABresult
0X0
100
111
464 or die "Invalid MCP user generation for '$key'\n";
465 0 $users{$user} = { scope => $scope, generation => $generation };
466 }
467 }
468  
469 0 \%users
470 }
471  
472 54 ▶sub user_credentials {
Subroutine user_credentials: called CC=5 SCAR=22.7
Pod documented
473 0 ▶ my ($self, $name, $program) = @_;
Subroutine user_credentials: not called CC=5 SCAR=22.7
Pod documented
474  
475 0 $name = AnyEvent::MCP::instance_name $name;
476 0 ▶ $program //= program_name;
Condition33%2/6
$program //= program_name()
l!l&&r!l&&!r
9450
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
477 0 my $users = $self->users ($name, $program);
478 0 my @credentials;
479  
480 0 ▶ for my $user (sort { ($a ne "default") <=> ($b ne "default") || $a cmp $b } keys %$users) {
Condition16%1/6
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
l!l&&r!l&&!r
2100
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
($a ne 'default') <=> ($b ne 'default') $a cmp $b
MC/DC0%0/2
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
($a ne 'default') <=> ($b ne 'default') $a cmp $b
Truth table33%1/3
A || B
A= ($a ne 'default') <=> ($b ne 'default')B= $a cmp $b
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ($a ne 'default') <=> ($b ne 'default')B= $a cmp $b
ABresult
1X1
011
000
481 0 my $info = $users->{$user};
482 0 ▶ next unless $info->{generation} > 0;
Branch: 2 branches
BranchTrueFalseTotal
unless $info->{"generation"} > 0 4 71 75
unless $info->{"generation"} > 0 0 0 0
483 my $token = MIME::Base64::encode_base64 ($self->derive_secret (
484 32, "user-token-v1", AnyEvent::MCP::utf8enc ($program),
485 AnyEvent::MCP::utf8enc ($name), $info->{scope},
486 0 AnyEvent::MCP::utf8enc ($user), "$info->{generation}"
487 ), "");
488 0 $token =~ y!+/=!-_!d;
489 0 push @credentials, [$user, "$info->{scope}:$user", $token];
490 }
491  
492 \@credentials
493 0 }
494  
495 39 ▶sub connect_user {
Subroutine connect_user: called CC=5 SCAR=25.7
Pod documented
496 0 ▶ my ($self, $name, $program) = @_;
Subroutine connect_user: not called CC=5 SCAR=25.7
Pod documented
497  
498 0 $name = AnyEvent::MCP::instance_name $name;
499 0 ▶ $program //= program_name;
Condition33%2/6
$program //= program_name()
l!l&&r!l&&!r
1380
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
500 my $user = $self->{section}{"instance $name"}{connect_user}
501 // $self->{section}{"program $program"}{connect_user}
502 0 ▶ // $self->{section}{global}{connect_user} // "default";
Condition18%3/16
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
l!l
039
($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}
l!l&&r!l&&!r
0039
$self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}
l!l&&r!l&&!r
0039
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
l!l
00
($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}
l!l&&r!l&&!r
000
$self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}
l!l&&r!l&&!r
000
MC/DC0%0/3
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
$self->{"section"}{"instance $name"}{"connect_user"} $self->{"section"}{"program $program"}{"connect_user"} $self->{"section"}{"global"}{"connect_user"}
MC/DC0%0/3
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
$self->{"section"}{"instance $name"}{"connect_user"} $self->{"section"}{"program $program"}{"connect_user"} $self->{"section"}{"global"}{"connect_user"}
Truth table25%1/4
A // B // C
A= $self->{"section"}{"instance $name"}{"connect_user"}B= $self->{"section"}{"program $program"}{"connect_user"}C= $self->{"section"}{"global"}{"connect_user"}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A // B // C
A= $self->{"section"}{"instance $name"}{"connect_user"}B= $self->{"section"}{"program $program"}{"connect_user"}C= $self->{"section"}{"global"}{"connect_user"}
ABCresult
1XX1
01X1
0011
0000
503 0 user_name $user
504 }
505  
506 63 ▶sub http_getopt {
Subroutine http_getopt: called CC=1 SCAR=1.2
Pod documented
507 0 ▶ my ($name, $bind, $print_url, $tokens) = @_;
Subroutine http_getopt: not called CC=1 SCAR=1.2
Pod documented
508  
509 0 "name=s" => $name, "bind=s" => $bind, "print-url|print-urls:s" => $print_url, "token=s@" => $tokens
510 }
511  
512 46 ▶sub auto_rules {
Subroutine auto_rules: called CC=7 SCAR=26.4
Pod documented
513 0 ▶ my ($self, $name) = @_;
Subroutine auto_rules: not called CC=7 SCAR=26.4
Pod documented
514 my %settings = (
515 0 ▶ %{$self->{section}{"program " . program_name ()} || {}},
Condition50%2/4
$self->{'section'}{'program ' . program_name()} || {}
l!l
838
$self->{'section'}{'program ' . program_name()} || {}
l!l
00
MC/DC100%1/1
$self->{'section'}{'program ' . program_name()} || {}
$self->{'section'}{'program ' . program_name()}
MC/DC0%0/1
$self->{'section'}{'program ' . program_name()} || {}
$self->{'section'}{'program ' . program_name()}
Truth table100%2/2
A
A= $self->{'section'}{'program ' . program_name()}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'section'}{'program ' . program_name()}
Aresult
11
00
516 0 ▶ %{$self->{section}{"instance " . AnyEvent::MCP::instance_name ($name)} || {}},
Condition50%2/4
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
l!l
1432
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
l!l
00
MC/DC100%1/1
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
MC/DC0%0/1
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Truth table100%2/2
A
A= $self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Aresult
11
00
517 );
518 0 my @rules;
519  
520 0 for my $key (sort grep /\Aauto-rule\./, keys %settings) {
521 0 ▶ $key =~ /\Aauto-rule\.(.+)\z/
Branch: 2 branches
BranchTrueFalseTotal
$key =~ /\Aauto-rule\.(.+)\z/u or die "Invalid automatic permission rule key '${key}'\n" 11 0 11
$key =~ /\Aauto-rule\.(.+)\z/u or die "Invalid automatic permission rule key '${key}'\n" 0 0 0
522 or die "Invalid automatic permission rule key '$key'\n";
523 0 my $value = $settings{$key};
524 0 ▶ next unless length $value;
Branch: 2 branches
BranchTrueFalseTotal
unless length $value 2 9 11
unless length $value 0 0 0
525 0 ▶ my ($decision, $spec) = $value =~ /\A(allow|deny)\s+(\S+)\z/
Branch: 2 branches
BranchTrueFalseTotal
() = $value =~ /\A(allow|deny)\s+(\S+)\z/u or die "Invalid configured automatic permission rule '${key}'\n" 8 1 9
() = $value =~ /\A(allow|deny)\s+(\S+)\z/u or die "Invalid configured automatic permission rule '${key}'\n" 0 0 0
526 or die "Invalid configured automatic permission rule '$key'\n";
527 0 push @rules, [$decision, $spec];
528 }
529  
530 @rules
531 0 }
532  
533 46 ▶sub server_port {
Subroutine server_port: called CC=1 SCAR=1.2
Pod documented
534 0 ▶ my ($self, $name) = @_;
Subroutine server_port: not called CC=1 SCAR=1.2
Pod documented
535  
536 0 $name = AnyEvent::MCP::instance_name $name;
537  
538 0 16384 + (
539 (unpack "n", $self->derive_secret (
540 2,
541 "port",
542 (AnyEvent::MCP::utf8enc program_name),
543 (AnyEvent::MCP::utf8enc $name))
544 ) & 16383)
545 }
546  
547 41 ▶sub http_settings {
Subroutine http_settings: called CC=19 SCAR=46.4
Pod documented
548 0 ▶ my ($self, $name, $bind, $default_port) = @_;
Subroutine http_settings: not called CC=19 SCAR=46.4
Pod documented
549  
550 0 $name = AnyEvent::MCP::instance_name $name;
551 my $settings = {
552 0 %{ $self->{section}{"program " . program_name ()} },
553 0 %{ $self->{section}{"instance $name"} },
554 };
555 0 ▶ my $port = $default_port // $self->server_port ($name);
Condition16%1/6
$default_port // $self->server_port($name)
l!l&&r!l&&!r
0410
$default_port // $self->server_port($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$default_port // $self->server_port($name)
$default_port $self->server_port($name)
MC/DC0%0/2
$default_port // $self->server_port($name)
$default_port $self->server_port($name)
Truth table33%1/3
A // B
A= $default_portB= $self->server_port($name)
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $default_portB= $self->server_port($name)
ABresult
1X1
011
000
556 0 ▶ my $configured = $settings->{bind} // "127.0.0.1:$port";
Condition50%2/4
$settings->{"bind"} // "127.0.0.1:$port"
l!l
1625
$settings->{"bind"} // "127.0.0.1:$port"
l!l
00
MC/DC100%1/1
$settings->{"bind"} // "127.0.0.1:$port"
$settings->{"bind"}
MC/DC0%0/1
$settings->{"bind"} // "127.0.0.1:$port"
$settings->{"bind"}
Truth table100%2/2
A
A= $settings->{"bind"}
Aresult
11
00
Truth table0%0/2
A
A= $settings->{"bind"}
Aresult
11
00
557 0 ▶ $configured = "127.0.0.1$configured" if $configured =~ /\A:[0-9]+\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $configured =~ /\A:[0-9]+\z/u 0 41 41
if $configured =~ /\A:[0-9]+\z/u 0 0 0
558 0 my ($configured_host, $configured_port) = AnyEvent::Socket::parse_hostport $configured, $port;
559 0 ▶ defined $configured_host && defined $configured_port
Branch: 2 branches
BranchTrueFalseTotal
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535 or die "Invalid MCP bind address '${configured}'\n" 41 0 41
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535 or die "Invalid MCP bind address '${configured}'\n" 0 0 0
Condition16%3/18
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
!ll&&!rl&&r
000
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
defined $configured_host and defined $configured_port
!ll&&!rl&&r
000
MC/DC0%0/4
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
defined $configured_host defined $configured_port $configured_port =~ /\A[0-9]+\z/u $configured_port <= 65535
MC/DC0%0/4
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
defined $configured_host defined $configured_port $configured_port =~ /\A[0-9]+\z/u $configured_port <= 65535
Truth table20%1/5
A and B and C and D
A= defined $configured_hostB= defined $configured_portC= $configured_port =~ /\A[0-9]+\z/uD= $configured_port <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= defined $configured_hostB= defined $configured_portC= $configured_port =~ /\A[0-9]+\z/uD= $configured_port <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
560 && $configured_port =~ /\A[0-9]+\z/ && $configured_port <= 65535
561 or die "Invalid MCP bind address '$configured'\n";
562  
563 0 ▶ $bind = $configured unless defined $bind;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bind 19 22 41
unless defined $bind 0 0 0
564 0 ▶ $bind = "127.0.0.1$bind" if $bind =~ /\A:[0-9]+\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $bind =~ /\A:[0-9]+\z/u 5 36 41
if $bind =~ /\A:[0-9]+\z/u 0 0 0
565  
566 0 my ($host, $service) = AnyEvent::Socket::parse_hostport $bind, $configured_port;
567 0 ▶ defined $host && defined $service && $service =~ /\A[0-9]+\z/ && $service <= 65535
Branch: 2 branches
BranchTrueFalseTotal
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535 or die "Invalid MCP bind address '${bind}'\n" 40 1 41
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535 or die "Invalid MCP bind address '${bind}'\n" 0 0 0
Condition22%4/18
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
!ll&&!rl&&r
0140
defined $host and defined $service and $service =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0041
defined $host and defined $service
!ll&&!rl&&r
0041
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
!ll&&!rl&&r
000
defined $host and defined $service and $service =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
defined $host and defined $service
!ll&&!rl&&r
000
MC/DC25%1/4
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
defined $host defined $service $service =~ /\A[0-9]+\z/u $service <= 65535
MC/DC0%0/4
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
defined $host defined $service $service =~ /\A[0-9]+\z/u $service <= 65535
Truth table40%2/5
A and B and C and D
A= defined $hostB= defined $serviceC= $service =~ /\A[0-9]+\z/uD= $service <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= defined $hostB= defined $serviceC= $service =~ /\A[0-9]+\z/uD= $service <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
568 or die "Invalid MCP bind address '$bind'\n";
569  
570 my %kv = (
571 0 ▶ (map { exists $settings->{$_} ? ($_ => $settings->{$_}) : () }
Branch: 2 branches
BranchTrueFalseTotal
exists $settings->{$_} ? : 2 198 200
exists $settings->{$_} ? : 0 0 0
572 qw(path url_host url_scheme tls_cert tls_key)),
573 name => $name,
574 host => $host,
575 port => $service,
576 );
577  
578 0 ▶ unless (AnyEvent::Socket::parse_address $host) {
Branch: 2 branches
BranchTrueFalseTotal
unless (&AnyEvent::Socket::parse_address($host)) 1 39 40
unless (&AnyEvent::Socket::parse_address($host)) 0 0 0
579 0 my $cv = AnyEvent->condvar;
580 0 AnyEvent::Socket::resolve_sockaddr $host, $service, "tcp", 0, 0, $cv;
581 0 my ($resolved) = $cv->recv;
582 0 ▶ $resolved or die "Cannot resolve MCP bind host '$host'\n";
Branch: 2 branches
BranchTrueFalseTotal
$resolved or die "Cannot resolve MCP bind host '${host}'\n" 1 0 1
$resolved or die "Cannot resolve MCP bind host '${host}'\n" 0 0 0
583  
584 0 my (undef, $address) = AnyEvent::Socket::unpack_sockaddr $resolved->[3];
585 0 ▶ $kv{url_host} //= $host;
Condition16%1/6
$kv{"url_host"} //= $host
l!l&&r!l&&!r
010
$kv{"url_host"} //= $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$kv{"url_host"} //= $host
$kv{"url_host"} $host
MC/DC0%0/2
$kv{"url_host"} //= $host
$kv{"url_host"} $host
Truth table33%1/3
A //= B
A= $kv{"url_host"}B= $host
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $kv{"url_host"}B= $host
ABresult
1X1
011
000
586 0 $kv{host} = AnyEvent::Socket::format_address $address;
587 }
588  
589 0 ▶ if (exists $settings->{tokens}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $settings->{'tokens'}) { } 6 34 40
if (exists $settings->{'tokens'}) { } 0 0 0
590 0 $kv{tokens} = [split /\s*,\s*/, $settings->{tokens}, -1];
591 0 $kv{connect_tokens} = $kv{tokens};
592 } else {
593 0 my $credentials = $self->user_credentials ($name);
594 0 $kv{tokens} = [map $_->[2], @$credentials];
595 0 $kv{token_users} = { map { $_->[2] => $_->[1] } @$credentials };
596 0 my $connect = $self->connect_user ($name);
597 0 $kv{connect_tokens} = [map $_->[2], grep $_->[0] eq $connect, @$credentials];
598 }
599  
600 0 %kv
601 }
602  
603 13 ▶sub http_url {
Subroutine http_url: called CC=10 SCAR=32.2
Pod documented
604 0 ▶ my ($self, $name, $bind, %options) = @_;
Subroutine http_url: not called CC=10 SCAR=32.2
Pod documented
605  
606 0 $name = AnyEvent::MCP::instance_name $name;
607  
608 # only a derived token depends on the master secret surviving this process.
609 0 ▶ if (($options{auth} || "") ne "none"
Branch: 2 branches
BranchTrueFalseTotal
if (($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}) 3 10 13
if (($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}) 0 0 0
Condition42%12/28
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
!ll&&!rl&&r
1003
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"}
!ll&&!rl&&r
1003
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
913
($options{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
184
$options{'auth'} || ''
l!l
112
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
000
$options{'auth'} || ''
l!l
00
MC/DC0%0/5
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
$options{'auth'} not exists $options{"tokens"} not exists $self->{"section"}{"instance $name"}{"tokens"} not exists $self->{"section"}{'program ' . program_name()}{"tokens"} not exists $self->{"section"}{"global"}{"tokens"}
MC/DC0%0/5
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
$options{'auth'} not exists $options{"tokens"} not exists $self->{"section"}{"instance $name"}{"tokens"} not exists $self->{"section"}{'program ' . program_name()}{"tokens"} not exists $self->{"section"}{"global"}{"tokens"}
Truth table0%0/6
A and not(B) and not(C) and not(D) and not(E)
A= $options{'auth'}B= not exists $options{"tokens"}C= not exists $self->{"section"}{"instance $name"}{"tokens"}D= not exists $self->{"section"}{'program ' . program_name()}{"tokens"}E= not exists $self->{"section"}{"global"}{"tokens"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table0%0/6
A and not(B) and not(C) and not(D) and not(E)
A= $options{'auth'}B= not exists $options{"tokens"}C= not exists $self->{"section"}{"instance $name"}{"tokens"}D= not exists $self->{"section"}{'program ' . program_name()}{"tokens"}E= not exists $self->{"section"}{"global"}{"tokens"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
610 && !exists $options{tokens}
611 && !exists $self->{section}{"instance $name"}{tokens}
612 && !exists $self->{section}{"program " . program_name ()}{tokens}
613 && !exists $self->{section}{global}{tokens}) {
614 0 my $stored = (ref $self)->new (file => $self->{file})->load;
615 $stored->{section}{global}{secret} eq $self->{section}{global}{secret}
616 0 ▶ or die "MCP master secret is not persistently stored in $self->{file}\n";
Branch: 2 branches
BranchTrueFalseTotal
$stored->{"section"}{"global"}{"secret"} eq $self->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $self->{"file"}\n] 2 1 3
$stored->{"section"}{"global"}{"secret"} eq $self->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $self->{"file"}\n] 0 0 0
617 }
618  
619 0 require AnyEvent::MCP::Server::HTTP;
620 0 my %http = $self->http_settings ($name, $bind, delete $options{default_port});
621 0 ▶ delete $http{tokens} if ($options{auth} || "") eq "none";
Branch: 2 branches
BranchTrueFalseTotal
if ($options{"auth"} || "") eq "none" 1 11 12
if ($options{"auth"} || "") eq "none" 0 0 0
Condition50%2/4
$options{'auth'} || ''
l!l
111
$options{'auth'} || ''
l!l
00
MC/DC100%1/1
$options{'auth'} || ''
$options{'auth'}
MC/DC0%0/1
$options{'auth'} || ''
$options{'auth'}
Truth table100%2/2
A
A= $options{'auth'}
Aresult
11
00
Truth table0%0/2
A
A= $options{'auth'}
Aresult
11
00
622  
623 0 AnyEvent::MCP::Server::HTTP->new (%http, %options)->url
624 }
625  
626  
627 103 1
« bin/mcp-http ↑ Summary blib/lib/AnyEvent/MCP/Extension.pm »