Skip to content

Commit 243b3fe

Browse files
Migrate fro passport-gitlab2 to passport-oauth2
1 parent 4f69c6d commit 243b3fe

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"dependencies": {
1212
"express": "4.18.1",
1313
"express-session": "1.17.3",
14-
"nodemon": "^2.0.16",
14+
"nodemon": "2.0.16",
1515
"passport": "0.6.0",
16-
"passport-gitlab2": "5.0.0"
16+
"passport-oauth2": "1.6.1"
1717
}
1818
}

pnpm-lock.yaml

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/server.js

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import url from 'node:url';
12
import express from 'express';
23
import passport from 'passport';
3-
import GitLabStrategy from 'passport-gitlab2';
4+
import OAuth2Strategy from 'passport-oauth2';
5+
import { InternalOAuthError } from 'passport-oauth2';
46
import session from 'express-session';
57

68
const app = express();
@@ -24,30 +26,62 @@ passport.deserializeUser(function(user, cb) {
2426
});
2527
});
2628

27-
passport.use(
28-
new GitLabStrategy(
29-
{
30-
clientID: process.env.GITLAB_CLIENT_ID,
31-
clientSecret: process.env.GITLAB_CLIENT_SECRET,
32-
callbackURL: 'http://127.0.0.1:3000/auth/gitlab/callback',
33-
baseURL: process.env.GITLAB_BASEURL
34-
},
35-
function(accessToken, refreshToken, profile, cb) {
36-
return cb(
37-
null,
38-
{
39-
id: profile.id,
40-
username: profile.username,
41-
accessToken: accessToken
42-
}
43-
);
44-
}
45-
)
29+
const gitlabOAuth2Strategy = new OAuth2Strategy(
30+
{
31+
// configuration inspired from https://github.com/fh1ch/passport-gitlab2/blob/4238b67438c1f1a7050908556ac010bc319b734a/lib/strategy.js
32+
clientID: process.env.GITLAB_CLIENT_ID,
33+
clientSecret: process.env.GITLAB_CLIENT_SECRET,
34+
authorizationURL: url.resolve(process.env.GITLAB_BASEURL, 'oauth/authorize'),
35+
tokenURL: url.resolve(process.env.GITLAB_BASEURL, 'oauth/token'),
36+
scope: "api email profile",
37+
scopeSeparator: " ",
38+
callbackURL: 'http://127.0.0.1:3000/auth/gitlab/callback'
39+
},
40+
function(accessToken, refreshToken, profile, cb) {
41+
return cb(
42+
null,
43+
{
44+
id: profile.id,
45+
username: profile.username,
46+
accessToken: accessToken
47+
}
48+
);
49+
}
4650
);
4751

52+
gitlabOAuth2Strategy.userProfile = function (accesstoken, done) {
53+
this._oauth2.get(
54+
url.resolve(process.env.GITLAB_BASEURL, 'api/v4/user'),
55+
accesstoken,
56+
(err, body) => {
57+
let json;
58+
if (err) {
59+
return done(new InternalOAuthError('Failed to fetch user profile', err));
60+
}
61+
62+
try {
63+
json = JSON.parse(body);
64+
} catch (ex) {
65+
return done(new Error('Failed to parse user profile'));
66+
}
67+
68+
const profile = {
69+
id: String(json.id),
70+
username: json.username,
71+
displayName: json.name,
72+
emails: [{value: json.email}],
73+
avatarUrl: json.avatar_url,
74+
profileUrl: json.web_url
75+
};
76+
77+
done(null, profile);
78+
}
79+
);
80+
};
81+
82+
passport.use(gitlabOAuth2Strategy);
83+
4884
app.get('/', (req, res) => {
49-
console.log('req.session');
50-
console.log(req.session);
5185
if (req.session?.passport?.user?.id) {
5286
res.send(`<ul>
5387
<li>id: ${req.session.passport.user.id}</li>
@@ -62,16 +96,13 @@ app.get('/', (req, res) => {
6296
app.get(
6397
'/auth/gitlab',
6498
passport.authenticate(
65-
'gitlab',
66-
{
67-
scope: ['api email profile'] // See https://github.com/fh1ch/passport-gitlab2/pull/17
68-
}
99+
'oauth2'
69100
)
70101
);
71102

72103
app.get(
73104
'/auth/gitlab/callback',
74-
passport.authenticate('gitlab', {
105+
passport.authenticate('oauth2', {
75106
failureRedirect: '/login'
76107
}),
77108
function(req, res) {

0 commit comments

Comments
 (0)