-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghe_server.js
executable file
·114 lines (105 loc) · 3.43 KB
/
ghe_server.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
Ghe = {};
OAuth.registerService('ghe', 2, null, function(query) {
var accessToken = getAccessToken(query);
var identity = getIdentity(accessToken);
var emails = getEmails(accessToken);
var primaryEmail = _.findWhere(emails, {
primary: true
});
return {
serviceData: {
id: identity.id,
accessToken: OAuth.sealSecret(accessToken),
email: identity.email || (primaryEmail && primaryEmail.email) || '',
username: identity.login,
emails: emails
},
options: {
profile: {
name: identity.name
}
}
};
});
// http://developer.github.com/v3/#user-agent-required
var userAgent = 'Meteor';
if (Meteor.release)
userAgent += '/' + Meteor.release;
var getAccessToken = function(query) {
var config = ServiceConfiguration.configurations.findOne({
service: 'ghe'
});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
'https://' + config.gheURL + '/login/oauth/access_token', {
headers: {
Accept: 'application/json',
'User-Agent': userAgent
},
params: {
code: query.code,
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
redirect_uri: OAuth._redirectUri('ghe', config),
state: query.state
}
});
} catch (err) {
throw _.extend(new Error('Failed to complete OAuth handshake with Github. ' + err.message), {
response: err.response
});
}
if (response.data.error) { // if the http response was a json object with an error attribute
throw new Error('Failed to complete OAuth handshake with GitHub. ' + response.data.error);
} else {
return response.data.access_token;
}
};
var getIdentity = function(accessToken) {
var config = ServiceConfiguration.configurations.findOne({
service: 'ghe'
});
if (!config)
throw new ServiceConfiguration.ConfigError();
try {
return HTTP.get(
'https://' + config.gheAPI + '/user', {
headers: {
'User-Agent': userAgent
}, // http://developer.github.com/v3/#user-agent-required
params: {
access_token: accessToken
}
}).data;
} catch (err) {
throw _.extend(new Error('Failed to fetch identity from Github. ' + err.message), {
response: err.response
});
}
};
var getEmails = function(accessToken) {
var config = ServiceConfiguration.configurations.findOne({
service: 'ghe'
});
if (!config)
throw new ServiceConfiguration.ConfigError();
try {
return HTTP.get(
'https://' + config.gheAPI + '/user/emails', {
headers: {
'User-Agent': userAgent
}, // http://developer.github.com/v3/#user-agent-required
params: {
access_token: accessToken
}
}).data;
} catch (err) {
return [];
}
};
Ghe.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};