forked from rethinkdb/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.js
53 lines (42 loc) · 1.73 KB
/
twitch.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
'use strict';
const logger = require('../logger');
const auth_utils = require('./utils');
const https = require('https');
const Joi = require('joi');
const querystring = require('querystring');
const url = require('url');
const options_schema = Joi.object().keys({
path: Joi.string().required(),
id: Joi.string().required(),
secret: Joi.string().required(),
}).unknown(false);
function twitch(horizon, raw_options) {
const options = Joi.attempt(raw_options, options_schema);
const client_id = options.id;
const client_secret = options.secret;
const provider = options.path;
const oauth_options = { horizon, provider };
oauth_options.make_acquire_url = (state, redirect_uri) =>
url.format({ protocol: 'https',
host: 'api.twitch.tv',
pathname: '/kraken/oauth2/authorize',
query: { client_id, redirect_uri, state, response_type: 'code', scope: 'user_read' } });
oauth_options.make_token_request = (code, redirect_uri) => {
const req = https.request({ method: 'POST',
host: 'api.twitch.tv',
path: '/kraken/oauth2/token' });
req.write(querystring.stringify({
client_id, redirect_uri, client_secret, code,
grant_type: 'authorization_code' }));
return req;
};
oauth_options.make_inspect_request = (access_token) => {
logger.debug(`using access token: ${access_token}`);
return https.request({ host: 'api.twitch.tv',
path: '/kraken/user',
headers: { authorization: `OAuth ${access_token}` } });
};
oauth_options.extract_id = (user_info) => user_info && user_info._id;
auth_utils.oauth2(oauth_options);
}
module.exports = twitch;