-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_auth.ts
109 lines (90 loc) · 3.23 KB
/
google_auth.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
import * as googleAuth from 'google-auth-library';
import { Credentials } from 'google-auth-library/build/src/auth/credentials';
import * as fs from 'fs';
import * as readline from 'readline';
// TODO
// * Get credentials from file in config.yaml -> google_credentials_file
// * Generate auth URL
// * Wait for user to come back with code
// * Take code and get the access token
// * Dump credentials data to file
// * Have main app read from the seprate file, rather than config.yaml
// * Have main app update access, refresh, and expiration configs
// automatically
const scopes: Array<string> = [
'https://www.googleapis.com/auth/admin.directory.group.member'
,'https://www.googleapis.com/auth/admin.directory.group'
,'https://mail.google.com/'
,'https://www.googleapis.com/auth/gmail.modify'
,'https://www.googleapis.com/auth/gmail.compose'
,'https://www.googleapis.com/auth/gmail.send'
];
/**
* Step 0: Create OAuth2 credentials at the Google Console (make sure to download JSON, not only just get key and secret)
*/
/**
* Step 1: Authorize in the browser
*/
export function getAuthorizeUrl(
credentials
,callback: (err: any, url: string) => any
): void {
const oauth2Client = new googleAuth.OAuth2Client(
credentials.installed.client_id
,credentials.installed.client_secret
,credentials.installed.redirect_uris[0]
);
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
callback(null, authUrl);
}
/**
* Step 2: Get auth token
*/
export function getAccessToken(
code
,credentials
,callback: (err: any, token?: Credentials | null) => any): void
{
const oauth2Client = new googleAuth.OAuth2Client(
credentials.installed.client_id
,credentials.installed.client_secret
,credentials.installed.redirect_uris[0]
);
oauth2Client.getToken( code, (err, token) => {
if(err) return console.log(err);
callback(null, token);
});
}
fs.readFile( 'google-credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
let credentials = JSON.parse( content.toString() );
getAuthorizeUrl(
credentials
,(err, url) => {
if(err) return console.log(err);
console.log("Auth url is: ", url);
let rl = readline.createInterface({
input: process.stdin
,output: process.stdout
});
rl.question( "Input token: ", (token) => {
getAccessToken( token, credentials, (err, access) => {
if(err) return console.log(err);
//console.log("Auth token is: ", token);
console.log( "google_client_token: " + token );
console.log( "google_access_token: "
+ access['access_token'] );
console.log( "google_token_type: "
+ access['token_type'] );
console.log( "google_refresh_token: "
+ access['refresh_token'] );
console.log( "google_expires_date: "
+ access['expiry_date'] );
});
});
}
);
});