-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.js
177 lines (152 loc) · 4.94 KB
/
auth.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
const nconf = require('nconf')
const request = require('request')
const queryString = require('query-string')
const passport = require('passport')
const TwitterStrategy = require('passport-twitter')
const httpAuth = require('http-auth')
require('dotenv').config()
const RequiredEnv = [
'TWITTER_CONSUMER_KEY',
'TWITTER_CONSUMER_SECRET',
'TWITTER_ACCESS_TOKEN',
'TWITTER_ACCESS_TOKEN_SECRET',
]
if (!RequiredEnv.every(key => typeof process.env[key] !== 'undefined')) {
console.error(`One of more of the required environment variables (${RequiredEnv.join(', ')}) are not defined. Please check your environment and try again.`)
process.exit(-1)
}
var auth = {}
// twitter info
auth.twitter_oauth = {
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
token: process.env.TWITTER_ACCESS_TOKEN,
token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
// basic auth middleware for express
if (typeof process.env.BASIC_AUTH_USER !== 'undefined' &&
typeof process.env.BASIC_AUTH_PASSWORD !== 'undefined') {
auth.basic = httpAuth.connect(httpAuth.basic({
realm: 'admin-dashboard'
}, function(username, password, callback) {
callback(username === process.env.BASIC_AUTH_USER && password === process.env.BASIC_AUTH_PASSWORD)
}))
} else {
console.warn([
'Your admin dashboard is accessible by everybody.',
'To restrict access, setup BASIC_AUTH_USER and BASIC_AUTH_PASSWORD',
'as environment variables.',
].join(' '))
}
// csrf protection middleware for express
auth.csrf = require('csurf')()
// Configure the Twitter strategy for use by Passport.
passport.use(new TwitterStrategy({
consumerKey: auth.twitter_oauth.consumer_key,
consumerSecret: auth.twitter_oauth.consumer_secret,
// we want force login, so we set the URL with the force_login=true
userAuthorizationURL: 'https://api.twitter.com/oauth/authenticate?force_login=true'
},
// stores profile and tokens in the sesion user object
// this may not be the best solution for your application
function(token, tokenSecret, profile, cb) {
return cb(null, {
profile: profile,
access_token: token,
access_token_secret: tokenSecret
})
}
))
// Configure Passport authenticated session persistence.
passport.serializeUser(function(user, cb) {
cb(null, user);
})
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
})
/**
* Retrieves a Twitter Sign-in auth URL for OAuth1.0a
*/
auth.get_twitter_auth_url = function (host, callback_action) {
// construct request to retrieve authorization token
var request_options = {
url: 'https://api.twitter.com/oauth/request_token',
method: 'POST',
oauth: {
callback: 'https://' + host + '/callbacks/twitter/' + callback_action,
consumer_key: auth.twitter_oauth.consumer_key,
consumer_secret: auth.twitter_oauth.consumer_secret
}
}
return new Promise (function (resolve, reject) {
request(request_options, function(error, response) {
if (error) {
reject(error)
}
else {
// construct sign-in URL from returned authorization token
var response_params = queryString.parse(response.body)
console.log(response_params)
var twitter_auth_url = 'https://api.twitter.com/oauth/authenticate?force_login=true&oauth_token=' + response_params.oauth_token
resolve({
response_params: response_params,
twitter_auth_url: twitter_auth_url
})
}
})
})
}
/**
* Retrieves a bearer token for OAuth2
*/
auth.get_twitter_bearer_token = function () {
// just return the bearer token if we already have one
if (auth.twitter_bearer_token) {
return new Promise (function (resolve, reject) {
resolve(auth.twitter_bearer_token)
})
}
// construct request for bearer token
var request_options = {
url: 'https://api.twitter.com/oauth2/token',
method: 'POST',
auth: {
user: auth.twitter_oauth.consumer_key,
pass: auth.twitter_oauth.consumer_secret
},
form: {
'grant_type': 'client_credentials'
}
}
return new Promise (function (resolve, reject) {
request(request_options, function(error, response) {
if (error) {
reject(error)
}
else {
var json_body = JSON.parse(response.body)
auth.twitter_bearer_token = json_body.access_token
resolve(auth.twitter_bearer_token)
}
})
})
}
auth.get_webhook_id = function (bearer_token) {
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/webhooks.json',
method: 'GET',
auth: { 'bearer' : bearer_token }
}
return new Promise (function (resolve, reject) {
request(request_options, function(error, response) {
if (error) {
reject(error)
} else {
const json_response = JSON.parse(response.body)
auth.webhook_id = json_response[0].id
resolve(auth.webhook_id)
}
})
})
}
module.exports = auth