Concierge::Auth Examples

Comprehensive usage examples for the Concierge::Auth module demonstrating authentication, token generation, and security best practices.

Overview

Concierge::Auth provides secure local authentication using Argon2 password hashing with bcrypt compatibility. These examples showcase various usage patterns from basic authentication to advanced integration scenarios.

Running Examples

Quick Start

# From the examples directory
cd /path/to/Local/examples/Auth

# Run individual examples
perl 01-basic-authentication.pl
perl 02-user-management.pl
perl 03-token-generation.pl

All Examples

# Run all examples in sequence
for example in *.pl; do
    echo "=== Running $example ==="
    perl "$example"
    echo
done

Example Files

01-basic-authentication.pl

Core authentication functionality

Key Concepts:

02-user-management.pl

User lifecycle operations

Key Concepts:

03-token-generation.pl

Token generation utilities

Key Concepts:

04-session-management.pl

Session handling system

Key Concepts:

05-api-keys.pl

API key management

Key Concepts:

06-file-management.pl

Authentication file operations

Key Concepts:

07-error-handling.pl

Error handling and validation

Key Concepts:

08-advanced-usage.pl

Complex integration patterns

Key Concepts:

Key Features Demonstrated

Security Features

Authentication Patterns

Token Generation

File Management

Production Usage Patterns

Web Application Integration

use Concierge::Auth;

# Initialize auth system
my $auth = Concierge::Auth->new({file => '/secure/path/users.db'});

# Registration endpoint
sub handle_register {
    my ($username, $password) = @_;
    my ($success, $message) = $auth->setPwd($username, $password);
    return $success ? success_response() : error_response($message);
}

# Authentication middleware
sub authenticate_request {
    my $token = get_session_token();
    my $username = validate_session($token);
    return $username || unauthorized_response();
}

CLI Application

use Concierge::Auth;

# User management CLI
my $auth = Concierge::Auth->new({file => "$ENV{HOME}/.myapp/users"});

# Command handlers
sub cmd_register {
    my ($username, $password) = @_;
    my ($success, $msg) = $auth->setPwd($username, $password);
    print $success ? "User registered\n" : "Error: $msg\n";
}

API Service

use Concierge::Auth;

# Token-based API
my $auth = Concierge::Auth->new({no_file => 1});

# Generate API keys
sub generate_api_key {
    my ($user_id, $permissions) = @_;
    return $auth->gen_random_token(32, 'url_safe');
}

Best Practices

Security

Error Handling

Performance

Testing Your Integration

# Test basic functionality
perl -MConcierge::Auth -e '
    my $auth = Concierge::Auth->new({file => "/tmp/test.db"});
    my ($s, $m) = $auth->setPwd("test", "password123");
    print $s ? "✓ Registration works\n" : "✗ Registration failed: $m\n";
    my $ok = $auth->checkPwd("test", "password123");
    print $ok ? "✓ Authentication works\n" : "✗ Authentication failed\n";
'

# Test token generation
perl -MConcierge::Auth -e '
    my $auth = Concierge::Auth->new({no_file => 1});
    print "Session token: " . $auth->gen_random_token(24) . "\n";
    print "API key: " . $auth->gen_random_token(32, "alphanumeric") . "\n";
    print "UUID: " . $auth->gen_uuid() . "\n";
'

Common Patterns

User Registration Flow

  1. Validate input format
  2. Check if user already exists
  3. Hash password securely
  4. Store user credentials
  5. Return success/failure

Authentication Flow

  1. Validate input format
  2. Look up user credentials
  3. Verify password against hash
  4. Generate session token on success
  5. Return authentication result

Session Management

  1. Generate secure session token
  2. Store session metadata
  3. Validate token on each request
  4. Update last active timestamp
  5. Handle session expiration

Security Considerations

See Also

Support

For questions, bug reports, or feature requests, please contact the maintainer or create an issue in the project repository.