1
0
Fork 0
peertube/server/controllers/api/oauth-clients.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-06-05 19:53:49 +00:00
import * as express from 'express'
2016-08-05 14:09:39 +00:00
2017-06-10 20:15:25 +00:00
import { CONFIG } from '../../initializers'
2017-05-15 20:22:03 +00:00
import { logger } from '../../helpers'
2017-05-22 18:58:25 +00:00
import { database as db } from '../../initializers/database'
2017-06-25 15:44:19 +00:00
import { OAuthClientLocal } from '../../../shared'
2016-08-19 19:34:51 +00:00
2017-06-25 15:44:19 +00:00
const oauthClientsRouter = express.Router()
2016-08-05 14:09:39 +00:00
2017-06-25 15:44:19 +00:00
oauthClientsRouter.get('/local', getLocalClient)
2016-08-05 14:09:39 +00:00
// Get the client credentials for the PeerTube front end
2017-06-10 20:15:25 +00:00
function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-15 20:22:03 +00:00
const serverHostname = CONFIG.WEBSERVER.HOSTNAME
const serverPort = CONFIG.WEBSERVER.PORT
let headerHostShouldBe = serverHostname
2016-08-05 14:09:39 +00: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) {
logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
2016-08-05 14:09:39 +00:00
return res.type('json').status(403).end()
}
db.OAuthClient.loadFirstClient()
.then(client => {
if (!client) throw new Error('No client available.')
const json: OAuthClientLocal = {
client_id: client.clientId,
client_secret: client.clientSecret
}
res.json(json)
})
.catch(err => next(err))
2016-08-05 14:09:39 +00:00
}
// ---------------------------------------------------------------------------
2017-05-15 20:22:03 +00:00
export {
2017-06-25 15:44:19 +00:00
oauthClientsRouter
2017-05-15 20:22:03 +00:00
}