From 1d217fc00e87fa334a9fcf26148b39c78385060d Mon Sep 17 00:00:00 2001 From: filipenevola Date: Thu, 22 Dec 2022 10:24:30 -0400 Subject: [PATCH 01/17] Updates meteorJsMinify to async --- packages/minifier-js/minifier-tests.js | 17 +++++++-------- packages/minifier-js/minifier.js | 21 ++++--------------- .../standard-minifier-js/plugin/minify-js.js | 8 +++---- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/packages/minifier-js/minifier-tests.js b/packages/minifier-js/minifier-tests.js index ec8cdc288e..dbca03c5d1 100644 --- a/packages/minifier-js/minifier-tests.js +++ b/packages/minifier-js/minifier-tests.js @@ -1,23 +1,22 @@ -Tinytest.add('minifier-js - verify how terser handles an empty string', (test) => { - let result = meteorJsMinify(''); +Tinytest.addAsync('minifier-js - verify how terser handles an empty string', async (test) => { + let result = await meteorJsMinify(''); test.equal(result.code, ''); test.equal(result.minifier, 'terser'); }); -Tinytest.add('minifier-js - verify terser is able to minify valid javascript', (test) => { - let result = meteorJsMinify('function add(first,second){return first + second; }\n'); +Tinytest.addAsync('minifier-js - verify terser is able to minify valid javascript', async (test) => { + let result = await meteorJsMinify('function add(first,second){return first + second; }\n'); test.equal(result.code, 'function add(n,d){return n+d}'); test.equal(result.minifier, 'terser'); }); -Tinytest.add('minifier-js - verify error handling is done as expected', (test) => { - test.throws( () => meteorJsMinify('let name = {;\n'), undefined ); +Tinytest.addAsync('minifier-js - verify error handling is done as expected', async (test) => { + await test.throwsAsync( async () => await meteorJsMinify('let name = {;\n'), undefined ); }); -Tinytest.add('minifier-js - verify tersers error object has the fields we use for reporting errors to users', (test) => { - let result; +Tinytest.addAsync('minifier-js - verify tersers error object has the fields we use for reporting errors to users', async (test) => { try { - result = meteorJsMinify('let name = {;\n'); + await meteorJsMinify('let name = {;\n'); } catch (err) { test.isNotUndefined(err.name); diff --git a/packages/minifier-js/minifier.js b/packages/minifier-js/minifier.js index e1053a7e15..c0fe999508 100644 --- a/packages/minifier-js/minifier.js +++ b/packages/minifier-js/minifier.js @@ -1,18 +1,11 @@ let terser; -const terserMinify = async (source, options, callback) => { +const terserMinify = async (source, options) => { terser = terser || Npm.require("terser"); - try { - const result = await terser.minify(source, options); - callback(null, result); - return result; - } catch (e) { - callback(e); - return e; - } + return await terser.minify(source, options); }; -export const meteorJsMinify = function (source) { +export const meteorJsMinify = async function (source) { const result = {}; const NODE_ENV = process.env.NODE_ENV || "development"; @@ -33,13 +26,7 @@ export const meteorJsMinify = function (source) { safari10: true, // set this option to true to work around the Safari 10/11 await bug }; - const terserJsMinify = Meteor.wrapAsync(terserMinify); - let terserResult; - try { - terserResult = terserJsMinify(source, options); - } catch (e) { - throw e; - } + const terserResult = await terserMinify(source, options); // this is kept to maintain backwards compatability result.code = terserResult.code; diff --git a/packages/standard-minifier-js/plugin/minify-js.js b/packages/standard-minifier-js/plugin/minify-js.js index 96ce3c75e7..48ccd57e85 100644 --- a/packages/standard-minifier-js/plugin/minify-js.js +++ b/packages/standard-minifier-js/plugin/minify-js.js @@ -9,7 +9,7 @@ Plugin.registerMinifier({ class MeteorMinifier { - processFilesForBundle (files, options) { + async processFilesForBundle (files, options) { const mode = options.minifyMode; // don't minify anything for development @@ -63,7 +63,7 @@ class MeteorMinifier { stats: Object.create(null) }; - files.forEach(file => { + for await (file of files) { // Don't reminify *.min.js. if (/\.min\.js$/.test(file.getPathInBundle())) { toBeAdded.data += file.getContentsAsString(); @@ -71,7 +71,7 @@ class MeteorMinifier { else { let minified; try { - minified = meteorJsMinify(file.getContentsAsString()); + minified = await meteorJsMinify(file.getContentsAsString()); } catch (err) { maybeThrowMinifyErrorBySourceFile(err, file); @@ -94,7 +94,7 @@ class MeteorMinifier { toBeAdded.data += '\n\n'; Plugin.nudge(); - }); + } // this is where the minified code gets added to one // JS file that is delivered to the client From 79a47c0b46d81143e517d09c82ba678b6bed815a Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 10:17:34 -0300 Subject: [PATCH 02/17] chore: made accounts-2fa server async --- packages/accounts-2fa/2fa-server.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/accounts-2fa/2fa-server.js b/packages/accounts-2fa/2fa-server.js index 111a655999..45f701a2ee 100644 --- a/packages/accounts-2fa/2fa-server.js +++ b/packages/accounts-2fa/2fa-server.js @@ -13,8 +13,8 @@ Accounts._check2faEnabled = user => { ); }; -Accounts._is2faEnabledForUser = () => { - const user = Meteor.user(); +Accounts._is2faEnabledForUser = async () => { + const user = await Meteor.user(); if (!user) { throw new Meteor.Error('no-logged-user', 'No user logged in.'); } @@ -34,9 +34,9 @@ Accounts._isTokenValid = (secret, code) => { }; Meteor.methods({ - generate2faActivationQrCode(appName) { + async generate2faActivationQrCode(appName) { check(appName, String); - const user = Meteor.user(); + const user = await Meteor.user(); if (!user) { throw new Meteor.Error( @@ -59,7 +59,7 @@ Meteor.methods({ }); const svg = new QRCode(uri).svg(); - Meteor.users.update( + await Meteor.users.update( { _id: user._id }, { $set: { @@ -72,9 +72,9 @@ Meteor.methods({ return { svg, secret, uri }; }, - enableUser2fa(code) { + async enableUser2fa(code) { check(code, String); - const user = Meteor.user(); + const user = await Meteor.user(); if (!user) { throw new Meteor.Error(400, 'No user logged in.'); @@ -94,7 +94,7 @@ Meteor.methods({ Accounts._handleError('Invalid 2FA code', true, 'invalid-2fa-code'); } - Meteor.users.update( + await Meteor.users.update( { _id: user._id }, { $set: { @@ -106,14 +106,14 @@ Meteor.methods({ } ); }, - disableUser2fa() { + async disableUser2fa() { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error(400, 'No user logged in.'); } - Meteor.users.update( + await Meteor.users.update( { _id: userId }, { $unset: { @@ -122,8 +122,8 @@ Meteor.methods({ } ); }, - has2faEnabled() { - return Accounts._is2faEnabledForUser(); + async has2faEnabled() { + return await Accounts._is2faEnabledForUser(); }, }); From 18b80d86fe26c1ee83a60ba2e366479ef2e29141 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 10:17:45 -0300 Subject: [PATCH 03/17] tests: solvedaccount - 2fa - has2faEnabled - server --- packages/accounts-2fa/server_tests.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/accounts-2fa/server_tests.js b/packages/accounts-2fa/server_tests.js index 8a95fc5602..2b627ff595 100644 --- a/packages/accounts-2fa/server_tests.js +++ b/packages/accounts-2fa/server_tests.js @@ -1,15 +1,16 @@ import { Accounts } from 'meteor/accounts-base'; import { Random } from 'meteor/random'; -const findUserById = id => Meteor.users.findOne(id); +const findUserById = + async id => await Meteor.users.findOne(id); -Tinytest.add('account - 2fa - has2faEnabled - server', test => { +Tinytest.addAsync('account - 2fa - has2faEnabled - server', async test => { // Create users - const userWithout2FA = Accounts.insertUserDoc( + const userWithout2FA = await Accounts.insertUserDoc( {}, { emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }] } ); - const userWith2FA = Accounts.insertUserDoc( + const userWith2FA = await Accounts.insertUserDoc( {}, { emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }], @@ -19,10 +20,10 @@ Tinytest.add('account - 2fa - has2faEnabled - server', test => { } ); - test.equal(Accounts._check2faEnabled(findUserById(userWithout2FA)), false); - test.equal(Accounts._check2faEnabled(findUserById(userWith2FA)), true); + test.equal(Accounts._check2faEnabled(await findUserById(userWithout2FA)), false); + test.equal(Accounts._check2faEnabled(await findUserById(userWith2FA)), true); // cleanup - Accounts.users.remove(userWithout2FA); - Accounts.users.remove(userWith2FA); + await Accounts.users.remove(userWithout2FA); + await Accounts.users.remove(userWith2FA); }); From b29920b8bfbef6bf4365ede6e078428ff5d59d76 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 10:30:00 -0300 Subject: [PATCH 04/17] docs: added accounts-2fa to changelog --- docs/history.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/history.md b/docs/history.md index 34d1da533e..2f5d67310b 100644 --- a/docs/history.md +++ b/docs/history.md @@ -7,6 +7,14 @@ * `email`: - `Email.send` is no longer available. Use `Email.sendAsync` instead. +* `accounts-2fa`: + - Some methods are now async. See below: + - `Accounts._is2faEnabledForUser` + - `(Meteor Method) - generate2faActivationQrCode` + - `(Meteor Method) - enableUser2fa` + - `(Meteor Method) - disableUser2fa` + - `(Meteor Method) - has2faEnabled` + * `accounts-password`: - `Accounts.sendResetPasswordEmail` is now async - `Accounts.sendEnrollmentEmail` is now async From 0b65346dbd002a89e819f378246e279aff122a0d Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 12:09:33 -0300 Subject: [PATCH 05/17] chore: made Assets.getText async --- packages/oauth/oauth_server.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/oauth/oauth_server.js b/packages/oauth/oauth_server.js index 1b591a455b..2bf60fe5bd 100644 --- a/packages/oauth/oauth_server.js +++ b/packages/oauth/oauth_server.js @@ -286,14 +286,17 @@ OAuth._renderOauthResults = (res, query, credentialSecret) => { } }; +const getAsset = async (name) => { + return await new Promise((resolve, reject) => Assets.getText( + `${name}.html`, + (err, data) => err ? reject(err) : resolve(data))) +} // This "template" (not a real Spacebars template, just an HTML file // with some ##PLACEHOLDER##s) communicates the credential secret back // to the main window and then closes the popup. -OAuth._endOfPopupResponseTemplate = Assets.getText( - "end_of_popup_response.html"); +OAuth._endOfPopupResponseTemplate = getAsset('end_of_popup_response') -OAuth._endOfRedirectResponseTemplate = Assets.getText( - "end_of_redirect_response.html"); +OAuth._endOfRedirectResponseTemplate = getAsset('end_of_redirect_response') // Renders the end of login response template into some HTML and JavaScript // that closes the popup or redirects at the end of the OAuth flow. From c913b8396b49b60a81b3558ebac38f6a8deff2f8 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 12:09:42 -0300 Subject: [PATCH 06/17] tests: solved pendingCredential handles Errors --- packages/oauth/oauth_tests.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/oauth/oauth_tests.js b/packages/oauth/oauth_tests.js index 49c3dcc648..73d02c8af9 100644 --- a/packages/oauth/oauth_tests.js +++ b/packages/oauth/oauth_tests.js @@ -1,16 +1,17 @@ -Tinytest.add("oauth - pendingCredential handles Errors", test => { - const credentialToken = Random.id(); +Tinytest.addAsync("oauth - pendingCredential handles Errors", + async test => { + const credentialToken = Random.id(); - const testError = new Error("This is a test error"); - testError.stack = 'test stack'; - OAuth._storePendingCredential(credentialToken, testError); + const testError = new Error("This is a test error"); + testError.stack = 'test stack'; + await OAuth._storePendingCredential(credentialToken, testError); - // Test that the result for the token is the expected error - const result = OAuth._retrievePendingCredential(credentialToken); - test.instanceOf(result, Error); - test.equal(result.message, testError.message); - test.equal(result.stack, testError.stack); -}); + // Test that the result for the token is the expected error + const result = await OAuth._retrievePendingCredential(credentialToken); + test.instanceOf(result, Error); + test.equal(result.message, testError.message); + test.equal(result.stack, testError.stack); + }); Tinytest.add("oauth - pendingCredential handles Meteor.Errors", test => { const credentialToken = Random.id(); From a39299fd879cba48cb1dcd283eae0aab730316d6 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 12:09:52 -0300 Subject: [PATCH 07/17] chore: _storePendingCredential is now async --- packages/oauth/pending_credentials.js | 74 ++++++++++++++------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/packages/oauth/pending_credentials.js b/packages/oauth/pending_credentials.js index c9fc60cf80..71623acc31 100644 --- a/packages/oauth/pending_credentials.js +++ b/packages/oauth/pending_credentials.js @@ -40,28 +40,29 @@ const _cleanupHandle = Meteor.setInterval(_cleanStaleResults, 60 * 1000); // @param credentialSecret {string} A secret that must be presented in // addition to the `key` to retrieve the credential // -OAuth._storePendingCredential = (key, credential, credentialSecret = null) => { - check(key, String); - check(credentialSecret, Match.Maybe(String)); +OAuth._storePendingCredential = + async (key, credential, credentialSecret = null) => { + check(key, String); + check(credentialSecret, Match.Maybe(String)); - if (credential instanceof Error) { - credential = storableError(credential); - } else { - credential = OAuth.sealSecret(credential); - } + if (credential instanceof Error) { + credential = storableError(credential); + } else { + credential = OAuth.sealSecret(credential); + } - // We do an upsert here instead of an insert in case the user happens - // to somehow send the same `state` parameter twice during an OAuth - // login; we don't want a duplicate key error. - OAuth._pendingCredentials.upsert({ - key, - }, { - key, - credential, - credentialSecret, - createdAt: new Date() - }); -}; + // We do an upsert here instead of an insert in case the user happens + // to somehow send the same `state` parameter twice during an OAuth + // login; we don't want a duplicate key error. + await OAuth._pendingCredentials.upsert({ + key, + }, { + key, + credential, + credentialSecret, + createdAt: new Date() + }); + }; // Retrieves and removes a credential from the _pendingCredentials collection @@ -69,24 +70,25 @@ OAuth._storePendingCredential = (key, credential, credentialSecret = null) => { // @param key {string} // @param credentialSecret {string} // -OAuth._retrievePendingCredential = (key, credentialSecret = null) => { - check(key, String); +OAuth._retrievePendingCredential = + async (key, credentialSecret = null) => { + check(key, String); - const pendingCredential = OAuth._pendingCredentials.findOne({ - key, - credentialSecret, - }); + const pendingCredential = await OAuth._pendingCredentials.findOne({ + key, + credentialSecret, + }); - if (pendingCredential) { - OAuth._pendingCredentials.remove({ _id: pendingCredential._id }); - if (pendingCredential.credential.error) - return recreateError(pendingCredential.credential.error); - else - return OAuth.openSecret(pendingCredential.credential); - } else { - return undefined; - } -}; + if (pendingCredential) { + await OAuth._pendingCredentials.remove({ _id: pendingCredential._id }); + if (pendingCredential.credential.error) + return recreateError(pendingCredential.credential.error); + else + return OAuth.openSecret(pendingCredential.credential); + } else { + return undefined; + } + }; // Convert an Error into an object that can be stored in mongo From c4e0baaaaaf503f9b67c9b0b02a414e94f2351d5 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 14:15:00 -0300 Subject: [PATCH 08/17] tests: solved pendingCredential handles Meteor.Errors --- packages/oauth/oauth_tests.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/oauth/oauth_tests.js b/packages/oauth/oauth_tests.js index 73d02c8af9..a4389bd3b5 100644 --- a/packages/oauth/oauth_tests.js +++ b/packages/oauth/oauth_tests.js @@ -13,15 +13,15 @@ Tinytest.addAsync("oauth - pendingCredential handles Errors", test.equal(result.stack, testError.stack); }); -Tinytest.add("oauth - pendingCredential handles Meteor.Errors", test => { +Tinytest.addAsync("oauth - pendingCredential handles Meteor.Errors", async test => { const credentialToken = Random.id(); const testError = new Meteor.Error(401, "This is a test error"); testError.stack = 'test stack'; - OAuth._storePendingCredential(credentialToken, testError); + await OAuth._storePendingCredential(credentialToken, testError); // Test that the result for the token is the expected error - const result = OAuth._retrievePendingCredential(credentialToken); + const result = await OAuth._retrievePendingCredential(credentialToken); test.instanceOf(result, Meteor.Error); test.equal(result.error, testError.error); test.equal(result.message, testError.message); From acefb4ae39f2693850c6494f893443b834043ef2 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 14:16:30 -0300 Subject: [PATCH 09/17] tests: solved null, undefined key for pendingCredential --- packages/oauth/oauth_tests.js | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/oauth/oauth_tests.js b/packages/oauth/oauth_tests.js index a4389bd3b5..36e2a07d5a 100644 --- a/packages/oauth/oauth_tests.js +++ b/packages/oauth/oauth_tests.js @@ -13,27 +13,29 @@ Tinytest.addAsync("oauth - pendingCredential handles Errors", test.equal(result.stack, testError.stack); }); -Tinytest.addAsync("oauth - pendingCredential handles Meteor.Errors", async test => { - const credentialToken = Random.id(); +Tinytest.addAsync("oauth - pendingCredential handles Meteor.Errors", + async test => { + const credentialToken = Random.id(); - const testError = new Meteor.Error(401, "This is a test error"); - testError.stack = 'test stack'; - await OAuth._storePendingCredential(credentialToken, testError); + const testError = new Meteor.Error(401, "This is a test error"); + testError.stack = 'test stack'; + await OAuth._storePendingCredential(credentialToken, testError); - // Test that the result for the token is the expected error - const result = await OAuth._retrievePendingCredential(credentialToken); - test.instanceOf(result, Meteor.Error); - test.equal(result.error, testError.error); - test.equal(result.message, testError.message); - test.equal(result.reason, testError.reason); - test.equal(result.stack, testError.stack); - test.isUndefined(result.meteorError); -}); + // Test that the result for the token is the expected error + const result = await OAuth._retrievePendingCredential(credentialToken); + test.instanceOf(result, Meteor.Error); + test.equal(result.error, testError.error); + test.equal(result.message, testError.message); + test.equal(result.reason, testError.reason); + test.equal(result.stack, testError.stack); + test.isUndefined(result.meteorError); + }); -Tinytest.add("oauth - null, undefined key for pendingCredential", test => { +Tinytest.addAsync("oauth - null, undefined key for pendingCredential", + async test => { const cred = Random.id(); - test.throws(() => OAuth._storePendingCredential(null, cred)); - test.throws(() => OAuth._storePendingCredential(undefined, cred)); + await test.throwsAsync(() => OAuth._storePendingCredential(null, cred)); + await test.throwsAsync(() => OAuth._storePendingCredential(undefined, cred)); }); Tinytest.add("oauth - pendingCredential handles duplicate key", test => { From 75b565f56b51c118cb180b02a11c00bc226de6ef Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 14:17:15 -0300 Subject: [PATCH 10/17] tests: solved pendingCredential handles duplicate key --- packages/oauth/oauth_tests.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/oauth/oauth_tests.js b/packages/oauth/oauth_tests.js index 36e2a07d5a..1ae3acd34d 100644 --- a/packages/oauth/oauth_tests.js +++ b/packages/oauth/oauth_tests.js @@ -33,18 +33,19 @@ Tinytest.addAsync("oauth - pendingCredential handles Meteor.Errors", Tinytest.addAsync("oauth - null, undefined key for pendingCredential", async test => { - const cred = Random.id(); - await test.throwsAsync(() => OAuth._storePendingCredential(null, cred)); - await test.throwsAsync(() => OAuth._storePendingCredential(undefined, cred)); -}); + const cred = Random.id(); + await test.throwsAsync(() => OAuth._storePendingCredential(null, cred)); + await test.throwsAsync(() => OAuth._storePendingCredential(undefined, cred)); + }); -Tinytest.add("oauth - pendingCredential handles duplicate key", test => { +Tinytest.addAsync("oauth - pendingCredential handles duplicate key", + async test => { const key = Random.id(); const cred = Random.id(); - OAuth._storePendingCredential(key, cred); + await OAuth._storePendingCredential(key, cred); const newCred = Random.id(); - OAuth._storePendingCredential(key, newCred); - test.equal(OAuth._retrievePendingCredential(key), newCred); + await OAuth._storePendingCredential(key, newCred); + test.equal(await OAuth._retrievePendingCredential(key), newCred); }); Tinytest.add("oauth - pendingCredential requires credential secret", test => { From 91dad1ab50c04bb5c3d96f51091671307bd77f1e Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 14:18:03 -0300 Subject: [PATCH 11/17] tests: solved pendingCredential requires credential secret --- packages/oauth/oauth_tests.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/oauth/oauth_tests.js b/packages/oauth/oauth_tests.js index 1ae3acd34d..3bfa231528 100644 --- a/packages/oauth/oauth_tests.js +++ b/packages/oauth/oauth_tests.js @@ -39,23 +39,24 @@ Tinytest.addAsync("oauth - null, undefined key for pendingCredential", }); Tinytest.addAsync("oauth - pendingCredential handles duplicate key", - async test => { - const key = Random.id(); - const cred = Random.id(); - await OAuth._storePendingCredential(key, cred); - const newCred = Random.id(); - await OAuth._storePendingCredential(key, newCred); - test.equal(await OAuth._retrievePendingCredential(key), newCred); -}); + async test => { + const key = Random.id(); + const cred = Random.id(); + await OAuth._storePendingCredential(key, cred); + const newCred = Random.id(); + await OAuth._storePendingCredential(key, newCred); + test.equal(await OAuth._retrievePendingCredential(key), newCred); + }); -Tinytest.add("oauth - pendingCredential requires credential secret", test => { - const key = Random.id(); - const cred = Random.id(); - const secret = Random.id(); - OAuth._storePendingCredential(key, cred, secret); - test.equal(OAuth._retrievePendingCredential(key), undefined); - test.equal(OAuth._retrievePendingCredential(key, secret), cred); -}); +Tinytest.addAsync("oauth - pendingCredential requires credential secret", + async test => { + const key = Random.id(); + const cred = Random.id(); + const secret = Random.id(); + await OAuth._storePendingCredential(key, cred, secret); + test.equal(await OAuth._retrievePendingCredential(key), undefined); + test.equal(await OAuth._retrievePendingCredential(key, secret), cred); + }); Tinytest.add("oauth - _endOfLoginResponse with popup loginStyle supports unspecified ROOT_URL_PATH_PREFIX", test => { From fda4c17d5c717c5de85735e5ab5bb607de321a89 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 28 Dec 2022 14:42:55 -0300 Subject: [PATCH 12/17] chore: made oauth server methods async Now is async: - renderEndOfLoginResponse - _renderOauthResults - _endOfLoginResponse _endOfRedirectResponseTemplate and _endOfPopupResponseTemplate are now async functions that return the same result as before --- packages/oauth/oauth_server.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/oauth/oauth_server.js b/packages/oauth/oauth_server.js index 2bf60fe5bd..7c705d07fd 100644 --- a/packages/oauth/oauth_server.js +++ b/packages/oauth/oauth_server.js @@ -251,7 +251,7 @@ const isSafe = value => { }; // Internal: used by the oauth1 and oauth2 packages -OAuth._renderOauthResults = (res, query, credentialSecret) => { +OAuth._renderOauthResults = async (res, query, credentialSecret) => { // For tests, we support the `only_credential_secret_for_test` // parameter, which just returns the credential secret without any // surrounding HTML. (The test needs to be able to easily grab the @@ -282,21 +282,23 @@ OAuth._renderOauthResults = (res, query, credentialSecret) => { } } - OAuth._endOfLoginResponse(res, details); + await OAuth._endOfLoginResponse(res, details); } }; -const getAsset = async (name) => { - return await new Promise((resolve, reject) => Assets.getText( +const getAsset = (name) => { + return new Promise((resolve, reject) => Assets.getText( `${name}.html`, (err, data) => err ? reject(err) : resolve(data))) } // This "template" (not a real Spacebars template, just an HTML file // with some ##PLACEHOLDER##s) communicates the credential secret back // to the main window and then closes the popup. -OAuth._endOfPopupResponseTemplate = getAsset('end_of_popup_response') +OAuth._endOfPopupResponseTemplate = + async () => await getAsset('end_of_popup_response') -OAuth._endOfRedirectResponseTemplate = getAsset('end_of_redirect_response') +OAuth._endOfRedirectResponseTemplate = + async () => await getAsset('end_of_redirect_response') // Renders the end of login response template into some HTML and JavaScript // that closes the popup or redirects at the end of the OAuth flow. @@ -309,7 +311,7 @@ OAuth._endOfRedirectResponseTemplate = getAsset('end_of_redirect_response') // - redirectUrl // - isCordova (boolean) // -const renderEndOfLoginResponse = options => { +const renderEndOfLoginResponse = async options => { // It would be nice to use Blaze here, but it's a little tricky // because our mustaches would be inside a