forked from robfallows/tunguska-imgur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitlab_server.js
215 lines (190 loc) · 6.33 KB
/
gitlab_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
/**
* Define the base object namespace. By convention we use the service name
* in PascalCase (aka UpperCamelCase). Note that this is defined as a package global.
*/
Gitlab = {};
/**
* Boilerplate hook for use by underlying Meteor code
*/
Gitlab.retrieveCredential = (credentialToken, credentialSecret) => {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};
/**
* Define the fields we want.
* id {Integer} The user's Gitlab id
* web_url {String} The account gitlab url
* name {String} The account 'real world' name
* username {String} The account username
* state {String} account status active | inactive
* email {String} account email
*/
Gitlab.whitelistedFields = ['id', 'email', 'state', 'web_url', 'name', 'username'];
/**
* Register this service with the underlying OAuth handler
* (name, oauthVersion, urls, handleOauthRequest):
* name = 'gitlab'
* oauthVersion = 2
* urls = null for OAuth 2
* handleOauthRequest = function(query) returns {serviceData, options} where options is optional
* serviceData will end up in the user's services.gitlab
*/
OAuth.registerService('gitlab', 2, null, function(query) {
/**
* Make sure we have a config object for subsequent use (boilerplate)
*/
const config = ServiceConfiguration.configurations.findOne({
service: 'gitlab'
});
if (!config) {
throw new ServiceConfiguration.ConfigError();
}
/**
* Get the token and username (Meteor handles the underlying authorization flow).
* Note that the username comes from this request in Gitlab.
*/
const response = getTokens(config, query);
const accessToken = response.accessToken;
const username = response.username;
/**
* If we got here, we can now request data from the account endpoints
* to complete our serviceData request.
* The identity object will contain the username plus *all* properties
* retrieved from the account and settings methods.
*/
const identity = _.extend(
{username},
getAccount(config, username, accessToken)
);
/**
* Build our serviceData object. This needs to contain
* accessToken
* expiresAt, as a ms epochtime
* refreshToken, if there is one
* id - note that there *must* be an id property for Meteor to work with
* email
* reputation
* created
* We'll put the username into the user's profile
*/
const serviceData = {
accessToken,
expiresAt: (+new Date) + (1000 * response.expiresIn)
};
if (response.refreshToken) {
serviceData.refreshToken = response.refreshToken;
}
_.extend(serviceData, _.pick(identity, Gitlab.whitelistedFields ));
/**
* Return the serviceData object along with an options object containing
* the initial profile object with the username.
*/
return {
serviceData: serviceData,
options: {
profile: {
name: response.username // comes from the token request
}
}
};
});
/**
* The following three utility functions are called in the above code to get
* the access_token, refresh_token and username (getTokens)
* account data (getAccount)
* settings data (getSettings)
* repectively.
*/
/** getTokens exchanges a code for a token in line with Gitlab's documentation
*
* returns an object containing:
* accessToken {String}
* expiresIn {Integer} Lifetime of token in seconds
* refreshToken {String} If this is the first authorization request
* account_username {String} User name of the current user
* token_type {String} Set to 'Bearer'
*
* @param {Object} config The OAuth configuration object
* @param {Object} query The OAuth query object
* @return {Object} The response from the token request (see above)
*/
const getTokens = function(config, query) {
const endpoint = config.gitlabInstanceUrl + '/oauth/token';
const redirectUri = Meteor.absoluteUrl() + '_oauth/gitlab';
/**
* Attempt the exchange of code for token
*/
let response;
try {
response = HTTP.post(
endpoint, {
params: {
code: query.code,
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
grant_type: 'authorization_code',
redirect_uri: redirectUri
}
});
} catch (err) {
throw _.extend(new Error(`Failed to complete OAuth handshake with Gitlab. ${err.message}`), {
response: err.response
});
}
if (response.data.error) {
/**
* The http response was a json object with an error attribute
*/
throw new Error(`Failed to complete OAuth handshake with Gitlab. ${response.data.error}`);
} else {
/** The exchange worked. We have an object containing
* access_token
* refresh_token
* expires_in
* token_type
* account_username
*
* Return an appropriately constructed object
*/
return {
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token,
expiresIn: response.data.expires_in,
username: response.data.account_username
};
}
};
/**
* getAccount gets Gitlab account data
*
* returns an object containing:
* id {Integer} The user's Gitlab id
* web_url {String} The account gitlab url
* name {String} The account 'real world' name
* username {String} The account username
* state {String} account status active | inactive
* email {String} account email
*
* @param {Object} config The OAuth configuration object
* @param {String} username The Gitlab username
* @param {String} accessToken The OAuth access token
* @return {Object} The response from the account request (see above)
*/
const getAccount = function(config, username, accessToken) {
const endpoint = config.gitlabInstanceUrl + `/api/v4/user`;
let accountObject;
try {
accountObject = HTTP.get(
endpoint, {
headers: {
Authorization: `Bearer ${accessToken}`
}
}
).data;
return accountObject;
} catch (err) {
throw _.extend(new Error(`Failed to fetch account data from Gitlab. ${err.message}`), {
response: err.response
});
}
};