-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
127 lines (97 loc) · 3.01 KB
/
app.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
const express = require('express')
const bodyParser = require('body-parser')
const session = require('express-session')
const passport = require('passport')
const TwitterStrategy = require('passport-twitter')
const uuid = require('uuid/v4')
const security = require('./helpers/security')
const auth = require('./helpers/auth')
const cacheRoute = require('./helpers/cache-route')
const socket = require('./helpers/socket')
const app = express()
app.set('port', (process.env.PORT || 5000))
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(passport.initialize());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}))
// start server
const server = app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'))
})
// initialize socket.io
socket.init(server)
// form parser middleware
var parseForm = bodyParser.urlencoded({ extended: false })
/**
* Receives challenge response check (CRC)
**/
app.get('/webhook/twitter', function(request, response) {
var crc_token = request.query.crc_token
if (crc_token) {
var hash = security.get_challenge_response(crc_token, auth.twitter_oauth.consumer_secret)
response.status(200);
response.send({
response_token: 'sha256=' + hash
})
} else {
response.status(400);
response.send('Error: crc_token missing from request.')
}
})
/**
* Receives Account Acitivity events
**/
app.post('/webhook/twitter', function(request, response) {
console.log(request.body)
socket.io.emit(socket.activity_event, {
internal_id: uuid(),
event: request.body
})
response.send('200 OK')
})
/**
* Serves the home page
**/
app.get('/', function(request, response) {
response.render('index')
})
/**
* Subscription management
**/
app.get('/subscriptions', auth.basic, cacheRoute(1000), require('./routes/subscriptions'))
/**
* Starts Twitter sign-in process for adding a user subscription
**/
app.get('/subscriptions/add', passport.authenticate('twitter', {
callbackURL: '/callbacks/addsub'
}));
/**
* Starts Twitter sign-in process for removing a user subscription
**/
app.get('/subscriptions/remove', passport.authenticate('twitter', {
callbackURL: '/callbacks/removesub'
}));
/**
* Webhook management routes
**/
var webhook_view = require('./routes/webhook')
app.get('/webhook', auth.basic, auth.csrf, webhook_view.get_config)
app.post('/webhook/update', parseForm, auth.csrf, webhook_view.update_config)
app.post('/webhook/validate', parseForm, auth.csrf, webhook_view.validate_config)
app.post('/webhook/delete', parseForm, auth.csrf, webhook_view.delete_config)
/**
* Activity view
**/
app.get('/activity', auth.basic, require('./routes/activity'))
/**
* Handles Twitter sign-in OAuth1.0a callbacks
**/
app.get('/callbacks/:action', passport.authenticate('twitter', { failureRedirect: '/' }),
require('./routes/sub-callbacks'))