-
Notifications
You must be signed in to change notification settings - Fork 3
/
userOperation.ts
149 lines (144 loc) · 4.96 KB
/
userOperation.ts
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
140
141
142
143
144
145
146
147
148
149
/* tslint:disable no-console*/
import * as fs from "fs";
import * as inquirer from "inquirer";
import * as jwt from "jsonwebtoken";
import * as path from "path";
import {User} from "../src/index";
import {log, logWithMessage} from "./Logger";
// tslint:disable-next-line
const Config = require("./project.json");
const keyFile = path.join(__dirname, "./private.key");
const privateKey = fs.readFileSync(keyFile, "utf8");
/**
* Generate a signed JWT for the provided user ID.
*/
function generateJWT(userID: string) {
return jwt.sign({pid: Config.projectId, sid: Config.segmentId, kid: Config.serviceKeyId}, privateKey, {
algorithm: "ES256",
expiresIn: "2m",
subject: userID,
});
}
/**
* Ask for a user ID to verify and print the results.
*/
function verifyUser() {
return inquirer
.prompt<{userID: string}>({
type: "input",
name: "userID",
message: "Input ID of user to verify: ",
})
.then(({userID}) => {
return User.verify(generateJWT(userID));
})
.then((verifyResult) => {
if (verifyResult) {
logWithMessage("User Exists!", verifyResult);
} else {
log("User does not exist!");
}
return Promise.resolve(false);
});
}
/**
* Sync a new user within the
*/
function createUser() {
return inquirer
.prompt<{userID: string; password: string; needsRotation: boolean}>([
{
type: "input",
name: "userID",
message: "Input ID of user to create: ",
},
{
type: "input",
name: "password",
message: "Input password to escrow users private key: ",
},
{
type: "confirm",
default: false,
name: "needsRotation",
message: "Create user with needs rotation?",
},
])
.then(({userID, password, needsRotation}) => User.create(generateJWT(userID), password, {needsRotation}))
.then((userInfo) => {
logWithMessage("User Created!", userInfo);
return Promise.resolve(false);
});
}
/**
* Ask for the users ID and password in order to generate them a new set of device keys. If successful write
* out the device key details into a `.device.json` file in this directory.
*/
function generateLocalDeviceKeys() {
return inquirer
.prompt<{userID: string; password: string; deviceName: string}>([
{
type: "input",
name: "userID",
message: "Input ID of user to generate device keys for: ",
},
{
type: "input",
name: "password",
message: "Input users private key escrow password: ",
},
{
type: "input",
name: "deviceName",
message: "Provide a name for these device keys: ",
},
])
.then(({userID, password, deviceName}) => {
return User.generateDeviceKeys(generateJWT(userID), password, {deviceName});
})
.then((deviceDetails) => {
fs.writeFileSync(path.join(__dirname, "./.device.json"), JSON.stringify(deviceDetails));
logWithMessage("New device keys generated and stored locally! SDK now available for use.", deviceDetails);
return Promise.resolve(true);
});
}
/**
* Ask for user related questions so we can (eventually) get device keys stored off to be able to initialize and use
* the IronNode SDK.
*/
export function askForUserOperation(message: string): Promise<void> {
return inquirer
.prompt<{userOperation: "verify" | "createUser" | "createDevice" | "quit"}>({
type: "list",
name: "userOperation",
message,
choices: [
{name: "Verify a user", value: "verify"},
{name: "Create a user", value: "createUser"},
{name: "Generate device keys for existing user", value: "createDevice"},
{name: "Quit", value: "quit"},
],
})
.then(({userOperation}) => {
if (userOperation === "verify") {
return verifyUser();
}
if (userOperation === "createUser") {
return createUser();
}
if (userOperation === "createDevice") {
return generateLocalDeviceKeys();
}
return process.exit();
})
.then((shouldGoIntoSdkOperations) => {
if (shouldGoIntoSdkOperations) {
return Promise.resolve();
}
return askForUserOperation("Pick a user operation to run.");
})
.catch((error) => {
logWithMessage("User operation failed!", error);
return askForUserOperation("Pick a user operation to run.");
});
}