oauth-clients.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import * as express from 'express'
  2. import { OAuthClientLocal } from '../../../shared'
  3. import { logger } from '../../helpers/logger'
  4. import { CONFIG } from '../../initializers/config'
  5. import { asyncMiddleware } from '../../middlewares'
  6. import { OAuthClientModel } from '../../models/oauth/oauth-client'
  7. const oauthClientsRouter = express.Router()
  8. oauthClientsRouter.get('/local',
  9. asyncMiddleware(getLocalClient)
  10. )
  11. // Get the client credentials for the PeerTube front end
  12. async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
  13. const serverHostname = CONFIG.WEBSERVER.HOSTNAME
  14. const serverPort = CONFIG.WEBSERVER.PORT
  15. let headerHostShouldBe = serverHostname
  16. if (serverPort !== 80 && serverPort !== 443) {
  17. headerHostShouldBe += ':' + serverPort
  18. }
  19. // Don't make this check if this is a test instance
  20. if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
  21. logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
  22. return res.type('json').status(403).end()
  23. }
  24. const client = await OAuthClientModel.loadFirstClient()
  25. if (!client) throw new Error('No client available.')
  26. const json: OAuthClientLocal = {
  27. client_id: client.clientId,
  28. client_secret: client.clientSecret
  29. }
  30. return res.json(json)
  31. }
  32. // ---------------------------------------------------------------------------
  33. export {
  34. oauthClientsRouter
  35. }