2020-12-13 14:36:07 +00:00
|
|
|
import { InvalidRequest } from '../error'
|
|
|
|
import { data as dataResponse } from '../response'
|
2020-06-05 15:18:23 +00:00
|
|
|
import wrap from '../wrap'
|
2017-08-23 20:13:45 +00:00
|
|
|
|
2020-12-13 14:36:07 +00:00
|
|
|
export const introspection = wrap(async function (req, res) {
|
2017-08-23 20:13:45 +00:00
|
|
|
let clientId = null
|
|
|
|
let clientSecret = null
|
|
|
|
|
|
|
|
if (req.body.client_id && req.body.client_secret) {
|
|
|
|
clientId = req.body.client_id
|
|
|
|
clientSecret = req.body.client_secret
|
|
|
|
console.debug('Client credentials parsed from body parameters ', clientId, clientSecret)
|
|
|
|
} else {
|
|
|
|
if (!req.headers || !req.headers.authorization) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('No authorization header passed')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pieces = req.headers.authorization.split(' ', 2)
|
|
|
|
if (!pieces || pieces.length !== 2) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('Authorization header is corrupted')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pieces[0] !== 'Basic') {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('Unsupported authorization method:', pieces[0])
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 16:23:03 +00:00
|
|
|
pieces = Buffer.from(pieces[1], 'base64').toString('ascii').split(':', 2)
|
2017-08-23 20:13:45 +00:00
|
|
|
if (!pieces || pieces.length !== 2) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('Authorization header has corrupted data')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clientId = pieces[0]
|
|
|
|
clientSecret = pieces[1]
|
|
|
|
console.debug('Client credentials parsed from basic auth header: ', clientId, clientSecret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.body.token) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('Token not provided in request body')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 18:30:21 +00:00
|
|
|
const token = await req.oauth2.model.accessToken.fetchByToken(req.body.token)
|
2017-08-23 20:13:45 +00:00
|
|
|
if (!token) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('Token does not exist')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 18:30:21 +00:00
|
|
|
const ttl = req.oauth2.model.accessToken.getTTL(token)
|
|
|
|
const resObj = {
|
2017-08-23 20:13:45 +00:00
|
|
|
token_type: 'bearer',
|
|
|
|
token: token.token,
|
|
|
|
expires_in: Math.floor(ttl / 1000)
|
|
|
|
}
|
|
|
|
|
2020-12-13 14:36:07 +00:00
|
|
|
dataResponse(req, res, resObj)
|
2017-08-23 20:13:45 +00:00
|
|
|
})
|