2022-10-07 18:58:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Factories;
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Exceptions\EncryptedMigrationException;
|
|
|
|
use App\Exceptions\UnsupportedMigrationException;
|
2022-10-07 18:58:48 +02:00
|
|
|
use App\Services\Migrators\AegisMigrator;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Services\Migrators\GoogleAuthMigrator;
|
2022-10-07 18:58:48 +02:00
|
|
|
use App\Services\Migrators\Migrator;
|
|
|
|
use App\Services\Migrators\PlainTextMigrator;
|
2022-10-11 11:20:07 +02:00
|
|
|
use App\Services\Migrators\TwoFASMigrator;
|
2022-12-14 22:20:03 +01:00
|
|
|
use App\Services\Migrators\TwoFAuthMigrator;
|
2022-10-07 18:58:48 +02:00
|
|
|
use Illuminate\Support\Arr;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2022-10-07 18:58:48 +02:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
|
|
class MigratorFactory implements MigratorFactoryInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Infer the type of migrator needed from a payload and create the migrator
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The migration payload used to infer the migrator type
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
public function create(string $migrationPayload) : Migrator
|
2022-10-07 18:58:48 +02:00
|
|
|
{
|
2022-12-14 22:20:03 +01:00
|
|
|
if ($this->isTwoFAuthJSON($migrationPayload)) {
|
|
|
|
return App::make(TwoFAuthMigrator::class);
|
|
|
|
} elseif ($this->isAegisJSON($migrationPayload)) {
|
2022-10-07 18:58:48 +02:00
|
|
|
return App::make(AegisMigrator::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
} elseif ($this->is2FASv2($migrationPayload)) {
|
2022-10-11 11:20:07 +02:00
|
|
|
return App::make(TwoFASMigrator::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
} elseif ($this->isGoogleAuth($migrationPayload)) {
|
2022-10-07 18:58:48 +02:00
|
|
|
return App::make(GoogleAuthMigrator::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
} elseif ($this->isPlainText($migrationPayload)) {
|
2022-10-07 18:58:48 +02:00
|
|
|
return App::make(PlainTextMigrator::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
|
|
|
throw new UnsupportedMigrationException();
|
2022-10-07 18:58:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-12 11:13:13 +02:00
|
|
|
* Determine if a payload comes from Google Authenticator
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The payload to analyse
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
private function isGoogleAuth(string $migrationPayload) : bool
|
2022-10-07 18:58:48 +02:00
|
|
|
{
|
|
|
|
// - Google Auth migration URI : a string starting with otpauth-migration://offline?data= on a single line
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$lines = preg_split('~\R~', $migrationPayload, -1, PREG_SPLIT_NO_EMPTY);
|
2022-10-07 18:58:48 +02:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! $lines || count($lines) != 1) {
|
2022-10-07 18:58:48 +02:00
|
|
|
return false;
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|
2022-10-07 18:58:48 +02:00
|
|
|
|
|
|
|
return preg_match('/^otpauth-migration:\/\/offline\?data=.+$/', $lines[0]) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-12 11:13:13 +02:00
|
|
|
* Determine if a payload is a plain text content
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The payload to analyse
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
private function isPlainText(string $migrationPayload) : bool
|
2022-10-07 18:58:48 +02:00
|
|
|
{
|
2023-01-25 13:12:03 +01:00
|
|
|
// - Plain text : one or more otpauth URIs (otpauth://(hotp|totp|steam)/...), one per line
|
2022-10-07 18:58:48 +02:00
|
|
|
|
|
|
|
return Validator::make(
|
2022-11-22 15:15:52 +01:00
|
|
|
preg_split('~\R~', $migrationPayload, -1, PREG_SPLIT_NO_EMPTY),
|
2022-10-07 18:58:48 +02:00
|
|
|
[
|
2023-01-25 13:12:03 +01:00
|
|
|
// The regex rule must be embraced with brackets when it cointains a pipe
|
|
|
|
'*' => ['regex:/^otpauth:\/\/(?:steam|totp|hotp)\//i'],
|
2022-10-07 18:58:48 +02:00
|
|
|
]
|
|
|
|
)->passes();
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:20:03 +01:00
|
|
|
/**
|
|
|
|
* Determine if a payload comes from 2FAuth in JSON format
|
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The payload to analyse
|
2022-12-14 22:20:03 +01:00
|
|
|
*/
|
|
|
|
private function isTwoFAuthJSON(string $migrationPayload) : bool
|
|
|
|
{
|
|
|
|
$json = json_decode($migrationPayload, true);
|
|
|
|
|
|
|
|
if (Arr::has($json, 'schema') && (strpos(Arr::get($json, 'app'), '2fauth_') === 0)) {
|
|
|
|
return count(Validator::validate(
|
|
|
|
$json,
|
|
|
|
[
|
2023-06-07 17:47:14 +02:00
|
|
|
'data.*.otp_type' => 'present',
|
|
|
|
'data.*.service' => 'present',
|
|
|
|
'data.*.account' => 'present',
|
|
|
|
'data.*.secret' => 'present',
|
|
|
|
'data.*.digits' => 'present',
|
|
|
|
'data.*.algorithm' => 'present',
|
2022-12-14 22:20:03 +01:00
|
|
|
'data.*.period' => 'present',
|
|
|
|
'data.*.counter' => 'present',
|
|
|
|
]
|
|
|
|
)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
/**
|
2022-10-12 11:13:13 +02:00
|
|
|
* Determine if a payload comes from Aegis Authenticator in JSON format
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The payload to analyse
|
2022-10-12 11:13:13 +02:00
|
|
|
* @return bool
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
private function isAegisJSON(string $migrationPayload) : mixed
|
2022-10-07 18:58:48 +02:00
|
|
|
{
|
|
|
|
// - Aegis JSON : is a JSON object with the key db.entries full of objects like
|
|
|
|
// {
|
|
|
|
// "type": "totp",
|
|
|
|
// "uuid": "5be1c189-240d-5fe1-930b-a78xb669zd86",
|
|
|
|
// "name": "John DOE",
|
|
|
|
// "issuer": "Facebook",
|
|
|
|
// "note": "",
|
|
|
|
// "icon": null,
|
|
|
|
// "info": {
|
|
|
|
// "secret": "A4GRFTVVRBGY7UIW",
|
|
|
|
// "algo": "SHA1",
|
|
|
|
// "digits": 6,
|
|
|
|
// "period": 30
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
$json = json_decode($migrationPayload, true);
|
|
|
|
|
|
|
|
if (Arr::has($json, 'db')) {
|
|
|
|
if (is_string($json['db']) && is_array(Arr::get($json, 'header.slots'))) {
|
|
|
|
throw new EncryptedMigrationException();
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2022-10-07 18:58:48 +02:00
|
|
|
return count(Validator::validate(
|
|
|
|
$json,
|
|
|
|
[
|
2023-06-07 17:47:14 +02:00
|
|
|
'db.entries.*.type' => 'present',
|
|
|
|
'db.entries.*.name' => 'present',
|
|
|
|
'db.entries.*.issuer' => 'present',
|
|
|
|
'db.entries.*.info' => 'present',
|
2022-10-07 18:58:48 +02:00
|
|
|
]
|
2022-10-12 11:13:13 +02:00
|
|
|
)) > 0;
|
2022-10-07 18:58:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-11 11:20:07 +02:00
|
|
|
/**
|
2022-10-12 11:13:13 +02:00
|
|
|
* Determine if a payload comes from 2FAS Authenticator
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload The payload to analyse
|
2022-10-12 11:13:13 +02:00
|
|
|
* @return bool
|
2022-10-11 11:20:07 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
private function is2FASv2(string $migrationPayload) : mixed
|
2022-10-11 11:20:07 +02:00
|
|
|
{
|
2023-12-11 14:10:51 +01:00
|
|
|
// - 2FAS JSON : is a JSON object with a 'schemaVersion' key and a key 'services' full of objects like
|
2022-10-11 11:20:07 +02:00
|
|
|
// {
|
|
|
|
// "secret": "A4GRFTVVRBGY7UIW",
|
|
|
|
// ...
|
|
|
|
// "otp":
|
|
|
|
// {
|
|
|
|
// "account": "John DOE",
|
|
|
|
// "digits": 6,
|
|
|
|
// "counter": 0,
|
|
|
|
// "period": 30,
|
|
|
|
// "algorithm": "SHA1",
|
|
|
|
// "tokenType": "TOTP"
|
|
|
|
// },
|
|
|
|
// "type": "ManuallyAdded",
|
|
|
|
// "name": "Facebook",
|
|
|
|
// "icon":
|
|
|
|
// {
|
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
$json = json_decode($migrationPayload, true);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-12-11 14:10:51 +01:00
|
|
|
if (Arr::has($json, 'schemaVersion') && (Arr::has($json, 'services') || Arr::has($json, 'servicesEncrypted'))) {
|
2022-10-11 11:20:07 +02:00
|
|
|
if (Arr::has($json, 'servicesEncrypted')) {
|
|
|
|
throw new EncryptedMigrationException();
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2022-10-11 11:20:07 +02:00
|
|
|
return count(Validator::validate(
|
|
|
|
$json,
|
|
|
|
[
|
2023-06-07 17:47:14 +02:00
|
|
|
'services.*.secret' => 'present',
|
|
|
|
'services.*.name' => 'present',
|
|
|
|
'services.*.otp' => 'present',
|
2022-10-11 11:20:07 +02:00
|
|
|
]
|
2022-10-12 11:13:13 +02:00
|
|
|
)) > 0;
|
2022-10-11 11:20:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-07 18:58:48 +02:00
|
|
|
}
|