diff --git a/WebAuthn/WebAuthn.php b/WebAuthn/WebAuthn.php index 91aa0de..e64908b 100644 --- a/WebAuthn/WebAuthn.php +++ b/WebAuthn/WebAuthn.php @@ -122,9 +122,12 @@ public function prepareChallengeForRegistration($username, $userid, $crossPlatfo * @param string $userwebauthn the exisitng webauthn field for the user from your * database (it's actaully a JSON string, but that's entirely internal to * this code) + * @param string $device_name the optional device name given by the user during + * the registration. The device name can be request with a prompt (check + * the example for an implementation) * @return string modified to store in the user's webauthn field in your database */ - public function register($info, $userwebauthn) + public function register($info, $userwebauthn, $device_name = '') { if (! is_string($info)) { $this->oops('info must be a string', 1); @@ -192,7 +195,11 @@ public function register($info, $userwebauthn) $publicKey = (object)array(); $publicKey->key = $ao->attData->keyBytes; $publicKey->id = $info->rawId; - //log($publicKey->key); + $publicKey->datetime = date('c'); + + if(is_string($device_name) && trim($device_name) != '') { + $publicKey->dname = trim($device_name); + } if (empty($userwebauthn)) { $userwebauthn = [$publicKey]; diff --git a/example/index.php b/example/index.php index 9efe9f9..9435e8e 100644 --- a/example/index.php +++ b/example/index.php @@ -76,8 +76,12 @@ function getuser($username){ if (empty($_SESSION['username'])) { oops('username not set'); } $user = getuser($_SESSION['username']); + /* get a device name from the user */ + $device_name = (isset($_POST['dname']) && $_POST['dname'] != '') + ? $_POST['dname'] : ''; + /* The heart of the matter */ - $user->webauthnkeys = $webauthn->register($_POST['register'], $user->webauthnkeys); + $user->webauthnkeys = $webauthn->register($_POST['register'], $user->webauthnkeys, $device_name); /* Save the result to enable a challenge to be raised agains this newly created key in order to log in */ @@ -269,9 +273,10 @@ function getuser($username){ /* activate the key and get the response */ webauthnRegister(j.challenge, function(success, info){ if (success) { + var device_name = prompt('Provide a name for this device (e.g. "John\'s phone" o "Macbook Sandra")'); $.ajax({url: '/', method: 'POST', - data: {register: info}, + data: {register: info, dname: device_name}, dataType: 'json', success: function(j){ $('#iregisterform,#iregisterdokey').toggle();