/
github.js
88 lines (76 loc) · 1.98 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
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
var auth = require("../../../util/auth");
var GithubStrategy = require('passport-github').Strategy;
var AbstractProvider = require("./abstract");
if (!process.configuration) {
process.configuration = {};
}
if (!process.configuration.providers) {
process.configuration.providers = {};
}
if (!process.configuration.providers.github) {
process.configuration.providers.github = {};
}
if (process.env.CLOUDCMS_AUTH_PROVIDERS_GITHUB_ENABLED === "true") {
process.configuration.providers.github.enabled = true;
}
/**
* "github" Authentication Provider
*
* Provider-specific configuration:
*
* "clientID": "[OAuth2 Client ID]",
* "clientSecret": "[OAuth2 Client Secret]",
*
*/
class GithubProvider extends AbstractProvider
{
constructor(req, config)
{
super(req, config);
if (!config.properties) {
config.properties = {};
}
if (!config.properties.id) {
config.properties.id = "id";
}
// passport
this.githubStrategy = new GithubStrategy({
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
passReqToCallback: true
}, auth.buildPassportCallback(config, this));
req.passport.use(this.githubStrategy);
}
/**
* @override
*/
handleAuth(req, res, next)
{
req.passport.authenticate("github",{
scope: ['user']
})(req, res, next);
};
/**
* @override
*/
handleAuthCallback(req, res, next, cb)
{
req.passport.authenticate("github", {
session: false
}, cb)(req, res, next);
};
/**
* @override
*/
load(properties, callback)
{
this.githubStrategy.userProfile(properties.token, function(err, profile) {
if (err) {
return callback(err);
}
callback(null, profile);
});
};
}
module.exports = GithubProvider;