@magic-ext/webauthn 4.1.0 → 6.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-ext/webauthn",
3
- "version": "4.1.0",
3
+ "version": "6.0.0",
4
4
  "description": "Magic SDK WabAuthn Extension",
5
5
  "author": "Magic <team@magic.link> (https://magic.link/)",
6
6
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  ]
29
29
  },
30
30
  "devDependencies": {
31
- "@magic-sdk/commons": "^6.1.0"
31
+ "@magic-sdk/commons": "^8.0.0"
32
32
  },
33
- "gitHead": "ce15cdf96479464a1c6fd705c8e00f157eb844dd"
33
+ "gitHead": "8ce6f975f0a1238c2945994f2d4aa6d6ac786998"
34
34
  }
package/dist/cjs/index.js DELETED
@@ -1,2 +0,0 @@
1
- var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var W=e=>g(e,"__esModule",{value:!0});var C=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},m=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of A(t))!R.call(e,r)&&(n||r!=="default")&&g(e,r,{get:()=>t[r],enumerable:!(a=w(t,r))||a.enumerable});return e};var E=(e=>(t,n)=>e&&e.get(t)||(n=m(W({}),t,1),e&&e.set(t,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var c=(e,t,n)=>new Promise((a,r)=>{var o=i=>{try{u(n.next(i))}catch(l){r(l)}},s=i=>{try{u(n.throw(i))}catch(l){r(l)}},u=i=>i.done?a(i.value):Promise.resolve(i.value).then(o,s);u((n=n.apply(e,t)).next())});var N={};C(N,{WebAuthnExtension:()=>_});var y=require("@magic-sdk/commons");var x="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function b(e){let t,n=e.length%3,a="",r,o;function s(i){return x.charAt(i)}function u(i){return s(i>>18&63)+s(i>>12&63)+s(i>>6&63)+s(i&63)}for(t=0,o=e.length-n;t<o;t+=3)r=(e[t]<<16)+(e[t+1]<<8)+e[t+2],a+=u(r);switch(n){case 1:r=e[e.length-1],a+=s(r>>2),a+=s(r<<4&63),a+="==";break;case 2:r=(e[e.length-2]<<8)+e[e.length-1],a+=s(r>>10),a+=s(r>>4&63),a+=s(r<<2&63),a+="=";break;default:break}return a}function h(e){return b(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function f(e){return b(e).replace(/\+/g,"-").replace(/\//g,"_")}function v(e){return Array.from(e).map(function(t){return`0${t.toString(16)}`.substr(-2)}).join("")}var p=e=>{let t=new Uint8Array(e.response.attestationObject),n=new Uint8Array(e.response.clientDataJSON),a=new Uint8Array(e.rawId),r=e.getClientExtensionResults();return{id:e.id,rawId:h(a),type:e.type,attObj:h(t),clientData:h(n),registrationClientExtensions:JSON.stringify(r)}},d=e=>{let t=new Uint8Array(e.response.authenticatorData),n=new Uint8Array(e.response.clientDataJSON),a=new Uint8Array(e.rawId),r=new Uint8Array(e.response.signature),o=e.getClientExtensionResults();return{id:e.id,rawId:h(a),type:e.type,authData:f(t),clientData:f(n),signature:v(r),assertionClientExtensions:JSON.stringify(o)}};var _=class extends y.Extension.Internal{constructor(){super(...arguments);this.name="webauthn";this.config={}}createWebAuthnNotSupportError(){this.createError("WEBAUTHN_NOT_SUPPORTED","WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(t){this.createError("WEBAUTHN_CREATE_CREDENTIAL_ERROR",`Error creating credential: ${t}`,{})}registerNewUser(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n,nickname:a=""}=t,r=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_registration_start",[{username:n}])),o;try{o=yield navigator.credentials.create({publicKey:r.credential_options})}catch(s){throw this.createWebAuthCreateCredentialError(s)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_register",[{id:r.id,nickname:a,transport:o.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(o)}]))})}login(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n}=t,a=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_web_authn",[{username:n}])),r;try{r=yield navigator.credentials.get({publicKey:a})}catch(o){throw this.createWebAuthCreateCredentialError(o)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_webauthn_verify",[{username:n,assertion_response:d(r)}]))})}updateInfo(t){let{id:n,nickname:a}=t,r=this.utils.createJsonRpcRequestPayload("magic_user_update_webauthn",[{webAuthnCredentialsId:n,nickname:a}]);return this.request(r)}unregisterDevice(t){let n=this.utils.createJsonRpcRequestPayload("magic_user_unregister_webauthn",[{webAuthnCredentialsId:t}]);return this.request(n)}registerNewDevice(t=""){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let n=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device_start",[])),a;try{a=yield navigator.credentials.create({publicKey:n.credential_options})}catch(r){throw this.createWebAuthCreateCredentialError(r)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device",[{nickname:t,transport:a.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(a)}]))})}getMetadata(){let t=this.utils.createJsonRpcRequestPayload("magic_user_get_webauthn_credentials",[]);return this.request(t)}};module.exports=E(N);
2
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/utils/webauthn.js"],
4
- "sourcesContent": ["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", "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"],
5
- "mappings": "2tBAAA,8CAA0B,8BCA1B,GAAM,GAAS,mEAGf,WAAuB,EAAO,CAC5B,GAAI,GACE,EAAa,EAAM,OAAS,EAC9B,EAAS,GACT,EACA,EAEJ,WAAgB,EAAK,CACnB,MAAO,GAAO,OAAO,GAGvB,WAAyB,EAAK,CAC5B,MAAO,GAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,EAAK,IAAQ,EAAO,EAAM,IAI5G,IAAK,EAAI,EAAG,EAAS,EAAM,OAAS,EAAY,EAAI,EAAQ,GAAK,EAC/D,EAAQ,GAAM,IAAM,IAAO,GAAM,EAAI,IAAM,GAAK,EAAM,EAAI,GAC1D,GAAU,EAAgB,GAI5B,OAAQ,OACD,GACH,EAAO,EAAM,EAAM,OAAS,GAC5B,GAAU,EAAO,GAAQ,GACzB,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,KACV,UACG,GACH,EAAQ,GAAM,EAAM,OAAS,IAAM,GAAK,EAAM,EAAM,OAAS,GAC7D,GAAU,EAAO,GAAQ,IACzB,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,IACV,cAEA,MAGJ,MAAO,GAIT,WAAgB,EAAK,CACnB,MAAO,GAAc,GAAK,QAAQ,MAAO,KAAK,QAAQ,MAAO,KAAK,QAAQ,KAAM,IAIlF,WAAmB,EA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KAAK,MAAM,4BAA4B,sCAA4C,IAC1G,MAAO,MAAK,QAAe",
6
- "names": []
7
- }
package/dist/es/index.js DELETED
@@ -1,2 +0,0 @@
1
- var c=(e,t,a)=>new Promise((n,r)=>{var o=i=>{try{u(a.next(i))}catch(l){r(l)}},s=i=>{try{u(a.throw(i))}catch(l){r(l)}},u=i=>i.done?n(i.value):Promise.resolve(i.value).then(o,s);u((a=a.apply(e,t)).next())});import{Extension as _}from"@magic-sdk/commons";var d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function p(e){let t,a=e.length%3,n="",r,o;function s(i){return d.charAt(i)}function u(i){return s(i>>18&63)+s(i>>12&63)+s(i>>6&63)+s(i&63)}for(t=0,o=e.length-a;t<o;t+=3)r=(e[t]<<16)+(e[t+1]<<8)+e[t+2],n+=u(r);switch(a){case 1:r=e[e.length-1],n+=s(r>>2),n+=s(r<<4&63),n+="==";break;case 2:r=(e[e.length-2]<<8)+e[e.length-1],n+=s(r>>10),n+=s(r>>4&63),n+=s(r<<2&63),n+="=";break;default:break}return n}function h(e){return p(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function b(e){return p(e).replace(/\+/g,"-").replace(/\//g,"_")}function y(e){return Array.from(e).map(function(t){return`0${t.toString(16)}`.substr(-2)}).join("")}var g=e=>{let t=new Uint8Array(e.response.attestationObject),a=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),r=e.getClientExtensionResults();return{id:e.id,rawId:h(n),type:e.type,attObj:h(t),clientData:h(a),registrationClientExtensions:JSON.stringify(r)}},f=e=>{let t=new Uint8Array(e.response.authenticatorData),a=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),r=new Uint8Array(e.response.signature),o=e.getClientExtensionResults();return{id:e.id,rawId:h(n),type:e.type,authData:b(t),clientData:b(a),signature:y(r),assertionClientExtensions:JSON.stringify(o)}};var w=class extends _.Internal{constructor(){super(...arguments);this.name="webauthn";this.config={}}createWebAuthnNotSupportError(){this.createError("WEBAUTHN_NOT_SUPPORTED","WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(t){this.createError("WEBAUTHN_CREATE_CREDENTIAL_ERROR",`Error creating credential: ${t}`,{})}registerNewUser(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:a,nickname:n=""}=t,r=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_registration_start",[{username:a}])),o;try{o=yield navigator.credentials.create({publicKey:r.credential_options})}catch(s){throw this.createWebAuthCreateCredentialError(s)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_register",[{id:r.id,nickname:n,transport:o.response.getTransports(),user_agent:navigator.userAgent,registration_response:g(o)}]))})}login(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:a}=t,n=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_web_authn",[{username:a}])),r;try{r=yield navigator.credentials.get({publicKey:n})}catch(o){throw this.createWebAuthCreateCredentialError(o)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_webauthn_verify",[{username:a,assertion_response:f(r)}]))})}updateInfo(t){let{id:a,nickname:n}=t,r=this.utils.createJsonRpcRequestPayload("magic_user_update_webauthn",[{webAuthnCredentialsId:a,nickname:n}]);return this.request(r)}unregisterDevice(t){let a=this.utils.createJsonRpcRequestPayload("magic_user_unregister_webauthn",[{webAuthnCredentialsId:t}]);return this.request(a)}registerNewDevice(t=""){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let a=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device_start",[])),n;try{n=yield navigator.credentials.create({publicKey:a.credential_options})}catch(r){throw this.createWebAuthCreateCredentialError(r)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device",[{nickname:t,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:g(n)}]))})}getMetadata(){let t=this.utils.createJsonRpcRequestPayload("magic_user_get_webauthn_credentials",[]);return this.request(t)}};export{w as WebAuthnExtension};
2
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/utils/webauthn.js"],
4
- "sourcesContent": ["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", "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"],
5
- "mappings": "6MAAA,+CCAA,GAAM,GAAS,mEAGf,WAAuB,EAAO,CAC5B,GAAI,GACE,EAAa,EAAM,OAAS,EAC9B,EAAS,GACT,EACA,EAEJ,WAAgB,EAAK,CACnB,MAAO,GAAO,OAAO,GAGvB,WAAyB,EAAK,CAC5B,MAAO,GAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,EAAK,IAAQ,EAAO,EAAM,IAI5G,IAAK,EAAI,EAAG,EAAS,EAAM,OAAS,EAAY,EAAI,EAAQ,GAAK,EAC/D,EAAQ,GAAM,IAAM,IAAO,GAAM,EAAI,IAAM,GAAK,EAAM,EAAI,GAC1D,GAAU,EAAgB,GAI5B,OAAQ,OACD,GACH,EAAO,EAAM,EAAM,OAAS,GAC5B,GAAU,EAAO,GA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AO,oBACV,KAAM,MAAK,gCAEb,GAAM,GAAU,KAAM,MAAK,QACzB,KAAK,MAAM,4BAA4B,4CAAuD,KAG5F,EACJ,GAAI,CACF,EAAc,KAAM,WAAU,YAAY,OAAO,CAC/C,UAAW,EAAQ,2BAEd,EAAP,CACA,KAAM,MAAK,mCAAmC,GAGhD,MAAO,MAAK,QACV,KAAK,MAAM,4BAA4B,sCAAkD,CACvF,CACE,WACA,UAAW,EAAW,SAAS,gBAC/B,WAAY,UAAU,UACtB,sBAAuB,EAA+B,SAMvD,aAAc,CACnB,GAAM,GAAiB,KAAK,MAAM,4BAA4B,sCAA4C,IAC1G,MAAO,MAAK,QAAe",
6
- "names": []
7
- }
package/dist/es/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- var c=(e,t,a)=>new Promise((n,r)=>{var o=i=>{try{u(a.next(i))}catch(l){r(l)}},s=i=>{try{u(a.throw(i))}catch(l){r(l)}},u=i=>i.done?n(i.value):Promise.resolve(i.value).then(o,s);u((a=a.apply(e,t)).next())});import{Extension as _}from"@magic-sdk/commons";var d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function p(e){let t,a=e.length%3,n="",r,o;function s(i){return d.charAt(i)}function u(i){return s(i>>18&63)+s(i>>12&63)+s(i>>6&63)+s(i&63)}for(t=0,o=e.length-a;t<o;t+=3)r=(e[t]<<16)+(e[t+1]<<8)+e[t+2],n+=u(r);switch(a){case 1:r=e[e.length-1],n+=s(r>>2),n+=s(r<<4&63),n+="==";break;case 2:r=(e[e.length-2]<<8)+e[e.length-1],n+=s(r>>10),n+=s(r>>4&63),n+=s(r<<2&63),n+="=";break;default:break}return n}function h(e){return p(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function b(e){return p(e).replace(/\+/g,"-").replace(/\//g,"_")}function y(e){return Array.from(e).map(function(t){return`0${t.toString(16)}`.substr(-2)}).join("")}var g=e=>{let t=new Uint8Array(e.response.attestationObject),a=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),r=e.getClientExtensionResults();return{id:e.id,rawId:h(n),type:e.type,attObj:h(t),clientData:h(a),registrationClientExtensions:JSON.stringify(r)}},f=e=>{let t=new Uint8Array(e.response.authenticatorData),a=new Uint8Array(e.response.clientDataJSON),n=new Uint8Array(e.rawId),r=new Uint8Array(e.response.signature),o=e.getClientExtensionResults();return{id:e.id,rawId:h(n),type:e.type,authData:b(t),clientData:b(a),signature:y(r),assertionClientExtensions:JSON.stringify(o)}};var w=class extends _.Internal{constructor(){super(...arguments);this.name="webauthn";this.config={}}createWebAuthnNotSupportError(){this.createError("WEBAUTHN_NOT_SUPPORTED","WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(t){this.createError("WEBAUTHN_CREATE_CREDENTIAL_ERROR",`Error creating credential: ${t}`,{})}registerNewUser(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:a,nickname:n=""}=t,r=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_registration_start",[{username:a}])),o;try{o=yield navigator.credentials.create({publicKey:r.credential_options})}catch(s){throw this.createWebAuthCreateCredentialError(s)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_register",[{id:r.id,nickname:n,transport:o.response.getTransports(),user_agent:navigator.userAgent,registration_response:g(o)}]))})}login(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:a}=t,n=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_web_authn",[{username:a}])),r;try{r=yield navigator.credentials.get({publicKey:n})}catch(o){throw this.createWebAuthCreateCredentialError(o)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_webauthn_verify",[{username:a,assertion_response:f(r)}]))})}updateInfo(t){let{id:a,nickname:n}=t,r=this.utils.createJsonRpcRequestPayload("magic_user_update_webauthn",[{webAuthnCredentialsId:a,nickname:n}]);return this.request(r)}unregisterDevice(t){let a=this.utils.createJsonRpcRequestPayload("magic_user_unregister_webauthn",[{webAuthnCredentialsId:t}]);return this.request(a)}registerNewDevice(t=""){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let a=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device_start",[])),n;try{n=yield navigator.credentials.create({publicKey:a.credential_options})}catch(r){throw this.createWebAuthCreateCredentialError(r)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device",[{nickname:t,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:g(n)}]))})}getMetadata(){let t=this.utils.createJsonRpcRequestPayload("magic_user_get_webauthn_credentials",[]);return this.request(t)}};export{w as WebAuthnExtension};
2
- //# sourceMappingURL=index.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/utils/webauthn.js"],
4
- "sourcesContent": ["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", "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"],
5
- "mappings": "6MAAA,+CCAA,GAAM,GAAS,mEAGf,WAAuB,EAAO,CAC5B,GAAI,GACE,EAAa,EAAM,OAAS,EAC9B,EAAS,GACT,EACA,EAEJ,WAAgB,EAAK,CACnB,MAA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AAK,MAAM,4BAA4B,wCAAgD,CACrF,CACE,WACA,mBAAoB,EAA4B,SAMjD,WAAW,EAAgD,CAChE,GAAM,CAAE,KAAI,YAAa,EACnB,EAAiB,KAAK,MAAM,4BAA4B,6BAA+C,CAC3G,CACE,sBAAuB,EACvB,cAGJ,MAAO,MAAK,QAAe,GAGtB,iBAAiB,EAAY,CAClC,GAAM,GAAiB,KAAK,MAAM,4BAA4B,iCAAoD,CAChH,CACE,sBAAuB,KAI3B,MAAO,MAAK,QAAa,GAGd,kBAAkB,EAAW,GAAI,gCAC5C,GAAI,CAAC,OAAO,oBACV,KAAM,MAAK,gCAEb,GAAM,GAAU,KAAM,MAAK,QACzB,KAAK,MAAM,4BAA4B,4CAAuD,KAG5F,EACJ,GAAI,CACF,EAAc,KAAM,WAAU,YAAY,OAAO,CAC/C,UAAW,EAAQ,2BAEd,EAAP,CACA,KAAM,MAAK,mCAAmC,GAGhD,MAAO,MAAK,QACV,KAAK,MAAM,4BAA4B,sCAAkD,CACvF,CACE,WACA,UAAW,EAAW,SAAS,gBAC/B,WAAY,UAAU,UACtB,sBAAuB,EAA+B,SAMvD,aAAc,CACnB,GAAM,GAAiB,KAAK,MAAM,4BAA4B,sCAA4C,IAC1G,MAAO,MAAK,QAAe",
6
- "names": []
7
- }
package/dist/extension.js DELETED
@@ -1,2 +0,0 @@
1
- var MagicWebAuthnExtension=(()=>{var m=Object.create;var c=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var x=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty;var f=e=>c(e,"__esModule",{value:!0});var N=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),q=(e,t)=>{for(var n in t)c(e,n,{get:t[n],enumerable:!0})},d=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of E(t))!v.call(e,r)&&(n||r!=="default")&&c(e,r,{get:()=>t[r],enumerable:!(i=C(t,r))||i.enumerable});return e},S=(e,t)=>d(f(c(e!=null?m(x(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),U=(e=>(t,n)=>e&&e.get(t)||(n=d(f({}),t,1),e&&e.set(t,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var h=(e,t,n)=>new Promise((i,r)=>{var o=a=>{try{u(n.next(a))}catch(g){r(g)}},s=a=>{try{u(n.throw(a))}catch(g){r(g)}},u=a=>a.done?i(a.value):Promise.resolve(a.value).then(o,s);u((n=n.apply(e,t)).next())});var _=N((T,y)=>{y.exports=Magic});var O={};q(O,{default:()=>J});var W=S(_());var D="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function w(e){let t,n=e.length%3,i="",r,o;function s(a){return D.charAt(a)}function u(a){return s(a>>18&63)+s(a>>12&63)+s(a>>6&63)+s(a&63)}for(t=0,o=e.length-n;t<o;t+=3)r=(e[t]<<16)+(e[t+1]<<8)+e[t+2],i+=u(r);switch(n){case 1:r=e[e.length-1],i+=s(r>>2),i+=s(r<<4&63),i+="==";break;case 2:r=(e[e.length-2]<<8)+e[e.length-1],i+=s(r>>10),i+=s(r>>4&63),i+=s(r<<2&63),i+="=";break;default:break}return i}function l(e){return w(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function A(e){return w(e).replace(/\+/g,"-").replace(/\//g,"_")}function I(e){return Array.from(e).map(function(t){return`0${t.toString(16)}`.substr(-2)}).join("")}var p=e=>{let t=new Uint8Array(e.response.attestationObject),n=new Uint8Array(e.response.clientDataJSON),i=new Uint8Array(e.rawId),r=e.getClientExtensionResults();return{id:e.id,rawId:l(i),type:e.type,attObj:l(t),clientData:l(n),registrationClientExtensions:JSON.stringify(r)}},R=e=>{let t=new Uint8Array(e.response.authenticatorData),n=new Uint8Array(e.response.clientDataJSON),i=new Uint8Array(e.rawId),r=new Uint8Array(e.response.signature),o=e.getClientExtensionResults();return{id:e.id,rawId:l(i),type:e.type,authData:A(t),clientData:A(n),signature:I(r),assertionClientExtensions:JSON.stringify(o)}};var b=class extends W.Extension.Internal{constructor(){super(...arguments);this.name="webauthn";this.config={}}createWebAuthnNotSupportError(){this.createError("WEBAUTHN_NOT_SUPPORTED","WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(t){this.createError("WEBAUTHN_CREATE_CREDENTIAL_ERROR",`Error creating credential: ${t}`,{})}registerNewUser(t){return h(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n,nickname:i=""}=t,r=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_registration_start",[{username:n}])),o;try{o=yield navigator.credentials.create({publicKey:r.credential_options})}catch(s){throw this.createWebAuthCreateCredentialError(s)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_register",[{id:r.id,nickname:i,transport:o.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(o)}]))})}login(t){return h(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n}=t,i=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_web_authn",[{username:n}])),r;try{r=yield navigator.credentials.get({publicKey:i})}catch(o){throw this.createWebAuthCreateCredentialError(o)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_webauthn_verify",[{username:n,assertion_response:R(r)}]))})}updateInfo(t){let{id:n,nickname:i}=t,r=this.utils.createJsonRpcRequestPayload("magic_user_update_webauthn",[{webAuthnCredentialsId:n,nickname:i}]);return this.request(r)}unregisterDevice(t){let n=this.utils.createJsonRpcRequestPayload("magic_user_unregister_webauthn",[{webAuthnCredentialsId:t}]);return this.request(n)}registerNewDevice(t=""){return h(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let n=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device_start",[])),i;try{i=yield navigator.credentials.create({publicKey:n.credential_options})}catch(r){throw this.createWebAuthCreateCredentialError(r)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device",[{nickname:t,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(i)}]))})}getMetadata(){let t=this.utils.createJsonRpcRequestPayload("magic_user_get_webauthn_credentials",[]);return this.request(t)}};var J=b;return U(O);})();
2
- if (MagicWebAuthnExtension && MagicWebAuthnExtension.default != null) { MagicWebAuthnExtension = Object.assign(MagicWebAuthnExtension.default, MagicWebAuthnExtension); delete MagicWebAuthnExtension.default; }
@@ -1,2 +0,0 @@
1
- var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var W=e=>g(e,"__esModule",{value:!0});var C=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},m=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of A(t))!R.call(e,r)&&(n||r!=="default")&&g(e,r,{get:()=>t[r],enumerable:!(a=w(t,r))||a.enumerable});return e};var E=(e=>(t,n)=>e&&e.get(t)||(n=m(W({}),t,1),e&&e.set(t,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var c=(e,t,n)=>new Promise((a,r)=>{var o=i=>{try{u(n.next(i))}catch(l){r(l)}},s=i=>{try{u(n.throw(i))}catch(l){r(l)}},u=i=>i.done?a(i.value):Promise.resolve(i.value).then(o,s);u((n=n.apply(e,t)).next())});var N={};C(N,{WebAuthnExtension:()=>_});var y=require("@magic-sdk/commons");var x="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function b(e){let t,n=e.length%3,a="",r,o;function s(i){return x.charAt(i)}function u(i){return s(i>>18&63)+s(i>>12&63)+s(i>>6&63)+s(i&63)}for(t=0,o=e.length-n;t<o;t+=3)r=(e[t]<<16)+(e[t+1]<<8)+e[t+2],a+=u(r);switch(n){case 1:r=e[e.length-1],a+=s(r>>2),a+=s(r<<4&63),a+="==";break;case 2:r=(e[e.length-2]<<8)+e[e.length-1],a+=s(r>>10),a+=s(r>>4&63),a+=s(r<<2&63),a+="=";break;default:break}return a}function h(e){return b(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function f(e){return b(e).replace(/\+/g,"-").replace(/\//g,"_")}function v(e){return Array.from(e).map(function(t){return`0${t.toString(16)}`.substr(-2)}).join("")}var p=e=>{let t=new Uint8Array(e.response.attestationObject),n=new Uint8Array(e.response.clientDataJSON),a=new Uint8Array(e.rawId),r=e.getClientExtensionResults();return{id:e.id,rawId:h(a),type:e.type,attObj:h(t),clientData:h(n),registrationClientExtensions:JSON.stringify(r)}},d=e=>{let t=new Uint8Array(e.response.authenticatorData),n=new Uint8Array(e.response.clientDataJSON),a=new Uint8Array(e.rawId),r=new Uint8Array(e.response.signature),o=e.getClientExtensionResults();return{id:e.id,rawId:h(a),type:e.type,authData:f(t),clientData:f(n),signature:v(r),assertionClientExtensions:JSON.stringify(o)}};var _=class extends y.Extension.Internal{constructor(){super(...arguments);this.name="webauthn";this.config={}}createWebAuthnNotSupportError(){this.createError("WEBAUTHN_NOT_SUPPORTED","WebAuthn is not supported in this device.",{})}createWebAuthCreateCredentialError(t){this.createError("WEBAUTHN_CREATE_CREDENTIAL_ERROR",`Error creating credential: ${t}`,{})}registerNewUser(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n,nickname:a=""}=t,r=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_registration_start",[{username:n}])),o;try{o=yield navigator.credentials.create({publicKey:r.credential_options})}catch(s){throw this.createWebAuthCreateCredentialError(s)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_webauthn_register",[{id:r.id,nickname:a,transport:o.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(o)}]))})}login(t){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let{username:n}=t,a=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_web_authn",[{username:n}])),r;try{r=yield navigator.credentials.get({publicKey:a})}catch(o){throw this.createWebAuthCreateCredentialError(o)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_login_with_webauthn_verify",[{username:n,assertion_response:d(r)}]))})}updateInfo(t){let{id:n,nickname:a}=t,r=this.utils.createJsonRpcRequestPayload("magic_user_update_webauthn",[{webAuthnCredentialsId:n,nickname:a}]);return this.request(r)}unregisterDevice(t){let n=this.utils.createJsonRpcRequestPayload("magic_user_unregister_webauthn",[{webAuthnCredentialsId:t}]);return this.request(n)}registerNewDevice(t=""){return c(this,null,function*(){if(!window.PublicKeyCredential)throw this.createWebAuthnNotSupportError();let n=yield this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device_start",[])),a;try{a=yield navigator.credentials.create({publicKey:n.credential_options})}catch(r){throw this.createWebAuthCreateCredentialError(r)}return this.request(this.utils.createJsonRpcRequestPayload("magic_auth_register_webauthn_device",[{nickname:t,transport:a.response.getTransports(),user_agent:navigator.userAgent,registration_response:p(a)}]))})}getMetadata(){let t=this.utils.createJsonRpcRequestPayload("magic_user_get_webauthn_credentials",[]);return this.request(t)}};module.exports=E(N);
2
- //# sourceMappingURL=index.native.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/index.native.ts", "../../src/index.ts", "../../src/utils/webauthn.js"],
4
- "sourcesContent": ["export * from './index';\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", "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"],
5
- "mappings": "2tBAAA,wCCAA,MAA0B,8BCA1B,GAAM,GAAS,mEAGf,WAAuB,EAAO,CAC5B,GAAI,GACE,EAAa,EAAM,OAAS,EAC9B,EAAS,GACT,EACA,EAEJ,WAAgB,EAAK,CACnB,MAAO,GAAO,OAAO,GAGvB,WAAyB,EAAK,CAC5B,MAAO,GAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,GAAM,IAAQ,EAAQ,GAAO,EAAK,IAAQ,EAAO,EAAM,IAI5G,IAAK,EAAI,EAAG,EAAS,EAAM,OAAS,EAAY,EAAI,EAAQ,GAAK,EAC/D,EAAQ,GAAM,IAAM,IAAO,GAAM,EAAI,IAAM,GAAK,EAAM,EAAI,GAC1D,GAAU,EAAgB,GAI5B,OAAQ,OACD,GACH,EAAO,EAAM,EAAM,OAAS,GAC5B,GAAU,EAAO,GAAQ,GACzB,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,KACV,UACG,GACH,EAAQ,GAAM,EAAM,OAAS,IAAM,GAAK,EAAM,EAAM,OAAS,GAC7D,GAAU,EAAO,GAAQ,IACzB,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,EAAQ,GAAQ,EAAK,IAC/B,GAAU,IACV,cAEA,MAGJ,MAAO,GAIT,WAAgB,EAAK,CACnB,MAAO,GAAc,GAAK,QAAQ,MAAO,KAAK,QAAQ,MAAO,KAAK,QAAQ,KAAM,IAIlF,WAAmB,EAAK,CACtB,MAAO,GAAc,GAAK,QAAQ,MAAO,KAAK,QAAQ,MAAO,KAI/D,WAAmB,EAAK,CACtB,MAAO,OAAM,KAAK,GACf,IAAI,SAAU,EAAG,CAChB,MAAO,IAAI,EAAE,SAAS,MAAM,OAAO,MAEpC,KAAK,IASH,GAAM,GA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sBAAuB,EAA+B,SAMjD,MAAM,EAA+C,gCAChE,GAAI,CAAC,OAAO,oBACV,KAAM,MAAK,gCAEb,GAAM,CAAE,YAAa,EAEf,EAAsC,KAAM,MAAK,QACrD,KAAK,MAAM,4BAA4B,kCAA8C,CAAC,CAAE,eAGtF,EACJ,GAAI,CACF,EAAa,KAAM,WAAU,YAAY,IAAI,CAC3C,UAAW,UAEN,EAAP,CACA,KAAM,MAAK,mCAAmC,GAGhD,MAAO,MAAK,QACV,KAAK,MAAM,4BAA4B,wCAAgD,CACrF,CACE,WACA,mBAAoB,EAA4B,SAMjD,WAAW,EAAgD,CAChE,GAAM,CAAE,KAAI,YAAa,EACnB,EAAiB,KAAK,MAAM,4BAA4B,6BAA+C,CAC3G,CACE,sBAAuB,EACvB,cAGJ,MAAO,MAAK,QAAe,GAGtB,iBAAiB,EAAY,CAClC,GAAM,GAAiB,KAAK,MAAM,4BAA4B,iCAAoD,CAChH,CACE,sBAAuB,KAI3B,MAAO,MAAK,QAAa,GAGd,kBAAkB,EAAW,GAAI,gCAC5C,GAAI,CAAC,OAAO,oBACV,KAAM,MAAK,gCAEb,GAAM,GAAU,KAAM,MAAK,QACzB,KAAK,MAAM,4BAA4B,4CAAuD,KAG5F,EACJ,GAAI,CACF,EAAc,KAAM,WAAU,YAAY,OAAO,CAC/C,UAAW,EAAQ,2BAEd,EAAP,CACA,KAAM,MAAK,mCAAmC,GAGhD,MAAO,MAAK,QACV,KAAK,MAAM,4BAA4B,sCAAkD,CACvF,CACE,WACA,UAAW,EAAW,SAAS,gBAC/B,WAAY,UAAU,UACtB,sBAAuB,EAA+B,SAMvD,aAAc,CACnB,GAAM,GAAiB,KAAK,MAAM,4BAA4B,sCAA4C,IAC1G,MAAO,MAAK,QAAe",
6
- "names": []
7
- }
@@ -1,2 +0,0 @@
1
- import { WebAuthnExtension } from './index';
2
- export default WebAuthnExtension;
@@ -1,26 +0,0 @@
1
- import { Extension } from '@magic-sdk/commons';
2
- import { RegisterNewUserConfiguration, LoginWithWebAuthnConfiguration, UpdateWebAuthnInfoConfiguration } from './types';
3
- export declare class WebAuthnExtension extends Extension.Internal<'webauthn', any> {
4
- name: "webauthn";
5
- config: any;
6
- private createWebAuthnNotSupportError;
7
- private createWebAuthCreateCredentialError;
8
- registerNewUser(configuration: RegisterNewUserConfiguration): Promise<string | null>;
9
- login(configuration: LoginWithWebAuthnConfiguration): Promise<string | null>;
10
- updateInfo(configuration: UpdateWebAuthnInfoConfiguration): import("@magic-sdk/commons").PromiEvent<any[], {
11
- done: (result: any[]) => void;
12
- error: (reason: any) => void;
13
- settled: () => void;
14
- }>;
15
- unregisterDevice(id: string): import("@magic-sdk/commons").PromiEvent<any, {
16
- done: (result: any) => void;
17
- error: (reason: any) => void;
18
- settled: () => void;
19
- }>;
20
- registerNewDevice(nickname?: string): Promise<string | null>;
21
- getMetadata(): import("@magic-sdk/commons").PromiEvent<any[], {
22
- done: (result: any[]) => void;
23
- error: (reason: any) => void;
24
- settled: () => void;
25
- }>;
26
- }
@@ -1 +0,0 @@
1
- export * from './index';
@@ -1,41 +0,0 @@
1
- export interface RegisterNewUserConfiguration {
2
- /**
3
- * The username of the user attempting to register.
4
- */
5
- username: string;
6
- /**
7
- * The nickname that the user attempts to set to this webauthn device.
8
- */
9
- nickname?: string;
10
- }
11
- export interface LoginWithWebAuthnConfiguration {
12
- /**
13
- * The username of the user attempting to register.
14
- */
15
- username: string;
16
- }
17
- export interface UpdateWebAuthnInfoConfiguration {
18
- /**
19
- * WebAuthn info id
20
- */
21
- id: string;
22
- /**
23
- * nickname that user attempts to update to the webauth device associate to the id.
24
- */
25
- nickname: string;
26
- }
27
- export declare enum MagicWebAuthnPayloadMethod {
28
- WebAuthnRegistrationStart = "magic_auth_webauthn_registration_start",
29
- RegisterWithWebAuth = "magic_auth_webauthn_register",
30
- LoginWithWebAuthn = "magic_auth_login_with_web_authn",
31
- WebAuthnLoginVerify = "magic_auth_login_with_webauthn_verify",
32
- GetWebAuthnInfo = "magic_user_get_webauthn_credentials",
33
- UpdateWebAuthnInfo = "magic_user_update_webauthn",
34
- UnregisterWebAuthDevice = "magic_user_unregister_webauthn",
35
- RegisterWebAuthDeviceStart = "magic_auth_register_webauthn_device_start",
36
- RegisterWebAuthDevice = "magic_auth_register_webauthn_device"
37
- }
38
- export declare enum WebAuthnSDKErrorCode {
39
- WebAuthnNotSupported = "WEBAUTHN_NOT_SUPPORTED",
40
- WebAuthnCreateCredentialError = "WEBAUTHN_CREATE_CREDENTIAL_ERROR"
41
- }
@@ -1,17 +0,0 @@
1
- export function transformNewAssertionForServer(newAssertion: PublicKeyCredential): {
2
- id: string;
3
- rawId: string;
4
- type: string;
5
- attObj: string;
6
- clientData: string;
7
- registrationClientExtensions: string;
8
- };
9
- export function transformAssertionForServer(newAssertion: PublicKeyCredential): {
10
- id: string;
11
- rawId: string;
12
- type: string;
13
- authData: string;
14
- clientData: string;
15
- signature: string;
16
- assertionClientExtensions: string;
17
- };