Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JS] feat: Added virtual authenticator
Relates #10541
- Loading branch information
1 parent
97aeedb
commit 0a387e2
Showing
10 changed files
with
1,309 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Virtual Authenticator Tests</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h1>Virtual Authenticator Tests</h1> | ||
|
||
<script> | ||
async function registerCredential(options={}) { | ||
options=Object.assign({ | ||
authenticatorSelection: { | ||
requireResidentKey: false, | ||
// userVerification: 'preferred' | ||
}, | ||
rp: { | ||
id: "localhost", | ||
name: "Selenium WebDriver Test", | ||
}, | ||
challenge: Int8Array.from("challenge"), | ||
pubKeyCredParams: [ | ||
{type: "public-key",alg: -7}, | ||
], | ||
user: { | ||
name: "name", | ||
displayName: "displayName", | ||
id: Int8Array.from([1]), | ||
}, | ||
},options); | ||
try { | ||
const credential=await navigator.credentials.create({publicKey: options}); | ||
return { | ||
status: "OK", | ||
credential: { | ||
id: credential.id, | ||
rawId: Array.from(new Uint8Array(credential.rawId)), | ||
transports: credential.response.getTransports(), | ||
} | ||
}; | ||
} catch(error) { | ||
return {status: error.toString()}; | ||
} | ||
} | ||
|
||
async function getCredential(credentials,options={}) { | ||
options=Object.assign({ | ||
challenge: Int8Array.from("Winter is Coming"), | ||
rpId: "localhost", | ||
allowCredentials: credentials, | ||
userVerification: "preferred", | ||
},options); | ||
try { | ||
const attestation=await navigator.credentials.get({publicKey: options}); | ||
return { | ||
status: "OK", | ||
attestation: { | ||
userHandle: new Uint8Array(attestation.response.userHandle), | ||
}, | ||
}; | ||
} catch(error) { | ||
return {status: error.toString()}; | ||
} | ||
} | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
javascript/node/selenium-webdriver/lib/virtual_authenticator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
'use strict' | ||
|
||
/** | ||
* Options for the creation of virtual authenticators. | ||
* @see http://w3c.github.io/webauthn/#sctn-automation | ||
*/ | ||
class VirtualAuthenticatorOptions { | ||
|
||
static Protocol = { | ||
"CTAP2": 'ctap2', | ||
"U2F": 'ctap1/u2f', | ||
} | ||
|
||
static Transport = { | ||
"BLE": 'ble', | ||
"USB": 'usb', | ||
"NFC": 'nfc', | ||
"INTERNAL": 'internal', | ||
} | ||
|
||
/** | ||
* Constructor to initialise VirtualAuthenticatorOptions object. | ||
*/ | ||
constructor() { | ||
this._protocol = VirtualAuthenticatorOptions.Protocol["CTAP2"] | ||
this._transport = VirtualAuthenticatorOptions.Transport["USB"] | ||
this._hasResidentKey = false | ||
this._hasUserVerification = false | ||
this._isUserConsenting = true | ||
this._isUserVerified = false | ||
} | ||
|
||
getProtocol() { | ||
return this._protocol | ||
} | ||
|
||
setProtocol(protocol) { | ||
this._protocol = protocol | ||
} | ||
|
||
getTransport() { | ||
return this._transport | ||
} | ||
|
||
setTransport(transport) { | ||
this._transport = transport | ||
} | ||
|
||
getHasResidentKey() { | ||
return this._hasResidentKey | ||
} | ||
|
||
setHasResidentKey(value) { | ||
this._hasResidentKey = value | ||
} | ||
|
||
getHasUserVerification() { | ||
return this._hasUserVerification | ||
} | ||
|
||
setHasUserVerification(value) { | ||
this._hasUserVerification = value | ||
} | ||
|
||
getIsUserConsenting() { | ||
return this._isUserConsenting | ||
} | ||
|
||
setIsUserConsenting(value) { | ||
this._isUserConsenting = value | ||
} | ||
|
||
getIsUserVerified() { | ||
return this._isUserVerified | ||
} | ||
|
||
setIsUserVerified(value) { | ||
this._isUserVerified = value | ||
} | ||
|
||
toDict() { | ||
return { | ||
"protocol": this.getProtocol(), | ||
"transport": this.getTransport(), | ||
"hasResidentKey": this.getHasResidentKey(), | ||
"hasUserVerification": this.getHasUserVerification(), | ||
"isUserConsenting": this.getIsUserConsenting(), | ||
"isUserVerified": this.getIsUserVerified(), | ||
|
||
} | ||
} | ||
} | ||
|
||
/** | ||
* A credential stored in a virtual authenticator. | ||
* @see https://w3c.github.io/webauthn/#credential-parameters | ||
*/ | ||
class Credential { | ||
constructor( | ||
credentialId, | ||
isResidentCredential, | ||
rpId, | ||
userHandle, | ||
privateKey, | ||
signCount | ||
) { | ||
this._id = credentialId | ||
this._isResidentCredential = isResidentCredential | ||
this._rpId = rpId | ||
this._userHandle = userHandle | ||
this._privateKey = privateKey | ||
this._signCount = signCount | ||
} | ||
|
||
id() { | ||
return this._id | ||
} | ||
|
||
isResidentCredential() { | ||
return this._isResidentCredential | ||
} | ||
|
||
rpId() { | ||
return this._rpId | ||
} | ||
|
||
userHandle() { | ||
if (this._userHandle != null) { | ||
return this._userHandle | ||
} | ||
return null | ||
} | ||
|
||
privateKey() { | ||
return this._privateKey | ||
} | ||
|
||
signCount() { | ||
return this._signCount | ||
} | ||
|
||
/** | ||
* Creates a resident (i.e. stateless) credential. | ||
* @param id Unique base64 encoded string. | ||
* @param rpId Relying party identifier. | ||
* @param userHandle userHandle associated to the credential. Must be Base64 encoded string. | ||
* @param privateKey Base64 encoded PKCS | ||
* @param signCount intital value for a signature counter. | ||
* @returns A resident credential | ||
*/ | ||
createResidentCredential(id, rpId, userHandle, privateKey, signCount) { | ||
return new Credential(id, true, rpId, userHandle, privateKey, signCount) | ||
} | ||
|
||
/** | ||
* Creates a non resident (i.e. stateless) credential. | ||
* @param id Unique base64 encoded string. | ||
* @param rpId Relying party identifier. | ||
* @param privateKey Base64 encoded PKCS | ||
* @param signCount intital value for a signature counter. | ||
* @returns A non-resident credential | ||
*/ | ||
createNonResidentCredential(id, rpId, privateKey, signCount) { | ||
return new Credential(id, false, rpId, null, privateKey, signCount) | ||
} | ||
|
||
toDict() { | ||
let credentialData = { | ||
'credentialId': Buffer.from(this._id).toString('base64url'), | ||
'isResidentCredential': this._isResidentCredential, | ||
'rpId': this._rpId, | ||
'privateKey': Buffer.from(this._privateKey, 'binary').toString('base64url'), | ||
'signCount': this._signCount, | ||
} | ||
|
||
if (this.userHandle() != null) { | ||
credentialData['userHandle'] = Buffer.from(this._userHandle).toString('base64url') | ||
} | ||
|
||
return credentialData | ||
} | ||
|
||
/** | ||
* Creates a credential from a map. | ||
*/ | ||
fromDict(data) { | ||
let id = new Uint8Array(Buffer.from(data['credentialId'], 'base64url')) | ||
let isResidentCredential = data['isResidentCredential'] | ||
let rpId = data['rpId'] | ||
let privateKey = Buffer.from(data['privateKey'], 'base64url').toString('binary') | ||
let signCount = data['signCount'] | ||
let userHandle | ||
|
||
if ('userHandle' in data) { | ||
userHandle = new Uint8Array(Buffer.from(data['userHandle'], 'base64url')) | ||
} else { | ||
userHandle = null | ||
} | ||
return new Credential( | ||
id, | ||
isResidentCredential, | ||
rpId, | ||
userHandle, | ||
privateKey, | ||
signCount | ||
) | ||
} | ||
} | ||
|
||
module.exports = { | ||
Credential, | ||
VirtualAuthenticatorOptions, | ||
} |
Oops, something went wrong.