greatcode/otp-reader

OTP Reader - Thread-safe, atomic, file-based Google OAuth credential storage library in native PHP with Mail Reader & Gmail Driver

Maintainers

Package info

github.com/gcteamsource/otp-reader

pkg:composer/greatcode/otp-reader

Transparency log

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.0.2 2026-07-31 02:11 UTC

This package is auto-updated.

Last update: 2026-07-31 02:11:27 UTC


README

PHP Version License: MIT PSR-12 Strict Types

An enterprise-grade, thread-safe, native PHP library designed for automated mail reading, OTP code extraction in web scraping, and atomic Google OAuth credential management.

Engineered specifically for high-concurrency production environments (PHP-FPM, Docker Replicas, Multi-Worker Scraping Pipelines) on shared Linux filesystems without requiring database dependencies.

๐ŸŒŸ Key Features

  • ๐Ÿ”’ Thread-Safe & Race-Condition Proof: Uses flock(LOCK_EX) on persistent .lock files to prevent concurrent workers or containers from triggering duplicate token refreshes simultaneously.
  • โšก Atomic Storage: File writes use temporary file buffering (.tmp -> fflush() -> rename()), ensuring JSON files are never corrupted even during sudden process termination or power loss.
  • ๐Ÿ›ก๏ธ Automated Corruption Recovery: Automatically recovers stored credentials from .bak backup files if primary JSON files are corrupted.
  • ๐Ÿ“ง Gmail REST API v1 Driver: Native cURL HTTP transport wrapper for Gmail APIโ€”no heavy SDK dependencies required.
  • ๐Ÿ”‘ Automatic OTP Extraction: Built-in heuristic engine for extracting 4โ€“8 digit or alphanumeric verification codes from both English and Indonesian email payloads.
  • โฑ๏ธ Smart Polling Retry & Delay: Retries message polling with customizable attempt limits and delay intervals until the email arrives.
  • ๐Ÿ“ซ Auto Mark-as-Read: Automatically marks processed emails as READ upon successfully retrieving OTP codes to prevent re-reading stale messages.
  • ๐ŸŽจ Glassmorphism Web UI: Includes a single-line Web Registration handler (MailReader::handleRegistration()) with a responsive, modern HTML interface.
  • 0๏ธโƒฃ Zero Framework Dependencies: Written in pure, strict-typed PHP 8.1+ under the Greatcode\OtpReader\ namespace adhering to PSR-12 standard.

๐Ÿ—๏ธ Architecture Overview

graph TD
    A["Scraping Script / Web App"] --> B["Greatcode\OtpReader\Mail\MailReader Facade"]
    B --> C["Greatcode\OtpReader\Mail\Drivers\GmailDriver"]
    C --> D["Greatcode\OtpReader\Google\CredentialManager"]
    D --> E["Greatcode\OtpReader\Google\CredentialLock (flock LOCK_EX)"]
    D --> F["Greatcode\OtpReader\Google\CredentialStorage"]
    F --> G[("JSON Filesystem /storage/google/")]
    C --> H["Gmail REST API v1"]
Loading

๐Ÿ“ฆ Installation

Install via Composer:

composer require greatcode/otp-reader

๐Ÿš€ Quick Start Guide

1. Read OTP for Web Scraping Scripts (1-Line Initialization)

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Greatcode\OtpReader\Mail\MailReader;

// 1. Initialize MailReader under Greatcode\OtpReader namespace in 1 line
$mailReader = MailReader::createGmail(
    storageDirectory: __DIR__ . '/storage/google',
    clientId: getenv('GOOGLE_CLIENT_ID'),
    clientSecret: getenv('GOOGLE_CLIENT_SECRET')
);

// 2. Poll & extract OTP in 1 line
// Automatically retries 5 times with 2s delay, filters last 5 mins, and marks email as READ
$otp = $mailReader->getLatestOtp(
    email: 'user@gmail.com',
    from: 'tokopedia',
    afterTime: time() - 300
);

if ($otp !== null) {
    echo "โœ… Extracted OTP: " . $otp;
} else {
    echo "โŒ OTP not received within timeout.";
}

2. Custom Dynamic Body Parser Callable

If your email uses custom verification token formatting (e.g. [AUTH-992810]), pass a custom callable parser:

use Greatcode\OtpReader\Mail\EmailMessage;

$token = $mailReader->getLatestOtp(
    email: 'user@gmail.com',
    from: 'service.com',
    afterTime: '10 minutes ago',
    parser: function (string $body, EmailMessage $message): ?string {
        if (preg_match('/\[AUTH-(\d{6})\]/', $body, $matches) === 1) {
            return $matches[1];
        }
        return null;
    },
    maxAttempts: 10,
    delaySeconds: 3
);

3. One-Line Web Account Registration UI (register.php)

Serve a self-contained, responsive Glassmorphism Web UI for registering accounts via browser in a single line of code:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Greatcode\OtpReader\Mail\MailReader;

// Serves HTML page, handles form submissions, and saves OAuth refresh tokens
MailReader::handleRegistration(
    storageDirectory: __DIR__ . '/storage/google',
    clientId: getenv('GOOGLE_CLIENT_ID'),
    clientSecret: getenv('GOOGLE_CLIENT_SECRET')
);

Run local PHP server to test:

php -S localhost:8080 examples/register_web.php

Open browser at http://localhost:8080 to access the registration UI.

๐Ÿ“– API Reference

Greatcode\OtpReader\Mail\MailReader

Method Description
createGmail($storageDir, $clientId, $clientSecret, $httpHandler = null) One-line static factory creating MailReader with Gmail Driver.
registerAccount($email, $refreshToken) Registers/updates an account's OAuth refresh token.
getLatestOtp($email, $from, $afterTime, $parser, $maxAttempts, $delaySeconds, $autoMarkAsRead) Polls, extracts OTP, retries with delay, and auto-marks message as read.
markAsRead($email, $messageId) Marks a specific email message as read in Gmail.
handleRegistration($storageDir, $clientId, $clientSecret) Static helper for handling web HTTP requests and rendering HTML UI.

๐Ÿ› ๏ธ Exception Hierarchy

All library exceptions extend from base exception classes:

Greatcode\OtpReader\Google\Exceptions\CredentialException
 โ”œโ”€โ”€ CredentialNotFoundException
 โ”œโ”€โ”€ CredentialCorruptedException
 โ”œโ”€โ”€ CredentialLockException
 โ”œโ”€โ”€ CredentialRefreshException
 โ””โ”€โ”€ CredentialStorageException

Greatcode\OtpReader\Mail\Exceptions\MailReaderException

๐Ÿงช Testing

Run full PHPUnit test suite:

vendor/bin/phpunit

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.