Concierge-Auth
view release on metacpan or search on metacpan
examples/05-api-keys.pl view on Meta::CPAN
return;
}
print "--- API Key Generation ---\n";
# Generate keys for different users and applications
my @key_specs = (
['alice', 'webapp', 'standard', ['read', 'write']],
['alice', 'mobile', 'readonly', ['read']],
['bob', 'dashboard', 'admin', ['read', 'write', 'delete', 'admin']],
['charlie', 'webhook', 'webhook', ['webhook']],
['alice', 'backup', 'readonly', ['read', 'export']]
);
my @generated_keys;
for my $spec (@key_specs) {
my ($username, $app, $type, $perms) = @$spec;
my $key_info = generate_api_key($username, $app, $type, $perms);
push @generated_keys, $key_info;
printf "%-8s/%-10s (%s): %s\n",
$username, $app, $type, $key_info->{key};
}
print "\n--- Key Information Extraction ---\n";
for my $key_info (@generated_keys) {
my $extracted = extract_key_info($key_info->{key});
if ($extracted) {
printf "Key: %s...\n", substr($key_info->{key}, 0, 20);
printf " User prefix: %s | App prefix: %s | Type: %s\n",
$extracted->{user_prefix}, $extracted->{app_prefix}, $extracted->{key_type};
printf " Created: %s\n", scalar localtime($extracted->{created_at});
}
print "\n";
}
print "--- Key Type Examples ---\n";
# Demonstrate different key types and their characteristics
my %key_types = (
'readonly' => {
desc => 'Read-only access, safe for client-side use',
perms => ['read', 'list', 'export'],
length => 'medium'
},
'standard' => {
desc => 'Standard API access for most applications',
perms => ['read', 'write', 'update'],
length => 'medium'
},
'admin' => {
desc => 'Full administrative access',
perms => ['read', 'write', 'delete', 'admin', 'user_management'],
length => 'long'
},
'webhook' => {
desc => 'Webhook validation and callbacks',
perms => ['webhook', 'callback', 'event_receive'],
length => 'medium'
}
);
for my $type (sort keys %key_types) {
my $info = $key_types{$type};
my $sample_key = generate_api_key('demo', 'example', $type, $info->{perms});
printf "%-10s: %s\n", uc($type), $info->{desc};
printf "%-10s Key: %s\n", '', $sample_key->{key};
printf "%-10s Permissions: %s\n", '', join(', ', @{$info->{perms}});
printf "%-10s Length: %d chars\n", '', length($sample_key->{key});
print "\n";
}
print "--- Structured Key Benefits ---\n";
print "Key format: {USER}_{APP}_{TIMESTAMP}_{RANDOM}_{TYPE}\n\n";
print "Benefits of structured keys:\n";
print " â User identification without database lookup\n";
print " â Application context for logging and analytics\n";
print " â Timestamp for age-based policies\n";
print " â Type suffix for permission quick-checking\n";
print " â Random component for security\n";
print " â Consistent format for validation\n";
print "\n--- Alternative Key Formats ---\n";
# Simple keys
print "Simple keys (no structure):\n";
for my $i (1..3) {
my $simple = $auth->gen_random_token(32, 'alphanumeric');
printf " %d: %s\n", $i, $simple;
}
# UUID-based keys
print "\nUUID-based keys:\n";
for my $i (1..3) {
my $uuid = $auth->gen_uuid();
printf " %d: api_%s\n", $i, $uuid;
}
# Prefixed random keys
print "\nPrefixed random keys:\n";
for my $i (1..3) {
my $random = $auth->gen_random_token(28, 'url_safe');
printf " %d: ak_%s\n", $i, $random;
}
print "\n--- Key Management Best Practices ---\n";
print "Generation:\n";
print " â Use cryptographically secure random sources\n";
print " â Sufficient length for security (24+ characters)\n";
print " â Clear format for easy identification\n";
print " â Include metadata in structure when helpful\n";
print "\nStorage:\n";
( run in 1.487 second using v1.01-cache-2.11-cpan-140bd7fdf52 )