2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2022-07-06 08:44:14 -05:00
|
|
|
import { isTestOrDevInstance } from '@server/helpers/core-utils'
|
2021-12-24 03:14:47 -06:00
|
|
|
import { OAuthClientModel } from '@server/models/oauth/oauth-client'
|
|
|
|
import { HttpStatusCode, OAuthClientLocal } from '@shared/models'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2023-06-20 07:17:34 -05:00
|
|
|
import { apiRateLimiter, asyncMiddleware, openapiOperationDoc } from '../../middlewares'
|
2016-08-19 14:34:51 -05:00
|
|
|
|
2017-06-25 10:44:19 -05:00
|
|
|
const oauthClientsRouter = express.Router()
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2023-06-20 07:17:34 -05:00
|
|
|
oauthClientsRouter.use(apiRateLimiter)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
oauthClientsRouter.get('/local',
|
2021-06-04 02:16:23 -05:00
|
|
|
openapiOperationDoc({ operationId: 'getOAuthClient' }),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(getLocalClient)
|
|
|
|
)
|
2016-08-05 09:09:39 -05:00
|
|
|
|
|
|
|
// Get the client credentials for the PeerTube front end
|
2017-10-25 04:55:06 -05:00
|
|
|
async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const serverHostname = CONFIG.WEBSERVER.HOSTNAME
|
|
|
|
const serverPort = CONFIG.WEBSERVER.PORT
|
2016-10-23 12:41:17 -05:00
|
|
|
let headerHostShouldBe = serverHostname
|
2016-08-05 09:09:39 -05:00
|
|
|
if (serverPort !== 80 && serverPort !== 443) {
|
|
|
|
headerHostShouldBe += ':' + serverPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't make this check if this is a test instance
|
2022-07-06 08:44:14 -05:00
|
|
|
if (!isTestOrDevInstance() && req.get('host') !== headerHostShouldBe) {
|
2023-07-26 02:25:10 -05:00
|
|
|
logger.info(
|
|
|
|
'Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe,
|
|
|
|
{ webserverConfig: CONFIG.WEBSERVER }
|
|
|
|
)
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: `Getting client tokens for host ${req.get('host')} is forbidden`
|
|
|
|
})
|
2016-08-05 09:09:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const client = await OAuthClientModel.loadFirstClient()
|
2017-10-25 04:55:06 -05: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 09:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
2017-06-25 10:44:19 -05:00
|
|
|
oauthClientsRouter
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|