2021-09-17 23:55:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2021-09-17 23:55:09 +02:00
|
|
|
use App\Exceptions\InvalidSecretException;
|
|
|
|
use App\Exceptions\InvalidOtpParameterException;
|
2021-10-11 23:11:52 +02:00
|
|
|
use App\Exceptions\UndecipherableException;
|
2021-09-17 23:55:09 +02:00
|
|
|
use App\Services\Dto\OtpDto;
|
|
|
|
use App\Services\Dto\TwoFAccountDto;
|
|
|
|
use OTPHP\TOTP;
|
|
|
|
use OTPHP\HOTP;
|
|
|
|
use OTPHP\Factory;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-17 23:55:09 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
|
|
class TwoFAccountService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private $token;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private array $supportedOtpTypes = [
|
|
|
|
"OTPHP\TOTP" => "totp",
|
|
|
|
"OTPHP\HOTP" => "hotp"
|
|
|
|
];
|
|
|
|
|
|
|
|
private const IMAGELINK_STORAGE_PATH = 'imagesLink/';
|
|
|
|
private const ICON_STORAGE_PATH = 'public/icons/';
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//$this->token = $otpType === TOTP::create($secret) : HOTP::create($secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an account using an otpauth URI
|
|
|
|
*
|
|
|
|
* @param string $uri
|
|
|
|
* @param bool $saveToDB Whether or not the created account should be saved to DB
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @return \App\Models\TwoFAccount The created account
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
|
|
|
public function createFromUri(string $uri, bool $saveToDB = true ) : TwoFAccount
|
|
|
|
{
|
|
|
|
// Instanciate the token
|
|
|
|
$this->initTokenWith($uri);
|
|
|
|
|
|
|
|
// Create the account
|
|
|
|
$twofaccount = new TwoFAccount;
|
|
|
|
$twofaccount->legacy_uri = $uri;
|
|
|
|
$this->fillWithToken($twofaccount);
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
if ( $saveToDB ) {
|
|
|
|
$twofaccount->save();
|
|
|
|
|
|
|
|
Log::info(sprintf('TwoFAccount #%d created (from URI)', $twofaccount->id));
|
|
|
|
}
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
return $twofaccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an account using a list of parameters
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param bool $saveToDB Whether or not the created account should be saved to DB
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @return \App\Models\TwoFAccount The created account
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
|
|
|
public function createFromParameters(array $data, bool $saveToDB = true) : TwoFAccount
|
|
|
|
{
|
|
|
|
// Instanciate the token
|
|
|
|
$this->initTokenWith($data);
|
|
|
|
|
|
|
|
// Create and fill the account
|
|
|
|
$twofaccount = new TwoFAccount;
|
|
|
|
$twofaccount->legacy_uri = $this->token->getProvisioningUri();
|
|
|
|
$twofaccount->icon = Arr::get($data, 'icon', null);
|
|
|
|
$this->fillWithToken($twofaccount);
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
if ( $saveToDB ) {
|
|
|
|
$twofaccount->save();
|
|
|
|
|
|
|
|
Log::info(sprintf('TwoFAccount #%d created (from parameters)', $twofaccount->id));
|
|
|
|
}
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
return $twofaccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an account using a list of parameters
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount $twofaccount The account
|
2021-09-17 23:55:09 +02:00
|
|
|
* @param array $data The parameters
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @return \App\Models\TwoFAccount The updated account
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
|
|
|
public function update(TwoFAccount $twofaccount, array $data) : TwoFAccount
|
|
|
|
{
|
|
|
|
// Instanciate the token
|
|
|
|
$this->initTokenWith($data);
|
|
|
|
|
|
|
|
$this->fillWithToken($twofaccount);
|
|
|
|
$twofaccount->icon = Arr::get($data, 'icon', null);
|
|
|
|
$twofaccount->save();
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info(sprintf('TwoFAccount #%d updated', $twofaccount->id));
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
return $twofaccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a One-Time Password (with its parameters) for the specified account
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount|TwoFAccountDto|int|string $data Data defining an account
|
2021-09-17 23:55:09 +02:00
|
|
|
*
|
|
|
|
* @return OtpDto an OTP DTO
|
|
|
|
*
|
|
|
|
* @throws InvalidSecretException The secret is not a valid base32 encoded string
|
2021-10-11 23:11:52 +02:00
|
|
|
* @throws UndecipherableException The secret cannot be deciphered
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
|
|
|
public function getOTP($data) : OtpDto
|
|
|
|
{
|
|
|
|
$this->initTokenWith($data);
|
|
|
|
$OtpDto = new OtpDto();
|
2021-10-11 23:11:52 +02:00
|
|
|
|
|
|
|
// Early exit if the model returned an undecipherable secret
|
|
|
|
if (strtolower($this->token->getSecret()) === __('errors.indecipherable')) {
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::error('Secret cannot be deciphered, OTP generation aborted');
|
|
|
|
|
2021-10-11 23:11:52 +02:00
|
|
|
throw new UndecipherableException();
|
|
|
|
}
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if ( $this->tokenOtpType() === 'totp' ) {
|
|
|
|
|
|
|
|
$OtpDto->generated_at = time();
|
2021-10-05 23:53:50 +02:00
|
|
|
$OtpDto->otp_type = 'totp';
|
2021-09-17 23:55:09 +02:00
|
|
|
$OtpDto->password = $this->token->at($OtpDto->generated_at);
|
2021-10-08 00:28:41 +02:00
|
|
|
$OtpDto->period = $this->token->getParameter('period');
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
else if ( $this->tokenOtpType() === 'hotp' ) {
|
|
|
|
|
|
|
|
$counter = $this->token->getCounter();
|
2021-10-05 23:53:50 +02:00
|
|
|
$OtpDto->otp_type = 'hotp';
|
2021-09-17 23:55:09 +02:00
|
|
|
$OtpDto->password = $this->token->at($counter);
|
|
|
|
$OtpDto->counter = $counter + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
|
|
|
// Currently a secret issue is the only possible exception thrown by OTPHP for this stack
|
|
|
|
// so it is Ok to send the corresponding 2FAuth exception.
|
|
|
|
// If the token package change it could be necessary to throw a more generic exception.
|
|
|
|
throw new InvalidSecretException($ex->getMessage());
|
|
|
|
}
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info(sprintf('New %s generated', $OtpDto->otp_type));
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
return $OtpDto;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a generated otpauth URI for the specified account
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount|TwoFAccountDto|int $data Data defining an account
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
|
|
|
public function getURI($data) : string
|
|
|
|
{
|
|
|
|
$this->initTokenWith($data);
|
|
|
|
|
|
|
|
return $this->token->getProvisioningUri();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Withdraw one or more twofaccounts from their group
|
2021-09-17 23:55:09 +02:00
|
|
|
*
|
2021-10-08 23:21:07 +02:00
|
|
|
* @param int|array|string $ids twofaccount ids to free
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function withdraw($ids) : void
|
2021-09-17 23:55:09 +02:00
|
|
|
{
|
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 = $this->commaSeparatedToArray($ids);
|
2021-09-17 23:55:09 +02:00
|
|
|
|
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)
|
|
|
|
->update(
|
|
|
|
['group_id' => NULL]
|
|
|
|
);
|
|
|
|
|
|
|
|
Log::info(sprintf('TwoFAccounts #%s withdrawn', implode(',#', $ids)));
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-18 12:31:24 +02:00
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Delete one or more twofaccounts
|
|
|
|
*
|
2021-10-08 23:21:07 +02:00
|
|
|
* @param int|array|string $ids twofaccount ids to delete
|
2021-09-21 22:46:35 +02:00
|
|
|
*
|
|
|
|
* @return int The number of deleted
|
2021-09-18 12:31:24 +02:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function delete($ids) : int
|
2021-09-18 12:31:24 +02:00
|
|
|
{
|
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 = $this->commaSeparatedToArray($ids);
|
2021-09-21 22:46:35 +02:00
|
|
|
$deleted = TwoFAccount::destroy($ids);
|
|
|
|
|
|
|
|
return $deleted;
|
2021-09-18 12:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
// ########################################################################################################################
|
|
|
|
// ########################################################################################################################
|
|
|
|
// ########################################################################################################################
|
|
|
|
// ########################################################################################################################
|
|
|
|
|
2021-10-08 23:21:07 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function commaSeparatedToArray($ids)
|
|
|
|
{
|
2021-11-30 17:39:33 +01:00
|
|
|
if(is_string($ids))
|
|
|
|
{
|
|
|
|
$regex = "/^\d+(,{1}\d+)*$/";
|
|
|
|
if (preg_match($regex, $ids)) {
|
|
|
|
$ids = explode(',', $ids);
|
|
|
|
}
|
2021-10-08 23:21:07 +02:00
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2021-10-08 23:21:07 +02:00
|
|
|
return $ids;
|
|
|
|
}
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inits the Token
|
|
|
|
*/
|
|
|
|
private function initTokenWith($data) : void
|
|
|
|
{
|
|
|
|
// init with a TwoFAccount instance
|
2021-12-02 13:15:53 +01:00
|
|
|
if ( is_object($data) && get_class($data) === 'App\Models\TwoFAccount' ) {
|
2021-09-17 23:55:09 +02:00
|
|
|
$this->initTokenWithTwoFAccount($data);
|
|
|
|
}
|
|
|
|
// init with a TwoFAccountDto instance
|
|
|
|
else if ( is_object($data) && get_class($data) === 'App\Services\Dto\TwoFAccountDto' ) {
|
|
|
|
$this->initTokenWithParameters($data);
|
|
|
|
}
|
|
|
|
// init with an account ID
|
|
|
|
else if ( is_integer($data) ) {
|
|
|
|
// we should have an ID
|
|
|
|
$twofaccount = TwoFAccount::findOrFail($data);
|
|
|
|
$this->initTokenWithTwoFAccount($twofaccount);
|
|
|
|
}
|
|
|
|
// init with an array of property
|
|
|
|
else if( is_array($data) ) {
|
|
|
|
$dto = $this->mapArrayToDto($data);
|
|
|
|
$this->initTokenWithParameters($dto);
|
|
|
|
}
|
|
|
|
// or with a string that should be an otpauth URI
|
|
|
|
else {
|
|
|
|
$this->initTokenWithUri($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps array items to a TwoFAccountDto instance
|
|
|
|
*
|
|
|
|
* @param array $array The array to map
|
|
|
|
*
|
|
|
|
* @returns TwoFAccountDto
|
|
|
|
*/
|
|
|
|
private function mapArrayToDto($array) : TwoFAccountDto
|
|
|
|
{
|
|
|
|
$dto = new TwoFAccountDto();
|
|
|
|
|
2021-11-22 01:09:54 +01:00
|
|
|
try {
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$dto->$key = ! Arr::has($array, $key) ?: $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\TypeError $ex) {
|
|
|
|
throw new InvalidOtpParameterException($ex->getMessage());
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $dto;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instanciates the token with a TwoFAccount
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount $twofaccount
|
2021-09-17 23:55:09 +02:00
|
|
|
*
|
|
|
|
* @param bool $usingUri Whether or not the token should be fed with the account uri
|
|
|
|
*/
|
2021-11-22 01:13:09 +01:00
|
|
|
private function initTokenWithTwoFAccount(TwoFAccount $twofaccount) : void
|
2021-09-17 23:55:09 +02:00
|
|
|
{
|
2021-11-22 01:13:09 +01:00
|
|
|
$dto = new TwoFAccountDto();
|
2021-09-17 23:55:09 +02:00
|
|
|
|
2021-11-22 01:13:09 +01:00
|
|
|
$dto->otp_type = $twofaccount->otp_type;
|
|
|
|
$dto->account = $twofaccount->account;
|
|
|
|
$dto->service = $twofaccount->service;
|
|
|
|
$dto->icon = $twofaccount->icon;
|
|
|
|
$dto->secret = $twofaccount->secret;
|
|
|
|
$dto->algorithm = $twofaccount->algorithm;
|
|
|
|
$dto->digits = $twofaccount->digits;
|
2021-09-17 23:55:09 +02:00
|
|
|
|
2021-11-22 01:13:09 +01:00
|
|
|
if ( $twofaccount->period ) $dto->period = $twofaccount->period;
|
|
|
|
if ( $twofaccount->counter ) $dto->counter = $twofaccount->counter;
|
2021-09-17 23:55:09 +02:00
|
|
|
|
2021-11-22 01:13:09 +01:00
|
|
|
$this->initTokenWithParameters($dto);
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instanciates the token object by parsing an otpauth URI
|
|
|
|
*
|
|
|
|
* @throws ValidationException The URI is not a valid otpauth URI
|
|
|
|
*/
|
|
|
|
private function initTokenWithUri(string $uri) : void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->token = Factory::loadFromProvisioningUri($uri);
|
|
|
|
}
|
2021-10-15 23:46:21 +02:00
|
|
|
catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
2021-09-17 23:55:09 +02:00
|
|
|
throw ValidationException::withMessages([
|
|
|
|
'uri' => __('validation.custom.uri.regex', ['attribute' => 'uri'])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// As loadFromProvisioningUri() accept URI without label (nor account nor service) we check
|
|
|
|
// that the account is set
|
|
|
|
if ( ! $this->token->getLabel() ) {
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::error('URI passed to initTokenWithUri() must contain a label');
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
throw ValidationException::withMessages([
|
|
|
|
'label' => __('validation.custom.label.required')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instanciates the token object by passing a list of parameters
|
|
|
|
*
|
|
|
|
* @throws ValidationException otp type not supported
|
|
|
|
* @throws InvalidOtpParameterException invalid otp parameters
|
|
|
|
*/
|
|
|
|
private function initTokenWithParameters(TwoFAccountDto $dto) : void
|
|
|
|
{
|
|
|
|
// Check OTP type again to ensure the upcoming OTPHP instanciation
|
|
|
|
if ( ! in_array($dto->otp_type, $this->supportedOtpTypes, true) ) {
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::error(sprintf('%s is not an OTP type supported by the current token', $dto->otp_type));
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
throw ValidationException::withMessages([
|
|
|
|
'otp_type' => __('validation.custom.otp_type.in', ['attribute' => 'otp type'])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ( $dto->otp_type === 'totp' ) {
|
|
|
|
$this->token = TOTP::create(
|
2021-10-08 00:31:17 +02:00
|
|
|
$dto->secret
|
2021-09-17 23:55:09 +02:00
|
|
|
);
|
2021-10-08 00:31:17 +02:00
|
|
|
|
|
|
|
if ($dto->period) $this->token->setParameter('period', $dto->period);
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
else if ( $dto->otp_type === 'hotp' ) {
|
|
|
|
$this->token = HOTP::create(
|
2021-10-08 00:31:17 +02:00
|
|
|
$dto->secret
|
2021-09-17 23:55:09 +02:00
|
|
|
);
|
2021-10-08 00:31:17 +02:00
|
|
|
|
|
|
|
if ($dto->counter) $this->token->setParameter('counter', $dto->counter);
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:42:58 +02:00
|
|
|
if ($dto->algorithm) $this->token->setParameter('algorithm', $dto->algorithm);
|
2021-10-08 00:31:17 +02:00
|
|
|
if ($dto->digits) $this->token->setParameter('digits', $dto->digits);
|
|
|
|
// if ($dto->epoch) $this->token->setParameter('epoch', $dto->epoch);
|
2021-09-17 23:55:09 +02:00
|
|
|
if ($dto->service) $this->token->setIssuer($dto->service);
|
|
|
|
if ($dto->account) $this->token->setLabel($dto->account);
|
|
|
|
}
|
|
|
|
catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
|
|
|
throw new InvalidOtpParameterException($ex->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills a TwoFAccount with token's parameters
|
|
|
|
*/
|
|
|
|
private function fillWithToken(TwoFAccount &$twofaccount) : void
|
|
|
|
{
|
|
|
|
$twofaccount->otp_type = $this->tokenOtpType();
|
|
|
|
$twofaccount->account = $this->token->getLabel();
|
|
|
|
$twofaccount->secret = $this->token->getSecret();
|
|
|
|
$twofaccount->service = $this->token->getIssuer();
|
|
|
|
$twofaccount->algorithm = $this->token->getDigest();
|
|
|
|
$twofaccount->digits = $this->token->getDigits();
|
|
|
|
$twofaccount->period = $this->token->hasParameter('period') ? $this->token->getParameter('period') : null;
|
|
|
|
$twofaccount->counter = $this->token->hasParameter('counter') ? $this->token->getParameter('counter') : null;
|
|
|
|
|
|
|
|
if ( $this->token->hasParameter('image') ) {
|
|
|
|
$twofaccount->icon = $this->storeTokenImageAsIcon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the otp_type that matchs the token instance class
|
|
|
|
*/
|
|
|
|
private function tokenOtpType() : string
|
|
|
|
{
|
|
|
|
return $this->supportedOtpTypes[get_class($this->token)];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the image resource pointed by the token image parameter and store it as an icon
|
|
|
|
*
|
2021-10-08 23:35:33 +02:00
|
|
|
* @return string|null The filename of the stored icon or null if the operation fails
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
2021-10-08 23:35:33 +02:00
|
|
|
private function storeTokenImageAsIcon()
|
2021-09-17 23:55:09 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$remoteImageURL = $this->token->getParameter('image');
|
|
|
|
$path_parts = pathinfo($remoteImageURL);
|
|
|
|
$newFilename = Str::random(40) . '.' . $path_parts['extension'];
|
|
|
|
$imageFile = self::IMAGELINK_STORAGE_PATH . $newFilename;
|
|
|
|
$iconFile = self::ICON_STORAGE_PATH . $newFilename;
|
|
|
|
|
|
|
|
Storage::disk('local')->put($imageFile, file_get_contents($remoteImageURL));
|
|
|
|
|
|
|
|
if ( in_array(Storage::mimeType($imageFile), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp'])
|
|
|
|
&& getimagesize(storage_path() . '/app/' . $imageFile) )
|
|
|
|
{
|
|
|
|
// Should be a valid image
|
|
|
|
Storage::move($imageFile, $iconFile);
|
2021-10-15 23:46:21 +02:00
|
|
|
|
|
|
|
Log::info(sprintf('Icon file %s stored', $newFilename));
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-11-22 01:09:54 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-17 23:55:09 +02:00
|
|
|
Storage::delete($imageFile);
|
|
|
|
throw new \Exception;
|
2021-11-22 01:09:54 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $newFilename;
|
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-17 23:55:09 +02:00
|
|
|
catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
2021-10-08 23:35:33 +02:00
|
|
|
return null;
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
}
|