Debian Codegraph @

flask_security/static/js/webauthn.js

raw
1 /**
2 * Thanks to duo-labs/py_webauthn - this is their OLD flask demo.
3 * Modified accordingly...
4 * This code should be considered to be licensed:
5 * Copyright (c) 2017 Duo Security, Inc. All rights reserved.
6 * with the BSD 3-Clause "New" or "Revised" License.
7 * Further changes:
8 * :copyright: (c) 2021-2022 by J. Christopher Wagner (jwag).
9 * :license: MIT, see LICENSE for more details.
10 */
11
12
13 function b64enc(buf) {
14 return base64js.fromByteArray(buf)
15 .replace(/\+/g, "-")
16 .replace(/\//g, "_")
17 .replace(/=/g, "");
18 }
19
20 function b64RawEnc(buf) {
21 return base64js.fromByteArray(buf)
22 .replace(/\+/g, "-")
23 .replace(/\//g, "_");
24 }
25
26 function encode(attr) {
27 return Uint8Array.from(
28 atob(attr.replace(/_/g, "/").replace(/-/g, "+")),
29 c => c.charCodeAt(0));
30 }
31
32 /**
33 * REGISTRATION FUNCTIONS
34 */
35
36 /*
37 * handleRegister - given the return from server of credential_options,
38 * parse those and pass to browser to create new credential, and transform
39 * that new credential for passing/storing to the server.
40 */
41 async function handleRegister(credential_options) {
42 const credentialCreateOptionsFromServer = JSON.parse(credential_options)
43
44 // convert certain members of the PublicKeyCredentialCreateOptions into
45 // byte arrays as expected by the spec.
46 const publicKeyCredentialCreateOptions = transformCredentialCreateOptions(credentialCreateOptionsFromServer)
47
48 // request the authenticator(s) to create a new credential keypair.
49 let credential, error_msg
50 try {
51 credential = await navigator.credentials.create({
52 publicKey: publicKeyCredentialCreateOptions
53 })
54 // we now have a new credential! We now need to encode the byte arrays
55 // in the credential into strings, for posting to our server.
56 credential = JSON.stringify(transformNewAssertionForServer(credential))
57 } catch (err) {
58 error_msg = `Error when creating credential: ${err}`
59 }
60 return {"credential": credential, "error_msg": error_msg}
61 }
62
63 /**
64 * Transforms items in the credentialCreateOptions generated on the server
65 * into byte arrays expected by the navigator.credentials.create() call
66 * @param {Object} credentialCreateOptionsFromServer
67 */
68 const transformCredentialCreateOptions = (credentialCreateOptionsFromServer) => {
69 let {challenge, user, excludeCredentials} = credentialCreateOptionsFromServer
70 user.id = encode(credentialCreateOptionsFromServer.user.id)
71 challenge = encode(credentialCreateOptionsFromServer.challenge)
72
73 excludeCredentials = excludeCredentials.map(credentialDescriptor => {
74 let {id} = credentialDescriptor;
75 id = encode(id)
76 return Object.assign({}, credentialDescriptor, {id})
77 })
78
79 const transformedCredentialCreateOptions = Object.assign(
80 {},
81 credentialCreateOptionsFromServer,
82 {challenge, user, excludeCredentials}
83 )
84
85 return transformedCredentialCreateOptions
86 }
87
88 /**
89 * Transforms the binary data in the credential into base64 strings
90 * for posting to the server.
91 * @param {PublicKeyCredential} newAssertion
92 */
93 const transformNewAssertionForServer = (newAssertion) => {
94 const attObj = new Uint8Array(
95 newAssertion.response.attestationObject);
96 const clientDataJSON = new Uint8Array(
97 newAssertion.response.clientDataJSON);
98 const rawId = new Uint8Array(
99 newAssertion.rawId);
100
101 const registrationClientExtensions = newAssertion.getClientExtensionResults();
102
103 // Not all browsers support getTransports() (e.g. Firefox)
104 let transports = null
105 if ("getTransports" in newAssertion.response) {
106 transports = newAssertion.response.getTransports()
107 }
108
109 return {
110 id: newAssertion.id,
111 rawId: b64enc(rawId),
112 type: newAssertion.type,
113 response: {"attestationObject": b64enc(attObj), "clientDataJSON": b64enc(clientDataJSON), "transports": transports},
114 extensions: JSON.stringify(registrationClientExtensions),
115 }
116 }
117
118
119
120 /**
121 * AUTHENTICATION FUNCTIONS
122 */
123
124
125 async function handleSignin(response) {
126 const credentialRequestOptionsFromServer = JSON.parse(response)
127 // convert certain members of the PublicKeyCredentialRequestOptions into
128 // byte arrays as expected by the spec.
129 const transformedCredentialRequestOptions = transformCredentialRequestOptions(
130 credentialRequestOptionsFromServer)
131
132 // request the authenticator to create an assertion signature using the
133 // credential private key
134 let assertion, credential, error_msg
135 try {
136 assertion = await navigator.credentials.get({
137 publicKey: transformedCredentialRequestOptions,
138 })
139 // we now have an authentication assertion! encode the byte arrays contained
140 // in the assertion data as strings for posting to the server
141 credential = JSON.stringify(transformAssertionForServer(assertion))
142 } catch (err) {
143 error_msg = `Error when retrieving credential: ${err}`
144 }
145 return {"credential": credential, "error_msg": error_msg}
146 }
147
148 const transformCredentialRequestOptions = (credentialRequestOptionsFromServer) => {
149 let {challenge, allowCredentials} = credentialRequestOptionsFromServer
150
151 challenge = encode(challenge)
152 allowCredentials = allowCredentials.map(credentialDescriptor => {
153 let {id} = credentialDescriptor
154 id = encode(id)
155 return Object.assign({}, credentialDescriptor, {id})
156 })
157
158 const transformedCredentialRequestOptions = Object.assign(
159 {},
160 credentialRequestOptionsFromServer,
161 {challenge, allowCredentials}
162 )
163
164 return transformedCredentialRequestOptions
165 }
166
167
168
169 /**
170 * Encodes the binary data in the assertion into strings for posting to the server.
171 * @param {PublicKeyCredential} newAssertion
172 */
173 const transformAssertionForServer = (newAssertion) => {
174 const authData = new Uint8Array(newAssertion.response.authenticatorData);
175 const clientDataJSON = new Uint8Array(newAssertion.response.clientDataJSON)
176 const rawId = new Uint8Array(newAssertion.rawId)
177 const sig = new Uint8Array(newAssertion.response.signature)
178 const userHandle = new Uint8Array(newAssertion.response.userHandle)
179 const assertionClientExtensions = newAssertion.getClientExtensionResults()
180
181 return {
182 id: newAssertion.id,
183 rawId: b64enc(rawId),
184 type: newAssertion.type,
185 response: {
186 authenticatorData: b64RawEnc(authData),
187 clientDataJSON: b64RawEnc(clientDataJSON),
188 signature: b64RawEnc(sig),
189 userHandle: b64RawEnc(userHandle)
190 },
191 assertionClientExtensions: JSON.stringify(assertionClientExtensions)
192 };
193 };