-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-other-user.js
executable file
·140 lines (124 loc) · 4.01 KB
/
auth-other-user.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
require('dotenv').config();
const request = require('request');
const queryString = require('query-string');
const passport = require('passport');
const TwitterStrategy = require('passport-twitter');
const httpAuth = require('http-auth');
// this is used only for autheticating all subscribed users.
// Both oauthToken and tokenSecret must be vinculated to a twitter_id in the database before.
// load config
const consumerKey = process.env.TWITTER_CONSUMER_KEY;
const consumerSecret = process.env.TWITTER_CONSUMER_SECRET;
const webhookEnv = process.env.TWITTER_WEBHOOK_ENV;
const authUser = process.env.BASIC_AUTH_USER;
const authPass = process.env.BASIC_AUTH_PASSWORD;
const auth = {};
function getAuth(data) { // userID means politicianID
auth.twitter_oauth = {
consumer_key: consumerKey,
consumer_secret: consumerSecret,
token: data.oauthToken,
token_secret: data.tokenSecret,
};
auth.twitter_webhook_environment = webhookEnv;
// basic auth middleware for express
auth.basic = httpAuth.connect(httpAuth.basic({
realm: 'admin-dashboard',
}, (username, password, callback) => {
callback(username === authUser && password === authPass);
}));
// 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
((token, tokenSecret, profile, cb) => cb(null, {
profile,
access_token: token,
access_token_secret: tokenSecret,
})),
));
// Configure Passport authenticated session persistence.
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((obj, cb) => {
cb(null, obj);
});
/**
* Retrieves a Twitter Sign-in auth URL for OAuth1.0a
*/
auth.get_twitter_auth_url = (host, callbackction) => {
// construct request to retrieve authorization token
const requestOptions = {
url: 'https://api.twitter.com/oauth/request_token',
method: 'POST',
oauth: {
callback: `https://${host}/callbacks/twitter/${callbackction}`,
consumer_key: auth.twitter_oauth.consumer_key,
consumer_secret: auth.twitter_oauth.consumer_secret,
},
};
return new Promise(((resolve, reject) => {
request(requestOptions, (error, response) => {
if (error) {
reject(error);
} else {
// construct sign-in URL from returned authorization token
const responseParams = queryString.parse(response.body);
console.log(responseParams);
const twitterAuthUrl = `https://api.twitter.com/oauth/authenticate?force_login=true&oauth_token=${responseParams.oauth_token}`;
resolve({
responseParams,
twitterAuthUrl,
});
}
});
}));
};
/**
* Retrieves a bearer token for OAuth2
*/
auth.get_twitter_bearer_token = () => {
// just return the bearer token if we already have one
if (auth.twitter_bearer_token) {
// return new Promise(((resolve, reject) => {
return new Promise(((resolve) => {
resolve(auth.twitter_bearer_token);
}));
}
// construct request for bearer token
const requestOptions = {
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(((resolve, reject) => {
request(requestOptions, (error, response) => {
if (error) {
reject(error);
} else {
const jsonBody = JSON.parse(response.body);
console.log('Bearer Token:', jsonBody.access_token);
auth.twitter_bearer_token = jsonBody.access_token;
resolve(auth.twitter_bearer_token);
}
});
}));
};
return auth;
}
module.exports.getAuth = getAuth;