|
| 1 | +# Masked Input Example |
| 2 | + |
| 3 | +This example demonstrates the **masked input functionality** in WebFiori CLI, which allows secure entry of sensitive data like passwords, PINs, and tokens. |
| 4 | + |
| 5 | +## Features Demonstrated |
| 6 | + |
| 7 | +- **Basic Password Input**: Default asterisk (*) masking with validation |
| 8 | +- **Custom Mask Characters**: Use different characters (•, #, X, -) for masking |
| 9 | +- **Input Validation**: Enforce security requirements and format validation |
| 10 | +- **Default Values**: Optional default values for sensitive fields |
| 11 | +- **Confirmation Prompts**: Verify critical inputs by asking twice |
| 12 | + |
| 13 | +## Running the Example |
| 14 | + |
| 15 | +### Basic Usage |
| 16 | +```bash |
| 17 | +php main.php secure-input |
| 18 | +``` |
| 19 | + |
| 20 | +### Run Specific Demos |
| 21 | +```bash |
| 22 | +# Password demo only |
| 23 | +php main.php secure-input --demo=password |
| 24 | + |
| 25 | +# PIN demo with custom mask |
| 26 | +php main.php secure-input --demo=pin |
| 27 | + |
| 28 | +# Token demo with default value |
| 29 | +php main.php secure-input --demo=token |
| 30 | + |
| 31 | +# All demos (default) |
| 32 | +php main.php secure-input --demo=all |
| 33 | +``` |
| 34 | + |
| 35 | +## Code Examples |
| 36 | + |
| 37 | +### Basic Masked Input |
| 38 | +```php |
| 39 | +// Simple password input with default * masking |
| 40 | +$password = $this->getMaskedInput('Enter password: '); |
| 41 | +``` |
| 42 | + |
| 43 | +### Custom Mask Character |
| 44 | +```php |
| 45 | +// Use # characters for PIN masking |
| 46 | +$pin = $this->getMaskedInput('Enter PIN: ', null, null, '#'); |
| 47 | +``` |
| 48 | + |
| 49 | +### With Validation |
| 50 | +```php |
| 51 | +$validator = new InputValidator(function($password) { |
| 52 | + return strlen($password) >= 8 && |
| 53 | + preg_match('/[A-Z]/', $password) && |
| 54 | + preg_match('/[0-9]/', $password); |
| 55 | +}, 'Password must be 8+ chars with uppercase and number!'); |
| 56 | + |
| 57 | +$password = $this->getMaskedInput('Password: ', null, $validator); |
| 58 | +``` |
| 59 | + |
| 60 | +### With Default Value |
| 61 | +```php |
| 62 | +// Provide a default token value |
| 63 | +$token = $this->getMaskedInput('API Token: ', 'default-token', null, '•'); |
| 64 | +``` |
| 65 | + |
| 66 | +## Method Signature |
| 67 | + |
| 68 | +```php |
| 69 | +public function getMaskedInput( |
| 70 | + string $prompt, // The prompt to display |
| 71 | + ?string $default = null, // Optional default value |
| 72 | + ?InputValidator $validator = null, // Optional input validator |
| 73 | + string $mask = '*' // Mask character (default: *) |
| 74 | +): ?string |
| 75 | +``` |
| 76 | + |
| 77 | +## Security Features |
| 78 | + |
| 79 | +### Input Masking |
| 80 | +- Characters are masked as you type |
| 81 | +- Only mask characters are displayed in terminal |
| 82 | +- Actual input is captured securely |
| 83 | +- Supports backspace for corrections |
| 84 | + |
| 85 | +### Validation Support |
| 86 | +- Enforce minimum length requirements |
| 87 | +- Validate character patterns (uppercase, numbers, symbols) |
| 88 | +- Custom validation logic |
| 89 | +- Automatic retry on validation failure |
| 90 | + |
| 91 | +### Safe Handling |
| 92 | +- Input is trimmed automatically |
| 93 | +- Empty prompts return null safely |
| 94 | +- Works with existing stream abstraction |
| 95 | +- Compatible with testing framework |
| 96 | + |
| 97 | +## Use Cases |
| 98 | + |
| 99 | +### 1. User Authentication |
| 100 | +```php |
| 101 | +$password = $this->getMaskedInput('Login Password: '); |
| 102 | +$confirmPassword = $this->getMaskedInput('Confirm Password: '); |
| 103 | + |
| 104 | +if ($password !== $confirmPassword) { |
| 105 | + $this->error('Passwords do not match!'); |
| 106 | + return 1; |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### 2. API Configuration |
| 111 | +```php |
| 112 | +$apiKey = $this->getMaskedInput('API Key: ', null, null, '•'); |
| 113 | +$secret = $this->getMaskedInput('API Secret: ', null, null, '-'); |
| 114 | +``` |
| 115 | + |
| 116 | +### 3. Database Setup |
| 117 | +```php |
| 118 | +$dbPassword = $this->getMaskedInput('Database Password: '); |
| 119 | + |
| 120 | +$validator = new InputValidator(function($host) { |
| 121 | + return filter_var($host, FILTER_VALIDATE_IP) || |
| 122 | + filter_var($host, FILTER_VALIDATE_DOMAIN); |
| 123 | +}, 'Invalid host format!'); |
| 124 | + |
| 125 | +$dbHost = $this->getInput('Database Host: ', 'localhost', $validator); |
| 126 | +``` |
| 127 | + |
| 128 | +### 4. Secure Token Entry |
| 129 | +```php |
| 130 | +$jwtSecret = $this->getMaskedInput('JWT Secret: ', null, |
| 131 | + new InputValidator(function($secret) { |
| 132 | + return strlen($secret) >= 32; |
| 133 | + }, 'JWT secret must be at least 32 characters!') |
| 134 | +); |
| 135 | +``` |
| 136 | + |
| 137 | +## Interactive Demo Features |
| 138 | + |
| 139 | +The example includes several interactive demonstrations: |
| 140 | + |
| 141 | +1. **Password Demo**: Shows validation with security requirements |
| 142 | +2. **PIN Demo**: Demonstrates custom mask characters (#) |
| 143 | +3. **Token Demo**: Shows default values with bullet (•) masking |
| 144 | +4. **Advanced Demo**: Multiple scenarios including confirmation prompts |
| 145 | + |
| 146 | +## Testing |
| 147 | + |
| 148 | +The masked input functionality is fully testable using the existing `CommandTestCase` framework: |
| 149 | + |
| 150 | +```php |
| 151 | +$output = $this->executeSingleCommand($command, [], ['secret123']); |
| 152 | +$this->assertContains('Password received: secret123', $output); |
| 153 | +``` |
| 154 | + |
| 155 | +## Best Practices |
| 156 | + |
| 157 | +1. **Always validate sensitive input** for security requirements |
| 158 | +2. **Use appropriate mask characters** for different data types |
| 159 | +3. **Implement confirmation prompts** for critical operations |
| 160 | +4. **Never log or display** the actual sensitive values |
| 161 | +5. **Provide clear error messages** for validation failures |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +**Ready to secure your CLI applications?** Try the different demo modes to see masked input in action! |
0 commit comments