tzsk/otp

repository·master·Indexed 42 days ago

https://github.com/tzsk/otp

A secure, database-free One-Time Password (OTP) generator and verifier for PHP, optimized for Laravel. It provides a fluent API to configure OTP digits and expiration times, with support for the `Otp` facade and `otp()` helper in Laravel, or manual instantiation in non-Laravel PHP environments.

Tokens
1.7K
Snippets
11
Records
12
Agent score
54%

What's inside tzsk/otp

  1. Use tzsk/otp in non-Laravel PHP applications

    master

    To use the package outside of Laravel, you must instantiate a manager by providing a filesystem path where the package can store its data.

    Note: Ensure the directory you provide is secured via your web server configuration (e.g., Apache or Nginx) to prevent unauthorized access to the OTP data.

    Unlike Laravel, which uses the default cache store, non-Laravel usage requires an explicit path to a directory.

    // 1. Initialize with a storage directory
    $manager = otp('./otp-tmp');
    
    // 2. Configure and Generate
    $manager->digits(6);
    $manager->expiry(20);
    $otp = $manager->generate($unique_secret);
    
    // 3. Match (must use same config)
    $valid = $manager->digits(6)->expiry(20)->match($otp, $unique_secret);
  2. OTP Method Aliases

    master

    The Otp class implements __call to provide several semantic aliases for its primary methods. This allows for more expressive code depending on your preference:

    Primary MethodAliases
    generate($key)make($key), create($key)
    check($code, $key)match($code, $key), verify($code, $key)
  3. Generate and match OTPs with custom digits and expiry

    master

    When using the Otp facade in Laravel, you can chain methods to customize the OTP properties.

    Generation Methods:

    • digits(int $digits): Sets the number of digits.
    • expiry(int $minutes): Sets the expiration time in minutes.
    • generate($unique_secret): Generates the OTP string.
    • make($unique_secret): Alias for generate.
    • create($unique_secret): Alias for generate.

    Verification Methods:

    • match($otp, $unique_secret): Returns boolean.
    • check($otp, $unique_secret): Alias for match.

    Example of matching specific configurations:

    // If generated with 8 digits and 30 min expiry:
    Otp::digits(8)->expiry(30)->generate($unique_secret);
    
    // You MUST match with the same settings:
    Otp::digits(8)->expiry(30)->check($otp, $unique_secret); // returns true
    Otp::check($otp, $unique_secret); // returns false
    // Generate
    Otp::digits(8)->generate($unique_secret);
    Otp::expiry(30)->generate($unique_secret);
    Otp::digits(8)->expiry(30)->generate($unique_secret);
    
    // Match
    Otp::check($otp, $unique_secret);
    Otp::digits(8)->check($otp, $unique_secret);
    Otp::expiry(30)->check($otp, $unique_secret);
    Otp::digits(8)->expiry(30)->check($otp, $unique_secret);
  4. Use the OTP Facade in Laravel

    master

    In Laravel, you can interact with the package using the Tzsk\Otp\Facades\Otp facade.

    Important Security Requirement: The configuration used during generation (number of digits and expiration time) must match exactly during verification. If you change these settings during generation, you must apply the same settings when matching the OTP, otherwise verification will fail.

    use Tzsk\Otp\Facades\Otp;
    
    // Basic usage
    $otp = Otp::generate($unique_secret);
    $valid = Otp::match($otp, $unique_secret);
    
    // Custom configuration
    $otp = Otp::digits(8)->expiry(30)->generate($unique_secret);
    $valid = Otp::digits(8)->expiry(30)->match($otp, $unique_secret);
  5. Use the otp() helper function

    master

    In Laravel, you can use the global otp() helper function as a shorthand for the Facade.

    $otp = otp()->make($secret);
    $otp = otp()->digits(8)->expiry(20)->make($secret);
  6. Generate an OTP with generate()

    master

    Use the generate(string $key) method to create a new One-Time Password. The method generates a secure secret, stores it in the underlying cache repository with an expiration time, and returns the calculated OTP string. The $key is a unique identifier (e.g., a user ID or email) used to associate the OTP with a specific context.

    Note: You can also use the aliases make() or create() to perform the same action.

    $otp = $otpService->generate('user-id-123');
  7. Configure OTP expiry and digits

    master

    The Otp class allows you to fluently configure the lifespan and length of the generated codes using the following methods:

    • expiry(int|string $expiry): Sets the expiration time in minutes. For example, passing 10 sets the expiry to 600 seconds. This method returns $this for chaining.
    • digits(int|string $digits): Sets the number of digits for the generated OTP. This method returns $this for chaining.
    $otpService->expiry(5)->digits(6);
  8. Manually remove an OTP with forget()

    master

    Use the forget(string $key): bool method to manually invalidate and remove the OTP associated with a specific $key from the cache store.

    $otpService->forget('user-id-123');
  9. Verify an OTP with check()

    master

    Use the check(mixed $code, string $key): bool method to verify if a provided code matches the stored OTP for a given $key.

    • The $code can be a string or an integer.
    • If the code is valid, the method returns true and automatically removes (forgets) the OTP from the store to prevent reuse.
    • If the code is invalid or expired, it returns false.

    Note: You can also use the aliases match() or verify() to perform the same action.

    $isValid = $otpService->check('1234', 'user-id-123');
    
    if ($isValid) {
        // Proceed with verification
    }