-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetToken.ts
192 lines (173 loc) · 4.62 KB
/
getToken.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as axios from 'axios';
import inquirer from 'inquirer';
import XDGAppPaths from 'xdg-app-paths';
import { readFile } from 'fs/promises';
import { NIL } from 'uuid';
import dotenv from 'dotenv';
const IRE_AUTH_KEY = dotenv.config().parsed.IRE_AUTH_KEY;
interface Answers {
mobileNumber: string;
code: string;
token: string;
}
async function main() {
const answers = await inquirer.prompt<Answers>([
{
message: 'Mobile number',
name: 'mobileNumber',
type: 'string',
async validate(mobile: string) {
const res = await get('RequestCode', { mobile });
if (res === false) {
throw new Error('Invalid mobile');
}
return true;
},
},
{
message: 'Code',
name: 'code',
type: 'string',
async validate(code: string, answers: Answers) {
const userModel = await get<ValidateCodeResponse>('ValidateCode', {
mobile: answers.mobileNumber,
code: code,
});
let codeStatus: RequestCodeStatus;
if (userModel.Mobile == 'MobileInUse')
codeStatus = RequestCodeStatus.MobileInUse;
else if (userModel.Mobile == 'PendingGroupMobile')
codeStatus = RequestCodeStatus.PendingGroupMobile;
else if (userModel.UserID == NIL) {
codeStatus = RequestCodeStatus.InvalidCode;
}
if (codeStatus) {
throw new Error(codeStatus.toString());
}
answers.token = userModel.Token;
console.log(userModel);
console.log('Token for', userModel.FirstName, 'is', userModel.Token);
return true;
},
},
]);
await setToken(answers.token);
}
enum RequestCodeStatus {
MobileInUse,
InvalidCode,
PendingGroupMobile,
}
interface ValidateCodeResponse {
ID: 0;
UserID: '00000000-0000-0000-0000-000000000000';
Token: '00000000-0000-0000-0000-000000000000';
Authenticated: boolean;
TokenExpiryUTC: '2022-02-05T10:01:50.4588742Z';
DataExpiryUTC: '2022-02-05T10:01:50.4588742Z';
FirstName: '';
LastName: '';
Mobile: 'MobileInUse';
Email: '';
ProfileImageURL: 'user_profile.png';
ConfirmOnHideProperty: true;
DevicePlatform: '';
ConfirmOnHideTip: true;
IdentityProviderType: 0;
ProviderUserID: '';
PushToken: '';
AppEnabled: false;
RatingActivated: 0;
RatingResponse: 0;
BuildNumber: 0;
LastRatingActivatedUTC: '2000-01-01T00:00:00';
TimeZone: '';
LeaseID: 0;
PropertyID: 0;
ProspectID: 0;
InspectionGUID: '00000000-0000-0000-0000-000000000000';
Preferences: {
Initialized: 0;
ID: 0;
MinBeds: 0;
MaxBeds: 0;
MinBaths: 0;
MaxBaths: 0;
MinCars: 0;
MaxCars: 0;
MinPrice: 0;
MaxPrice: 0;
MoveInBy: 0;
MoveInWith: 0;
MoveInDateUTC: '2000-01-01T00:00:00';
MoveInDate: '2000-01-01T00:00:00+00:00';
PropertySearchTypes: [];
PropertyFeatureList: [];
Suburbs: [];
Status: 1;
Keywords: null;
EarlyBirdUnsubDateUTC: '2000-01-01T00:00:00';
UploadError: false;
};
PreferencesJSON: null;
}
async function readJson<T>(filename: string): Promise<T> {
return JSON.parse((await readFile(filename)).toString()) as T;
}
async function setToken(token: string) {
const project = await readJson<{ projectId: string; orgId: string }>(
'.vercel/project.json'
);
const key = 'TWO_APPLY_TOKEN';
const configFilename =
XDGAppPaths('com.vercel.cli').dataDirs()[0] + '/auth.json';
const vercelToken = await readJson<{ token: string }>(configFilename);
const ax = new axios.Axios({
headers: {
Authorization: 'Bearer ' + vercelToken,
},
baseURL: 'https://api.vercel.com/v7',
});
ax.interceptors.response.use((value) => {
value.data = JSON.parse(value.data);
return value;
});
const envs = (
await ax.get<{ envs: { key: string; id: string }[] }>(
`/projects/${project.projectId}/env`
)
).data;
const ident = envs.envs.find((env: { key: string }) => env.key == key)!.id;
console.log(
(
await ax.patch(
`/projects/${project.projectId}/env/${ident}`,
JSON.stringify({
target: ['production', 'preview', 'development'],
value: token,
})
)
).data
);
}
async function get<T>(route: string, params: any) {
let res: axios.AxiosResponse<T, any>;
try {
res = await axios.default.get<T>(
'https://inspectre-ta.azurewebsites.net/api/Account/' + route,
{
params,
headers: {
AuthKey: IRE_AUTH_KEY,
},
}
);
} catch (e) {
if (axios.default.isAxiosError(e)) {
console.log(e.response);
}
throw e;
}
return res.data;
}
main().then(() => process.exit());