Path: blob/trunk/javascript/selenium-webdriver/lib/virtual_authenticator.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819/**20* Protocol for virtual authenticators21* @enum {string}22*/23const Protocol = {24CTAP2: 'ctap2',25U2F: 'ctap1/u2f',26}2728/**29* AuthenticatorTransport values30* @enum {string}31*/32const Transport = {33BLE: 'ble',34USB: 'usb',35NFC: 'nfc',36INTERNAL: 'internal',37}3839/**40* Options for the creation of virtual authenticators.41* @see http://w3c.github.io/webauthn/#sctn-automation42*/43class VirtualAuthenticatorOptions {44/**45* Constructor to initialise VirtualAuthenticatorOptions object.46*/47constructor() {48this._protocol = Protocol['CTAP2']49this._transport = Transport['USB']50this._hasResidentKey = false51this._hasUserVerification = false52this._isUserConsenting = true53this._isUserVerified = false54}5556getProtocol() {57return this._protocol58}5960setProtocol(protocol) {61this._protocol = protocol62}6364getTransport() {65return this._transport66}6768setTransport(transport) {69this._transport = transport70}7172getHasResidentKey() {73return this._hasResidentKey74}7576setHasResidentKey(value) {77this._hasResidentKey = value78}7980getHasUserVerification() {81return this._hasUserVerification82}8384setHasUserVerification(value) {85this._hasUserVerification = value86}8788getIsUserConsenting() {89return this._isUserConsenting90}9192setIsUserConsenting(value) {93this._isUserConsenting = value94}9596getIsUserVerified() {97return this._isUserVerified98}99100setIsUserVerified(value) {101this._isUserVerified = value102}103104toDict() {105return {106protocol: this.getProtocol(),107transport: this.getTransport(),108hasResidentKey: this.getHasResidentKey(),109hasUserVerification: this.getHasUserVerification(),110isUserConsenting: this.getIsUserConsenting(),111isUserVerified: this.getIsUserVerified(),112}113}114}115116/**117* A credential stored in a virtual authenticator.118* @see https://w3c.github.io/webauthn/#credential-parameters119*/120class Credential {121constructor(credentialId, isResidentCredential, rpId, userHandle, privateKey, signCount) {122this._id = credentialId123this._isResidentCredential = isResidentCredential124this._rpId = rpId125this._userHandle = userHandle126this._privateKey = privateKey127this._signCount = signCount128}129130static createResidentCredential(id, rpId, userHandle, privateKey, signCount) {131return new Credential(id, true, rpId, userHandle, privateKey, signCount)132}133134static createNonResidentCredential(id, rpId, privateKey, signCount) {135return new Credential(id, false, rpId, null, privateKey, signCount)136}137138id() {139return this._id140}141142isResidentCredential() {143return this._isResidentCredential144}145146rpId() {147return this._rpId148}149150userHandle() {151if (this._userHandle != null) {152return this._userHandle153}154return null155}156157privateKey() {158return this._privateKey159}160161signCount() {162return this._signCount163}164165/**166* Creates a resident (i.e. stateless) credential.167* @param id Unique base64 encoded string.168* @param rpId Relying party identifier.169* @param userHandle userHandle associated to the credential. Must be Base64 encoded string.170* @param privateKey Base64 encoded PKCS171* @param signCount initial value for a signature counter.172* @deprecated This method has been made static. Call it with class name. Example, Credential.createResidentCredential()173* @returns A resident credential174*/175createResidentCredential(id, rpId, userHandle, privateKey, signCount) {176return new Credential(id, true, rpId, userHandle, privateKey, signCount)177}178179/**180* Creates a non-resident (i.e. stateless) credential.181* @param id Unique base64 encoded string.182* @param rpId Relying party identifier.183* @param privateKey Base64 encoded PKCS184* @param signCount initial value for a signature counter.185* @deprecated This method has been made static. Call it with class name. Example, Credential.createNonResidentCredential()186* @returns A non-resident credential187*/188createNonResidentCredential(id, rpId, privateKey, signCount) {189return new Credential(id, false, rpId, null, privateKey, signCount)190}191192toDict() {193let credentialData = {194credentialId: Buffer.from(this._id).toString('base64url'),195isResidentCredential: this._isResidentCredential,196rpId: this._rpId,197privateKey: Buffer.from(this._privateKey, 'binary').toString('base64url'),198signCount: this._signCount,199}200201if (this.userHandle() != null) {202credentialData['userHandle'] = Buffer.from(this._userHandle).toString('base64url')203}204205return credentialData206}207208/**209* Creates a credential from a map.210*/211fromDict(data) {212let id = new Uint8Array(Buffer.from(data['credentialId'], 'base64url'))213let isResidentCredential = data['isResidentCredential']214let rpId = data['rpId']215let privateKey = Buffer.from(data['privateKey'], 'base64url').toString('binary')216let signCount = data['signCount']217let userHandle218219if ('userHandle' in data) {220userHandle = new Uint8Array(Buffer.from(data['userHandle'], 'base64url'))221} else {222userHandle = null223}224return new Credential(id, isResidentCredential, rpId, userHandle, privateKey, signCount)225}226}227228// PUBLIC API229230module.exports = {231Credential,232VirtualAuthenticatorOptions,233Transport,234Protocol,235}236237238