forked from rethinkdb/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
52 lines (38 loc) · 1.52 KB
/
github.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
'use strict';
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 github(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: 'github.com',
pathname: '/login/oauth/authorize',
query: { client_id, redirect_uri, state } });
oauth_options.make_token_request = (code, redirect_uri) => {
const req = https.request({ method: 'POST',
host: 'github.com',
path: '/login/oauth/access_token',
req.write(querystring.stringify({ code, client_id, client_secret, redirect_uri }));
return req;
};
oauth_options.make_inspect_request = (access_token) =>
https.request({ host: 'api.github.com',
path: '/user',
headers: { authorization: `OAuth ${access_token}` } });
});
oauth_options.extract_id = (user_info) => user_info && user_info.id;
auth_utils.oauth2(oauth_options);
}
module.exports = github;