@magic-ext/webauthn 1.0.0 → 2.1.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,36 @@
1
+ # v2.0.1 (Tue Jan 25 2022)
2
+
3
+ #### 🐛 Bug Fix
4
+
5
+ - Fix CDN build targets [#270](https://github.com/magiclabs/magic-js/pull/270) ([@smithki](https://github.com/smithki))
6
+ - Fix CDN issues related to ESBuild ([@smithki](https://github.com/smithki))
7
+
8
+ #### Authors: 1
9
+
10
+ - Ian K Smith ([@smithki](https://github.com/smithki))
11
+
12
+ ---
13
+
14
+ # v2.0.0 (Tue Jan 25 2022)
15
+
16
+ #### 💥 Breaking Change
17
+
18
+ - Switch from `microbundle` to `esbuild` [#220](https://github.com/magiclabs/magic-js/pull/220) ([@smithki](https://github.com/smithki))
19
+
20
+ #### 🐛 Bug Fix
21
+
22
+ - Merge with master ([@smithki](https://github.com/smithki))
23
+ - Merge branch 'master' into feat/faster-builds ([@smithki](https://github.com/smithki))
24
+ - Modify CDN default exports ([@smithki](https://github.com/smithki))
25
+ - Fix build errors related to isolatedModules ([@smithki](https://github.com/smithki))
26
+ - Replace microbundle with ESBuild ([@smithki](https://github.com/smithki))
27
+
28
+ #### Authors: 1
29
+
30
+ - Ian K Smith ([@smithki](https://github.com/smithki))
31
+
32
+ ---
33
+
1
34
  # v0.2.4 (Thu Oct 21 2021)
2
35
 
3
36
  #### 🐛 Bug Fix
package/dist/cjs/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var e,t,r=require("@magic-sdk/commons");function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e){var t,r,n,i,a=e.length%3,o="";function u(e){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)}for(t=0,n=e.length-a;t<n;t+=3)o+=u((i=r=(e[t]<<16)+(e[t+1]<<8)+e[t+2])>>18&63)+u(i>>12&63)+u(i>>6&63)+u(63&i);switch(a){case 1:o+=u((r=e[e.length-1])>>2),o+=u(r<<4&63),o+="==";break;case 2:o+=u((r=(e[e.length-2]<<8)+e[e.length-1])>>10),o+=u(r>>4&63),o+=u(r<<2&63),o+="="}return o}function a(e){return i(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function o(e){return i(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"}(e||(e={})),function(e){e.WebAuthnNotSupported="WEBAUTHN_NOT_SUPPORTED",e.WebAuthnCreateCredentialError="WEBAUTHN_CREATE_CREDENTIAL_ERROR"}(t||(t={}));var u=function(e){var 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)}};function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}exports.WebAuthnExtension=function(r){var i,c;function h(){for(var e,t=arguments.length,n=new Array(t),i=0;i<t;i++)n[i]=arguments[i];return(e=r.call.apply(r,[this].concat(n))||this).name="webauthn",e.config={},e}c=r,(i=h).prototype=Object.create(c.prototype),i.prototype.constructor=i,n(i,c);var l=h.prototype;return l.createWebAuthnNotSupportError=function(){this.createError(t.WebAuthnNotSupported,"WebAuthn is not supported in this device.",{})},l.createWebAuthCreateCredentialError=function(e){this.createError(t.WebAuthnCreateCredentialError,"Error creating credential: "+e,{})},l.registerNewUser=function(t){try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();var n=t.nickname,i=void 0===n?"":n;return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.WebAuthnRegistrationStart,[{username:t.username}]))).then(function(t){var n;function a(a){return r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWithWebAuth,[{id:t.id,nickname:i,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(n)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:t.credential_options})).then(function(e){n=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.login=function(t){try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();var n=t.username;return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.LoginWithWebAuthn,[{username:n}]))).then(function(t){var i;function u(t){return r.request(r.utils.createJsonRpcRequestPayload(e.WebAuthnLoginVerify,[{username:n,assertion_response:(u=i,c=new Uint8Array(u.response.authenticatorData),h=new Uint8Array(u.response.clientDataJSON),l=new Uint8Array(u.rawId),g=new Uint8Array(u.response.signature),p=u.getClientExtensionResults(),{id:u.id,rawId:a(l),type:u.type,authData:o(c),clientData:o(h),signature:(s=g,Array.from(s).map(function(e){return("0"+e.toString(16)).substr(-2)}).join("")),assertionClientExtensions:JSON.stringify(p)})}]));var u,s,c,h,l,g,p}var c=s(function(){return Promise.resolve(navigator.credentials.get({publicKey:t})).then(function(e){i=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return c&&c.then?c.then(u):u()})}catch(e){return Promise.reject(e)}},l.updateInfo=function(t){var r=this.utils.createJsonRpcRequestPayload(e.UpdateWebAuthnInfo,[{webAuthnCredentialsId:t.id,nickname:t.nickname}]);return this.request(r)},l.unregisterDevice=function(t){var r=this.utils.createJsonRpcRequestPayload(e.UnregisterWebAuthDevice,[{webAuthnCredentialsId:t}]);return this.request(r)},l.registerNewDevice=function(t){void 0===t&&(t="");try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWebAuthDeviceStart,[]))).then(function(n){var i;function a(n){return r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWebAuthDevice,[{nickname:t,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(i)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:n.credential_options})).then(function(e){i=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.getMetadata=function(){var t=this.utils.createJsonRpcRequestPayload(e.GetWebAuthnInfo,[]);return this.request(t)},h}(r.Extension.Internal);
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
2
  //# sourceMappingURL=index.js.map
@@ -1 +1,7 @@
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","temp","length","num","extraBytes","output","encode","charAt","b64enc","buf","replace","b64RawEnc","transformNewAssertionForServer","newAssertion","attObj","Uint8Array","response","attestationObject","clientDataJSON","rawId","registrationClientExtensions","getClientExtensionResults","id","type","clientData","JSON","stringify","name","config","createWebAuthnNotSupportError","this","createError","WebAuthnNotSupported","createWebAuthCreateCredentialError","message","WebAuthnCreateCredentialError","registerNewUser","configuration","window","PublicKeyCredential","nickname","_this3","request","utils","createJsonRpcRequestPayload","WebAuthnRegistrationStart","username","options","credential","RegisterWithWebAuth","transport","getTransports","user_agent","navigator","userAgent","registration_response","credentials","create","publicKey","credential_options","err","login","_this5","LoginWithWebAuthn","transformedCredentialRequestOptions","assertion","WebAuthnLoginVerify","assertion_response","authData","authenticatorData","sig","signature","assertionClientExtensions","Array","from","map","x","toString","substr","join","get","updateInfo","requestPayload","UpdateWebAuthnInfo","webAuthnCredentialsId","unregisterDevice","UnregisterWebAuthDevice","registerNewDevice","_this7","RegisterWebAuthDeviceStart","RegisterWebAuthDevice","getMetadata","GetWebAuthnInfo","Extension","Internal"],"mappings":"IA+BYA,EAYAC,8HCxCZ,SAASC,EAAcC,GACrB,IAAIC,EAGAC,EACAC,EAMqBC,EATnBC,EAAaL,EAAMG,OAAS,EAC9BG,EAAS,GAIb,SAASC,EAAOH,GACd,MAXW,mEAWGI,OAAOJ,GAQvB,IAAKH,EAAI,EAAGE,EAASH,EAAMG,OAASE,EAAYJ,EAAIE,EAAQF,GAAK,EAE/DK,GANOC,GADgBH,EAMvBF,GAAQF,EAAMC,IAAM,KAAOD,EAAMC,EAAI,IAAM,GAAKD,EAAMC,EAAI,KALpC,GAAM,IAAQM,EAAQH,GAAO,GAAM,IAAQG,EAAQH,GAAO,EAAK,IAAQG,EAAa,GAANH,GAUtG,OAAQC,GACN,OAEEC,GAAUC,GADVL,EAAOF,EAAMA,EAAMG,OAAS,KACH,GACzBG,GAAUC,EAAQL,GAAQ,EAAK,IAC/BI,GAAU,KACV,MACF,OAEEA,GAAUC,GADVL,GAAQF,EAAMA,EAAMG,OAAS,IAAM,GAAKH,E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4BAA4BhD,EAA2BuF,mBAAoB,CAC3G,CACEC,sBAHqB/C,EAAjBf,GAIJkB,SAJqBH,EAAbG,YAOZ,YAAYE,QAAewC,MAGtBG,iBAAA,SAAiB/D,GACtB,IAAM4D,EAAiBpD,KAAKa,MAAMC,4BAA4BhD,EAA2B0F,wBAAyB,CAChH,CACEF,sBAAuB9D,KAI3B,YAAYoB,QAAawC,MAGdK,2BAAkB/C,YAAAA,IAAAA,EAAW,cAEhCV,KADR,IAAKQ,OAAOC,oBACV,QAAWV,uDAES2D,EAAK9C,QACzB8C,EAAK7C,MAAMC,4BAA4BhD,EAA2B6F,2BAA4B,oBAD1F1C,OAIFC,uBASGwC,EAAK9C,QACV8C,EAAK7C,MAAMC,4BAA4BhD,EAA2B8F,sBAAuB,CACvF,CACElD,SAAAA,EACAU,UAAWF,EAAWhC,SAASmC,gBAC/BC,WAAYC,UAAUC,UACtBC,sBAAuB3C,EAA+BoC,iDAbtCK,UAAUG,YAAYC,OAAO,CAC/CC,UAAWX,EAAQY,uCADrBX,gBAGOY,GACP,QAAW3B,mCAAmC2B,sCA5GpD,sCA2HS+B,YAAA,WACL,IAAMT,EAAiBpD,KAAKa,MAAMC,4BAA4BhD,EAA2BgG,gBAAiB,IAC1G,YAAYlD,QAAewC,OA7HQW,YAAUC"}
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,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,GA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UACtB,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
+ }
package/dist/es/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{Extension as e}from"@magic-sdk/commons";function t(e,r){return(t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,r)}var r,n;function i(e){var t,r,n,i,a=e.length%3,o="";function u(e){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)}for(t=0,n=e.length-a;t<n;t+=3)o+=u((i=r=(e[t]<<16)+(e[t+1]<<8)+e[t+2])>>18&63)+u(i>>12&63)+u(i>>6&63)+u(63&i);switch(a){case 1:o+=u((r=e[e.length-1])>>2),o+=u(r<<4&63),o+="==";break;case 2:o+=u((r=(e[e.length-2]<<8)+e[e.length-1])>>10),o+=u(r>>4&63),o+=u(r<<2&63),o+="="}return o}function a(e){return i(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function o(e){return i(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"}(r||(r={})),function(e){e.WebAuthnNotSupported="WEBAUTHN_NOT_SUPPORTED",e.WebAuthnCreateCredentialError="WEBAUTHN_CREATE_CREDENTIAL_ERROR"}(n||(n={}));var u=function(e){var 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)}};function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var c=function(e){var i,c;function h(){for(var t,r=arguments.length,n=new Array(r),i=0;i<r;i++)n[i]=arguments[i];return(t=e.call.apply(e,[this].concat(n))||this).name="webauthn",t.config={},t}c=e,(i=h).prototype=Object.create(c.prototype),i.prototype.constructor=i,t(i,c);var l=h.prototype;return l.createWebAuthnNotSupportError=function(){this.createError(n.WebAuthnNotSupported,"WebAuthn is not supported in this device.",{})},l.createWebAuthCreateCredentialError=function(e){this.createError(n.WebAuthnCreateCredentialError,"Error creating credential: "+e,{})},l.registerNewUser=function(e){try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();var n=e.nickname,i=void 0===n?"":n;return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.WebAuthnRegistrationStart,[{username:e.username}]))).then(function(e){var n;function a(a){return t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWithWebAuth,[{id:e.id,nickname:i,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(n)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:e.credential_options})).then(function(e){n=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.login=function(e){try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();var n=e.username;return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.LoginWithWebAuthn,[{username:n}]))).then(function(e){var i;function u(e){return t.request(t.utils.createJsonRpcRequestPayload(r.WebAuthnLoginVerify,[{username:n,assertion_response:(u=i,c=new Uint8Array(u.response.authenticatorData),h=new Uint8Array(u.response.clientDataJSON),l=new Uint8Array(u.rawId),g=new Uint8Array(u.response.signature),p=u.getClientExtensionResults(),{id:u.id,rawId:a(l),type:u.type,authData:o(c),clientData:o(h),signature:(s=g,Array.from(s).map(function(e){return("0"+e.toString(16)).substr(-2)}).join("")),assertionClientExtensions:JSON.stringify(p)})}]));var u,s,c,h,l,g,p}var c=s(function(){return Promise.resolve(navigator.credentials.get({publicKey:e})).then(function(e){i=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return c&&c.then?c.then(u):u()})}catch(e){return Promise.reject(e)}},l.updateInfo=function(e){var t=this.utils.createJsonRpcRequestPayload(r.UpdateWebAuthnInfo,[{webAuthnCredentialsId:e.id,nickname:e.nickname}]);return this.request(t)},l.unregisterDevice=function(e){var t=this.utils.createJsonRpcRequestPayload(r.UnregisterWebAuthDevice,[{webAuthnCredentialsId:e}]);return this.request(t)},l.registerNewDevice=function(e){void 0===e&&(e="");try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWebAuthDeviceStart,[]))).then(function(n){var i;function a(n){return t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWebAuthDevice,[{nickname:e,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(i)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:n.credential_options})).then(function(e){i=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.getMetadata=function(){var e=this.utils.createJsonRpcRequestPayload(r.GetWebAuthnInfo,[]);return this.request(e)},h}(e.Internal);export{c as WebAuthnExtension};
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
2
  //# sourceMappingURL=index.js.map
@@ -1 +1,7 @@
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","temp","length","num","extraBytes","output","encode","charAt","b64enc","buf","replace","b64RawEnc","transformNewAssertionForServer","newAssertion","attObj","Uint8Array","response","attestationObject","clientDataJSON","rawId","registrationClientExtensions","getClientExtensionResults","id","type","clientData","JSON","stringify","WebAuthnExtension","name","config","createWebAuthnNotSupportError","this","createError","WebAuthnNotSupported","createWebAuthCreateCredentialError","message","WebAuthnCreateCredentialError","registerNewUser","configuration","window","PublicKeyCredential","nickname","_this3","request","utils","createJsonRpcRequestPayload","WebAuthnRegistrationStart","username","options","credential","RegisterWithWebAuth","transport","getTransports","user_agent","navigator","userAgent","registration_response","credentials","create","publicKey","credential_options","err","login","_this5","LoginWithWebAuthn","transformedCredentialRequestOptions","assertion","WebAuthnLoginVerify","assertion_response","authData","authenticatorData","sig","signature","assertionClientExtensions","Array","from","map","x","toString","substr","join","get","updateInfo","requestPayload","UpdateWebAuthnInfo","webAuthnCredentialsId","unregisterDevice","UnregisterWebAuthDevice","registerNewDevice","_this7","RegisterWebAuthDeviceStart","RegisterWebAuthDevice","getMetadata","GetWebAuthnInfo","Extension","Internal"],"mappings":"+IA+BYA,EAYAC,ECxCZ,SAASC,EAAcC,GACrB,IAAIC,EAGAC,EACAC,EAMqBC,EATnBC,EAAaL,EAAMG,OAAS,EAC9BG,EAAS,GAIb,SAASC,EAAOH,GACd,MAXW,mEAWGI,OAAOJ,GAQvB,IAAKH,EAAI,EAAGE,EAASH,EAAMG,OAASE,EAAYJ,EAAIE,EAAQF,GAAK,EAE/DK,GANOC,GADgBH,EAMvBF,GAAQF,EAAMC,IAAM,KAAOD,EAAMC,EAAI,IAAM,GAAKD,EAAMC,EAAI,KALpC,GAAM,IAAQM,EAAQH,GAAO,GAAM,IAAQG,EAAQH,GAAO,EAAK,IAAQG,EAAa,GAANH,GAUtG,OAAQC,GACN,OAEEC,GAAUC,GADVL,EAAOF,EAAMA,EAAMG,OAAS,KACH,GACzBG,GAAUC,EAAQL,GAAQ,EAAK,IAC/BI,GAAU,KACV,MACF,OAEEA,GAAUC,GADVL,GAAQF,EAAMA,EAAMG,OAAS,IAAM,GAAKH,EAAMA,EAAMG,OAAS,KACpC,IACzBG,GAAUC,EAAQL,GAAQ,EAAK,IAC/BI,GAAUC,EAAQL,GAAQ,EAAK,IAC/BI,GAAU,IAMd,OAAOA,EAIT,SAASG,EAAOC,GACd,OAAOX,EAAcW,GAAKC,QAAQ,MAAO,KAAKA,QAAQ,MAAO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B2F,wBAAyB,CAChH,CACEF,sBAAuB/D,KAI3B,YAAYqB,QAAawC,MAGdK,2BAAkB/C,YAAAA,IAAAA,EAAW,cAEhCV,KADR,IAAKQ,OAAOC,oBACV,QAAWV,uDAES2D,EAAK9C,QACzB8C,EAAK7C,MAAMC,4BAA4BjD,EAA2B8F,2BAA4B,oBAD1F1C,OAIFC,uBASGwC,EAAK9C,QACV8C,EAAK7C,MAAMC,4BAA4BjD,EAA2B+F,sBAAuB,CACvF,CACElD,SAAAA,EACAU,UAAWF,EAAWjC,SAASoC,gBAC/BC,WAAYC,UAAUC,UACtBC,sBAAuB5C,EAA+BqC,iDAbtCK,UAAUG,YAAYC,OAAO,CAC/CC,UAAWX,EAAQY,uCADrBX,gBAGOY,GACP,QAAW3B,mCAAmC2B,sCA5GpD,sCA2HS+B,YAAA,WACL,IAAMT,EAAiBpD,KAAKa,MAAMC,4BAA4BjD,EAA2BiG,gBAAiB,IAC1G,YAAYlD,QAAewC,OA7HQW,EAAUC"}
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,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AAI,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
+ }
@@ -0,0 +1,2 @@
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
@@ -0,0 +1,7 @@
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,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,QA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AC1G,MAAO,MAAK,QAAe",
6
+ "names": []
7
+ }
package/dist/extension.js CHANGED
@@ -1 +1,2 @@
1
- var MagicWebAuthnExtension=function(e){function t(e,r){return(t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,r)}var r,n;function i(e){var t,r,n,i,a=e.length%3,u="";function o(e){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)}for(t=0,n=e.length-a;t<n;t+=3)u+=o((i=r=(e[t]<<16)+(e[t+1]<<8)+e[t+2])>>18&63)+o(i>>12&63)+o(i>>6&63)+o(63&i);switch(a){case 1:u+=o((r=e[e.length-1])>>2),u+=o(r<<4&63),u+="==";break;case 2:u+=o((r=(e[e.length-2]<<8)+e[e.length-1])>>10),u+=o(r>>4&63),u+=o(r<<2&63),u+="="}return u}function a(e){return i(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function u(e){return i(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"}(r||(r={})),function(e){e.WebAuthnNotSupported="WEBAUTHN_NOT_SUPPORTED",e.WebAuthnCreateCredentialError="WEBAUTHN_CREATE_CREDENTIAL_ERROR"}(n||(n={}));var o=function(e){var 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)}};function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}return function(e){var i,c;function h(){for(var t,r=arguments.length,n=new Array(r),i=0;i<r;i++)n[i]=arguments[i];return(t=e.call.apply(e,[this].concat(n))||this).name="webauthn",t.config={},t}c=e,(i=h).prototype=Object.create(c.prototype),i.prototype.constructor=i,t(i,c);var l=h.prototype;return l.createWebAuthnNotSupportError=function(){this.createError(n.WebAuthnNotSupported,"WebAuthn is not supported in this device.",{})},l.createWebAuthCreateCredentialError=function(e){this.createError(n.WebAuthnCreateCredentialError,"Error creating credential: "+e,{})},l.registerNewUser=function(e){try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();var n=e.nickname,i=void 0===n?"":n;return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.WebAuthnRegistrationStart,[{username:e.username}]))).then(function(e){var n;function a(a){return t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWithWebAuth,[{id:e.id,nickname:i,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:o(n)}]))}var u=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:e.credential_options})).then(function(e){n=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return u&&u.then?u.then(a):a()})}catch(e){return Promise.reject(e)}},l.login=function(e){try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();var n=e.username;return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.LoginWithWebAuthn,[{username:n}]))).then(function(e){var i;function o(e){return t.request(t.utils.createJsonRpcRequestPayload(r.WebAuthnLoginVerify,[{username:n,assertion_response:(o=i,c=new Uint8Array(o.response.authenticatorData),h=new Uint8Array(o.response.clientDataJSON),l=new Uint8Array(o.rawId),g=new Uint8Array(o.response.signature),p=o.getClientExtensionResults(),{id:o.id,rawId:a(l),type:o.type,authData:u(c),clientData:u(h),signature:(s=g,Array.from(s).map(function(e){return("0"+e.toString(16)).substr(-2)}).join("")),assertionClientExtensions:JSON.stringify(p)})}]));var o,s,c,h,l,g,p}var c=s(function(){return Promise.resolve(navigator.credentials.get({publicKey:e})).then(function(e){i=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return c&&c.then?c.then(o):o()})}catch(e){return Promise.reject(e)}},l.updateInfo=function(e){var t=this.utils.createJsonRpcRequestPayload(r.UpdateWebAuthnInfo,[{webAuthnCredentialsId:e.id,nickname:e.nickname}]);return this.request(t)},l.unregisterDevice=function(e){var t=this.utils.createJsonRpcRequestPayload(r.UnregisterWebAuthDevice,[{webAuthnCredentialsId:e}]);return this.request(t)},l.registerNewDevice=function(e){void 0===e&&(e="");try{var t=this;if(!window.PublicKeyCredential)throw t.createWebAuthnNotSupportError();return Promise.resolve(t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWebAuthDeviceStart,[]))).then(function(n){var i;function a(n){return t.request(t.utils.createJsonRpcRequestPayload(r.RegisterWebAuthDevice,[{nickname:e,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:o(i)}]))}var u=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:n.credential_options})).then(function(e){i=e})},function(e){throw t.createWebAuthCreateCredentialError(e)});return u&&u.then?u.then(a):a()})}catch(e){return Promise.reject(e)}},l.getMetadata=function(){var e=this.utils.createJsonRpcRequestPayload(r.GetWebAuthnInfo,[]);return this.request(e)},h}(e.Extension.Internal)}(Magic);
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 +1,2 @@
1
- var e,t,r=require("@magic-sdk/commons");function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e){var t,r,n,i,a=e.length%3,o="";function u(e){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)}for(t=0,n=e.length-a;t<n;t+=3)o+=u((i=r=(e[t]<<16)+(e[t+1]<<8)+e[t+2])>>18&63)+u(i>>12&63)+u(i>>6&63)+u(63&i);switch(a){case 1:o+=u((r=e[e.length-1])>>2),o+=u(r<<4&63),o+="==";break;case 2:o+=u((r=(e[e.length-2]<<8)+e[e.length-1])>>10),o+=u(r>>4&63),o+=u(r<<2&63),o+="="}return o}function a(e){return i(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function o(e){return i(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"}(e||(e={})),function(e){e.WebAuthnNotSupported="WEBAUTHN_NOT_SUPPORTED",e.WebAuthnCreateCredentialError="WEBAUTHN_CREATE_CREDENTIAL_ERROR"}(t||(t={}));var u=function(e){var 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)}};function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}exports.WebAuthnExtension=function(r){var i,c;function h(){for(var e,t=arguments.length,n=new Array(t),i=0;i<t;i++)n[i]=arguments[i];return(e=r.call.apply(r,[this].concat(n))||this).name="webauthn",e.config={},e}c=r,(i=h).prototype=Object.create(c.prototype),i.prototype.constructor=i,n(i,c);var l=h.prototype;return l.createWebAuthnNotSupportError=function(){this.createError(t.WebAuthnNotSupported,"WebAuthn is not supported in this device.",{})},l.createWebAuthCreateCredentialError=function(e){this.createError(t.WebAuthnCreateCredentialError,"Error creating credential: "+e,{})},l.registerNewUser=function(t){try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();var n=t.nickname,i=void 0===n?"":n;return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.WebAuthnRegistrationStart,[{username:t.username}]))).then(function(t){var n;function a(a){return r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWithWebAuth,[{id:t.id,nickname:i,transport:n.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(n)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:t.credential_options})).then(function(e){n=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.login=function(t){try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();var n=t.username;return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.LoginWithWebAuthn,[{username:n}]))).then(function(t){var i;function u(t){return r.request(r.utils.createJsonRpcRequestPayload(e.WebAuthnLoginVerify,[{username:n,assertion_response:(u=i,c=new Uint8Array(u.response.authenticatorData),h=new Uint8Array(u.response.clientDataJSON),l=new Uint8Array(u.rawId),g=new Uint8Array(u.response.signature),p=u.getClientExtensionResults(),{id:u.id,rawId:a(l),type:u.type,authData:o(c),clientData:o(h),signature:(s=g,Array.from(s).map(function(e){return("0"+e.toString(16)).substr(-2)}).join("")),assertionClientExtensions:JSON.stringify(p)})}]));var u,s,c,h,l,g,p}var c=s(function(){return Promise.resolve(navigator.credentials.get({publicKey:t})).then(function(e){i=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return c&&c.then?c.then(u):u()})}catch(e){return Promise.reject(e)}},l.updateInfo=function(t){var r=this.utils.createJsonRpcRequestPayload(e.UpdateWebAuthnInfo,[{webAuthnCredentialsId:t.id,nickname:t.nickname}]);return this.request(r)},l.unregisterDevice=function(t){var r=this.utils.createJsonRpcRequestPayload(e.UnregisterWebAuthDevice,[{webAuthnCredentialsId:t}]);return this.request(r)},l.registerNewDevice=function(t){void 0===t&&(t="");try{var r=this;if(!window.PublicKeyCredential)throw r.createWebAuthnNotSupportError();return Promise.resolve(r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWebAuthDeviceStart,[]))).then(function(n){var i;function a(n){return r.request(r.utils.createJsonRpcRequestPayload(e.RegisterWebAuthDevice,[{nickname:t,transport:i.response.getTransports(),user_agent:navigator.userAgent,registration_response:u(i)}]))}var o=s(function(){return Promise.resolve(navigator.credentials.create({publicKey:n.credential_options})).then(function(e){i=e})},function(e){throw r.createWebAuthCreateCredentialError(e)});return o&&o.then?o.then(a):a()})}catch(e){return Promise.reject(e)}},l.getMetadata=function(){var t=this.utils.createJsonRpcRequestPayload(e.GetWebAuthnInfo,[]);return this.request(t)},h}(r.Extension.Internal);
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
2
  //# sourceMappingURL=index.native.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"index.native.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","temp","length","num","extraBytes","output","encode","charAt","b64enc","buf","replace","b64RawEnc","transformNewAssertionForServer","newAssertion","attObj","Uint8Array","response","attestationObject","clientDataJSON","rawId","registrationClientExtensions","getClientExtensionResults","id","type","clientData","JSON","stringify","name","config","createWebAuthnNotSupportError","this","createError","WebAuthnNotSupported","createWebAuthCreateCredentialError","message","WebAuthnCreateCredentialError","registerNewUser","configuration","window","PublicKeyCredential","nickname","_this3","request","utils","createJsonRpcRequestPayload","WebAuthnRegistrationStart","username","options","credential","RegisterWithWebAuth","transport","getTransports","user_agent","navigator","userAgent","registration_response","credentials","create","publicKey","credential_options","err","login","_this5","LoginWithWebAuthn","transformedCredentialRequestOptions","assertion","WebAuthnLoginVerify","assertion_response","authData","authenticatorData","sig","signature","assertionClientExtensions","Array","from","map","x","toString","substr","join","get","updateInfo","requestPayload","UpdateWebAuthnInfo","webAuthnCredentialsId","unregisterDevice","UnregisterWebAuthDevice","registerNewDevice","_this7","RegisterWebAuthDeviceStart","RegisterWebAuthDevice","getMetadata","GetWebAuthnInf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,YAAYC,OAAO,CAC/CC,UAAWX,EAAQY,uCADrBX,gBAGOY,GACP,QAAW3B,mCAAmC2B,sCA5GpD,sCA2HS+B,YAAA,WACL,IAAMT,EAAiBpD,KAAKa,MAAMC,4BAA4BhD,EAA2BgG,gBAAiB,IAC1G,YAAYlD,QAAewC,OA7HQW,YAAUC"}
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,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AM,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 +1,2 @@
1
- import { WebAuthnExtension } from './index';
2
- export { WebAuthnExtension as default };
1
+ import { WebAuthnExtension } from './index';
2
+ export default WebAuthnExtension;
@@ -1,26 +1,26 @@
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
+ 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
+ }
@@ -0,0 +1 @@
1
+ export * from './index';
@@ -1,41 +1,41 @@
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
+ 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
+ }
@@ -0,0 +1,17 @@
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-ext/webauthn",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Magic SDK WabAuthn Extension",
5
5
  "author": "Magic <team@magic.link> (https://magic.link/)",
6
6
  "license": "MIT",
@@ -11,7 +11,7 @@
11
11
  "files": [
12
12
  "dist"
13
13
  ],
14
- "target": "web",
14
+ "target": "neutral",
15
15
  "cdnGlobalName": "MagicWebAuthnExtension",
16
16
  "main": "./dist/cjs/index.js",
17
17
  "module": "./dist/es/index.js",
@@ -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.mjs",
22
+ "import": "./dist/es/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": "^3.0.0"
31
+ "@magic-sdk/commons": "^4.1.0"
32
32
  },
33
- "gitHead": "a85f41892cbd7985ee1743764f7f905b89004eaa"
33
+ "gitHead": "6aa5a25b33865cfe27444ca6094efade16a82f9f"
34
34
  }
@@ -1,2 +0,0 @@
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.mjs.map
@@ -1 +0,0 @@
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,EC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