From f49ca9f89a823af0aafac83d5a6ff1a26e6e6a7d Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 19 Dec 2016 18:11:11 -0800 Subject: [PATCH] PAss the token around. --- app.js | 10 ++++++++-- client/coral-embed-stream/src/CommentStream.js | 4 ++-- client/coral-framework/actions/auth.js | 14 ++++++++++---- client/coral-framework/constants/auth.js | 1 + client/coral-framework/reducers/auth.js | 6 +++++- client/coral-plugin-csrf/FormCSRF.js | 11 ----------- client/coral-plugin-csrf/FormCSRFField.js | 7 +++++++ client/coral-sign-in/components/ForgotContent.js | 6 +++++- client/coral-sign-in/components/SignDialog.js | 2 +- client/coral-sign-in/components/SignInContent.js | 4 ++++ client/coral-sign-in/components/SignUpContent.js | 4 ++++ .../coral-sign-in/containers/SignInContainer.js | 7 +++++-- routes/api/auth/index.js | 16 +++++++--------- routes/index.js | 2 +- tests/routes/api/auth/index.js | 3 --- 15 files changed, 60 insertions(+), 37 deletions(-) delete mode 100644 client/coral-plugin-csrf/FormCSRF.js create mode 100644 client/coral-plugin-csrf/FormCSRFField.js diff --git a/app.js b/app.js index c3fa5d8d6..f64880e64 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,7 @@ const passport = require('./services/passport'); const session = require('express-session'); const RedisStore = require('connect-redis')(session); const redis = require('./services/redis'); -const csrf = require('csurf'); +const cookieParser = require('cookie-parser'); const app = express(); @@ -70,9 +70,15 @@ app.use(session(session_opts)); // CSRF MIDDLEWARE //============================================================================== -app.use(csrf()); +// Use CSRF only if we are not testing. +if (app.get('env') !== 'test') { + console.log('DEBUGT NO TEST'); + app.use(cookieParser()); +} app.use((err, req, res, next) => { + console.log('DEBUG EN SERVER', req.csrfToken()); + res.locals._csrf = req.csrfToken(); return next(); }); diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index 7d1e6c612..4c9cd6f08 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -96,7 +96,7 @@ class CommentStream extends Component { const rootItem = this.props.items.assets && this.props.items.assets[rootItemId]; const {actions, users, comments} = this.props.items; const {status, moderation, closedMessage, charCount, charCountEnable} = this.props.config; - const {loggedIn, isAdmin, user, showSignInDialog, signInOffset} = this.props.auth; + const {loggedIn, isAdmin, user, showSignInDialog, signInOffset, csrfToken} = this.props.auth; const {activeTab} = this.state; const banned = (this.props.userData.status === 'banned'); @@ -138,7 +138,7 @@ class CommentStream extends Component { :

{closedMessage}

} - {!loggedIn && } + {!loggedIn && } { rootItem.comments && rootItem.comments.map((commentId) => { const comment = comments[commentId]; diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index c5390607c..27f432c37 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -24,6 +24,7 @@ const signInSuccess = (user, isAdmin) => ({type: actions.FETCH_SIGNIN_SUCCESS, u const signInFailure = error => ({type: actions.FETCH_SIGNIN_FAILURE, error}); export const fetchSignIn = (formData) => dispatch => { + console.log('DEBUG FORMDATA', formData); dispatch(signInRequest()); coralApi('/auth/local', {method: 'POST', body: formData}) .then(({user}) => { @@ -120,17 +121,22 @@ export const invalidForm = error => ({type: actions.INVALID_FORM, error}); const checkLoginRequest = () => ({type: actions.CHECK_LOGIN_REQUEST}); const checkLoginSuccess = (user, isAdmin) => ({type: actions.CHECK_LOGIN_SUCCESS, user, isAdmin}); const checkLoginFailure = error => ({type: actions.CHECK_LOGIN_FAILURE, error}); +const checkCSRFToken = (csrfToken) => ({type: actions.CHECK_CSRF_TOKEN, csrfToken}); export const checkLogin = () => dispatch => { dispatch(checkLoginRequest()); coralApi('/auth') - .then(user => { - if (!user) { + .then((result) => { + if (result.csrfToken !== null) { + dispatch(checkCSRFToken(result.csrfToken)); + } + + if (!result.user) { throw new Error('Not logged in'); } - const isAdmin = !!user.roles.filter(i => i === 'admin').length; - dispatch(checkLoginSuccess(user, isAdmin)); + const isAdmin = !!result.user.roles.filter(i => i === 'admin').length; + dispatch(checkLoginSuccess(result.user, isAdmin)); }) .catch(error => dispatch(checkLoginFailure(error))); }; diff --git a/client/coral-framework/constants/auth.js b/client/coral-framework/constants/auth.js index 07ca2e661..5742adf75 100644 --- a/client/coral-framework/constants/auth.js +++ b/client/coral-framework/constants/auth.js @@ -31,3 +31,4 @@ export const CHECK_LOGIN_REQUEST = 'CHECK_LOGIN_REQUEST'; export const CHECK_LOGIN_SUCCESS = 'CHECK_LOGIN_SUCCESS'; export const CHECK_LOGIN_FAILURE = 'CHECK_LOGIN_FAILURE'; +export const CHECK_CSRF_TOKEN = 'CHECK_CSRF_TOKEN'; diff --git a/client/coral-framework/reducers/auth.js b/client/coral-framework/reducers/auth.js index d32956b84..fc00bc68c 100644 --- a/client/coral-framework/reducers/auth.js +++ b/client/coral-framework/reducers/auth.js @@ -11,7 +11,8 @@ const initialState = Map({ error: '', passwordRequestSuccess: null, passwordRequestFailure: null, - successSignUp: false + successSignUp: false, + csrfToken: '' }); const purge = user => { @@ -41,6 +42,9 @@ export default function auth (state = initialState, action) { .set('view', action.view); case actions.CLEAN_STATE: return initialState; + case actions.CHECK_CSRF_TOKEN: + return state + .set('csrfToken', action.csrfToken); case actions.FETCH_SIGNIN_REQUEST: return state .set('isLoading', true); diff --git a/client/coral-plugin-csrf/FormCSRF.js b/client/coral-plugin-csrf/FormCSRF.js deleted file mode 100644 index 7f555d986..000000000 --- a/client/coral-plugin-csrf/FormCSRF.js +++ /dev/null @@ -1,11 +0,0 @@ -import React from 'react'; - -export const FormCSRFInput = React.createClass({ - render() { - const {csrfToken} = this.props; - - return ( - - ); - } -}); diff --git a/client/coral-plugin-csrf/FormCSRFField.js b/client/coral-plugin-csrf/FormCSRFField.js new file mode 100644 index 000000000..65432efb1 --- /dev/null +++ b/client/coral-plugin-csrf/FormCSRFField.js @@ -0,0 +1,7 @@ +import React from 'react'; + +const FormCSRFField = ({...props}) => ( + +); + +export default FormCSRFField; diff --git a/client/coral-sign-in/components/ForgotContent.js b/client/coral-sign-in/components/ForgotContent.js index f76ebe45d..d17768479 100644 --- a/client/coral-sign-in/components/ForgotContent.js +++ b/client/coral-sign-in/components/ForgotContent.js @@ -1,6 +1,7 @@ import React from 'react'; import styles from './styles.css'; import Button from 'coral-ui/components/Button'; +import FormCSRFField from 'coral-plugin-csrf/FormCSRFField'; import I18n from 'coral-framework/modules/i18n/i18n'; import translations from '../translations'; const lang = new I18n(translations); @@ -17,7 +18,7 @@ class ForgotContent extends React.Component { } render () { - const {changeView, auth} = this.props; + const {changeView, auth, csrfToken} = this.props; const {passwordRequestSuccess, passwordRequestFailure} = auth; return ( @@ -26,6 +27,9 @@ class ForgotContent extends React.Component {

{lang.t('signIn.recoverPassword')}

+
( open={open} style={{ position: 'relative', - top: offset !== 0 && offset + top: offset !== 0 && offset }}> × {view === 'SIGNIN' && } diff --git a/client/coral-sign-in/components/SignInContent.js b/client/coral-sign-in/components/SignInContent.js index de5e77a49..b4ba1b2f9 100644 --- a/client/coral-sign-in/components/SignInContent.js +++ b/client/coral-sign-in/components/SignInContent.js @@ -1,6 +1,7 @@ import React from 'react'; import Button from 'coral-ui/components/Button'; import FormField from './FormField'; +import FormCSRFField from 'coral-plugin-csrf/FormCSRFField'; import Alert from './Alert'; import Spinner from 'coral-ui/components/Spinner'; import styles from './styles.css'; @@ -27,6 +28,9 @@ const SignInContent = ({handleChange, formData, ...props}) => (
{ props.auth.error && {props.auth.error} } + ( { props.auth.error && {props.auth.error} } + {!noButton &&