AnyEvent-MCP
view release on metacpan or search on metacpan
lib/AnyEvent/MCP/Config.pm view on Meta::CPAN
=head1 NAME
AnyEvent::MCP::Config - named servers and derived MCP credentials
=head1 SYNOPSIS
use AnyEvent::MCP::Config;
my $config = AnyEvent::MCP::Config::global_config;
my $token = $config->user_credentials ("agni")->[0][2];
my $url = $config->http_url ("agni", undef, path => "/");
=head1 DESCRIPTION
An MCP server and its clients often need matching credentials. Rather than
maintaining a separate password for each server and user, this module can
derive them from one private master secret. Users are defined by
C<user.NAME = GENERATION> in C<[global]>, C<[program PROGRAM]> or
C<[instance NAME]>, in that order of precedence. An absent global
C<user.default> is implicitly enabled at generation 1.
The shared file format is described in L<AnyEvent::MCP/GLOBAL CONFIGURATION>.
Loading this module does not read any files. C<global_config> loads the
configuration, ensures a persistent master secret exists and caches the
initialized object for the process.
A missing secret is generated using C<AnyEvent::MCP::random_bytes> and
appended under a file lock. Failure to save the secret is fatal, rather
than using a temporary secret that other processes cannot reproduce.
A supplied invalid secret is an error. Changing or losing the master
secret invalidates all credentials derived from it.
Derivation uses C<derive_secret> with the random master key.
The application namespace, purpose, program, instance name and username are
separately length-prefixed, so distinct contexts derive different credentials.
Deriving a password does not create an account or configure the server to
accept it: authentication still belongs to the server.
=head1 METHODS
=over
=item $config = AnyEvent::MCP::Config::global_config (file => $file)
Return the process-global configuration object, loaded and initialized.
The first call accepts the same arguments as C<new>, and ensures that the
master secret is persistently stored. Later calls return the same object
without rereading or reinitializing it.
=item $config = AnyEvent::MCP::Config->new (file => $file)
Construct an independent configuration object without reading or writing
the file. C<$file> defaults to the global configuration path.
=item $config->load
Reload the file, replacing the object's sections. A missing file yields an
empty configuration; other read and parsing errors are fatal. Sections are
available as C<< $config->{section}{$section} >>, each a hash reference of
string values. Named servers use C<[instance NAME]>. An optional raw file
handle can be supplied when the caller already holds the file lock.
=item $config->save
Write the loaded sections back as canonical UTF-8 INI text, sorting
sections and keys. Comments and original formatting are not retained.
The caller must hold C<AnyEvent::MCP::file_lock> through the update.
Returns the configuration object.
=item $config->ensure_secret
Ensure that C<[global]> contains a valid persistent master secret. If
missing, lock and reread the file, generate a secret only if still absent,
and append it without changing other settings. Fail if it cannot be saved
or is invalid. Returns the configuration object.
=item $bytes = $config->derive_secret ($length, @info)
=item $bytes = AnyEvent::MCP::Config->derive_secret ($length, @info)
Derive C<$length> secret bytes from the configuration's master secret and
an ordered list of context byte strings C<@info>. The class form uses
C<global_config>, loading the default configuration on first use and reusing it
thereafter. The object form uses that object's configuration, including
one loaded from an explicit file.
The same master secret and context always produce the same bytes. Use a
distinctive application/purpose string as the first context field, followed
by names or other values identifying the result:
my $secret = AnyEvent::MCP::Config->derive_secret (
32, "my application token v1", AnyEvent::MCP::utf8enc ($name)
);
The result contains raw bytes; encode it yourself if a printable token is
needed. This method does not add the result to a server's accepted tokens.
( run in 1.004 second using v1.01-cache-2.11-cpan-f5f21f1076c )