1
1
import url from 'node:url' ;
2
2
import express from 'express' ;
3
- import passport from 'passport' ;
4
- import OAuth2Strategy from 'passport-oauth2' ;
5
- import { InternalOAuthError } from 'passport-oauth2' ;
6
3
import session from 'express-session' ;
4
+ import PassportOAuth2Middleware from './middleware.js' ;
7
5
8
6
const app = express ( ) ;
9
7
const port = 3000 ;
@@ -14,20 +12,10 @@ app.use(session({
14
12
saveUninitialized : true
15
13
} ) ) ;
16
14
17
- passport . serializeUser ( function ( user , cb ) {
18
- process . nextTick ( function ( ) {
19
- cb ( null , { id : user . id , username : user . username , accessToken : user . accessToken } ) ;
20
- } ) ;
21
- } ) ;
22
-
23
- passport . deserializeUser ( function ( user , cb ) {
24
- process . nextTick ( function ( ) {
25
- return cb ( null , user ) ;
26
- } ) ;
27
- } ) ;
28
15
29
- const gitlabOAuth2Strategy = new OAuth2Strategy (
30
- {
16
+ app . use (
17
+ '/' ,
18
+ PassportOAuth2Middleware ( {
31
19
// configuration inspired from https://github.com/fh1ch/passport-gitlab2/blob/4238b67438c1f1a7050908556ac010bc319b734a/lib/strategy.js
32
20
clientID : process . env . GITLAB_CLIENT_ID ,
33
21
clientSecret : process . env . GITLAB_CLIENT_SECRET ,
@@ -36,51 +24,9 @@ const gitlabOAuth2Strategy = new OAuth2Strategy(
36
24
scope : "api email profile" ,
37
25
scopeSeparator : " " ,
38
26
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
- }
27
+ } )
50
28
) ;
51
29
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
-
84
30
app . get ( '/' , ( req , res ) => {
85
31
if ( req . session ?. passport ?. user ?. id ) {
86
32
res . send ( `<ul>
@@ -96,25 +42,6 @@ app.get('/', (req, res) => {
96
42
}
97
43
} ) ;
98
44
99
- app . get (
100
- '/auth/gitlab' ,
101
- passport . authenticate (
102
- 'oauth2'
103
- )
104
- ) ;
105
-
106
- app . get (
107
- '/auth/gitlab/callback' ,
108
- passport . authenticate ( 'oauth2' , {
109
- failureRedirect : '/login'
110
- } ) ,
111
- function ( req , res ) {
112
- // Successful authentication, redirect home.
113
- console . log ( 'Successful authentication, redirect home' ) ;
114
- res . redirect ( '/' ) ;
115
- }
116
- ) ;
117
-
118
45
app . get ( '/logout' , ( req , res ) => {
119
46
req . session . destroy ( function ( err ) {
120
47
res . redirect ( '/' ) ;
0 commit comments