Server: move clients in its own file
This commit is contained in:
parent
9bd2662976
commit
6606150c49
4 changed files with 44 additions and 28 deletions
|
@ -8,7 +8,7 @@ import { User } from './user.model';
|
|||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private static BASE_CLIENT_URL = '/api/v1/users/client';
|
||||
private static BASE_CLIENT_URL = '/api/v1/clients/local';
|
||||
private static BASE_TOKEN_URL = '/api/v1/users/token';
|
||||
|
||||
loginChangedSource: Observable<AuthStatus>;
|
||||
|
|
40
server/controllers/api/v1/clients.js
Normal file
40
server/controllers/api/v1/clients.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
'use strict'
|
||||
|
||||
const config = require('config')
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const Client = mongoose.model('OAuthClient')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/local', getLocalClient)
|
||||
|
||||
// Get the client credentials for the PeerTube front end
|
||||
function getLocalClient (req, res, next) {
|
||||
const serverHost = config.get('webserver.host')
|
||||
const serverPort = config.get('webserver.port')
|
||||
let headerHostShouldBe = serverHost
|
||||
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) {
|
||||
return res.type('json').status(403).end()
|
||||
}
|
||||
|
||||
Client.loadFirstClient(function (err, client) {
|
||||
if (err) return next(err)
|
||||
if (!client) return next(new Error('No client available.'))
|
||||
|
||||
res.json({
|
||||
client_id: client._id,
|
||||
client_secret: client.clientSecret
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = router
|
|
@ -4,11 +4,13 @@ const express = require('express')
|
|||
|
||||
const router = express.Router()
|
||||
|
||||
const clientsController = require('./clients')
|
||||
const podsController = require('./pods')
|
||||
const remoteController = require('./remote')
|
||||
const usersController = require('./users')
|
||||
const videosController = require('./videos')
|
||||
|
||||
router.use('/clients', clientsController)
|
||||
router.use('/pods', podsController)
|
||||
router.use('/remote', remoteController)
|
||||
router.use('/users', usersController)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const each = require('async/each')
|
||||
const config = require('config')
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
@ -14,7 +13,6 @@ const admin = middlewares.admin
|
|||
const oAuth = middlewares.oauth
|
||||
const validatorsUsers = middlewares.validators.users
|
||||
|
||||
const Client = mongoose.model('OAuthClient')
|
||||
const User = mongoose.model('User')
|
||||
const Video = mongoose.model('Video')
|
||||
|
||||
|
@ -41,7 +39,7 @@ router.delete('/:username',
|
|||
validatorsUsers.usersRemove,
|
||||
removeUser
|
||||
)
|
||||
router.get('/client', getAngularClient)
|
||||
|
||||
router.post('/token', oAuth.token, success)
|
||||
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
||||
|
||||
|
@ -65,30 +63,6 @@ function createUser (req, res, next) {
|
|||
})
|
||||
}
|
||||
|
||||
function getAngularClient (req, res, next) {
|
||||
const serverHost = config.get('webserver.host')
|
||||
const serverPort = config.get('webserver.port')
|
||||
let headerHostShouldBe = serverHost
|
||||
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) {
|
||||
return res.type('json').status(403).end()
|
||||
}
|
||||
|
||||
Client.loadFirstClient(function (err, client) {
|
||||
if (err) return next(err)
|
||||
if (!client) return next(new Error('No client available.'))
|
||||
|
||||
res.json({
|
||||
client_id: client._id,
|
||||
client_secret: client.clientSecret
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function listUsers (req, res, next) {
|
||||
User.list(function (err, usersList) {
|
||||
if (err) return next(err)
|
||||
|
|
Loading…
Reference in a new issue