|
| 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 | +} |
0 commit comments