-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
141 lines (124 loc) · 3.6 KB
/
app.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
var path = require('path')
var tako = require('tako')
var filed = require('filed')
var gist = require('gist')
var request = require('request')
var qs = require('querystring')
var flatdb = require('flatdb')
var https = require('https')
var htmldir = path.resolve(__dirname, 'attachments')
var t = tako()
var options = {
clientID: process.env['GITHUB_KEY'],
clientSecret: process.env['GITHUB_SECRET'],
callbackURL: process.env['VHOST'] + "/githuboauthcallback"
}
t.route('/', function (req, resp) {
filed(path.join(htmldir, 'index.html')).pipe(resp)
})
t.route('/me', function (req, resp) {
resp.setHeader('content-type', 'application/json')
resp.end(JSON.stringify(req.user))
}).must('auth')
t.route('/logout', function (req, resp) {
setCookie('', resp)
resp.statusCode = 302
resp.setHeader('location', process.env['VHOST'])
resp.end()
})
t.route('/githuboauth', function (req, resp) {
var u = 'https://github.com/login/oauth/authorize'
+ '?client_id=' + options.clientID
+ '&redirect_uri=' + options.callbackURL
+ '&scope=user,public_repo,repo,gist'
resp.statusCode = 302
resp.setHeader('location', u)
resp.end()
})
t.route('/githuboauthcallback', function (req, resp) {
var reqBody = {
client_id: options.clientID,
client_secret: options.clientSecret,
redirect_uri: options.callbackURL,
code: req.qs.code
}
request.post({
uri: 'https://github.com/login/oauth/access_token',
headers: {
"content-type": "application/x-www-form-urlencoded",
"accept": "application/json"
},
body: qs.encode(reqBody)
},
function (e, r, body) {
resp.statusCode = 200
var data = JSON.parse(body)
if (data.error) return resp.end(JSON.stringify(data))
var token = data.access_token
setCookie(token, resp)
saveUser(token, resp)
}
)
})
t.route('/*').files(htmldir)
t.auth(function(req, resp, cb) {
var token = extractToken(req)
if (!token) return cb(null)
getDB('users', function(err, db) {
db.find({token: token}, function(err, docs) {
if (err) return cb(null)
if (!docs || docs.length === 0) return cb(null)
cb(docs[0])
})
})
})
function getProfile(token, cb) {
return request('https://api.github.com/user?' + qs.stringify({access_token: token}))
}
function saveUser(token, resp) {
getDB('users', function(err, db) {
db.save({token: token}, function(err) {
if (err) {
resp.statusCode = 500
return resp.end('error saving user')
}
resp.statusCode = 302
resp.setHeader('location', process.env['VHOST'])
resp.end()
})
})
}
function getDB(name, cb) {
if (!t.dbs) t.dbs = {}
if (t.dbs[name]) return cb(null, t.dbs[name])
var db = new flatdb(name)
db.on('connect', function() {
t.dbs[name] = db
cb(null, db)
})
}
function setCookie(id, resp) {
var twoWeeks = new Date(new Date().getTime()+1209726000).toUTCString()
resp.setHeader('set-cookie', ['Token='+id + '; Version=1; Path=/; HttpOnly; Expires=' + twoWeeks])
resp.setHeader('x-token', id)
}
function extractToken(req) {
if (req.headers.cookie) {
var cookies = parseCookies(req.headers.cookie)
if (cookies['Token']) return cookies['Token']
}
if (req.headers['x-token']) return req.headers['x-token']
if (req.qs && req.qs.token) return req.qs.token
return false
}
function parseCookies(cookie) {
var cookies = {}
cookie.split(';').forEach(function( cookie ) {
var parts = cookie.split('=')
cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim()
})
return cookies
}
t.httpServer.listen(8000, function () {
console.log('dun runnin')
})