-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth.js
87 lines (77 loc) · 2.49 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
const readlineSync = require('readline-sync');
const util = require('util');
const EventEmitter = require('events');
const GitHubApi = require("github");
if (typeof localStorage === "undefined" || localStorage === null) {
const LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
}
var clientid = process.env.APP_CLIENT_ID;
var clientsecret = process.env.APP_CLIENT_SECRET;
var appname = process.env.APP_NAME;
function MyGitHubApi(config) {
this.events = new EventEmitter.EventEmitter();
GitHubApi.call(this, config);
}
util.inherits(MyGitHubApi, GitHubApi);
var github = new MyGitHubApi({
// required
version: "3.0.0",
// optional
//debug: true,
// protocol: "https",
// host: "api.github.com", // should be api.github.com for GitHub
// pathPrefix: "/api/v3", // for some GHEs; none for GitHub
//timeout: 5000,
headers: {
"user-agent": appname // GitHub is happy with a unique user agent
}
});
MyGitHubApi.prototype.login = function() {
var user = localStorage.getItem('user');
var token = localStorage.getItem('token');
if (user != null && token != null)
{
github.authenticate({
type:"oauth",
token:token,
});
this.events.emit('authenticated', token);
}
else {
user = readlineSync.question('User :', {
});
var pwd = readlineSync.question('Password :', {
hideEchoBack: true
});
github.authenticate({
type:"basic",
username:user,
password:pwd
});
var twofa = readlineSync.question('2fa: ');
github.authorization.create({
scopes: ["public_repo", "repo"],
note: appname,
note_url: "http://url-to-this-auth-app",
client_id: clientid,
client_secret: clientsecret,
fingerprint: new Date().toISOString(),
headers: {
"X-GitHub-OTP": twofa
}
}, function(err, res) {
console.error(JSON.stringify(res));
if (res != null && res.token) {
localStorage.setItem('user', user);
localStorage.setItem('token', res.token);
github.authenticate({
type:"oauth",
token:res.token,
});
github.events.emit('authenticated', res.token);
}
});
}
}
module.exports = github;