-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
208 lines (184 loc) · 6.01 KB
/
oauth.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var connect = process.env.MONGODB_URI;
var models = require('./models');
var {rtm, web} =require('./bot')
var google = require('googleapis');
var calendar = google.calendar('v3');
var OAuth2 = google.auth.OAuth2;
mongoose.connect(connect);
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}))
// generate id: with mongoose user model
app.get('/connect', function(req, res){
if(req.query.auth_id){
var oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.DOMAIN+'/connect/callback'//developer console redirect url
);
// generate a url that asks permissions for Google+ and Google Calendar scopes
var url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/calendar'
],
// Optional property that passes state parameters to redirect URI
state: encodeURIComponent(JSON.stringify({
auth_id: req.query.auth_id}))
// state: {
// auth_id: req.query.auth_id
// }
});
res.redirect(url)
}
})
// state=parse req.query.state to get auth_id
//oauth stuff
app.get('/connect/callback', function(req, res){
var oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.DOMAIN+'/connect/callback'//developer console redirect url
);
var authid=decodeURIComponent(req.query.state)
var code=req.query.code
oauth2Client.getToken(code, function (err, tokens) {//contain google.token
// Now tokens contains an access_token and an optional refresh_token. Save them.
// Add google token to Schema
models.User.findOne({_id: JSON.parse(authid).auth_id}, function(err, user){
user.google=tokens
user.save(function(err, user) {
if (err) {
console.log('CHECK', err);
} else {
if (!err) {
//console.log(dbuser.pendingAction.channel)
oauth2Client.setCredentials(tokens);
rtm.sendMessage('Ok thank you! Remind me to do something. Beware that you must give me a subject and date!', user.slack_dmid )
res.send("You've granted me google access, congrats!");
}
console.log('SAVED', user);
}
});
})
});
})
// after authentication when user hits interactive message
app.post('/interactive', function(req, res) {
console.log('ENTERED INTERACTIVE')
var oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.DOMAIN+'/connect/callback'//developer console redirect url
);
var channel
var b0dy = JSON.parse(req.body.payload);
models.User.findOne({
slack_id: b0dy.user.id
}, function (err, user){
if (err) {
console.log('Found user err is', err);
} else {
oauth2Client.setCredentials(user.google)
console.log("****", user);
channel= user.pendingAction.channel
var wutDidTheySay = b0dy.actions[0].name;
//var slackID = b0dy.user.id;
//var tasks = Task.find({user_id:slackID});
var timeInMs = Date.now()
if (timeInMs > user.google.expiry_date){
oauth2Client.refreshAccessToken(function(err, tokens) {
oauth2Client.setCredentials(tokens);
user.google=tokens
user.save(function(err, user) {
if (err) {
console.log('ERR REFRESHING TOKENS', err);
} else {
console.log('REFRESHed TOKENS');
}
});
})
}
if (wutDidTheySay === 'yes') {
console.log('INTERACTIVE YES')
var event = {
'summary': user.pendingAction.subject,
'start': {
'date': user.pendingAction.date,
'time': 'America/Los_Angeles'
},
'end': {
'date': user.pendingAction.date,
'time': 'America/Los_Angeles'
},
'recurrence': [],
'attendees': [],
'reminders': {
'userDefault': false,
'overrides': [],
}
// save event based on google calendar api to save task
};
var task= models.Task({
date: event.start.date,
subject: event.summary,
requesterId: user.slack_id,
requesterchannel: user.slack_dmid,
})
task.save(function(err, user) {
if (err) {
console.log('CHECK', err);
} else {
console.log('NEW TASK SAVED')
}
})
user.pendingAction=null
user.save(function(err, user) {
if (err) {
console.log('CHECK', err);
} else {
console.log('cleared pending action');
}
})
calendar.events.insert({
auth: oauth2Client,
calendarId: 'primary',
resource: event,
}, function(err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
}
else {
console.log('Event created: %s', event.htmlLink);
}
});
rtm.sendMessage('Ok! Adding task now! ', channel)
}
if (wutDidTheySay === 'no'){
console.log('INTERACTIVE NO')
user.pendingAction=null
user.save(function(err, user) {
if (err) {
console.log('CHECK', err);
} else {
console.log('cleared pending action');
rtm.sendMessage("Ok! I won't add the task!", channel)
}
})
}
res.end();
}
});
})
rtm.start()
var port = process.env.PORT || 3000;
app.listen(3000);
console.log('Express started. Listening on port %s', port);
module.exports = app;