Spomky-Labs / otphp

:closed_lock_with_key: A PHP library for generating one time passwords according to RFC 4226 (HOTP) and the RFC 6238 (TOTP)
MIT License
1.31k stars 150 forks source link

OTPHP not being loaded via autoload #200

Closed whitsey closed 11 months ago

whitsey commented 11 months ago

Version(s) affected

10.0.3

Description

Load OTPHP using autoload.php

configuration.php:

require ROOT_DIR . "vendor/autoload.php"; // (This works because it is loading other libraries e.g. PHPMailer

profile.manage.php:

include('../configuration.php');

$user = new User();
print json_encode( $user->getUserHTML() );

class.user.php:

use OTPHP\TOTP;

class User {
  public function getUserHTML() {

    $otp = TOTP::generate();
    echo "The OTP secret is: {$otp->getSecret()}\n";

  }
}

REESULT: PHP Fatal error: Uncaught Error: Call to undefined method OTPHP\TOTP::generate() in class.user.php:582

How to reproduce

As above

Possible Solution

Make the autoloaded classes available inside the class function

I notice, if I output the autoload in configuration.php I get the AutoLoad\ClassLoader Object with the OTPHP package present. BUT, when I output the autoload in class User I get null/empty string...

Additional Context

I know autoload is autoloading because I am using a number of other packages which are autoloaded.

Spomky commented 11 months ago

Hi,

Actually it is loaded. The problem is the way you instantiate the object and PHP is telling it to you Call to undefined method OTPHP\TOTP::generate(). generate is available for version 11.0+ and not 10.x. The documentation for this version is available here: https://github.com/Spomky-Labs/otphp/blob/10.0.x/doc/index.md

whitsey commented 11 months ago

Oh. Ok. I just followed the docs link from packagist which lead here - https://github.com/Spomky-Labs/otphp/blob/HEAD/doc/index.md I followed that guide but looks like composer require spomky-labs/otphp downloaded v10 (the wrong version).

Thanks for prompt response.