Server: move clients in its own file
This commit is contained in:
parent
9bd2662976
commit
6606150c49
|
@ -8,7 +8,7 @@ import { User } from './user.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
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';
|
private static BASE_TOKEN_URL = '/api/v1/users/token';
|
||||||
|
|
||||||
loginChangedSource: Observable<AuthStatus>;
|
loginChangedSource: Observable<AuthStatus>;
|
||||||
|
|
|
@ -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 router = express.Router()
|
||||||
|
|
||||||
|
const clientsController = require('./clients')
|
||||||
const podsController = require('./pods')
|
const podsController = require('./pods')
|
||||||
const remoteController = require('./remote')
|
const remoteController = require('./remote')
|
||||||
const usersController = require('./users')
|
const usersController = require('./users')
|
||||||
const videosController = require('./videos')
|
const videosController = require('./videos')
|
||||||
|
|
||||||
|
router.use('/clients', clientsController)
|
||||||
router.use('/pods', podsController)
|
router.use('/pods', podsController)
|
||||||
router.use('/remote', remoteController)
|
router.use('/remote', remoteController)
|
||||||
router.use('/users', usersController)
|
router.use('/users', usersController)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const each = require('async/each')
|
const each = require('async/each')
|
||||||
const config = require('config')
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
const waterfall = require('async/waterfall')
|
const waterfall = require('async/waterfall')
|
||||||
|
@ -14,7 +13,6 @@ const admin = middlewares.admin
|
||||||
const oAuth = middlewares.oauth
|
const oAuth = middlewares.oauth
|
||||||
const validatorsUsers = middlewares.validators.users
|
const validatorsUsers = middlewares.validators.users
|
||||||
|
|
||||||
const Client = mongoose.model('OAuthClient')
|
|
||||||
const User = mongoose.model('User')
|
const User = mongoose.model('User')
|
||||||
const Video = mongoose.model('Video')
|
const Video = mongoose.model('Video')
|
||||||
|
|
||||||
|
@ -41,7 +39,7 @@ router.delete('/:username',
|
||||||
validatorsUsers.usersRemove,
|
validatorsUsers.usersRemove,
|
||||||
removeUser
|
removeUser
|
||||||
)
|
)
|
||||||
router.get('/client', getAngularClient)
|
|
||||||
router.post('/token', oAuth.token, success)
|
router.post('/token', oAuth.token, success)
|
||||||
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
// 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) {
|
function listUsers (req, res, next) {
|
||||||
User.list(function (err, usersList) {
|
User.list(function (err, usersList) {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
|
|
Loading…
Reference in New Issue