refactor channel checks with isApprovedChannel

This commit is contained in:
Travis Eden 2018-09-20 13:34:53 -04:00
parent 3c21ce36b7
commit 0a74e50167
7 changed files with 39 additions and 11 deletions

View file

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { NavLink, withRouter } from 'react-router-dom'; import { NavLink, withRouter } from 'react-router-dom';
import NavBarChannelOptionsDropdown from '@components/NavBarChannelOptionsDropdown'; import NavBarChannelOptionsDropdown from '@components/NavBarChannelOptionsDropdown';
import isApprovedChannel from '../../utils/isApprovedChannel';
const VIEW = 'VIEW'; const VIEW = 'VIEW';
const LOGOUT = 'LOGOUT'; const LOGOUT = 'LOGOUT';
@ -31,7 +32,7 @@ class NavigationLinks extends React.Component {
const { site, channelLongId, channelName } = this.props; const { site, channelLongId, channelName } = this.props;
return ( return (
<div className='navigation-links'> <div className='navigation-links'>
{(!site.publishOnlyApproved || site.approvedChannels.includes(channelLongId)) && <NavLink {(!site.publishOnlyApproved || isApprovedChannel({ longId: channelLongId }, site.approvedChannels)) && <NavLink
className='nav-bar-link link--nav' className='nav-bar-link link--nav'
activeClassName='link--nav-active' activeClassName='link--nav-active'
to='/' to='/'

View file

@ -0,0 +1,9 @@
function isApprovedChannel (channel, channels) {
const { name, shortId: short, longId: long } = channel;
return Boolean(
(long && channels.find(chan => chan.longId === long)) ||
(name && short && channels.find(chan => chan.name === name && chan.shortId === short))
);
}
module.exports = isApprovedChannel;

View file

@ -1,8 +1,7 @@
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const getChannelData = require('./getChannelData.js'); const getChannelData = require('./getChannelData.js');
const { publishing: { serveOnlyApproved, approvedChannels } } = require('@config/siteConfig'); const isApprovedChannel = require('../../../../utils/isApprovedChannel');
const { publishing: { serveOnlyApproved } } = require('@config/siteConfig');
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
/* /*
@ -15,12 +14,17 @@ const channelData = ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName; const channelName = params.channelName;
let channelClaimId = params.channelClaimId; let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null; if (channelClaimId === 'none') channelClaimId = null;
if (serveOnlyApproved && approvedChannels && !approvedChannels.includes(channelClaimId)) {
const chanObj = {};
if (channelName) chanObj.name = channelName;
if (channelClaimId) chanObj[(channelClaimId.length === 40 ? 'longId' : 'shortId')] = channelClaimId;
if (serveOnlyApproved && !isApprovedChannel(chanObj)) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'This spee.ch instance serves limited content which does not include this asset', message: 'This spee.ch instance serves limited content which does not include this asset',
}); });
} }
getChannelData(channelName, channelClaimId) getChannelData(channelName, channelClaimId)
.then(data => { .then(data => {
res.status(200).json({ res.status(200).json({

View file

@ -3,7 +3,8 @@ const logger = require('winston');
const { details: { host }, publishing: { disabled, disabledMessage } } = require('@config/siteConfig'); const { details: { host }, publishing: { disabled, disabledMessage } } = require('@config/siteConfig');
const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js'); const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js');
const { publishing: { publishOnlyApproved, approvedChannels } } = require('@config/siteConfig'); const isApprovedChannel = require('../../../../utils/isApprovedChannel');
const { publishing: { publishOnlyApproved } } = require('@config/siteConfig');
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
@ -56,7 +57,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
// check channel authorization // check channel authorization
authenticateUser(channelName, channelId, channelPassword, user) authenticateUser(channelName, channelId, channelPassword, user)
.then(({ channelName, channelClaimId }) => { .then(({ channelName, channelClaimId }) => {
if (publishOnlyApproved && approvedChannels && !approvedChannels.includes(channelClaimId)) { if (publishOnlyApproved && !isApprovedChannel({ longId: channelClaimId })) {
const error = { const error = {
name : UNAPPROVED_CHANNEL, name : UNAPPROVED_CHANNEL,
message: 'This spee.ch instance only allows publishing to approved channels', message: 'This spee.ch instance only allows publishing to approved channels',

View file

@ -1,6 +1,7 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../../../models'); const db = require('../../../models');
const isApprovedChannel = require('../../../utils/isApprovedChannel');
const getClaimId = require('../../utils/getClaimId.js'); const getClaimId = require('../../utils/getClaimId.js');
const { handleErrorResponse } = require('../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../utils/errorHandlers.js');
@ -13,7 +14,7 @@ const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
const NO_FILE = 'NO_FILE'; const NO_FILE = 'NO_FILE';
const UNAPPROVED_CHANNEL = 'UNAPPROVED_CHANNEL'; const UNAPPROVED_CHANNEL = 'UNAPPROVED_CHANNEL';
const { publishing: { serveOnlyApproved, approvedChannels } } = require('@config/siteConfig'); const { publishing: { serveOnlyApproved } } = require('@config/siteConfig');
const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => { const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => {
getClaimId(channelName, channelClaimId, claimName, claimId) getClaimId(channelName, channelClaimId, claimName, claimId)
@ -28,7 +29,7 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId
}); });
}) })
.then(claim => { .then(claim => {
if (serveOnlyApproved && !approvedChannels.includes(claim.dataValues.certificateId)) { if (serveOnlyApproved && !isApprovedChannel({ longId: claim.dataValues.certificateId })) {
throw new Error(UNAPPROVED_CHANNEL); throw new Error(UNAPPROVED_CHANNEL);
} }
logger.debug('Outpoint:', claim.dataValues.outpoint); logger.debug('Outpoint:', claim.dataValues.outpoint);

View file

@ -1,7 +1,8 @@
const logger = require('winston'); const logger = require('winston');
const returnShortId = require('./utils/returnShortId.js'); const returnShortId = require('./utils/returnShortId.js');
const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('@config/siteConfig'); const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('@config/siteConfig');
const { publishing: { serveOnlyApproved, approvedChannels } } = require('@config/siteConfig'); const { publishing: { serveOnlyApproved } } = require('@config/siteConfig');
const isApprovedChannel = require('../utils/isApprovedChannel');
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
const NOT_ALLOWED = 'NOT_ALLOWED'; const NOT_ALLOWED = 'NOT_ALLOWED';
@ -364,7 +365,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
where: { name, claimId }, where: { name, claimId },
}) })
.then(claimArray => { .then(claimArray => {
if (serveOnlyApproved && !approvedChannels.includes(claimArray[0].dataValues.certificateId)) { if (serveOnlyApproved && !isApprovedChannel({ longId: claimArray[0].dataValues.certificateId })) {
reject(NOT_ALLOWED); reject(NOT_ALLOWED);
} }
switch (claimArray.length) { switch (claimArray.length) {

View file

@ -0,0 +1,11 @@
const { publishing: { approvedChannels } } = require('@config/siteConfig');
function isApprovedChannel (channel, channels = approvedChannels) {
const { name, shortId: short, longId: long } = channel;
return Boolean(
(long && channels.find(chan => chan.longId === long)) ||
(name && short && channels.find(chan => chan.name === name && chan.shortId === short))
);
}
module.exports = isApprovedChannel;