Skip to content

Commit

Permalink
Make test pass
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Mar 25, 2024
1 parent 1968659 commit 31fc994
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 33 deletions.
53 changes: 50 additions & 3 deletions lib/Service/IdentifyMethod/AbstractIdentifyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ abstract class AbstractIdentifyMethod implements IIdentifyMethod {
protected string $codeSentByUser = '';
protected array $settings = [];
protected bool $willNotify = true;
/**
* @var string[]
*/
public array $availableSignatureMethods;
/**
* @var AbstractSignatureMethod[]
*/
Expand Down Expand Up @@ -89,11 +93,28 @@ public function signatureMethodsToArray(): array {
return array_map(function (AbstractSignatureMethod $method) {
return [
'label' => $method->friendlyName,
'name' => $method->getName(),
'enabled' => $method->isEnabled(),
];
}, $this->signatureMethods);
}

public function getEmptyInstanceOfSignatureMethodByName(string $name): AbstractSignatureMethod {
if (!in_array($name, $this->availableSignatureMethods)) {
throw new InvalidArgumentException(sprintf('%s is not a valid signature method of identify method %s', $name, $this->getName()));
}
$className = 'OCA\Libresign\Service\IdentifyMethod\\SignatureMethod\\' . ucfirst($name);
if (!class_exists($className)) {
throw new InvalidArgumentException('Invalid signature method. Set at identify method the list of available signature methdos with right values.');
}
$signatureMethod = clone \OC::$server->get($className);
$signatureMethod->cleanEntity();
return $signatureMethod;
}

/**
* @return AbstractSignatureMethod[]
*/
public function getSignatureMethods(): array {
return $this->signatureMethods;
}
Expand Down Expand Up @@ -298,10 +319,15 @@ private function loadSavedSettings(): void {
return;
}
foreach ($this->settings['signatureMethods'] as $method => $settings) {
$this->signatureMethods[$method]->setEntity($this->getEntity());
if (is_object($this->signatureMethods[$method]) && isset($settings['enabled']) && $settings['enabled']) {
$this->signatureMethods[$method]->enable();
if (!in_array($method, $this->availableSignatureMethods)) {
unset($this->settings['signatureMethods'][$method]);
continue;
}
$signatureMethod = $this->getEmptyInstanceOfSignatureMethodByName($method);
if (isset($settings['enabled']) && $settings['enabled']) {
$signatureMethod->enable();
}
$this->signatureMethods[$method] = $signatureMethod;
}
}

Expand Down Expand Up @@ -334,6 +360,27 @@ private function removeKeysThatDontExists(array $default): void {
}
}

/**
* @author Gajus Kuizinas <gk@anuary.com>
* @version 1.0.0 (2013 03 19)
*
* https://www.php.net/manual/en/function.array-diff-key.php#112558
*/
public function arrayDiffKeyRecursive(array $arr1, array $arr2) {
$diff = array_diff_key($arr1, $arr2);
$intersect = array_intersect_key($arr1, $arr2);
foreach ($intersect as $k => $v) {
if (is_array($arr1[$k]) && is_array($arr2[$k])) {
$d = $this->arrayDiffKeyRecursive($arr1[$k], $arr2[$k]);
if ($d) {
$diff[$k] = $d;
}
}
}

return $diff;
}

public function validateToRenew(?IUser $user = null): void {
$this->throwIfMaximumValidityExpired();
$this->throwIfAlreadySigned();
Expand Down
17 changes: 6 additions & 11 deletions lib/Service/IdentifyMethod/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ClickToSign;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\EmailToken;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\Password;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod;
use OCA\Libresign\Service\MailService;
use OCA\Libresign\Service\SessionService;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -43,6 +41,11 @@
use Psr\Log\LoggerInterface;

class Account extends AbstractIdentifyMethod {
public array $availableSignatureMethods = [
ISignatureMethod::SIGNATURE_METHOD_CLICK_TO_SIGN,
ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN,
ISignatureMethod::SIGNATURE_METHOD_PASSWORD,
];
public function __construct(
protected IdentifyService $identifyService,
private IUserManager $userManager,
Expand All @@ -56,17 +59,9 @@ public function __construct(
private LoggerInterface $logger,
private SessionService $sessionService,
private MailService $mail,
private Password $password,
private ClickToSign $clickToSign,
private EmailToken $emailToken,
) {
// TRANSLATORS Name of possible authenticator method. This signalize that the signer could be identified by Nextcloud acccount
$this->friendlyName = $this->identifyService->getL10n()->t('Account');
$this->signatureMethods = [
$this->password->getName() => $this->password,
$this->clickToSign->getName() => $this->clickToSign,
$this->emailToken->getName() => $this->emailToken,
];
parent::__construct(
$identifyService,
);
Expand Down
13 changes: 5 additions & 8 deletions lib/Service/IdentifyMethod/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,29 @@
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ClickToSign;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\EmailToken;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod;
use OCA\Libresign\Service\SessionService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IRootFolder;
use OCP\IUser;
use OCP\IUserSession;

class Email extends AbstractIdentifyMethod {
public array $availableSignatureMethods = [
ISignatureMethod::SIGNATURE_METHOD_CLICK_TO_SIGN,
ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN,
];
public function __construct(
protected IdentifyService $identifyService,
private IdentifyMethodMapper $identifyMethodMapper,
private IRootFolder $root,
private ITimeFactory $timeFactory,
private SessionService $sessionService,
private FileElementMapper $fileElementMapper,
private ClickToSign $clickToSign,
private EmailToken $emailToken,
private IUserSession $userSession,
) {
// TRANSLATORS Name of possible authenticator method. This signalize that the signer could be identified by email
$this->friendlyName = $this->identifyService->getL10n()->t('Email');
$this->signatureMethods = [
$this->clickToSign->getName() => $this->clickToSign,
$this->emailToken->getName() => $this->emailToken,
];
parent::__construct(
$identifyService,
);
Expand Down
4 changes: 1 addition & 3 deletions lib/Service/IdentifyMethod/IIdentifyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function setCodeSentByUser(string $code): void;
public function cleanEntity(): void;
public function setEntity(IdentifyMethod $entity): void;
public function getEntity(): IdentifyMethod;
/**
* @return AbstractSignatureMethod[]
*/
public function getEmptyInstanceOfSignatureMethodByName(string $name): AbstractSignatureMethod;
public function getSignatureMethods(): array;
public function signatureMethodsToArray(): array;
public function getSettings(): array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
namespace OCA\Libresign\Service\IdentifyMethod\SignatureMethod;

interface ISignatureMethod {
public const SIGNATURE_METHOD_CLICK_TO_SIGN = 'clickToSign';
public const SIGNATURE_METHOD_EMAIL_TOKEN = 'emailToken';
public const SIGNATURE_METHOD_PASSWORD = 'password';
public function enable(): void;
public function isEnabled(): bool;
public function toArray(): array;
Expand Down
7 changes: 3 additions & 4 deletions lib/Service/IdentifyMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,9 @@ public function getSignMethodsOfIdentifiedFactors(int $signRequestId): array {
if (empty($identifyMethod->getEntity()->getIdentifiedAtDate())) {
continue;
}
foreach ($identifyMethod->getSignatureMethods() as $signatureMethod) {
if (!$signatureMethod->isEnabled()) {
continue;
}
$signatureMethods = $identifyMethod->getSignatureMethods();
foreach ($signatureMethods as $signatureMethod) {
$signatureMethod->setEntity($identifyMethod->getEntity());
$return[$signatureMethod->getName()] = $signatureMethod->toArray();
}
}
Expand Down
9 changes: 5 additions & 4 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,13 @@ public function requestCode(
throw new LibresignException($this->l10n->t('Invalid identification method'));
}
foreach ($identifyMethods[$identifyMethodName] as $identifyMethod) {
$signatureMethods = $identifyMethod->getSignatureMethods();
if (empty($signatureMethods[$signMethodName])) {
throw new LibresignException($this->l10n->t('Invalid identification method'));
try {
$signatureMethod = $identifyMethod->getEmptyInstanceOfSignatureMethodByName($signMethodName);
$signatureMethod->setEntity($identifyMethod->getEntity());
} catch (InvalidArgumentException $th) {
continue;
}
/** @var EmailToken $signatureMethod */
$signatureMethod = $signatureMethods[$signMethodName];
$signatureMethod->requestCode($identify);
return;
}
Expand Down

0 comments on commit 31fc994

Please sign in to comment.