|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +'use strict' |
| 19 | + |
| 20 | +/** |
| 21 | + * Options for the creation of virtual authenticators. |
| 22 | + * @see http://w3c.github.io/webauthn/#sctn-automation |
| 23 | + */ |
| 24 | +class VirtualAuthenticatorOptions { |
| 25 | + |
| 26 | + static Protocol = { |
| 27 | + "CTAP2": 'ctap2', |
| 28 | + "U2F": 'ctap1/u2f', |
| 29 | + } |
| 30 | + |
| 31 | + static Transport = { |
| 32 | + "BLE": 'ble', |
| 33 | + "USB": 'usb', |
| 34 | + "NFC": 'nfc', |
| 35 | + "INTERNAL": 'internal', |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor to initialise VirtualAuthenticatorOptions object. |
| 40 | + */ |
| 41 | + constructor() { |
| 42 | + this._protocol = VirtualAuthenticatorOptions.Protocol["CTAP2"] |
| 43 | + this._transport = VirtualAuthenticatorOptions.Transport["USB"] |
| 44 | + this._hasResidentKey = false |
| 45 | + this._hasUserVerification = false |
| 46 | + this._isUserConsenting = true |
| 47 | + this._isUserVerified = false |
| 48 | + } |
| 49 | + |
| 50 | + getProtocol() { |
| 51 | + return this._protocol |
| 52 | + } |
| 53 | + |
| 54 | + setProtocol(protocol) { |
| 55 | + this._protocol = protocol |
| 56 | + } |
| 57 | + |
| 58 | + getTransport() { |
| 59 | + return this._transport |
| 60 | + } |
| 61 | + |
| 62 | + setTransport(transport) { |
| 63 | + this._transport = transport |
| 64 | + } |
| 65 | + |
| 66 | + getHasResidentKey() { |
| 67 | + return this._hasResidentKey |
| 68 | + } |
| 69 | + |
| 70 | + setHasResidentKey(value) { |
| 71 | + this._hasResidentKey = value |
| 72 | + } |
| 73 | + |
| 74 | + getHasUserVerification() { |
| 75 | + return this._hasUserVerification |
| 76 | + } |
| 77 | + |
| 78 | + setHasUserVerification(value) { |
| 79 | + this._hasUserVerification = value |
| 80 | + } |
| 81 | + |
| 82 | + getIsUserConsenting() { |
| 83 | + return this._isUserConsenting |
| 84 | + } |
| 85 | + |
| 86 | + setIsUserConsenting(value) { |
| 87 | + this._isUserConsenting = value |
| 88 | + } |
| 89 | + |
| 90 | + getIsUserVerified() { |
| 91 | + return this._isUserVerified |
| 92 | + } |
| 93 | + |
| 94 | + setIsUserVerified(value) { |
| 95 | + this._isUserVerified = value |
| 96 | + } |
| 97 | + |
| 98 | + toDict() { |
| 99 | + return { |
| 100 | + "protocol": this.getProtocol(), |
| 101 | + "transport": this.getTransport(), |
| 102 | + "hasResidentKey": this.getHasResidentKey(), |
| 103 | + "hasUserVerification": this.getHasUserVerification(), |
| 104 | + "isUserConsenting": this.getIsUserConsenting(), |
| 105 | + "isUserVerified": this.getIsUserVerified(), |
| 106 | + |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * A credential stored in a virtual authenticator. |
| 113 | + * @see https://w3c.github.io/webauthn/#credential-parameters |
| 114 | + */ |
| 115 | +class Credential { |
| 116 | + constructor( |
| 117 | + credentialId, |
| 118 | + isResidentCredential, |
| 119 | + rpId, |
| 120 | + userHandle, |
| 121 | + privateKey, |
| 122 | + signCount |
| 123 | + ) { |
| 124 | + this._id = credentialId |
| 125 | + this._isResidentCredential = isResidentCredential |
| 126 | + this._rpId = rpId |
| 127 | + this._userHandle = userHandle |
| 128 | + this._privateKey = privateKey |
| 129 | + this._signCount = signCount |
| 130 | + } |
| 131 | + |
| 132 | + id() { |
| 133 | + return this._id |
| 134 | + } |
| 135 | + |
| 136 | + isResidentCredential() { |
| 137 | + return this._isResidentCredential |
| 138 | + } |
| 139 | + |
| 140 | + rpId() { |
| 141 | + return this._rpId |
| 142 | + } |
| 143 | + |
| 144 | + userHandle() { |
| 145 | + if (this._userHandle != null) { |
| 146 | + return this._userHandle |
| 147 | + } |
| 148 | + return null |
| 149 | + } |
| 150 | + |
| 151 | + privateKey() { |
| 152 | + return this._privateKey |
| 153 | + } |
| 154 | + |
| 155 | + signCount() { |
| 156 | + return this._signCount |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Creates a resident (i.e. stateless) credential. |
| 161 | + * @param id Unique base64 encoded string. |
| 162 | + * @param rpId Relying party identifier. |
| 163 | + * @param userHandle userHandle associated to the credential. Must be Base64 encoded string. |
| 164 | + * @param privateKey Base64 encoded PKCS |
| 165 | + * @param signCount intital value for a signature counter. |
| 166 | + * @returns A resident credential |
| 167 | + */ |
| 168 | + createResidentCredential(id, rpId, userHandle, privateKey, signCount) { |
| 169 | + return new Credential(id, true, rpId, userHandle, privateKey, signCount) |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Creates a non resident (i.e. stateless) credential. |
| 174 | + * @param id Unique base64 encoded string. |
| 175 | + * @param rpId Relying party identifier. |
| 176 | + * @param privateKey Base64 encoded PKCS |
| 177 | + * @param signCount intital value for a signature counter. |
| 178 | + * @returns A non-resident credential |
| 179 | + */ |
| 180 | + createNonResidentCredential(id, rpId, privateKey, signCount) { |
| 181 | + return new Credential(id, false, rpId, null, privateKey, signCount) |
| 182 | + } |
| 183 | + |
| 184 | + toDict() { |
| 185 | + let credentialData = { |
| 186 | + 'credentialId': Buffer.from(this._id).toString('base64url'), |
| 187 | + 'isResidentCredential': this._isResidentCredential, |
| 188 | + 'rpId': this._rpId, |
| 189 | + 'privateKey': Buffer.from(this._privateKey, 'binary').toString('base64url'), |
| 190 | + 'signCount': this._signCount, |
| 191 | + } |
| 192 | + |
| 193 | + if (this.userHandle() != null) { |
| 194 | + credentialData['userHandle'] = Buffer.from(this._userHandle).toString('base64url') |
| 195 | + } |
| 196 | + |
| 197 | + return credentialData |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Creates a credential from a map. |
| 202 | + */ |
| 203 | + fromDict(data) { |
| 204 | + let id = new Uint8Array(Buffer.from(data['credentialId'], 'base64url')) |
| 205 | + let isResidentCredential = data['isResidentCredential'] |
| 206 | + let rpId = data['rpId'] |
| 207 | + let privateKey = Buffer.from(data['privateKey'], 'base64url').toString('binary') |
| 208 | + let signCount = data['signCount'] |
| 209 | + let userHandle |
| 210 | + |
| 211 | + if ('userHandle' in data) { |
| 212 | + userHandle = new Uint8Array(Buffer.from(data['userHandle'], 'base64url')) |
| 213 | + } else { |
| 214 | + userHandle = null |
| 215 | + } |
| 216 | + return new Credential( |
| 217 | + id, |
| 218 | + isResidentCredential, |
| 219 | + rpId, |
| 220 | + userHandle, |
| 221 | + privateKey, |
| 222 | + signCount |
| 223 | + ) |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +module.exports = { |
| 228 | + Credential, |
| 229 | + VirtualAuthenticatorOptions, |
| 230 | +} |
0 commit comments