Skip to content

Commit 1af6102

Browse files
committed
feat(roadmap): Allow signin with GitLab to show confidential issues
fix #3
1 parent f60a7c4 commit 1af6102

File tree

12 files changed

+272
-18
lines changed

12 files changed

+272
-18
lines changed

app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const app = express()
55
// setup express app
66
require('./config/express')(app)
77

8+
// setup passport
9+
require('./config/passport')(app)
10+
811
app.use('/', require('./routes')())
912

1013
// setup error handlers

app.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@
2525
"description": "ID of GitLab issue board",
2626
"required": true
2727
},
28+
"GL_RM_INT_CONFIG_GL_APP": {
29+
"description": "Enable login with GitLab?",
30+
"value":"false",
31+
"required": false
32+
},
33+
"GL_RM_INT_CONFIG_GL_CLIENT_ID": {
34+
"description": "GitLab application client ID",
35+
"required": false
36+
},
37+
"GL_RM_INT_CONFIG_GL_CLIENT_SECRET": {
38+
"description": "GitLab application client secret",
39+
"required": false
40+
},
41+
"GL_RM_INT_CONFIG_BASE_URL": {
42+
"description": "Base url of the roadmap",
43+
"required": false
44+
},
45+
"GL_RM_INT_CONFIG_COOKIE_SECRET": {
46+
"description": "Random secret for cookies",
47+
"generator":"secret",
48+
"required": false
49+
},
2850
"GL_RM_CONFIG_THEME": {
2951
"description": "Bootstrap CSS theme",
3052
"value": "https://bootswatch.com/4/materia/bootstrap.min.css",

config.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ internal-config:
33
url: https://gitlab.com # gitlab instance address
44
project-id: filiosoft/roadmap # can be ID or path
55
board-id: 49 # issue board ID
6+
gl-app: true # enable login with GitLab?
7+
gl-client-id: f135d8df73f083f632d877ffa4d5252e953255de5bd78458cbe994dbb92cc9a9 # gitlab application client id
8+
gl-client-secret: 51ca773af0789f1c6afe1a8a118364cace26fa06a511227acfa9f5de0c83da13 # gitlab application client secret
9+
base-url: https://roadmap.filiosoft.com # base url of roadmap
10+
cookie-secret: change-me # random secret
611
config:
712
theme: https://bootswatch.com/4/materia/bootstrap.min.css # boostrap theme
813
domain: roadmap.filiosoft.com # domain to deploy to

config/config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ const getConfig = () => {
44
const internalConfig = {}
55
const config = {}
66

7-
// convert undefined string to actual undefined
8-
const convertUndefined = input => {
7+
// convert undefined, true, and false string to type
8+
const convert = input => {
99
if (input === 'undefined') {
1010
return undefined
11+
} else if (input === 'false') {
12+
return false
13+
} else if (input === 'true') {
14+
return true
1115
}
1216
return input
1317
}
1418

1519
const loadConfig = (prefix, object) => {
1620
for (const key in process.env) {
1721
if (key.startsWith(prefix)) {
18-
const newValue = convertUndefined(process.env[key])
22+
const newValue = convert(process.env[key])
1923
const newKey = key.replace(prefix, '').toLowerCase()
2024

2125
object[newKey] = newValue

config/express.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const axios = require('../config/axios')(internalConfig)
1010
module.exports = async app => {
1111
// set config globals
1212
app.locals.config = config
13+
app.locals.appEnabled = internalConfig.gl_app
1314
try {
1415
const projectResp = await axios.get('/')
1516
app.locals.project = projectResp.data

config/passport.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const passport = require('passport')
2+
const GitLabStrategy = require('passport-gitlab2').Strategy
3+
const cookieSession = require('cookie-session')
4+
const express = require('express')
5+
6+
const { internalConfig } = require('./config')()
7+
8+
const router = express.Router()
9+
10+
module.exports = app => {
11+
// only enable if gl_app var is true
12+
if (internalConfig.gl_app) {
13+
// cookieSession config
14+
app.use(
15+
cookieSession({
16+
maxAge: 24 * 60 * 60 * 1000, // One day in milliseconds
17+
keys: [internalConfig.cookie_secret]
18+
})
19+
)
20+
21+
app.use(passport.initialize()) // Used to initialize passport
22+
app.use(passport.session()) // Used to persist login sessions
23+
24+
passport.use(
25+
new GitLabStrategy(
26+
{
27+
clientID: internalConfig.gl_client_id,
28+
clientSecret: internalConfig.gl_client_secret,
29+
callbackURL: `${internalConfig.base_url}/auth/gitlab/callback`,
30+
baseURL: internalConfig.url
31+
},
32+
(accessToken, refreshToken, profile, cb) => {
33+
const user = {
34+
id: profile.id,
35+
username: profile.username,
36+
accessToken
37+
}
38+
return cb(null, user)
39+
}
40+
)
41+
)
42+
43+
passport.serializeUser((user, done) => {
44+
done(null, user)
45+
})
46+
47+
// Used to decode the received cookie and persist session
48+
passport.deserializeUser((user, done) => {
49+
done(null, user)
50+
})
51+
52+
// passport.authenticate middleware is used here to authenticate the request
53+
router.get(
54+
'/gitlab',
55+
passport.authenticate('gitlab', {
56+
scope: ['api'] // Used to specify the required data
57+
})
58+
)
59+
60+
// The middleware receives the data from GitLab and runs the function on Strategy config
61+
router.get(
62+
'/gitlab/callback',
63+
passport.authenticate('gitlab'),
64+
(req, res) => {
65+
res.redirect('/')
66+
}
67+
)
68+
69+
// Logout route
70+
router.get('/logout', (req, res) => {
71+
req.logout()
72+
res.redirect('/')
73+
})
74+
75+
app.use('/auth', router)
76+
}
77+
}

example.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ GL_RM_INT_CONFIG_URL=https://gitlab.com
33
GL_RM_INT_CONFIG_PROJECT_ID=filiosoft/roadmap
44
GL_RM_INT_CONFIG_BOARD_ID=49
55

6+
GL_RM_INT_CONFIG_GL_APP=true
7+
GL_RM_INT_CONFIG_GL_CLIENT_ID=f135d8df73f083f632d877ffa4d5252e953255de5bd78458cbe994dbb92cc9a9
8+
GL_RM_INT_CONFIG_GL_CLIENT_SECRET=51ca773af0789f1c6afe1a8a118364cace26fa06a511227acfa9f5de0c83da13
9+
GL_RM_INT_CONFIG_BASE_URL=https://roadmap.filiosoft.com
10+
GL_RM_INT_CONFIG_COOKIE_SECRET=YV9k9GO7Y1pt
11+
612
GL_RM_CONFIG_THEME=https://bootswatch.com/4/materia/bootstrap.min.css
713
GL_RM_CONFIG_DOMAIN=roadmap.filiosoft.com
814
GL_RM_CONFIG_LINK=https://filiosoft.com

0 commit comments

Comments
 (0)