Repository navigation
Expand file tree
/
Copy pathoauth.js
More file actions
95 lines (82 loc) · 2.62 KB
/
Copy pathoauth.js
File metadata and controls
95 lines (82 loc) · 2.62 KB
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
/** section: github, internal
* OAuth
*
* OAuth usage example.
*
* Copyright 2012 Cloud9 IDE, Inc.
*
* This product includes software developed by
* Cloud9 IDE, Inc (http://c9.io).
*
* Author: Mike de Boer <mike@c9.io>
**/
var http = require("http");
var Url = require("url");
var querystring = require("querystring");
var Client = require("../index");
var OAuth2 = require("oauth").OAuth2;
var github = new Client({
version: "3.0.0"
});
var clientId = "e8c434a1c92e9de7ff8d";
var secret = "1d0fcbb060e1dd86a0aa3d12265419c9bb19a333";
var oauth = new OAuth2(clientId, secret, "https://github.com/", "login/oauth/authorize", "login/oauth/access_token");
// for demo purposes use one global access token
// in production this has to be stored in a user session
var accessToken = "";
http.createServer(function(req, res) {
var url = Url.parse(req.url);
var path = url.pathname;
var query = querystring.parse(url.query);
if (path == "/" || path.match(/^\/user\/?$/)) {
// redirect to github if there is no access token
if (!accessToken) {
res.writeHead(303, {
Location: oauth.getAuthorizeUrl({
redirect_uri: 'http://localhost:7878/github-callback',
scope: "user,repo,gist"
})
});
res.end();
return;
}
// use github API
github.user.get({}, function(err, user) {
if (err) {
res.writeHead(err.code);
res.end(err + "");
return;
}
res.writeHead(200);
res.end(JSON.stringify(user));
});
return;
}
// URL called by github after authenticating
else if (path.match(/^\/github-callback\/?$/)) {
// upgrade the code to an access token
oauth.getOAuthAccessToken(query.code, {}, function (err, access_token, refresh_token) {
if (err) {
console.log(err);
res.writeHead(500);
res.end(err + "");
return;
}
accessToken = access_token;
// authenticate github API
github.authenticate({
type: "oauth",
token: accessToken
});
//redirect back
res.writeHead(303, {
Location: "/"
});
res.end();
});
return;
}
res.writeHead(404);
res.end("404 - Not found");
}).listen(7878);
console.log("listening at http://localhost:7878");