-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle-client.js
127 lines (101 loc) · 3.25 KB
/
google-client.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
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
'use strict';
const google = require('googleapis');
const redis = require('redis');
const SCOPES = ['https://www.googleapis.com/auth/drive.readonly'];
const EventEmitter2 = require('eventemitter2').EventEmitter2;
class GoogleClient extends EventEmitter2 {
constructor(config) {
super(config);
this.slack = config.slack;
this.logger = config.logger;
this.redis = redis.createClient(process.env.REDIS_URL);
this.authorized = false;
// Load client secrets from environment variable
if (!process.env.GOOGLE_CLIENT_SECRETS) {
this.logger.error('client_secrets.jsonを読み込めなかったの……ぐすん');
return;
}
try {
this.credentials = JSON.parse(process.env.GOOGLE_CLIENT_SECRETS);
} catch (error) {
console.error(`client_secrets.jsonがパースできなかったの……: ${error}`);
}
// Authorize a client with the loaded credentials
if (this.slack.connected) {
this.authorize();
} else {
this.slack.on('open', () => {
this.authorize();
});
}
}
authorize(callback) {
var clientSecret = this.credentials.installed.client_secret;
var clientId = this.credentials.installed.client_id;
var redirectUrl = this.credentials.installed.redirect_uris[0];
this.client = new google.auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
this.redis.get("google_token", (err, token) => {
if (err || token === null) {
this.getNewToken(callback);
} else {
this.client.credentials = JSON.parse(token);
this.authorized = true;
this.emit('authorize');
if (typeof callback === 'function') {
callback.call(this);
}
}
});
}
getNewToken(callback) {
var authUrl = this.client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
Object.keys(this.slack.dms).forEach((id) => {
const dm = this.slack.getDMByID(id);
if (dm.name === 'hakatashi') {
dm.send('Hey hakatashi');
dm.send(`Autorhize me by visiting this url: ${authUrl}\nand send me secret code!`);
}
});
this.slack.on('message', processMessage.bind(this));
function processMessage(message) {
const dm = this.slack.getDMByID(message.channel);
if (dm && dm.name === 'hakatashi' && message.type === 'message' && this.slack.getUserByID(message.user).name === 'hakatashi') {
const code = message.text.trim();
this.client.getToken(code, (error, token) => {
if (error) {
this.logger.error(`アクセストークンを取得できなかったの……: ${error}`);
dm.send(`Error while trying to retrieve access token: ${error}`);
return;
}
dm.send('Thanks!');
this.client.credentials = token;
this.authorized = true;
this.storeToken();
this.emit('authorize');
if (typeof callback === 'function') {
callback.call(this);
}
});
this.slack.removeAllListeners('message');
}
}
}
storeToken() {
this.redis.set('google_token', JSON.stringify(this.client.credentials));
this.logger.log('Google APIのトークンを保存したよ!');
}
refresh(callback) {
this.client.refreshAccessToken((error, tokens) => {
this.client.credentials = tokens;
this.storeToken();
if (typeof callback === 'function') {
callback();
}
});
}
}
module.exports = GoogleClient;