Skip to content

Commit

Permalink
Merge pull request #125 from appwrite/dev
Browse files Browse the repository at this point in the history
fix: mfa
  • Loading branch information
christyjacob4 authored Apr 17, 2024
2 parents a0e8f72 + 795b3f1 commit 908cd72
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Once the installation is complete, you can verify the install using

```sh
$ appwrite -v
5.0.2
5.0.3
```

### Install using prebuilt binaries
Expand Down Expand Up @@ -60,7 +60,7 @@ $ scoop install https://raw.githubusercontent.com/appwrite/sdk-for-cli/master/sc
Once the installation completes, you can verify your install using
```
$ appwrite -v
5.0.2
5.0.3
```

## Getting Started
Expand Down
4 changes: 2 additions & 2 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# You can use "View source" of this page to see the full script.

# REPO
$GITHUB_x64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.2/appwrite-cli-win-x64.exe"
$GITHUB_arm64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.2/appwrite-cli-win-arm64.exe"
$GITHUB_x64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.3/appwrite-cli-win-x64.exe"
$GITHUB_arm64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.3/appwrite-cli-win-arm64.exe"

$APPWRITE_BINARY_NAME = "appwrite.exe"

Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ printSuccess() {
downloadBinary() {
echo "[2/4] Downloading executable for $OS ($ARCH) ..."

GITHUB_LATEST_VERSION="5.0.2"
GITHUB_LATEST_VERSION="5.0.3"
GITHUB_FILE="appwrite-cli-${OS}-${ARCH}"
GITHUB_URL="https://github.com/$GITHUB_REPOSITORY_NAME/releases/download/$GITHUB_LATEST_VERSION/$GITHUB_FILE"

Expand Down
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Client {
'x-sdk-name': 'Command Line',
'x-sdk-platform': 'console',
'x-sdk-language': 'cli',
'x-sdk-version': '5.0.2',
'user-agent' : `AppwriteCLI/5.0.2 (${os.type()} ${os.version()}; ${os.arch()})`,
'x-sdk-version': '5.0.3',
'user-agent' : `AppwriteCLI/5.0.3 (${os.type()} ${os.version()}; ${os.arch()})`,
'X-Appwrite-Response-Format' : '1.5.0',
};
}
Expand Down
43 changes: 40 additions & 3 deletions lib/commands/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Client = require("../client");
const { sdkForConsole } = require("../sdks");
const { globalConfig, localConfig } = require("../config");
const { actionRunner, success, parseBool, commandDescriptions, log, parse } = require("../parser");
const { questionsLogin } = require("../questions");
const { accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");

const login = new Command("login")
.description(commandDescriptions['login'])
Expand All @@ -24,7 +24,44 @@ const login = new Command("login")
sdk: client
})

success()
client.setCookie(globalConfig.getCookie());

let account;

try {
account = await accountGet({
sdk: client,
parseOutput: false
});
} catch(error) {
if (error.response === 'user_more_factors_required') {
const { factor } = await inquirer.prompt(questionsListFactors);

const challenge = await accountCreateMfaChallenge({
factor,
parseOutput: false,
sdk: client
});

const { otp } = await inquirer.prompt(questionsMfaChallenge);

await accountUpdateMfaChallenge({
challengeId: challenge.$id,
otp,
parseOutput: false,
sdk: client
});

account = await accountGet({
sdk: client,
parseOutput: false
});
} else {
throw error;
}
}

success("Signed in as user with ID: " + account.$id);
}));

const logout = new Command("logout")
Expand Down
38 changes: 19 additions & 19 deletions lib/commands/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,9 +1032,9 @@ const messagingUpdateMailgunProvider = async ({ providerId, name, apiKey, domain
* @typedef {Object} MessagingCreateMsg91ProviderRequestParams
* @property {string} providerId Provider ID. Choose a custom ID or generate a random ID with 'ID.unique()'. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.
* @property {string} name Provider name.
* @property {string} templateId Msg91 template ID
* @property {string} senderId Msg91 sender ID.
* @property {string} authKey Msg91 auth key.
* @property {string} from Sender Phone number. Format this number with a leading '+' and a country code, e.g., +16175551212.
* @property {string} senderId Msg91 Sender ID.
* @property {string} authKey Msg91 Auth Key.
* @property {boolean} enabled Set as enabled.
* @property {boolean} parseOutput
* @property {libClient | undefined} sdk
Expand All @@ -1043,7 +1043,7 @@ const messagingUpdateMailgunProvider = async ({ providerId, name, apiKey, domain
/**
* @param {MessagingCreateMsg91ProviderRequestParams} params
*/
const messagingCreateMsg91Provider = async ({ providerId, name, templateId, senderId, authKey, enabled, parseOutput = true, sdk = undefined}) => {
const messagingCreateMsg91Provider = async ({ providerId, name, from, senderId, authKey, enabled, parseOutput = true, sdk = undefined}) => {
let client = !sdk ? await sdkForProject() : sdk;
let apiPath = '/messaging/providers/msg91';
let payload = {};
Expand All @@ -1053,8 +1053,8 @@ const messagingCreateMsg91Provider = async ({ providerId, name, templateId, send
if (typeof name !== 'undefined') {
payload['name'] = name;
}
if (typeof templateId !== 'undefined') {
payload['templateId'] = templateId;
if (typeof from !== 'undefined') {
payload['from'] = from;
}
if (typeof senderId !== 'undefined') {
payload['senderId'] = senderId;
Expand Down Expand Up @@ -1085,17 +1085,17 @@ const messagingCreateMsg91Provider = async ({ providerId, name, templateId, send
* @property {string} providerId Provider ID.
* @property {string} name Provider name.
* @property {boolean} enabled Set as enabled.
* @property {string} templateId Msg91 template ID.
* @property {string} senderId Msg91 sender ID.
* @property {string} authKey Msg91 auth key.
* @property {string} senderId Msg91 Sender ID.
* @property {string} authKey Msg91 Auth Key.
* @property {string} from Sender number.
* @property {boolean} parseOutput
* @property {libClient | undefined} sdk
*/

/**
* @param {MessagingUpdateMsg91ProviderRequestParams} params
*/
const messagingUpdateMsg91Provider = async ({ providerId, name, enabled, templateId, senderId, authKey, parseOutput = true, sdk = undefined}) => {
const messagingUpdateMsg91Provider = async ({ providerId, name, enabled, senderId, authKey, from, parseOutput = true, sdk = undefined}) => {
let client = !sdk ? await sdkForProject() : sdk;
let apiPath = '/messaging/providers/msg91/{providerId}'.replace('{providerId}', providerId);
let payload = {};
Expand All @@ -1105,15 +1105,15 @@ const messagingUpdateMsg91Provider = async ({ providerId, name, enabled, templat
if (typeof enabled !== 'undefined') {
payload['enabled'] = enabled;
}
if (typeof templateId !== 'undefined') {
payload['templateId'] = templateId;
}
if (typeof senderId !== 'undefined') {
payload['senderId'] = senderId;
}
if (typeof authKey !== 'undefined') {
payload['authKey'] = authKey;
}
if (typeof from !== 'undefined') {
payload['from'] = from;
}

let response = undefined;

Expand Down Expand Up @@ -2497,9 +2497,9 @@ messaging
.description(`Create a new MSG91 provider.`)
.requiredOption(`--providerId <providerId>`, `Provider ID. Choose a custom ID or generate a random ID with 'ID.unique()'. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.`)
.requiredOption(`--name <name>`, `Provider name.`)
.option(`--templateId <templateId>`, `Msg91 template ID`)
.option(`--senderId <senderId>`, `Msg91 sender ID.`)
.option(`--authKey <authKey>`, `Msg91 auth key.`)
.option(`--from <from>`, `Sender Phone number. Format this number with a leading '+' and a country code, e.g., +16175551212.`)
.option(`--senderId <senderId>`, `Msg91 Sender ID.`)
.option(`--authKey <authKey>`, `Msg91 Auth Key.`)
.option(`--enabled <enabled>`, `Set as enabled.`, parseBool)
.action(actionRunner(messagingCreateMsg91Provider))

Expand All @@ -2509,9 +2509,9 @@ messaging
.requiredOption(`--providerId <providerId>`, `Provider ID.`)
.option(`--name <name>`, `Provider name.`)
.option(`--enabled <enabled>`, `Set as enabled.`, parseBool)
.option(`--templateId <templateId>`, `Msg91 template ID.`)
.option(`--senderId <senderId>`, `Msg91 sender ID.`)
.option(`--authKey <authKey>`, `Msg91 auth key.`)
.option(`--senderId <senderId>`, `Msg91 Sender ID.`)
.option(`--authKey <authKey>`, `Msg91 Auth Key.`)
.option(`--from <from>`, `Sender number.`)
.action(actionRunner(messagingUpdateMsg91Provider))

messaging
Expand Down
59 changes: 57 additions & 2 deletions lib/questions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { localConfig } = require('./config');
const { projectsList } = require('./commands/projects');
const { functionsListRuntimes } = require('./commands/functions');
const { accountListMfaFactors } = require("./commands/account");
const { sdkForConsole } = require("./sdks");

const { databasesList } = require('./commands/databases');
const JSONbig = require("json-bigint")({ storeAsString: false });

Expand Down Expand Up @@ -387,7 +390,57 @@ const questionsDeployTeams = [
name: "override",
message: 'Are you sure you want to override this team? This can lead to loss of data! Type "YES" to confirm.'
},
]
];

const questionsListFactors = [
{
type: "list",
name: "factor",
message: "Your account is protected by multiple factors. Which factor would you like to use to authenticate?",
choices: async () => {
let client = await sdkForConsole(false);
const factors = await accountListMfaFactors({
sdk: client,
parseOutput: false
});

const choices = [
{
name: `TOTP (Time-based One-time Password)`,
value: 'totp'
},
{
name: `E-mail`,
value: 'email'
},
{
name: `Phone (SMS)`,
value: 'phone'
},
{
name: `Recovery code`,
value: 'recoveryCode'
}
].filter((ch) => factors[ch.value] === true);

return choices;
}
}
];

const questionsMfaChallenge = [
{
type: "input",
name: "otp",
message: "Enter OTP",
validate(value) {
if (!value) {
return "Please enter OTP";
}
return true;
},
}
];

module.exports = {
questionsInitProject,
Expand All @@ -398,5 +451,7 @@ module.exports = {
questionsDeployCollections,
questionsDeployBuckets,
questionsDeployTeams,
questionsGetEntrypoint
questionsGetEntrypoint,
questionsListFactors,
questionsMfaChallenge
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "appwrite-cli",
"homepage": "https://appwrite.io/support",
"description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API",
"version": "5.0.2",
"version": "5.0.3",
"license": "BSD-3-Clause",
"main": "index.js",
"bin": {
Expand Down
6 changes: 3 additions & 3 deletions scoop/appwrite.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"$schema": "https://raw.githubusercontent.com/ScoopInstaller/Scoop/master/schema.json",
"version": "5.0.2",
"version": "5.0.3",
"description": "The Appwrite CLI is a command-line application that allows you to interact with Appwrite and perform server-side tasks using your terminal.",
"homepage": "https://github.com/appwrite/sdk-for-cli",
"license": "BSD-3-Clause",
"architecture": {
"64bit": {
"url": "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.2/appwrite-cli-win-x64.exe",
"url": "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.3/appwrite-cli-win-x64.exe",
"bin": [
[
"appwrite-cli-win-x64.exe",
Expand All @@ -15,7 +15,7 @@
]
},
"arm64": {
"url": "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.2/appwrite-cli-win-arm64.exe",
"url": "https://github.com/appwrite/sdk-for-cli/releases/download/5.0.3/appwrite-cli-win-arm64.exe",
"bin": [
[
"appwrite-cli-win-arm64.exe",
Expand Down

0 comments on commit 908cd72

Please sign in to comment.