2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// response
|
|
|
|
// external modules
|
2021-02-15 03:42:51 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2021-03-11 10:40:24 -05:00
|
|
|
const fetch = require('node-fetch')
|
2017-03-08 05:45:51 -05:00
|
|
|
// core
|
2021-02-15 03:42:51 -05:00
|
|
|
const config = require('./config')
|
|
|
|
const logger = require('./logger')
|
|
|
|
const models = require('./models')
|
2019-10-27 08:51:53 -04:00
|
|
|
const noteUtil = require('./web/note/util')
|
|
|
|
const errors = require('./errors')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// public
|
2021-02-15 03:42:51 -05:00
|
|
|
const response = {
|
2017-03-08 05:45:51 -05:00
|
|
|
showIndex: showIndex,
|
|
|
|
githubActions: githubActions,
|
|
|
|
gitlabActions: gitlabActions
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function showIndex (req, res, next) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const authStatus = req.isAuthenticated()
|
|
|
|
const deleteToken = ''
|
2018-05-25 12:19:31 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const data = {
|
2018-05-25 12:19:31 -04:00
|
|
|
signin: authStatus,
|
2017-03-08 05:45:51 -05:00
|
|
|
infoMessage: req.flash('info'),
|
2018-05-22 19:14:52 -04:00
|
|
|
errorMessage: req.flash('error'),
|
2019-08-26 08:55:41 -04:00
|
|
|
imprint: fs.existsSync(path.join(config.docsPath, 'imprint.md')),
|
2018-05-22 19:14:52 -04:00
|
|
|
privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')),
|
2018-05-25 12:19:31 -04:00
|
|
|
termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')),
|
|
|
|
deleteToken: deleteToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authStatus) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (user) {
|
|
|
|
data.deleteToken = user.deleteToken
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
2015-09-22 00:06:13 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function githubActions (req, res, next) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const noteId = req.params.noteId
|
2019-10-27 08:51:53 -04:00
|
|
|
noteUtil.findNote(req, res, function (note) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const action = req.params.action
|
2017-03-08 05:45:51 -05:00
|
|
|
switch (action) {
|
|
|
|
case 'gist':
|
|
|
|
githubActionGist(req, res, note)
|
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function githubActionGist (req, res, note) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const code = req.query.code
|
|
|
|
const state = req.query.state
|
2017-03-08 05:45:51 -05:00
|
|
|
if (!code || !state) {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const data = {
|
2017-03-08 05:45:51 -05:00
|
|
|
client_id: config.github.clientID,
|
|
|
|
client_secret: config.github.clientSecret,
|
|
|
|
code: code,
|
|
|
|
state: state
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const authUrl = 'https://github.com/login/oauth/access_token'
|
2021-03-11 10:40:24 -05:00
|
|
|
fetch(authUrl, {
|
2017-03-08 05:45:51 -05:00
|
|
|
method: 'POST',
|
2021-03-11 10:40:24 -05:00
|
|
|
body: JSON.stringify(data),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Accept: 'application/json'
|
|
|
|
}
|
|
|
|
}).then(resp => {
|
|
|
|
if (!resp.ok) {
|
|
|
|
throw new Error('forbidden')
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}).then(body => {
|
|
|
|
const accessToken = body.access_token
|
|
|
|
if (!accessToken) {
|
|
|
|
throw new Error('forbidden')
|
|
|
|
}
|
|
|
|
const content = note.content
|
|
|
|
const title = models.Note.decodeTitle(note.title)
|
|
|
|
const filename = title.replace('/', ' ') + '.md'
|
|
|
|
const gist = {
|
|
|
|
files: {}
|
|
|
|
}
|
|
|
|
gist.files[filename] = {
|
|
|
|
content: content
|
|
|
|
}
|
|
|
|
const gistUrl = 'https://api.github.com/gists'
|
|
|
|
return fetch(gistUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(gist),
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'HedgeDoc',
|
|
|
|
Authorization: 'token ' + accessToken,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Accept: 'application/json'
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2021-03-11 10:40:24 -05:00
|
|
|
})
|
|
|
|
}).then(resp => {
|
|
|
|
if (resp.status !== 201) {
|
|
|
|
throw new Error('forbidden')
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}).then(body => {
|
|
|
|
res.setHeader('referer', '')
|
|
|
|
res.redirect(body.html_url)
|
|
|
|
}).catch(error => {
|
|
|
|
if (error.message === 'forbidden') {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2021-03-11 10:40:24 -05:00
|
|
|
logger.error('GitHub Gist auth failed: ' + error)
|
|
|
|
return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
}
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function gitlabActions (req, res, next) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const noteId = req.params.noteId
|
2019-10-27 08:51:53 -04:00
|
|
|
noteUtil.findNote(req, res, function (note) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const action = req.params.action
|
2017-03-08 05:45:51 -05:00
|
|
|
switch (action) {
|
|
|
|
case 'projects':
|
|
|
|
gitlabActionProjects(req, res, note)
|
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function gitlabActionProjects (req, res, note) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
2021-10-29 14:54:57 -04:00
|
|
|
if (!user) {
|
|
|
|
return errors.errorNotFound(res)
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
|
2017-03-08 05:45:51 -05:00
|
|
|
ret.accesstoken = user.accessToken
|
|
|
|
ret.profileid = user.profileid
|
2021-03-11 10:40:24 -05:00
|
|
|
const apiUrl = `${config.gitlab.baseURL}/api/${config.gitlab.version}/projects?membership=yes&per_page=100&access_token=${user.accessToken}`
|
|
|
|
fetch(apiUrl).then(resp => {
|
|
|
|
if (!resp.ok) {
|
|
|
|
res.send(ret)
|
2021-10-29 14:54:57 -04:00
|
|
|
return Promise.reject(new Error('HTTP request returned not okay-ish status'))
|
2019-05-30 18:27:56 -04:00
|
|
|
}
|
2021-03-11 10:40:24 -05:00
|
|
|
return resp.json()
|
|
|
|
}).then(body => {
|
|
|
|
ret.projects = body
|
|
|
|
return res.send(ret)
|
2021-10-29 14:54:57 -04:00
|
|
|
}).catch(err => {
|
|
|
|
logger.error('gitlab action projects failed: ', err)
|
2021-03-11 10:40:24 -05:00
|
|
|
})
|
2017-03-08 05:45:51 -05:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('gitlab action projects failed: ' + err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = response
|