-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgitlab.js
More file actions
49 lines (42 loc) · 1.33 KB
/
gitlab.js
File metadata and controls
49 lines (42 loc) · 1.33 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
'use strict';
const { URL } = require('url');
const { httpsGetJson } = require('../oauth/http');
function gitlabApiBase(userRoute) {
try {
const u = new URL(userRoute);
const m = u.pathname.match(/^(.*\/api\/v4)\b/);
return `${u.protocol}//${u.host}${m ? m[1] : '/api/v4'}`;
} catch {
return 'https://gitlab.com/api/v4';
}
}
module.exports = {
type: 'gitlab',
matches({ profile }, strategyConfig) {
if (strategyConfig && strategyConfig.userRoute && /\/api\/v4\/user\b/.test(strategyConfig.userRoute)) {
return true;
}
if (profile && (profile.web_url || profile.avatar_url) &&
profile.username && profile.id && profile.state) {
return true;
}
return false;
},
parse(profile) {
return {
id: profile.id,
displayName: profile.username || profile.name,
fullname: profile.name || profile.username,
picture: profile.avatar_url,
email: profile.email || profile.public_email || undefined,
email_verified: profile.confirmed_at ? true : undefined,
username: profile.username,
};
},
async fetchEmail({ strategyConfig, accessToken }) {
const base = gitlabApiBase(strategyConfig.userRoute);
const json = await httpsGetJson({ url: `${base}/user`, accessToken });
return json && json.email ? { email: json.email, verified: !!json.confirmed_at } : null;
},
};
module.exports.gitlabApiBase = gitlabApiBase;