1
+ import url from 'node:url' ;
1
2
import express from 'express' ;
2
3
import passport from 'passport' ;
3
- import GitLabStrategy from 'passport-gitlab2' ;
4
+ import OAuth2Strategy from 'passport-oauth2' ;
5
+ import { InternalOAuthError } from 'passport-oauth2' ;
4
6
import session from 'express-session' ;
5
7
6
8
const app = express ( ) ;
@@ -24,30 +26,62 @@ passport.deserializeUser(function(user, cb) {
24
26
} ) ;
25
27
} ) ;
26
28
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
+ }
46
50
) ;
47
51
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
+
48
84
app . get ( '/' , ( req , res ) => {
49
- console . log ( 'req.session' ) ;
50
- console . log ( req . session ) ;
51
85
if ( req . session ?. passport ?. user ?. id ) {
52
86
res . send ( `<ul>
53
87
<li>id: ${ req . session . passport . user . id } </li>
@@ -62,16 +96,13 @@ app.get('/', (req, res) => {
62
96
app . get (
63
97
'/auth/gitlab' ,
64
98
passport . authenticate (
65
- 'gitlab' ,
66
- {
67
- scope : [ 'api email profile' ] // See https://github.com/fh1ch/passport-gitlab2/pull/17
68
- }
99
+ 'oauth2'
69
100
)
70
101
) ;
71
102
72
103
app . get (
73
104
'/auth/gitlab/callback' ,
74
- passport . authenticate ( 'gitlab ' , {
105
+ passport . authenticate ( 'oauth2 ' , {
75
106
failureRedirect : '/login'
76
107
} ) ,
77
108
function ( req , res ) {
0 commit comments