mirror of
https://github.com/nextcloud/server.git
synced 2026-04-20 22:00:39 -04:00
feat: Add option to update the public key of a user
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
This commit is contained in:
parent
5b92f58734
commit
234f183f8e
3 changed files with 54 additions and 3 deletions
|
|
@ -15,6 +15,7 @@ use OCP\IUserManager;
|
|||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Verify extends Command {
|
||||
|
|
@ -34,6 +35,12 @@ class Verify extends Command {
|
|||
InputArgument::REQUIRED,
|
||||
'User ID of the user to verify'
|
||||
)
|
||||
->addOption(
|
||||
'update',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Save the derived public key to match the private key'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
@ -72,12 +79,17 @@ class Verify extends Command {
|
|||
$output->writeln($publicKeyDerived);
|
||||
|
||||
if ($publicKey != $publicKeyDerived) {
|
||||
$output->writeln('<error>Stored public key does not match stored private key</error>');
|
||||
return static::FAILURE;
|
||||
if (!$input->getOption('update')) {
|
||||
$output->writeln('<error>Stored public key does not match stored private key</error>');
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
$this->keyManager->setPublicKey($user, $publicKeyDerived);
|
||||
$output->writeln('<info>Derived public key did not match, successfully updated</info>');
|
||||
return static::SUCCESS;
|
||||
}
|
||||
|
||||
$output->writeln('<info>Stored public key matches stored private key</info>');
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,6 +135,18 @@ class Manager {
|
|||
return $this->retrieveKey('user-' . $uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set public key for $user
|
||||
*/
|
||||
public function setPublicKey(IUser $user, string $publicKey): void {
|
||||
$id = 'user-' . $user->getUID();
|
||||
|
||||
$folder = $this->appData->getFolder($id);
|
||||
$folder->newFile('public', $publicKey);
|
||||
|
||||
$this->cache->set($id . '-public', $publicKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance wide public and private key
|
||||
*
|
||||
|
|
|
|||
|
|
@ -153,6 +153,33 @@ class ManagerTest extends TestCase {
|
|||
$this->assertEquals($expected, $this->manager->getKey($user));
|
||||
}
|
||||
|
||||
public function testSetPublicKey(): void {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user
|
||||
->expects($this->exactly(1))
|
||||
->method('getUID')
|
||||
->willReturn('MyUid');
|
||||
$publicFile = $this->createMock(ISimpleFile::class);
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$folder
|
||||
->expects($this->once())
|
||||
->method('newFile')
|
||||
->willReturnMap([
|
||||
['public', 'MyNewPublicKey', $publicFile],
|
||||
]);
|
||||
$this->appData
|
||||
->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('user-MyUid')
|
||||
->willReturn($folder);
|
||||
$this->cache
|
||||
->expects($this->once())
|
||||
->method('set')
|
||||
->with('user-MyUid-public', 'MyNewPublicKey');
|
||||
|
||||
$this->manager->setPublicKey($user, 'MyNewPublicKey');
|
||||
}
|
||||
|
||||
public function testGetKeyWithNotExistingKey(): void {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user
|
||||
|
|
|
|||
Loading…
Reference in a new issue