2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { OAuthClientLocal } from '../../../shared'
|
2021-07-16 04:42:24 -04:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2021-06-04 03:16:23 -04:00
|
|
|
import { asyncMiddleware, openapiOperationDoc } from '../../middlewares'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { OAuthClientModel } from '../../models/oauth/oauth-client'
|
2016-08-19 15:34:51 -04:00
|
|
|
|
2017-06-25 11:44:19 -04:00
|
|
|
const oauthClientsRouter = express.Router()
|
2016-08-05 10:09:39 -04:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
oauthClientsRouter.get('/local',
|
2021-06-04 03:16:23 -04:00
|
|
|
openapiOperationDoc({ operationId: 'getOAuthClient' }),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(getLocalClient)
|
|
|
|
)
|
2016-08-05 10:09:39 -04:00
|
|
|
|
|
|
|
// Get the client credentials for the PeerTube front end
|
2017-10-25 05:55:06 -04:00
|
|
|
async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const serverHostname = CONFIG.WEBSERVER.HOSTNAME
|
|
|
|
const serverPort = CONFIG.WEBSERVER.PORT
|
2016-10-23 13:41:17 -04:00
|
|
|
let headerHostShouldBe = serverHostname
|
2016-08-05 10:09:39 -04:00
|
|
|
if (serverPort !== 80 && serverPort !== 443) {
|
|
|
|
headerHostShouldBe += ':' + serverPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't make this check if this is a test instance
|
|
|
|
if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
|
2016-11-01 14:14:33 -04:00
|
|
|
logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: `Getting client tokens for host ${req.get('host')} is forbidden`
|
|
|
|
})
|
2016-08-05 10:09:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const client = await OAuthClientModel.loadFirstClient()
|
2017-10-25 05:55:06 -04:00
|
|
|
if (!client) throw new Error('No client available.')
|
|
|
|
|
|
|
|
const json: OAuthClientLocal = {
|
|
|
|
client_id: client.clientId,
|
|
|
|
client_secret: client.clientSecret
|
|
|
|
}
|
|
|
|
return res.json(json)
|
2016-08-05 10:09:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
2017-06-25 11:44:19 -04:00
|
|
|
oauthClientsRouter
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|