Merge pull request #1671 from SkynetLabs/expose-internal-authenticate-function

expose internal authenticate function on health-check cli
This commit is contained in:
Karol Wypchło 2022-02-09 16:04:25 +01:00 committed by GitHub
commit 735fb4cd5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -6,6 +6,16 @@ require("yargs/yargs")(process.argv.slice(2))
.help() .help()
.demandCommand() .demandCommand()
.strict(true) .strict(true)
.command(
"__authenticate", // Internal only function - this function will be removed when API keys are implemented
false, // hide this function cli help
() => {},
async () => {
const { getAuthCookie } = require("../src/utils");
console.log(await getAuthCookie(true));
}
)
.command( .command(
"enable", "enable",
"Mark portal as enabled", "Mark portal as enabled",

View File

@ -48,26 +48,37 @@ function ensureValidJSON(object) {
* Authenticate with given credentials and return auth cookie * Authenticate with given credentials and return auth cookie
* Creates new account if username does not exist * Creates new account if username does not exist
* Only authenticates when portal is set to authenticated users only mode * Only authenticates when portal is set to authenticated users only mode
* @param {boolean} forceAuth forcibly ensure authentication with test credentials
*/ */
function getAuthCookie() { function getAuthCookie(forceAuth = false) {
// cache auth promise so only one actual request will be made // cache auth promise so only one actual request will be made
if (getAuthCookie.cache) return getAuthCookie.cache; if (getAuthCookie.cache) return getAuthCookie.cache;
// do not authenticate if it is not necessary // accounts disabled, do not try to authenticate
if (!["authenticated", "subscription"].includes(process.env.ACCOUNTS_LIMIT_ACCESS)) return {}; if (!isPortalModuleEnabled("a")) return "";
// do not authenticate if it is not required by portal limit access rule
if (!forceAuth && !["authenticated", "subscription"].includes(process.env.ACCOUNTS_LIMIT_ACCESS)) return "";
const portalDomain = process.env.PORTAL_DOMAIN;
const email = process.env.ACCOUNTS_TEST_USER_EMAIL; const email = process.env.ACCOUNTS_TEST_USER_EMAIL;
const password = process.env.ACCOUNTS_TEST_USER_PASSWORD; const password = process.env.ACCOUNTS_TEST_USER_PASSWORD;
try {
if (!portalDomain) throw new Error("PORTAL_DOMAIN cannot be empty");
if (!email) throw new Error("ACCOUNTS_TEST_USER_EMAIL cannot be empty"); if (!email) throw new Error("ACCOUNTS_TEST_USER_EMAIL cannot be empty");
if (!password) throw new Error("ACCOUNTS_TEST_USER_PASSWORD cannot be empty"); if (!password) throw new Error("ACCOUNTS_TEST_USER_PASSWORD cannot be empty");
} catch (error) {
console.log(error.message);
process.exit(1);
}
async function authenticate() { async function authenticate() {
const got = require("got"); const got = require("got");
try { try {
// authenticate with given test user credentials // authenticate with given test user credentials
const response = await got.post(`${process.env.SKYNET_DASHBOARD_URL}/api/login`, { const response = await got.post(`https://account.${process.env.PORTAL_DOMAIN}/api/login`, {
json: { email, password }, json: { email, password },
}); });
@ -89,7 +100,7 @@ function getAuthCookie() {
// 401 means that service worked but user could not have been authenticated // 401 means that service worked but user could not have been authenticated
if (error.response && error.response.statusCode === 401) { if (error.response && error.response.statusCode === 401) {
// sign up with the given credentials // sign up with the given credentials
await got.post(`${process.env.SKYNET_DASHBOARD_URL}/api/user`, { await got.post(`https://account.${process.env.PORTAL_DOMAIN}/api/user`, {
json: { email, password }, json: { email, password },
}); });