<?php

namespace App\Lib;

use Exception;

class GoogleAuthenticator
{
    protected int $_codeLength = 6;

    /**
     * @throws Exception
     */
    public function createSecret($secretLength = 16): string
    {
        $validChars = $this->_getBase32LookupTable();

        // Valid secret lengths are 80 to 640 bits
        if ($secretLength < 16 || $secretLength > 128) throw new Exception('Bad secret length');

        $secret = '';
        $rnd    = false;

        if (function_exists('random_bytes')) {
            $rnd = random_bytes($secretLength);
        } elseif (function_exists('mcrypt_create_iv')) {
            $rnd = mcrypt_create_iv($secretLength, MCRYPT_DEV_URANDOM);
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
            $rnd = openssl_random_pseudo_bytes($secretLength, $cryptoStrong);

            if (!$cryptoStrong) $rnd = false;
        }

        if ($rnd !== false) {
            for ($i = 0; $i < $secretLength; ++$i) {
                $secret .= $validChars[ord($rnd[$i]) & 31];
            }
        } else {
            throw new Exception('No source of secure random');
        }

        return $secret;
    }

    public function getCode($secret, $timeSlice = null): string
    {
        if ($timeSlice === null) $timeSlice = floor(time() / 30);

        $secretKey = $this->_base32Decode($secret);

        // Pack time into binary string
        $time = chr(0) . chr(0) . chr(0) . chr(0) . pack('N*', $timeSlice);

        // Hash it with users secret key
        $hm = hash_hmac('SHA1', $time, $secretKey, true);

        // Use last nipple of result as index/offset
        $offset = ord(substr($hm, -1)) & 0x0F;

        // grab 4 bytes of the result
        $hashPart = substr($hm, $offset, 4);

        // Unpack binary value
        $value = unpack('N', $hashPart);
        $value = $value[1];

        // Only 32 bits
        $value  = $value & 0x7FFFFFFF;
        $modulo = pow(10, $this->_codeLength);

        return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
    }

    /**
     * Get QR-Code URL for image, from google charts.
     *
     * @param string $name
     * @param string $secret
     * @param string|null $title
     * @param array $params
     *
     * @return string
     */
    public function getQRCodeGoogleUrl(string $name, string $secret, string $title = null, array $params = array()): string
    {
        $width  = !empty($params['width']) && (int)$params['width'] > 0 ? (int)$params['width'] : 200;
        $height = !empty($params['height']) && (int)$params['height'] > 0 ? (int)$params['height'] : 200;

        $urlencoded = urlencode('otpauth://totp/' . $name . '?secret=' . $secret);

        if (isset($title)) $urlencoded .= urlencode('&issuer=' . urlencode($title));

        return 'https://api.qrserver.com/v1/create-qr-code/?size=' . $width . 'x' . $height . '&data=' . $urlencoded;
    }

    public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null): bool
    {
        if ($currentTimeSlice === null) $currentTimeSlice = floor(time() / 30);

        if (strlen($code) != 6) return false;

        for ($i = -$discrepancy; $i <= $discrepancy; ++$i) {
            $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);

            if ($this->timingSafeEquals($calculatedCode, $code)) return true;
        }

        return false;
    }

    public function setCodeLength($length): static
    {
        $this->_codeLength = $length;

        return $this;
    }

    protected function _base32Decode($secret): bool|string
    {
        if (empty($secret)) return '';

        $base32chars        = $this->_getBase32LookupTable();
        $base32charsFlipped = array_flip($base32chars);

        $paddingCharCount = substr_count($secret, $base32chars[32]);
        $allowedValues    = array(6, 4, 3, 1, 0);

        if (!in_array($paddingCharCount, $allowedValues)) return false;

        for ($i = 0; $i < 4; ++$i) {
            if (
                $paddingCharCount == $allowedValues[$i] &&
                substr($secret, - ($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])
            ) {
                return false;
            }
        }

        $secret       = str_replace('=', '', $secret);
        $secret       = str_split($secret);
        $binaryString = '';

        for ($i = 0; $i < count($secret); $i += 8) {
            $x = '';

            if (!in_array($secret[$i], $base32chars)) return false;

            for ($j = 0; $j < 8; ++$j) {
                $currentChar = $secret[$i + $j] ?? null;
                $charValue   = $base32charsFlipped[$currentChar] ?? null;

                if ($charValue === null) return false;

                $x .= str_pad(base_convert($charValue, 10, 2), 5, '0', STR_PAD_LEFT);
            }

            $eightBits = str_split($x, 8);

            for ($z = 0; $z < count($eightBits); ++$z) {
                $binaryString .= (($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48) ? $y : '';
            }
        }

        return $binaryString;
    }

    protected function _getBase32LookupTable(): array
    {
        return array(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //  7
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
            'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
            '=',  // padding char
        );
    }

    private function timingSafeEquals($safeString, $userString): bool
    {
        if (function_exists('hash_equals')) return hash_equals($safeString, $userString);

        $safeLen = strlen($safeString);
        $userLen = strlen($userString);

        if ($userLen != $safeLen) return false;

        $result = 0;

        for ($i = 0; $i < $userLen; ++$i) $result |= (ord($safeString[$i]) ^ ord($userString[$i]));

        // They are only identical strings if $result is exactly 0...
        return $result === 0;
    }
}
