This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
IcyNet.eu/server/api/oauth2/response.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-08-23 20:13:45 +00:00
import query from 'querystring'
import error from './error'
function data (req, res, code, data) {
res.header('Cache-Control', 'no-store')
res.header('Pragma', 'no-cache')
res.status(code).send(data)
console.debug('Response: ', data)
}
function redirect (req, res, redirectUri) {
res.header('Location', redirectUri)
res.status(302).end()
console.debug('Redirecting to ', redirectUri)
}
module.exports.error = function (req, res, err, redirectUri) {
// Transform unknown error
if (!(err instanceof error.OAuth2Error)) {
console.error(err.stack)
err = new error.ServerError('Uncaught exception')
} else {
console.error('Exception caught', err.stack)
}
if (redirectUri) {
2020-05-28 18:30:21 +00:00
const obj = {
2017-08-23 20:13:45 +00:00
error: err.code,
error_description: err.message
}
if (req.query.state) {
obj.state = req.query.state
}
redirectUri += '?' + query.stringify(obj)
redirect(req, res, redirectUri)
} else {
2020-05-28 18:30:21 +00:00
data(req, res, err.status, { error: err.code, error_description: err.message })
2017-08-23 20:13:45 +00:00
}
}
module.exports.data = function (req, res, obj, redirectUri, fragment) {
2017-08-23 20:13:45 +00:00
if (redirectUri) {
if (fragment) {
2017-08-23 20:13:45 +00:00
redirectUri += '#'
} else {
redirectUri += (redirectUri.indexOf('?') === -1 ? '?' : '&')
}
if (req.query.state) {
obj.state = req.query.state
}
redirectUri += query.stringify(obj)
redirect(req, res, redirectUri)
} else {
data(req, res, 200, obj)
}
}