oauth-clients.ts 1.6 KB

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