-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
44 lines (36 loc) · 1.4 KB
/
cli.js
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
/**
* @file cli implementation to run teslaAuth.js
* @author Fredrik Lidström
* @copyright 2019-2022 Fredrik Lidström
* @license MIT (MIT)
*/
async function main() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const teslaAuth = require("./teslaAuth");
const session = teslaAuth.newSession();
console.log(`Please copy and visit the URL below to start your Tesla SSO Authentication`)
console.log(``);
console.log(`${session.url}`);
console.log(``);
console.log(`Once Sign In is completed, copy the full landing page URL (https://auth.tesla.com/void/callback?code=...) and paste it below`);
readline.question("url: ", async (url) => {
try {
const response = teslaAuth.decodeCallbackURL(url);
if (response.state && response.state !== session.state) {
console.error("Incorrect authentication response. State in requestion and authorization response do not match.");
}
console.log(``);
console.log(`Collecting bearer token`);
const bearer = await teslaAuth.bearerToken(response.code, session.codeVerifier, response.issuer);
console.debug(bearer);
process.exit(0);
} catch (error) {
console.error(error.message);
process.exit(-1);
}
});
}
main();