@magic-ext/webauthn 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,40 @@
1
+ # v0.2.4 (Thu Oct 21 2021)
2
+
3
+ #### 🐛 Bug Fix
4
+
5
+ - Revert to `.js` extension for ES module builds targeting webpack/CRA [#232](https://github.com/magiclabs/magic-js/pull/232) ([@smithki](https://github.com/smithki))
6
+
7
+ #### Authors: 1
8
+
9
+ - Ian K Smith ([@smithki](https://github.com/smithki))
10
+
11
+ ---
12
+
13
+ # v0.2.3 (Wed Oct 20 2021)
14
+
15
+ #### ⚠️ Pushed to `master`
16
+
17
+ - Merge branch 'master' of github.com:magiclabs/magic-js ([@smithki](https://github.com/smithki))
18
+ - Force re-publish ([@smithki](https://github.com/smithki))
19
+
20
+ #### Authors: 1
21
+
22
+ - Ian K Smith ([@smithki](https://github.com/smithki))
23
+
24
+ ---
25
+
26
+ # v0.2.2 (Wed Oct 20 2021)
27
+
28
+ #### 🐛 Bug Fix
29
+
30
+ - Use '.mjs' extension for ESM build files [#230](https://github.com/magiclabs/magic-js/pull/230) ([@smithki](https://github.com/smithki))
31
+
32
+ #### Authors: 1
33
+
34
+ - Ian K Smith ([@smithki](https://github.com/smithki))
35
+
36
+ ---
37
+
1
38
  # v0.1.1 (Thu Sep 23 2021)
2
39
 
3
40
  #### 🐛 Bug Fix
package/README.md ADDED
@@ -0,0 +1 @@
1
+ <!-- force publish -->
@@ -1,2 +1,2 @@
1
1
  import{Extension as e}from"@magic-sdk/commons";var t,r;function n(e){let t;const r=e.length%3;let n,a,i="";function s(e){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)}for(t=0,a=e.length-r;t<a;t+=3)n=(e[t]<<16)+(e[t+1]<<8)+e[t+2],i+=s((u=n)>>18&63)+s(u>>12&63)+s(u>>6&63)+s(63&u);var u;switch(r){case 1:n=e[e.length-1],i+=s(n>>2),i+=s(n<<4&63),i+="==";break;case 2:n=(e[e.length-2]<<8)+e[e.length-1],i+=s(n>>10),i+=s(n>>4&63),i+=s(n<<2&63),i+="="}return i}function a(e){return n(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function i(e){return n(e).replace(/\+/g,"-").replace(/\//g,"_")}!function(e){e.WebAuthnRegistrationStart="magic_auth_webauthn_registration_start",e.RegisterWithWebAuth="magic_auth_webauthn_register",e.LoginWithWebAuthn="magic_auth_login_with_web_authn",e.WebAuthnLoginVerify="magic_auth_login_with_webauthn_verify",e.GetWebAuthnInfo="magic_user_get_webauthn_credentials",e.UpdateWebAuthnInfo="magic_user_update_webauthn",e.UnregisterWebAuthDevice="magic_user_unregister_webauthn",e.RegisterWebAuthDeviceStart="magic_auth_register_webauthn_device_start",e.RegisterWebAuthDevice="magic_auth_register_webauthn_device"}(t||(t={})),function(e){e.WebAuthnNotSupported="WEBAUTHN_NOT_SUPPORTED",e.WebAuthnCreateCredentialError="WEBAUTHN_CREATE_CREDENTIAL_ERROR"}(r||(r={}));const s=e=>{const t=new Uint8Array(e.response.attestationObject),r=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),i=e.getClientExtensionResults();return{id:e.id,rawId:a(n),type:e.type,attObj:a(t),clientData:a(r),registrationClientExtensions:JSON.stringify(i)}},u=e=>{const t=new Uint8Array(e.response.authenticatorData),r=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),s=new Uint8Array(e.response.signature),u=e.getClientExtensionResults();return{id:e.id,rawId:a(n),type:e.type,authData:i(t),clientData:i(r),signature:(o=s,Array.from(o).map(function(e){return`0${e.toString(16)}`.substr(-2)}).join("")),assertionClientExtensions:JSON.stringify(u)};var o};class o extends e.Internal{constructor(...e){super(...e),this.name="webauthn",this.config={}}createWebAuthnNotSupportError(){this.createError(r.WebAuthnNotSupported,"WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(e){this.createError(r.WebAuthnCreateCredentialError,`Error creating credential: ${e}`,{})}async registerNewUser(e){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();const{username:r,nickname:n=""}=e,a=await this.request(this.utils.createJsonRpcRequestPayload(t.WebAuthnRegistrationStart,[{username:r}]));let i;try{i=await navigator.credentials.create({publicKey:a.credential_options})}catch(e){throw this.createWebAuthCreateCredentialError(e)}return this.request(this.utils.createJsonRpcRequestPayload(t.RegisterWithWebAuth,[{id:a.id,nickname:n,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:s(i)}]))}async login(e){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();const{username:r}=e,n=await this.request(this.utils.createJsonRpcRequestPayload(t.LoginWithWebAuthn,[{username:r}]));let a;try{a=await navigator.credentials.get({publicKey:n})}catch(e){throw this.createWebAuthCreateCredentialError(e)}return this.request(this.utils.createJsonRpcRequestPayload(t.WebAuthnLoginVerify,[{username:r,assertion_response:u(a)}]))}updateInfo(e){const{id:r,nickname:n}=e,a=this.utils.createJsonRpcRequestPayload(t.UpdateWebAuthnInfo,[{webAuthnCredentialsId:r,nickname:n}]);return this.request(a)}unregisterDevice(e){const r=this.utils.createJsonRpcRequestPayload(t.UnregisterWebAuthDevice,[{webAuthnCredentialsId:e}]);return this.request(r)}async registerNewDevice(e=""){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();const r=await this.request(this.utils.createJsonRpcRequestPayload(t.RegisterWebAuthDeviceStart,[]));let n;try{n=await navigator.credentials.create({publicKey:r.credential_options})}catch(e){throw this.createWebAuthCreateCredentialError(e)}return this.request(this.utils.createJsonRpcRequestPayload(t.RegisterWebAuthDevice,[{nickname:e,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:s(n)}]))}getMetadata(){const e=this.utils.createJsonRpcRequestPayload(t.GetWebAuthnInfo,[]);return this.request(e)}}export{o as WebAuthnExtension};
2
- //# sourceMappingURL=index.js.map
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../../src/types.ts","../../src/utils/webauthn.js","../../src/index.ts"],"sourcesContent":["export interface RegisterNewUserConfiguration {\n /**\n * The username of the user attempting to register.\n */\n username: string;\n\n /**\n * The nickname that the user attempts to set to this webauthn device.\n */\n nickname?: string;\n}\n\nexport interface LoginWithWebAuthnConfiguration {\n /**\n * The username of the user attempting to register.\n */\n username: string;\n}\n\nexport interface UpdateWebAuthnInfoConfiguration {\n /**\n * WebAuthn info id\n */\n id: string;\n\n /**\n * nickname that user attempts to update to the webauth device associate to the id.\n */\n nickname: string;\n}\n\nexport enum MagicWebAuthnPayloadMethod {\n WebAuthnRegistrationStart = 'magic_auth_webauthn_registration_start',\n RegisterWithWebAuth = 'magic_auth_webauthn_register',\n LoginWithWebAuthn = 'magic_auth_login_with_web_authn',\n WebAuthnLoginVerify = 'magic_auth_login_with_webauthn_verify',\n GetWebAuthnInfo = 'magic_user_get_webauthn_credentials',\n UpdateWebAuthnInfo = 'magic_user_update_webauthn',\n UnregisterWebAuthDevice = 'magic_user_unregister_webauthn',\n RegisterWebAuthDeviceStart = 'magic_auth_register_webauthn_device_start',\n RegisterWebAuthDevice = 'magic_auth_register_webauthn_device',\n}\n\nexport enum WebAuthnSDKErrorCode {\n WebAuthnNotSupported = 'WEBAUTHN_NOT_SUPPORTED',\n WebAuthnCreateCredentialError = 'WEBAUTHN_CREATE_CREDENTIAL_ERROR',\n}\n","const lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\n/* eslint-disable */\n/* istanbul ignore next */\nfunction fromByteArray(uint8) {\n let i;\n const extraBytes = uint8.length % 3; // if we have 1 byte left, pad 2 bytes\n let output = '';\n let temp;\n let length;\n\n function encode(num) {\n return lookup.charAt(num);\n }\n\n function tripletToBase64(num) {\n return encode((num >> 18) & 0x3f) + encode((num >> 12) & 0x3f) + encode((num >> 6) & 0x3f) + encode(num & 0x3f);\n }\n\n // go through the array every three bytes, we'll deal with trailing stuff later\n for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {\n temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2];\n output += tripletToBase64(temp);\n }\n\n // pad the end with zeros, but make sure to not forget the extra bytes\n switch (extraBytes) {\n case 1:\n temp = uint8[uint8.length - 1];\n output += encode(temp >> 2);\n output += encode((temp << 4) & 0x3f);\n output += '==';\n break;\n case 2:\n temp = (uint8[uint8.length - 2] << 8) + uint8[uint8.length - 1];\n output += encode(temp >> 10);\n output += encode((temp >> 4) & 0x3f);\n output += encode((temp << 2) & 0x3f);\n output += '=';\n break;\n default:\n break;\n }\n\n return output;\n}\n\n/* istanbul ignore next */\nfunction b64enc(buf) {\n return fromByteArray(buf).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n}\n\n/* istanbul ignore next */\nfunction b64RawEnc(buf) {\n return fromByteArray(buf).replace(/\\+/g, '-').replace(/\\//g, '_');\n}\n\n/* istanbul ignore next */\nfunction hexEncode(buf) {\n return Array.from(buf)\n .map(function (x) {\n return `0${x.toString(16)}`.substr(-2);\n })\n .join('');\n}\n\n/**\n * Transforms the binary data in the credential into base64 strings\n * for posting to the server.\n * @param {PublicKeyCredential} newAssertion\n */\n/* istanbul ignore next */\nexport const transformNewAssertionForServer = (newAssertion) => {\n const attObj = new Uint8Array(newAssertion.response.attestationObject);\n const clientDataJSON = new Uint8Array(newAssertion.response.clientDataJSON);\n const rawId = new Uint8Array(newAssertion.rawId);\n\n const registrationClientExtensions = newAssertion.getClientExtensionResults();\n\n return {\n id: newAssertion.id,\n rawId: b64enc(rawId),\n type: newAssertion.type,\n attObj: b64enc(attObj),\n clientData: b64enc(clientDataJSON),\n registrationClientExtensions: JSON.stringify(registrationClientExtensions),\n };\n};\n\n/**\n * Encodes the binary data in the assertion into strings for posting to the server.\n * @param {PublicKeyCredential} newAssertion\n */\n/* istanbul ignore next */\nexport const transformAssertionForServer = (newAssertion) => {\n const authData = new Uint8Array(newAssertion.response.authenticatorData);\n const clientDataJSON = new Uint8Array(newAssertion.response.clientDataJSON);\n const rawId = new Uint8Array(newAssertion.rawId);\n const sig = new Uint8Array(newAssertion.response.signature);\n const assertionClientExtensions = newAssertion.getClientExtensionResults();\n\n return {\n id: newAssertion.id,\n rawId: b64enc(rawId),\n type: newAssertion.type,\n authData: b64RawEnc(authData),\n clientData: b64RawEnc(clientDataJSON),\n signature: hexEncode(sig),\n assertionClientExtensions: JSON.stringify(assertionClientExtensions),\n };\n};\n","import { Extension } from '@magic-sdk/commons';\nimport {\n RegisterNewUserConfiguration,\n LoginWithWebAuthnConfiguration,\n MagicWebAuthnPayloadMethod,\n WebAuthnSDKErrorCode,\n UpdateWebAuthnInfoConfiguration,\n} from './types';\nimport { transformAssertionForServer, transformNewAssertionForServer } from './utils/webauthn.js';\n\nexport class WebAuthnExtension extends Extension.Internal<'webauthn', any> {\n name = 'webauthn' as const;\n config: any = {};\n\n private createWebAuthnNotSupportError() {\n this.createError(WebAuthnSDKErrorCode.WebAuthnNotSupported, 'WebAuthn is not supported in this device.', {});\n }\n\n private createWebAuthCreateCredentialError(message: string) {\n this.createError(WebAuthnSDKErrorCode.WebAuthnCreateCredentialError, `Error creating credential: ${message}`, {});\n }\n\n public async registerNewUser(configuration: RegisterNewUserConfiguration) {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const { username, nickname = '' } = configuration;\n\n const options = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.WebAuthnRegistrationStart, [{ username }]),\n );\n\n let credential;\n try {\n credential = (await navigator.credentials.create({\n publicKey: options.credential_options,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWithWebAuth, [\n {\n id: options.id,\n nickname,\n transport: credential.response.getTransports(),\n user_agent: navigator.userAgent,\n registration_response: transformNewAssertionForServer(credential),\n },\n ]),\n );\n }\n\n public async login(configuration: LoginWithWebAuthnConfiguration) {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const { username } = configuration;\n\n const transformedCredentialRequestOptions = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.LoginWithWebAuthn, [{ username }]),\n );\n\n let assertion;\n try {\n assertion = (await navigator.credentials.get({\n publicKey: transformedCredentialRequestOptions,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.WebAuthnLoginVerify, [\n {\n username,\n assertion_response: transformAssertionForServer(assertion),\n },\n ]),\n );\n }\n\n public updateInfo(configuration: UpdateWebAuthnInfoConfiguration) {\n const { id, nickname } = configuration;\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.UpdateWebAuthnInfo, [\n {\n webAuthnCredentialsId: id,\n nickname,\n },\n ]);\n return this.request<any[]>(requestPayload);\n }\n\n public unregisterDevice(id: string) {\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.UnregisterWebAuthDevice, [\n {\n webAuthnCredentialsId: id,\n },\n ]);\n\n return this.request<any>(requestPayload);\n }\n\n public async registerNewDevice(nickname = '') {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const options = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWebAuthDeviceStart, []),\n );\n\n let credential;\n try {\n credential = (await navigator.credentials.create({\n publicKey: options.credential_options,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWebAuthDevice, [\n {\n nickname,\n transport: credential.response.getTransports(),\n user_agent: navigator.userAgent,\n registration_response: transformNewAssertionForServer(credential),\n },\n ]),\n );\n }\n\n public getMetadata() {\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.GetWebAuthnInfo, []);\n return this.request<any[]>(requestPayload);\n }\n}\n"],"names":["MagicWebAuthnPayloadMethod","WebAuthnSDKErrorCode","fromByteArray","uint8","i","extraBytes","length","temp","output","encode","num","charAt","b64enc","buf","replace","b64RawEnc","transformNewAssertionForServer","newAssertion","attObj","Uint8Array","response","attestationObject","clientDataJSON","rawId","registrationClientExtensions","getClientExtensionResults","id","type","clientData","JSON","stringify","transformAssertionForServer","authData","authenticatorData","sig","signature","assertionClientExtensions","Array","from","map","x","toString","substr","join","WebAuthnExtension","Extension","Internal","name","config","createWebAuthnNotSupportError","this","createError","WebAuthnNotSupported","createWebAuthCreateCredentialError","message","WebAuthnCreateCredentialError","registerNewUser","configuration","window","PublicKeyCredential","username","nickname","options","request","utils","createJsonRpcRequestPayload","WebAuthnRegistrationStart","credential","navigator","credentials","create","publicKey","credential_options","err","RegisterWithWebAuth","transport","getTransports","user_agent","userAgent","registration_response","login","transformedCredentialRequestOptions","LoginWithWebAuthn","assertion","get","WebAuthnLoginVerify","assertion_response","updateInfo","requestPayload","UpdateWebAuthnInfo","webAuthnCredentialsId","unregisterDevice","UnregisterWebAuthDevice","registerNewDevice","RegisterWebAuthDeviceStart","RegisterWebAuthDevice","getMetadata","GetWebAuthnInfo"],"mappings":"mDA+BYA,EAYAC,ECxCZ,SAASC,EAAcC,GACrB,IAAIC,EACJ,MAAMC,EAAaF,EAAMG,OAAS,EAClC,IACIC,EACAD,EAFAE,EAAS,GAIb,SAASC,EAAOC,GACd,MAXW,mEAWGC,OAAOD,GAQvB,IAAKN,EAAI,EAAGE,EAASH,EAAMG,OAASD,EAAYD,EAAIE,EAAQF,GAAK,EAC/DG,GAAQJ,EAAMC,IAAM,KAAOD,EAAMC,EAAI,IAAM,GAAKD,EAAMC,EAAI,GAC1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AEtB,SAAAA,MAG1F,IAAIuB,EACJ,IACEA,QAAmBf,UAAUC,YAAYe,IAAI,CAC3Cb,UAAWU,IAEb,MAAOR,GACP,WAAWpB,mCAAmCoB,GAGhD,YAAYV,QACVb,KAAKc,MAAMC,4BAA4BjE,EAA2BqF,oBAAqB,CACrF,CACEzB,SAAAA,EACA0B,mBAAoBvD,EAA4BoD,OAMjDI,WAAW9B,GAChB,MAAM/B,GAAEA,EAAFmC,SAAMA,GAAaJ,EACnB+B,EAAiBtC,KAAKc,MAAMC,4BAA4BjE,EAA2ByF,mBAAoB,CAC3G,CACEC,sBAAuBhE,EACvBmC,SAAAA,KAGJ,YAAYE,QAAeyB,GAGtBG,iBAAiBjE,GACtB,MAAM8D,EAAiBtC,KAAKc,MAAMC,4BAA4BjE,EAA2B4F,wBAAyB,CAChH,CACEF,sBAAuBhE,KAI3B,YAAYqC,QAAayB,GAGGK,wBAAChC,EAAW,IACxC,IAAKH,OAAOC,oBACV,WAAWV,gCAEb,MAAMa,aAAqBC,QACzBb,KAAKc,MAAMC,4BAA4BjE,EAA2B8F,2BAA4B,KAGhG,IAAI3B,EACJ,IACEA,QAAoBC,UAAUC,YAAYC,OAAO,CAC/CC,UAAWT,EAAQU,qBAErB,MAAOC,GACP,WAAWpB,mCAAmCoB,GAGhD,YAAYV,QACVb,KAAKc,MAAMC,4BAA4BjE,EAA2B+F,sBAAuB,CACvF,CACElC,SAAAA,EACAc,UAAWR,EAAW/C,SAASwD,gBAC/BC,WAAYT,UAAUU,UACtBC,sBAAuB/D,EAA+BmD,OAMvD6B,cACL,MAAMR,EAAiBtC,KAAKc,MAAMC,4BAA4BjE,EAA2BiG,gBAAiB,IAC1G,YAAYlC,QAAeyB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-ext/webauthn",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Magic SDK WabAuthn Extension",
5
5
  "author": "Magic <team@magic.link> (https://magic.link/)",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "jsdelivr": "./dist/extension.js",
20
20
  "react-native": "./dist/react-native/index.native.js",
21
21
  "exports": {
22
- "import": "./dist/modern/index.js",
22
+ "import": "./dist/modern/index.mjs",
23
23
  "require": "./dist/cjs/index.js"
24
24
  },
25
25
  "externals": {
@@ -28,7 +28,7 @@
28
28
  ]
29
29
  },
30
30
  "devDependencies": {
31
- "@magic-sdk/commons": "^2.1.1"
31
+ "@magic-sdk/commons": "^2.2.0"
32
32
  },
33
- "gitHead": "020422fd3c496a0c9cfefdd3273d01f5a0960d25"
33
+ "gitHead": "1cd791e160d7665ae0d4194646aceca6e678d2ed"
34
34
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../../src/types.ts","../../src/utils/webauthn.js","../../src/index.ts"],"sourcesContent":["export interface RegisterNewUserConfiguration {\n /**\n * The username of the user attempting to register.\n */\n username: string;\n\n /**\n * The nickname that the user attempts to set to this webauthn device.\n */\n nickname?: string;\n}\n\nexport interface LoginWithWebAuthnConfiguration {\n /**\n * The username of the user attempting to register.\n */\n username: string;\n}\n\nexport interface UpdateWebAuthnInfoConfiguration {\n /**\n * WebAuthn info id\n */\n id: string;\n\n /**\n * nickname that user attempts to update to the webauth device associate to the id.\n */\n nickname: string;\n}\n\nexport enum MagicWebAuthnPayloadMethod {\n WebAuthnRegistrationStart = 'magic_auth_webauthn_registration_start',\n RegisterWithWebAuth = 'magic_auth_webauthn_register',\n LoginWithWebAuthn = 'magic_auth_login_with_web_authn',\n WebAuthnLoginVerify = 'magic_auth_login_with_webauthn_verify',\n GetWebAuthnInfo = 'magic_user_get_webauthn_credentials',\n UpdateWebAuthnInfo = 'magic_user_update_webauthn',\n UnregisterWebAuthDevice = 'magic_user_unregister_webauthn',\n RegisterWebAuthDeviceStart = 'magic_auth_register_webauthn_device_start',\n RegisterWebAuthDevice = 'magic_auth_register_webauthn_device',\n}\n\nexport enum WebAuthnSDKErrorCode {\n WebAuthnNotSupported = 'WEBAUTHN_NOT_SUPPORTED',\n WebAuthnCreateCredentialError = 'WEBAUTHN_CREATE_CREDENTIAL_ERROR',\n}\n","const lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\n/* eslint-disable */\n/* istanbul ignore next */\nfunction fromByteArray(uint8) {\n let i;\n const extraBytes = uint8.length % 3; // if we have 1 byte left, pad 2 bytes\n let output = '';\n let temp;\n let length;\n\n function encode(num) {\n return lookup.charAt(num);\n }\n\n function tripletToBase64(num) {\n return encode((num >> 18) & 0x3f) + encode((num >> 12) & 0x3f) + encode((num >> 6) & 0x3f) + encode(num & 0x3f);\n }\n\n // go through the array every three bytes, we'll deal with trailing stuff later\n for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {\n temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2];\n output += tripletToBase64(temp);\n }\n\n // pad the end with zeros, but make sure to not forget the extra bytes\n switch (extraBytes) {\n case 1:\n temp = uint8[uint8.length - 1];\n output += encode(temp >> 2);\n output += encode((temp << 4) & 0x3f);\n output += '==';\n break;\n case 2:\n temp = (uint8[uint8.length - 2] << 8) + uint8[uint8.length - 1];\n output += encode(temp >> 10);\n output += encode((temp >> 4) & 0x3f);\n output += encode((temp << 2) & 0x3f);\n output += '=';\n break;\n default:\n break;\n }\n\n return output;\n}\n\n/* istanbul ignore next */\nfunction b64enc(buf) {\n return fromByteArray(buf).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n}\n\n/* istanbul ignore next */\nfunction b64RawEnc(buf) {\n return fromByteArray(buf).replace(/\\+/g, '-').replace(/\\//g, '_');\n}\n\n/* istanbul ignore next */\nfunction hexEncode(buf) {\n return Array.from(buf)\n .map(function (x) {\n return `0${x.toString(16)}`.substr(-2);\n })\n .join('');\n}\n\n/**\n * Transforms the binary data in the credential into base64 strings\n * for posting to the server.\n * @param {PublicKeyCredential} newAssertion\n */\n/* istanbul ignore next */\nexport const transformNewAssertionForServer = (newAssertion) => {\n const attObj = new Uint8Array(newAssertion.response.attestationObject);\n const clientDataJSON = new Uint8Array(newAssertion.response.clientDataJSON);\n const rawId = new Uint8Array(newAssertion.rawId);\n\n const registrationClientExtensions = newAssertion.getClientExtensionResults();\n\n return {\n id: newAssertion.id,\n rawId: b64enc(rawId),\n type: newAssertion.type,\n attObj: b64enc(attObj),\n clientData: b64enc(clientDataJSON),\n registrationClientExtensions: JSON.stringify(registrationClientExtensions),\n };\n};\n\n/**\n * Encodes the binary data in the assertion into strings for posting to the server.\n * @param {PublicKeyCredential} newAssertion\n */\n/* istanbul ignore next */\nexport const transformAssertionForServer = (newAssertion) => {\n const authData = new Uint8Array(newAssertion.response.authenticatorData);\n const clientDataJSON = new Uint8Array(newAssertion.response.clientDataJSON);\n const rawId = new Uint8Array(newAssertion.rawId);\n const sig = new Uint8Array(newAssertion.response.signature);\n const assertionClientExtensions = newAssertion.getClientExtensionResults();\n\n return {\n id: newAssertion.id,\n rawId: b64enc(rawId),\n type: newAssertion.type,\n authData: b64RawEnc(authData),\n clientData: b64RawEnc(clientDataJSON),\n signature: hexEncode(sig),\n assertionClientExtensions: JSON.stringify(assertionClientExtensions),\n };\n};\n","import { Extension } from '@magic-sdk/commons';\nimport {\n RegisterNewUserConfiguration,\n LoginWithWebAuthnConfiguration,\n MagicWebAuthnPayloadMethod,\n WebAuthnSDKErrorCode,\n UpdateWebAuthnInfoConfiguration,\n} from './types';\nimport { transformAssertionForServer, transformNewAssertionForServer } from './utils/webauthn.js';\n\nexport class WebAuthnExtension extends Extension.Internal<'webauthn', any> {\n name = 'webauthn' as const;\n config: any = {};\n\n private createWebAuthnNotSupportError() {\n this.createError(WebAuthnSDKErrorCode.WebAuthnNotSupported, 'WebAuthn is not supported in this device.', {});\n }\n\n private createWebAuthCreateCredentialError(message: string) {\n this.createError(WebAuthnSDKErrorCode.WebAuthnCreateCredentialError, `Error creating credential: ${message}`, {});\n }\n\n public async registerNewUser(configuration: RegisterNewUserConfiguration) {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const { username, nickname = '' } = configuration;\n\n const options = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.WebAuthnRegistrationStart, [{ username }]),\n );\n\n let credential;\n try {\n credential = (await navigator.credentials.create({\n publicKey: options.credential_options,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWithWebAuth, [\n {\n id: options.id,\n nickname,\n transport: credential.response.getTransports(),\n user_agent: navigator.userAgent,\n registration_response: transformNewAssertionForServer(credential),\n },\n ]),\n );\n }\n\n public async login(configuration: LoginWithWebAuthnConfiguration) {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const { username } = configuration;\n\n const transformedCredentialRequestOptions = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.LoginWithWebAuthn, [{ username }]),\n );\n\n let assertion;\n try {\n assertion = (await navigator.credentials.get({\n publicKey: transformedCredentialRequestOptions,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.WebAuthnLoginVerify, [\n {\n username,\n assertion_response: transformAssertionForServer(assertion),\n },\n ]),\n );\n }\n\n public updateInfo(configuration: UpdateWebAuthnInfoConfiguration) {\n const { id, nickname } = configuration;\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.UpdateWebAuthnInfo, [\n {\n webAuthnCredentialsId: id,\n nickname,\n },\n ]);\n return this.request<any[]>(requestPayload);\n }\n\n public unregisterDevice(id: string) {\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.UnregisterWebAuthDevice, [\n {\n webAuthnCredentialsId: id,\n },\n ]);\n\n return this.request<any>(requestPayload);\n }\n\n public async registerNewDevice(nickname = '') {\n if (!window.PublicKeyCredential) {\n throw this.createWebAuthnNotSupportError();\n }\n const options = await this.request<any>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWebAuthDeviceStart, []),\n );\n\n let credential;\n try {\n credential = (await navigator.credentials.create({\n publicKey: options.credential_options,\n })) as any;\n } catch (err: any) {\n throw this.createWebAuthCreateCredentialError(err);\n }\n\n return this.request<string | null>(\n this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.RegisterWebAuthDevice, [\n {\n nickname,\n transport: credential.response.getTransports(),\n user_agent: navigator.userAgent,\n registration_response: transformNewAssertionForServer(credential),\n },\n ]),\n );\n }\n\n public getMetadata() {\n const requestPayload = this.utils.createJsonRpcRequestPayload(MagicWebAuthnPayloadMethod.GetWebAuthnInfo, []);\n return this.request<any[]>(requestPayload);\n }\n}\n"],"names":["MagicWebAuthnPayloadMethod","WebAuthnSDKErrorCode","fromByteArray","uint8","i","extraBytes","length","temp","output","encode","num","charAt","b64enc","buf","replace","b64RawEnc","transformNewAssertionForServer","newAssertion","attObj","Uint8Array","response","attestationObject","clientDataJSON","rawId","registrationClientExtensions","getClientExtensionResults","id","type","clientData","JSON","stringify","transformAssertionForServer","authData","authenticatorData","sig","signature","assertionClientExtensions","Array","from","map","x","toString","substr","join","WebAuthnExtension","Extension","Internal","name","config","createWebAuthnNotSupportError","this","createError","WebAuthnNotSupported","createWebAuthCreateCredentialError","message","WebAuthnCreateCredentialError","registerNewUser","configuration","window","PublicKeyCredential","username","nickname","options","request","utils","createJsonRpcRequestPayload","WebAuthnRegistrationStart","credential","navigator","credentials","create","publicKey","credential_options","err","RegisterWithWebAuth","transport","getTransports","user_agent","userAgent","registration_response","login","transformedCredentialRequestOptions","LoginWithWebAuthn","assertion","get","WebAuthnLoginVerify","assertion_response","updateInfo","requestPayload","UpdateWebAuthnInfo","webAuthnCredentialsId","unregisterDevice","UnregisterWebAuthDevice","registerNewDevice","RegisterWebAuthDeviceStart","RegisterWebAuthDevice","getMetadata","GetWebAuthnInfo"],"mappings":"mDA+BYA,EAYAC,ECxCZ,SAASC,EAAcC,GACrB,IAAIC,EACJ,MAAMC,EAAaF,EAAMG,OAAS,EAClC,IACIC,EACAD,EAFAE,EAAS,GAIb,SAASC,EAAOC,GACd,MAXW,mEAWGC,OAAOD,GAQvB,IAAKN,EAAI,EAAGE,EAASH,EAAMG,OAASD,EAAYD,EAAIE,EAAQF,GAAK,EAC/DG,GAAQJ,EAAMC,IAAM,KAAOD,EAAMC,EAAI,IAAM,GAAKD,EAAMC,EAAI,GAC1DI,GANOC,GADgBC,EAOGH,IANJ,GAAM,IAAQE,EAAQC,GAAO,GAAM,IAAQD,EAAQC,GAAO,EAAK,IAAQD,EAAa,GAANC,GADtG,IAAyBA,EAWzB,OAAQL,GACN,OACEE,EAAOJ,EAAMA,EAAMG,OAAS,GAC5BE,GAAUC,EAAOF,GAAQ,GACzBC,GAAUC,EAAQF,GAAQ,EAAK,IAC/BC,GAAU,KACV,MACF,OACED,GAAQJ,EAAMA,EAAMG,OAAS,IAAM,GAAKH,EAAMA,EAAMG,OAAS,GAC7DE,GAAUC,EAAOF,GAAQ,IACzBC,GAAUC,EAAQF,GAAQ,EAAK,IAC/BC,GAAUC,EAAQF,GAAQ,EAAK,IAC/BC,GAAU,IAMd,OAAOA,EAIT,SAASI,EAAOC,GACd,OAAOX,EAAcW,GAAKC,QAAQ,MAAO,KAAKA,QAAQ,MAAO,KAAKA,QAAQ,KAAM,IAIlF,SAASC,EAAUF,GACjB,OAAOX,EAAcW,GAAKC,QAAQ,MAAO,KAAKA,QAAQ,MAAO,MDtB/D,SAAYd,GACVA,qEACAA,qDACAA,sDACAA,8DACAA,wDACAA,kDACAA,2DACAA,yEACAA,8DATF,CAAYA,IAAAA,OAYZ,SAAYC,GACVA,gDACAA,mEAFF,CAAYA,IAAAA,aC4BCe,EAAkCC,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