Ver Fonte

Fix #160 : Steam otpauth URIs are rejected by the Import feature

Bubka há 2 anos atrás
pai
commit
664bca3448

+ 3 - 2
app/Factories/MigratorFactory.php

@@ -67,12 +67,13 @@ class MigratorFactory implements MigratorFactoryInterface
      */
     private function isPlainText(string $migrationPayload) : bool
     {
-        // - Plain text : one or more otpauth URIs (otpauth://[t|h]otp/...), one per line
+        // - Plain text : one or more otpauth URIs (otpauth://(hotp|totp|steam)/...), one per line
 
         return Validator::make(
             preg_split('~\R~', $migrationPayload, -1 , PREG_SPLIT_NO_EMPTY),
             [
-                '*' => 'regex:/^otpauth:\/\/[h,t]otp\//i',
+                // The regex rule must be embraced with brackets when it cointains a pipe
+                '*' => ['regex:/^otpauth:\/\/(?:steam|totp|hotp)\//i'],
             ]
         )->passes();
     }

+ 2 - 1
app/Models/TwoFAccount.php

@@ -408,7 +408,7 @@ class TwoFAccount extends Model implements Sortable
     {
         // First we instanciate the OTP generator
         try {
-            $this->generator = Factory::loadFromProvisioningUri($uri);
+            $this->generator = Factory::loadFromProvisioningUri($isSteamTotp ? str_replace('otpauth://steam', 'otpauth://totp', $uri) : $uri);
         }
         catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
             throw ValidationException::withMessages([
@@ -459,6 +459,7 @@ class TwoFAccount extends Model implements Sortable
     private function enforceAsSteam() : void
     {
         $this->otp_type  = self::STEAM_TOTP;
+        $this->service   = 'Steam';
         $this->digits    = 5;
         $this->algorithm = self::SHA1;
         $this->period    = 30;

+ 2 - 2
app/Services/Migrators/PlainTextMigrator.php

@@ -23,7 +23,7 @@ class PlainTextMigrator extends Migrator
     {
         $otpauthURIs = preg_split('~\R~', $migrationPayload);
         $otpauthURIs = Arr::where($otpauthURIs, function ($value, $key) {
-            return Str::startsWith($value, ['otpauth://totp/', 'otpauth://hotp/']);
+            return Str::startsWith($value, ['otpauth://totp/', 'otpauth://hotp/', 'otpauth://steam/']);
         });
 
         if (count($otpauthURIs) < 1) {
@@ -35,7 +35,7 @@ class PlainTextMigrator extends Migrator
 
             try {
                $twofaccounts[$key] = new TwoFAccount;
-               $twofaccounts[$key]->fillWithURI($uri);
+               $twofaccounts[$key]->fillWithURI($uri, str_starts_with($uri, 'otpauth://steam/'));
             }
             catch (\Exception $exception) {