ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth-clients.ts 1.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import express from 'express'
  2. import { HttpStatusCode, OAuthClientLocal } from '@peertube/peertube-models'
  3. import { isTestOrDevInstance } from '@peertube/peertube-node-utils'
  4. import { OAuthClientModel } from '@server/models/oauth/oauth-client.js'
  5. import { logger } from '../../helpers/logger.js'
  6. import { CONFIG } from '../../initializers/config.js'
  7. import { apiRateLimiter, asyncMiddleware, openapiOperationDoc } from '../../middlewares/index.js'
  8. const oauthClientsRouter = express.Router()
  9. oauthClientsRouter.use(apiRateLimiter)
  10. oauthClientsRouter.get('/local',
  11. openapiOperationDoc({ operationId: 'getOAuthClient' }),
  12. asyncMiddleware(getLocalClient)
  13. )
  14. // Get the client credentials for the PeerTube front end
  15. async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
  16. const serverHostname = CONFIG.WEBSERVER.HOSTNAME
  17. const serverPort = CONFIG.WEBSERVER.PORT
  18. let headerHostShouldBe = serverHostname
  19. if (serverPort !== 80 && serverPort !== 443) {
  20. headerHostShouldBe += ':' + serverPort
  21. }
  22. // Don't make this check if this is a test instance
  23. if (!isTestOrDevInstance() && req.get('host') !== headerHostShouldBe) {
  24. logger.info(
  25. 'Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe,
  26. { webserverConfig: CONFIG.WEBSERVER }
  27. )
  28. return res.fail({
  29. status: HttpStatusCode.FORBIDDEN_403,
  30. message: `Getting client tokens for host ${req.get('host')} is forbidden`
  31. })
  32. }
  33. const client = await OAuthClientModel.loadFirstClient()
  34. if (!client) throw new Error('No client available.')
  35. const json: OAuthClientLocal = {
  36. client_id: client.clientId,
  37. client_secret: client.clientSecret
  38. }
  39. return res.json(json)
  40. }
  41. // ---------------------------------------------------------------------------
  42. export {
  43. oauthClientsRouter
  44. }