Skip to content

Commit 01d07d7

Browse files
authored
Merge pull request #26 from VaalaCat/gitlab
add gitlab oauth support
2 parents 3da541f + 3a2496b commit 01d07d7

File tree

6 files changed

+120
-7
lines changed

6 files changed

+120
-7
lines changed

.DS_Store

6 KB
Binary file not shown.

lib/backend.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function setupRouter (config) {
106106
require('./register')(config)
107107
require('./steam')(config)
108108
require('./github')(config)
109+
require('./gitlab')(config)
109110
require('./menu.js')(config)
110111
}
111112

lib/gitlab/index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const path = require('path')
2+
const express = require('express')
3+
const bodyParser = require('body-parser')
4+
const cookieParser = require('cookie-parser')
5+
6+
const passport = require('passport')
7+
const GitLabStrategy = require('passport-gitlab2').Strategy
8+
9+
const authlib = require(path.join(path.dirname(require.main.filename), '../lib/authlib'))
10+
const auth = require(path.join(path.dirname(require.main.filename), '../lib/game/api/auth'))
11+
12+
const app = new express.Router()
13+
14+
module.exports = function (config) {
15+
const clientId = process.env.GITLAB_APP_ID || (config.auth.screepsrc.gitlab && config.auth.screepsrc.gitlab.appId) || null
16+
const clientSecret = process.env.GITLAB_APP_SECRET || (config.auth.screepsrc.gitlab && config.auth.screepsrc.gitlab.appSecret) || null
17+
const enabled = !!(clientId && clientSecret)
18+
const gitlabURL = process.env.GITLAB_URL || "https://gitlab.com"
19+
20+
config.auth.info.gitlab = enabled
21+
let registered = false
22+
23+
app.use(cookieParser())
24+
25+
app.get('/', (req, res, next) => {
26+
let { token } = req.query
27+
if (token) res.cookie('auth_token', token)
28+
if (!registered) {
29+
registered = true
30+
let proto = req.get('X-Forwarded-Proto') || req.protocol || 'http'
31+
let baseUrl = `${proto}://${req.get('host')}`
32+
passport.use('gitlab', new GitLabStrategy({
33+
callbackURL: baseUrl + '/api/auth/gitlab/return',
34+
baseURL: gitlabURL,
35+
clientID: clientId,
36+
clientSecret: clientSecret
37+
}, (accessToken, refreshTokem, profile, done) => done(null, profile.id)))
38+
}
39+
setTimeout(next, 100)
40+
}, passport.authenticate('gitlab'))
41+
42+
app.get('/return', passport.authenticate('gitlab', { failureRedirect: '/' }), (req, res) => {
43+
let user = null
44+
let token = req.cookies.auth_token
45+
res.clearCookie('auth_token')
46+
if (token) user = authlib.checkToken(token)
47+
gitlabFindOrCreateUser(user, req.user)
48+
.then(user => authlib.genToken(user._id))
49+
.then(token => {
50+
let json = JSON.stringify({ username: req.user.username, token })
51+
res.end(`<html><body><script type="text/javascript">opener.postMessage(JSON.stringify(${json}), '*');window.close();</script></body>`)
52+
})
53+
.catch(err => res.end('Failed to auth'))
54+
})
55+
// })
56+
config.auth.router.use('/api/auth/gitlab', app)
57+
config.auth.router.post('/api/user/unlink-gitlab', auth.tokenAuth, (req, res) => {
58+
if (!req.user) return
59+
config.common.storage.db.users.update({ _id: req.user._id }, { $unset: { gitlab: true } })
60+
res.json({ ok: 1 })
61+
})
62+
63+
function gitlabFindOrCreateUser(user, id) {
64+
let { db, env } = config.common.storage
65+
if (user) {
66+
return user.then((user) => {
67+
return db.users.update({ _id: user._id }, { $set: { gitlab: { id } } })
68+
.then(() => user)
69+
})
70+
}
71+
return db.users.findOne({ 'gitlab.id': id })
72+
.then((user) => {
73+
if (user) return user
74+
user = {
75+
gitlab: { id },
76+
cpu: 100,
77+
cpuAvailable: 0,
78+
registeredDate: new Date(),
79+
money: 0,
80+
gcl: 0
81+
};
82+
return db.users.insert(user)
83+
.then(result => {
84+
user = result;
85+
return db['users.code'].insert({
86+
user: user._id,
87+
modules: { main: '' },
88+
branch: 'default',
89+
activeWorld: true,
90+
activeSim: true
91+
})
92+
})
93+
.then(() => env.set('scrUserMemory:' + user._id, JSON.stringify({})))
94+
.then(() => user)
95+
})
96+
}
97+
}

lib/menu.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = config => {
33
const authTypes = ['password']
44
if (config.auth.info.steam) authTypes.push('steam')
55
if (config.auth.info.github) authTypes.push('github')
6-
6+
if (config.auth.info.gitlab) authTypes.push('gitlab')
7+
78
config.backend.features.push(...[
89
{
910
name: 'auth',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"ini": "^1.3.4",
2626
"passport": "^0.3.2",
2727
"passport-github": "^1.1.0",
28+
"passport-gitlab2": "^5.0.0",
2829
"passport-http": "^0.3.0",
2930
"passport-local": "^1.0.0",
3031
"passport-steam": "^1.0.9"

yarn.lock

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ ajv@^6.5.5:
3838
json-schema-traverse "^0.4.1"
3939
uri-js "^4.2.2"
4040

41-
ansi-regex@*:
42-
version "4.1.0"
43-
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997"
44-
integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==
45-
4641
ansi-regex@^2.0.0:
4742
version "2.1.1"
4843
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df"
@@ -889,7 +884,7 @@ iferr@^0.1.5:
889884
resolved "https://registry.yarnpkg.com/iferr/-/iferr-0.1.5.tgz#c60eed69e6d8fdb6b3104a1fcbca1c192dc5b501"
890885
integrity sha1-xg7taebY/bazEEofy8ocGS3FtQE=
891886

892-
imurmurhash@*, imurmurhash@^0.1.4:
887+
imurmurhash@^0.1.4:
893888
version "0.1.4"
894889
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
895890
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -1460,6 +1455,13 @@ passport-github@^1.1.0:
14601455
dependencies:
14611456
passport-oauth2 "1.x.x"
14621457

1458+
passport-gitlab2@^5.0.0:
1459+
version "5.0.0"
1460+
resolved "https://registry.yarnpkg.com/passport-gitlab2/-/passport-gitlab2-5.0.0.tgz#ea37e5285321c026a02671e87469cac28cce9b69"
1461+
integrity sha512-cXQMgM6JQx9wHVh7JLH30D8fplfwjsDwRz+zS0pqC8JS+4bNmc1J04NGp5g2M4yfwylH9kQRrMN98GxMw7q7cg==
1462+
dependencies:
1463+
passport-oauth2 "^1.4.0"
1464+
14631465
passport-http@^0.3.0:
14641466
version "0.3.0"
14651467
resolved "https://registry.yarnpkg.com/passport-http/-/passport-http-0.3.0.tgz#8ee53d4380be9c60df2151925029826f77115603"
@@ -1485,6 +1487,17 @@ [email protected]:
14851487
uid2 "0.0.x"
14861488
utils-merge "1.x.x"
14871489

1490+
passport-oauth2@^1.4.0:
1491+
version "1.6.1"
1492+
resolved "https://registry.yarnpkg.com/passport-oauth2/-/passport-oauth2-1.6.1.tgz#c5aee8f849ce8bd436c7f81d904a3cd1666f181b"
1493+
integrity sha512-ZbV43Hq9d/SBSYQ22GOiglFsjsD1YY/qdiptA+8ej+9C1dL1TVB+mBE5kDH/D4AJo50+2i8f4bx0vg4/yDDZCQ==
1494+
dependencies:
1495+
base64url "3.x.x"
1496+
oauth "0.9.x"
1497+
passport-strategy "1.x.x"
1498+
uid2 "0.0.x"
1499+
utils-merge "1.x.x"
1500+
14881501
passport-openid@^0.4.0:
14891502
version "0.4.0"
14901503
resolved "https://registry.yarnpkg.com/passport-openid/-/passport-openid-0.4.0.tgz#c2b58f5ff4a28f285700250712d8ea5677405cd6"

0 commit comments

Comments
 (0)