2FAuth/app/Services/TwoFAccountService.php

139 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Factories\MigratorFactoryInterface;
use App\Helpers\Helpers;
2023-02-25 21:12:10 +01:00
use App\Models\TwoFAccount;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
2022-11-22 15:15:52 +01:00
use Illuminate\Support\Facades\Log;
class TwoFAccountService
{
/**
2022-11-22 15:15:52 +01:00
* @var MigratorFactoryInterface The Migration service
*/
protected $migratorFactory;
/**
* Constructor
*/
public function __construct(MigratorFactoryInterface $migratorFactory)
{
$this->migratorFactory = $migratorFactory;
}
/**
* Withdraw one or more twofaccounts from their group
2022-11-22 15:15:52 +01:00
*
2024-04-20 19:03:44 +02:00
* @param int|array|string $ids twofaccount ids to free
*/
public static function withdraw($ids) : void
{
2021-10-08 23:21:07 +02:00
// $ids as string could be a comma-separated list of ids
// so in this case we explode the string to an array
$ids = Helpers::commaSeparatedToArray($ids);
2021-10-08 23:21:07 +02:00
// whereIn() expects an array
$ids = is_array($ids) ? $ids : func_get_args();
2021-11-30 17:39:33 +01:00
TwoFAccount::whereIn('id', $ids)
2023-12-20 16:55:58 +01:00
->update(
['group_id' => null]
);
2022-11-22 15:15:52 +01:00
Log::info(sprintf('TwoFAccounts IDs #%s withdrawn', implode(',', $ids)));
}
/**
* Convert a migration payload to a set of TwoFAccount objects
2022-11-22 15:15:52 +01:00
*
2024-04-20 19:03:44 +02:00
* @param string $migrationPayload Migration payload from 2FA apps export feature
2022-11-21 11:16:43 +01:00
* @return \Illuminate\Support\Collection<int|string, TwoFAccount> The converted accounts
*/
public function migrate(string $migrationPayload) : Collection
{
2022-11-22 15:15:52 +01:00
$migrator = $this->migratorFactory->create($migrationPayload);
$twofaccounts = $migrator->migrate($migrationPayload);
return self::markAsDuplicate($twofaccounts);
}
/**
* Export one or more twofaccounts
*
2024-04-20 19:03:44 +02:00
* @param int|array|string $ids twofaccount ids to delete
* @return \Illuminate\Support\Collection<int, TwoFAccount> The converted accounts
*/
public static function export($ids) : Collection
{
2023-03-10 22:59:46 +01:00
$ids = Helpers::commaSeparatedToArray($ids);
$ids = is_array($ids) ? $ids : func_get_args();
2023-03-10 22:59:46 +01:00
$twofaccounts = TwoFAccount::whereIn('id', $ids)->get();
return $twofaccounts;
}
/**
* Delete one or more twofaccounts
2022-11-22 15:15:52 +01:00
*
2024-04-20 19:03:44 +02:00
* @param int|array|string $ids twofaccount ids to delete
* @return int The number of deleted
*/
public static function delete($ids) : int
{
2021-10-08 23:21:07 +02:00
// $ids as string could be a comma-separated list of ids
// so in this case we explode the string to an array
$ids = Helpers::commaSeparatedToArray($ids);
2022-11-22 15:15:52 +01:00
Log::info(sprintf('Deletion of TwoFAccounts #%s requested', is_array($ids) ? implode(',#', $ids) : $ids));
$deleted = TwoFAccount::destroy($ids);
return $deleted;
}
/**
* Set owner of given twofaccounts
2023-12-20 16:55:58 +01:00
*
* @param \Illuminate\Support\Collection<int, TwoFAccount> $twofaccounts
*/
public static function setUser(Collection $twofaccounts, User $user) : void
{
$twofaccounts->each(function ($twofaccount, $key) use ($user) {
$twofaccount->user_id = $user->id;
$twofaccount->save();
});
}
/**
* Return the given collection with items marked as Duplicates (using id=-1) if similar records exist
* in the authenticated user accounts
2022-11-22 15:15:52 +01:00
*
* @param \Illuminate\Support\Collection<int|string, TwoFAccount> $twofaccounts
2022-11-21 11:16:43 +01:00
* @return \Illuminate\Support\Collection<int|string, TwoFAccount>
*/
private static function markAsDuplicate(Collection $twofaccounts) : Collection
{
$userTwofaccounts = Auth::user()->twofaccounts;
$twofaccounts = $twofaccounts->map(function ($twofaccount, $key) use ($userTwofaccounts) {
if ($userTwofaccounts->contains(function ($value, $key) use ($twofaccount) {
return $value->secret == $twofaccount->secret
&& $value->service == $twofaccount->service
&& $value->account == $twofaccount->account
&& $value->otp_type == $twofaccount->otp_type
&& $value->digits == $twofaccount->digits
&& $value->algorithm == $twofaccount->algorithm;
})) {
2022-10-10 13:44:12 +02:00
$twofaccount->id = TwoFAccount::DUPLICATE_ID;
}
return $twofaccount;
});
return $twofaccounts;
}
2022-11-22 15:15:52 +01:00
}