Skip to content

Commit

Permalink
feat(webauthn): Add user verification to webauthn challenges
Browse files Browse the repository at this point in the history
Require user verification if all tokens are registered

with UV flag, else discourage it

Signed-off-by: S1m <git@sgougeon.fr>

Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>

[skip ci]
  • Loading branch information
p1gp1g authored and backportbot[bot] committed Aug 15, 2024
1 parent 3b6d9eb commit e950200
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
27 changes: 27 additions & 0 deletions core/Migrations/Version30000Date20240815080800.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 S1m <git@sgougeon.fr>
* SPDX-FileCopyrightText: 2024 Richard Steinmetz <richard@steinmetz.cloud>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Migrations;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version30000Date20240815080800 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('webauthn');
$table->addColumn('user_verification', Types::BOOLEAN, ['notnull' => false, 'default' => false]);
return $schema;
}
}
9 changes: 7 additions & 2 deletions lib/private/Authentication/WebAuthn/CredentialRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCre
}, $entities);
}

public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null): PublicKeyCredentialEntity {
public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null, bool $userVerification = false): PublicKeyCredentialEntity {
$oldEntity = null;

try {
Expand All @@ -58,13 +58,18 @@ public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicK
$name = 'default';
}

$entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource);
$entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource, $userVerification);

if ($oldEntity) {
$entity->setId($oldEntity->getId());
if ($defaultName) {
$entity->setName($oldEntity->getName());
}

// Don't downgrade UV just because it was skipped during a login due to another key
if ($oldEntity->getUserVerification()) {
$entity->setUserVerification(true);
}
}

return $this->credentialMapper->insertOrUpdate($entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
* @method void setPublicKeyCredentialId(string $id);
* @method string getData();
* @method void setData(string $data);
*
* @since 30.0.0 Add userVerification attribute
* @method bool|null getUserVerification();
* @method void setUserVerification(bool $userVerification);
*/
class PublicKeyCredentialEntity extends Entity implements JsonSerializable {
/** @var string */
Expand All @@ -37,20 +41,25 @@ class PublicKeyCredentialEntity extends Entity implements JsonSerializable {
/** @var string */
protected $data;

/** @var bool|null */
protected $userVerification;

public function __construct() {
$this->addType('name', 'string');
$this->addType('uid', 'string');
$this->addType('publicKeyCredentialId', 'string');
$this->addType('data', 'string');
$this->addType('userVerification', 'boolean');
}

public static function fromPublicKeyCrendentialSource(string $name, PublicKeyCredentialSource $publicKeyCredentialSource): PublicKeyCredentialEntity {
public static function fromPublicKeyCrendentialSource(string $name, PublicKeyCredentialSource $publicKeyCredentialSource, bool $userVerification): PublicKeyCredentialEntity {
$publicKeyCredentialEntity = new self();

$publicKeyCredentialEntity->setName($name);
$publicKeyCredentialEntity->setUid($publicKeyCredentialSource->getUserHandle());
$publicKeyCredentialEntity->setPublicKeyCredentialId(base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()));
$publicKeyCredentialEntity->setData(json_encode($publicKeyCredentialSource));
$publicKeyCredentialEntity->setUserVerification($userVerification);

return $publicKeyCredentialEntity;
}
Expand Down
15 changes: 10 additions & 5 deletions lib/private/Authentication/WebAuthn/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function startRegistration(IUser $user, string $serverHost): PublicKeyCre
];

$authenticatorSelectionCriteria = new AuthenticatorSelectionCriteria(
null,
AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED,
AuthenticatorSelectionCriteria::AUTHENTICATOR_ATTACHMENT_NO_PREFERENCE,
AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_PREFERRED,
null,
false,
);
Expand Down Expand Up @@ -151,7 +151,8 @@ public function finishRegister(PublicKeyCredentialCreationOptions $publicKeyCred
}

// Persist the data
return $this->repository->saveAndReturnCredentialSource($publicKeyCredentialSource, $name);
$userVerification = $response->attestationObject->authData->isUserVerified();
return $this->repository->saveAndReturnCredentialSource($publicKeyCredentialSource, $name, $userVerification);
}

private function stripPort(string $serverHost): string {
Expand All @@ -160,7 +161,11 @@ private function stripPort(string $serverHost): string {

public function startAuthentication(string $uid, string $serverHost): PublicKeyCredentialRequestOptions {
// List of registered PublicKeyCredentialDescriptor classes associated to the user
$registeredPublicKeyCredentialDescriptors = array_map(function (PublicKeyCredentialEntity $entity) {
$userVerificationRequirement = AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_REQUIRED;
$registeredPublicKeyCredentialDescriptors = array_map(function (PublicKeyCredentialEntity $entity) use (&$userVerificationRequirement) {
if ($entity->getUserVerification() !== true) {
$userVerificationRequirement = AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED;
}
$credential = $entity->toPublicKeyCredentialSource();
return new PublicKeyCredentialDescriptor(
$credential->type,
Expand All @@ -173,7 +178,7 @@ public function startAuthentication(string $uid, string $serverHost): PublicKeyC
random_bytes(32), // Challenge
$this->stripPort($serverHost), // Relying Party ID
$registeredPublicKeyCredentialDescriptors, // Registered PublicKeyCredentialDescriptor classes
AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED,
$userVerificationRequirement,
60000, // Timeout
);
}
Expand Down

0 comments on commit e950200

Please sign in to comment.