-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsdkOperation.ts
More file actions
139 lines (135 loc) · 5.47 KB
/
Copy pathsdkOperation.ts
File metadata and controls
139 lines (135 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* tslint:disable no-console cyclomatic-complexity*/
import * as inquirer from "inquirer";
import * as path from "path";
import {SDK} from "../ironnode";
import {initialize} from "../src/index";
import * as Documents from "./Documents";
import * as Groups from "./Groups";
import {log} from "./Logger";
import * as Users from "./Users";
const topLevelPrompt: inquirer.ListQuestion<{operation: string}> = {
type: "list",
name: "operation",
message: "Which operation do you want to run?",
pageSize: 25,
choices: [
{name: "Document List", value: "docList"},
{name: "Document Get", value: "docGet"},
{name: "Document Parse ID", value: "docParse"},
{name: "Document Decrypt", value: "docDecrypt"},
{name: "Document Encrypt", value: "docEncrypt"},
{name: "Update Document Data", value: "docUpdateData"},
{name: "Update Document Name", value: "docUpdateName"},
{name: "Grant Document Access", value: "docGrant"},
{name: "Revoke Document Access", value: "docRevoke"},
new inquirer.Separator(),
{name: "Group List", value: "groupList"},
{name: "Group Get", value: "groupGet"},
{name: "Group Create", value: "groupCreate"},
{name: "Group Update", value: "groupUpdate"},
{name: "Group Private Key Rotate", value: "groupRotate"},
{name: "Group Add Admins", value: "groupAddAdmins"},
{name: "Group Remove Admins", value: "groupRemoveAdmins"},
{name: "Group Add Members", value: "groupAddMembers"},
{name: "Group Remove Members", value: "groupRemoveMembers"},
{name: "Group Delete", value: "groupDelete"},
new inquirer.Separator(),
{name: "User Public Key Lookup", value: "userKeyLookup"},
{name: "User Device List", value: "userDeviceList"},
{name: "User Device Delete", value: "userDeviceDelete"},
{name: "Rotate User Master Private Key", value: "rotateUserKey"},
{name: "Change Escrow Password", value: "changePassword"},
new inquirer.Separator(),
{name: "Quit", value: "quit"},
new inquirer.Separator(),
],
};
/**
* Route the top level prompt to the correct operation.
*/
function routeAnswerToOperation(IronNode: SDK, answer: string) {
switch (answer) {
case "docList":
return Documents.list(IronNode);
case "docGet":
return Documents.get(IronNode);
case "docParse":
return Documents.parseID(IronNode);
case "docEncrypt":
return Documents.encryptDocument(IronNode);
case "docDecrypt":
return Documents.decryptDocument(IronNode);
case "docUpdateName":
return Documents.updateDocumentName(IronNode);
case "docUpdateData":
return Documents.updateDocumentData(IronNode);
case "docGrant":
return Documents.grantDocumentAccess(IronNode);
case "docRevoke":
return Documents.revokeDocumentAccess(IronNode);
case "groupList":
return Groups.list(IronNode);
case "groupGet":
return Groups.get(IronNode);
case "groupCreate":
return Groups.create(IronNode);
case "groupUpdate":
return Groups.update(IronNode);
case "groupRotate":
return Groups.rotatePrivateKey(IronNode);
case "groupAddAdmins":
return Groups.addAdmins(IronNode);
case "groupRemoveAdmins":
return Groups.removeAdmins(IronNode);
case "groupAddMembers":
return Groups.addMembers(IronNode);
case "groupRemoveMembers":
return Groups.removeMembers(IronNode);
case "groupDelete":
return Groups.deleteGroup(IronNode);
case "userKeyLookup":
return Users.publicKeyLookup(IronNode);
case "userDeviceList":
return Users.deviceList(IronNode);
case "userDeviceDelete":
return Users.deviceDelete(IronNode);
case "rotateUserKey":
return Users.rotateMasterKey(IronNode);
case "changePassword":
return Users.changePassword(IronNode);
case "quit":
return process.exit();
default:
console.error("Method not yet implemented!");
return process.exit(-1);
}
}
/**
* Ask the top level question about which operation to run. If it fails log the error. In both cases, recursively
* show the top level operation prompt again.
*/
function askForOperation(IronNode: SDK): Promise<void> {
return inquirer
.prompt(topLevelPrompt)
.then(({operation}) => {
return routeAnswerToOperation(IronNode, operation).catch((error) => {
console.log("\x1Bc");
console.error(`${error}\n\n`);
//Even if an error occurs, recover and go back to the operation list
return Promise.resolve();
});
})
.then(() => askForOperation(IronNode))
.catch((e) => {
console.error(`${e.message}\n\n`);
});
}
export function initializeSDKWithLocalDevice() {
const Config = require(path.join(__dirname, "./.device.json"));
return initialize(Config.accountID, Config.segmentID, Config.deviceKeys.privateKey, Config.signingKeys.privateKey)
.then((IronNode) => {
log(IronNode.userContext);
return askForOperation(IronNode);
})
.catch((error) => console.error(`SDK Initialization Error: ${error.message}`));
}