From 060af3a95717a6fa6e2d4ebc76b86b5afc839e20 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 27 Apr 2018 14:29:00 -0700 Subject: [PATCH] organized auth, fallback, and page controllers; --- server/auth/authentication.js | 74 ------------------ .../api/claim/publish/authentication.js | 75 +++++++++++++++++++ server/controllers/api/claim/publish/index.js | 8 +- .../assets/serveByIdentifierAndClaim/index.js | 5 +- ...Compatibility.js => flipClaimNameAndId.js} | 6 +- .../auth/{login.js => login/index.js} | 2 +- .../auth/{logout.js => logout/index.js} | 0 .../auth/{signup.js => signup/index.js} | 0 .../auth/{user.js => user/index.js} | 0 server/controllers/fallback/sendReactApp.js | 7 -- .../controllers/{pages => utils}/redirect.js | 0 server/routes/fallback/index.js | 2 +- server/routes/pages/index.js | 2 +- 13 files changed, 90 insertions(+), 91 deletions(-) delete mode 100644 server/auth/authentication.js create mode 100644 server/controllers/api/claim/publish/authentication.js rename server/controllers/assets/utils/{flipClaimNameAndIdForBackwardsCompatibility.js => flipClaimNameAndId.js} (79%) rename server/controllers/auth/{login.js => login/index.js} (90%) rename server/controllers/auth/{logout.js => logout/index.js} (100%) rename server/controllers/auth/{signup.js => signup/index.js} (100%) rename server/controllers/auth/{user.js => user/index.js} (100%) delete mode 100644 server/controllers/fallback/sendReactApp.js rename server/controllers/{pages => utils}/redirect.js (100%) diff --git a/server/auth/authentication.js b/server/auth/authentication.js deleted file mode 100644 index accff721..00000000 --- a/server/auth/authentication.js +++ /dev/null @@ -1,74 +0,0 @@ -const logger = require('winston'); -const db = require('../models'); - -module.exports = { - authenticateUser (channelName, channelId, channelPassword, user) { - // case: no channelName or channel Id are provided (anonymous), regardless of whether user token is provided - if (!channelName && !channelId) { - return { - channelName : null, - channelClaimId: null, - }; - } - // case: channelName or channel Id are provided with user token - if (user) { - if (channelName && channelName !== user.channelName) { - throw new Error('the provided channel name does not match user credentials'); - } - if (channelId && channelId !== user.channelClaimId) { - throw new Error('the provided channel id does not match user credentials'); - } - return { - channelName : user.channelName, - channelClaimId: user.channelClaimId, - }; - } - // case: channelName or channel Id are provided with password instead of user token - if (!channelPassword) throw new Error('no channel password provided'); - return module.exports.authenticateChannelCredentials(channelName, channelId, channelPassword); - }, - authenticateChannelCredentials (channelName, channelId, userPassword) { - return new Promise((resolve, reject) => { - // hoisted variables - let channelData; - // build the params for finding the channel - let channelFindParams = {}; - if (channelName) channelFindParams['channelName'] = channelName; - if (channelId) channelFindParams['channelClaimId'] = channelId; - // find the channel - db.Channel - .findOne({ - where: channelFindParams, - }) - .then(channel => { - if (!channel) { - logger.debug('no channel found'); - throw new Error('Authentication failed, you do not have access to that channel'); - } - channelData = channel.get(); - logger.debug('channel data:', channelData); - return db.User.findOne({ - where: { userName: channelData.channelName.substring(1) }, - }); - }) - .then(user => { - if (!user) { - logger.debug('no user found'); - throw new Error('Authentication failed, you do not have access to that channel'); - } - return user.comparePassword(userPassword); - }) - .then(isMatch => { - if (!isMatch) { - logger.debug('incorrect password'); - throw new Error('Authentication failed, you do not have access to that channel'); - } - logger.debug('...password was a match...'); - resolve(channelData); - }) - .catch(error => { - reject(error); - }); - }); - }, -}; diff --git a/server/controllers/api/claim/publish/authentication.js b/server/controllers/api/claim/publish/authentication.js new file mode 100644 index 00000000..d41902ef --- /dev/null +++ b/server/controllers/api/claim/publish/authentication.js @@ -0,0 +1,75 @@ +const logger = require('winston'); +const db = require('../../../../models/index'); + +const authenticateChannelCredentials = (channelName, channelId, userPassword) => { + return new Promise((resolve, reject) => { + // hoisted variables + let channelData; + // build the params for finding the channel + let channelFindParams = {}; + if (channelName) channelFindParams['channelName'] = channelName; + if (channelId) channelFindParams['channelClaimId'] = channelId; + // find the channel + db.Channel + .findOne({ + where: channelFindParams, + }) + .then(channel => { + if (!channel) { + logger.debug('no channel found'); + throw new Error('Authentication failed, you do not have access to that channel'); + } + channelData = channel.get(); + logger.debug('channel data:', channelData); + return db.User.findOne({ + where: { userName: channelData.channelName.substring(1) }, + }); + }) + .then(user => { + if (!user) { + logger.debug('no user found'); + throw new Error('Authentication failed, you do not have access to that channel'); + } + return user.comparePassword(userPassword); + }) + .then(isMatch => { + if (!isMatch) { + logger.debug('incorrect password'); + throw new Error('Authentication failed, you do not have access to that channel'); + } + logger.debug('...password was a match...'); + resolve(channelData); + }) + .catch(error => { + reject(error); + }); + }); +}; + +const authenticateUser = (channelName, channelId, channelPassword, user) => { + // case: no channelName or channel Id are provided (anonymous), regardless of whether user token is provided + if (!channelName && !channelId) { + return { + channelName : null, + channelClaimId: null, + }; + } + // case: channelName or channel Id are provided with user token + if (user) { + if (channelName && channelName !== user.channelName) { + throw new Error('the provided channel name does not match user credentials'); + } + if (channelId && channelId !== user.channelClaimId) { + throw new Error('the provided channel id does not match user credentials'); + } + return { + channelName : user.channelName, + channelClaimId: user.channelClaimId, + }; + } + // case: channelName or channel Id are provided with password instead of user token + if (!channelPassword) throw new Error('no channel password provided'); + return authenticateChannelCredentials(channelName, channelId, channelPassword); +}; + +module.exports = authenticateUser; diff --git a/server/controllers/api/claim/publish/index.js b/server/controllers/api/claim/publish/index.js index 2d4ee10d..1e95d317 100644 --- a/server/controllers/api/claim/publish/index.js +++ b/server/controllers/api/claim/publish/index.js @@ -1,13 +1,17 @@ const { details: { host } } = require('../../../../../config/siteConfig.js'); -const { authenticateUser } = require('../../../../auth/authentication.js'); + const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js'); + const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); + +const checkClaimAvailability = require('../utils/checkClaimAvailability.js'); + const publish = require('./publish.js'); const createBasicPublishParams = require('./createBasicPublishParams.js'); const createThumbnailPublishParams = require('./createThumbnailPublishParams.js'); const parsePublishApiRequestBody = require('./parsePublishApiRequestBody.js'); const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js'); -const checkClaimAvailability = require('../utils/checkClaimAvailability.js'); +const authenticateUser = require('./authentication.js'); /* diff --git a/server/controllers/assets/serveByIdentifierAndClaim/index.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js index 2fbdb7bb..e493e01e 100644 --- a/server/controllers/assets/serveByIdentifierAndClaim/index.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -5,7 +5,7 @@ const lbryUri = require('../utils/lbryUri.js'); const determineResponseType = require('../utils/determineResponseType.js'); const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js'); -const flipClaimNameAndIdForBackwardsCompatibility = require('../utils/flipClaimNameAndIdForBackwardsCompatibility.js'); +const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js'); const logRequestData = require('../utils/logRequestData.js'); const SERVE = 'SERVE'; @@ -46,8 +46,9 @@ const serverAssetByIdentifierAndClaim = (req, res) => { } catch (error) { return res.status(400).json({success: false, message: error.message}); } + // for backwards compatability, flip claim name and claim id if necessary if (!isChannel) { - [claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName); + [claimId, claimName] = flipClaimNameAndId(claimId, claimName); } // log the request data for debugging logRequestData(responseType, claimName, channelName, claimId); diff --git a/server/controllers/assets/utils/flipClaimNameAndIdForBackwardsCompatibility.js b/server/controllers/assets/utils/flipClaimNameAndId.js similarity index 79% rename from server/controllers/assets/utils/flipClaimNameAndIdForBackwardsCompatibility.js rename to server/controllers/assets/utils/flipClaimNameAndId.js index 47ebab32..c41ebd02 100644 --- a/server/controllers/assets/utils/flipClaimNameAndIdForBackwardsCompatibility.js +++ b/server/controllers/assets/utils/flipClaimNameAndId.js @@ -10,8 +10,8 @@ function isValidShortIdOrClaimId (input) { return (isValidClaimId(input) || isValidShortId(input)); }; -const flipClaimNameAndIdForBackwardsCompatibility = (identifier, name) => { - // this is a patch for backwards compatability with '/name/claim_id' url format +const flipClaimNameAndId = (identifier, name) => { + // this is a patch for backwards compatability with '/name/claimId' url format if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) { const tempName = name; name = identifier; @@ -20,4 +20,4 @@ const flipClaimNameAndIdForBackwardsCompatibility = (identifier, name) => { return [identifier, name]; }; -module.exports = flipClaimNameAndIdForBackwardsCompatibility; +module.exports = flipClaimNameAndId; diff --git a/server/controllers/auth/login.js b/server/controllers/auth/login/index.js similarity index 90% rename from server/controllers/auth/login.js rename to server/controllers/auth/login/index.js index 229b4e98..e88de217 100644 --- a/server/controllers/auth/login.js +++ b/server/controllers/auth/login/index.js @@ -1,4 +1,4 @@ -const speechPassport = require('../../speechPassport/index'); +const speechPassport = require('../../../speechPassport/index'); const login = (req, res, next) => { speechPassport.authenticate('local-login', (err, user, info) => { diff --git a/server/controllers/auth/logout.js b/server/controllers/auth/logout/index.js similarity index 100% rename from server/controllers/auth/logout.js rename to server/controllers/auth/logout/index.js diff --git a/server/controllers/auth/signup.js b/server/controllers/auth/signup/index.js similarity index 100% rename from server/controllers/auth/signup.js rename to server/controllers/auth/signup/index.js diff --git a/server/controllers/auth/user.js b/server/controllers/auth/user/index.js similarity index 100% rename from server/controllers/auth/user.js rename to server/controllers/auth/user/index.js diff --git a/server/controllers/fallback/sendReactApp.js b/server/controllers/fallback/sendReactApp.js deleted file mode 100644 index e291fd0a..00000000 --- a/server/controllers/fallback/sendReactApp.js +++ /dev/null @@ -1,7 +0,0 @@ -const handlePageRender = require('../../render/build/handlePageRender.js'); - -const sendReactApp = (req, res) => { - handlePageRender(req, res); -}; - -module.exports = sendReactApp; diff --git a/server/controllers/pages/redirect.js b/server/controllers/utils/redirect.js similarity index 100% rename from server/controllers/pages/redirect.js rename to server/controllers/utils/redirect.js diff --git a/server/routes/fallback/index.js b/server/routes/fallback/index.js index 0fc57d0c..cc453b81 100644 --- a/server/routes/fallback/index.js +++ b/server/routes/fallback/index.js @@ -1,4 +1,4 @@ -const handlePageRequest = require('../../controllers/fallback/sendReactApp'); +const handlePageRequest = require('../../controllers/pages/sendReactApp'); module.exports = (app) => { app.get('*', handlePageRequest); diff --git a/server/routes/pages/index.js b/server/routes/pages/index.js index a7c3a936..fc825225 100644 --- a/server/routes/pages/index.js +++ b/server/routes/pages/index.js @@ -1,6 +1,6 @@ const handlePageRequest = require('../../controllers/pages/sendReactApp'); const handleEmbedRequest = require('../../controllers/pages/sendEmbedPage'); -const redirect = require('../../controllers/pages/redirect'); +const redirect = require('../../controllers/utils/redirect'); module.exports = (app) => { app.get('/', handlePageRequest);