Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ::create API, add ::createFromSecret and ::generate APIs #166

Merged
merged 6 commits into from Nov 5, 2022

Conversation

Slamdunk
Copy link
Contributor

Q A
Branch? v11
Bug fix? no
New feature? yes
Deprecations? yes
Tickets Fix #165
License MIT

@gitguardian
Copy link

gitguardian bot commented Oct 13, 2022

⚠️ GitGuardian has uncovered 13 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id Secret Commit Filename
- Generic High Entropy Secret 5275d3d tests/HOTPTest.php View secret
- Generic High Entropy Secret 5275d3d tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/HOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/TOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/TOTPTest.php View secret
- Generic High Entropy Secret c7135f4 tests/TOTPTest.php View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

@Spomky
Copy link
Member

Spomky commented Oct 19, 2022

Excellent. Many thanks.
By the way, I am wondering if it is necessary to have all these optional arguments in the named constructors:

int $period = 30,
string $digest = 'sha1',
int $digits = 6,
int $epoch = 0

It could be a good opportunity to refactor here and have explicit setters:

$otp = TOTP::createFromSecret($mySecret) // or ::generate()
    ->withPeriod(42)
    ->withDigits(12)
;

Note that the setters already exist (e.g. here), but do not return $this.
WDYT?

@Spomky Spomky added this to the V11 milestone Oct 19, 2022
@Slamdunk
Copy link
Contributor Author

$otp = TOTP::createFromSecret($mySecret) // or ::generate()
    ->withPeriod(42)
    ->withDigits(12)
;

Abolutely yes, but:

return $this.

Absolutely NO fluent interfaces please.
If we aim at that kind of factories, OTP should became @immutable just like
https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface

That means:

public function withPeriod(int $digits): static
{
    $new = clone $this;
    $new->digits = $digits;

    return $new;
}

I can propose a full PR here to:

  1. Add all the necessary new methods
  2. Deprecate existing ones in favour of the new with* variant

And after that you can branch off the new v12 were I can propose a PR to drop all deprecated methods and add the new magic @immutable, see PSalm and PHPStan notes

Ping me if you agree before I start the whole work

@Spomky
Copy link
Member

Spomky commented Oct 19, 2022

What Marco wrote is legit to me.
This is fine and still readable:

$otp = TOTP::createFromSecret($mySecret); // or ::generate()
$otp->setPeriod(42);
$otp->setDigits(12);

Keep It Simple. We can just drop the optional parameters from the static methods.

@Slamdunk
Copy link
Contributor Author

Done

@Spomky Spomky merged commit 9170b8e into Spomky-Labs:v11.0 Nov 5, 2022
@Spomky
Copy link
Member

Spomky commented Nov 5, 2022

Excellent 👌! Many thanks

@Spomky Spomky added this to the 11.1.0 milestone Nov 5, 2022
@Slamdunk Slamdunk deleted the misuse_resistant_factory_methods branch November 7, 2022 06:42
@Spomky Spomky mentioned this pull request Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OTPInterface::create(null|string $secret = null) is prone to misuse
2 participants