fixed story retrieval

This commit is contained in:
notnull 2019-07-13 17:40:34 -04:00
parent 521512e9a3
commit 0615286d33
8 changed files with 29 additions and 32 deletions

View File

@ -15,7 +15,7 @@ router.get('/', async (req, res, next) => {
router.get('/:hash', async (req, res, next) => { router.get('/:hash', async (req, res, next) => {
try { try {
const hash = await Blockchain.findOne({ const hash = await Blockchain.findOne({
where: { hashedStory: req.params.hashedStory }, where: { hashedStory: req.params.hash },
}) })
res.status(201).json(hash) res.status(201).json(hash)

View File

@ -12,11 +12,11 @@ router.post('/', async (req, res, next) => {
} }
}) })
router.post('/read', async (req, res, next) => { router.get('/:hash', async (req, res, next) => {
try { try {
const story = await Story.find({ const story = await Story.findOne({
where: { where: {
hash: req.body.hash, hashedStory: req.params.hash,
}, },
}) })

View File

@ -32,6 +32,7 @@ class App extends React.Component {
this.state = initialState this.state = initialState
this.navigate = this.navigate.bind(this) this.navigate = this.navigate.bind(this)
this.submitStory = this.submitStory.bind(this) this.submitStory = this.submitStory.bind(this)
this.fetchStory = this.fetchStory.bind(this)
} }
componentWillMount() { componentWillMount() {
@ -75,17 +76,16 @@ class App extends React.Component {
} }
} }
async fetchStory(encryptionKey, hash) { async fetchStory(submittedData) {
const { encryptionKey, hash } = submittedData
try { try {
const retrievedStory = await axios.get(`/api/stories/${hash}`) const { data } = await axios.get(API + `/api/stories/${hash}`)
console.log(data)
const decryptedStory = decryptString( const decryptedStory = decryptString(data.encryptedStory, encryptionKey)
retrievedStory.encryptedStory, this.setState({ decryptedStory })
encryptionKey
)
return this.setState({ decryptedStory })
} catch (e) { } catch (e) {
return this.setState({ retrievalError: 'Story not found' }) console.log(e)
this.setState({ retrievalError: 'Story not found.' })
} }
} }

View File

@ -17,7 +17,6 @@ class StoryForm extends React.Component {
} }
handleChange(e) { handleChange(e) {
console.log(this.state)
this.setState({ [e.target.name]: e.target.value }) this.setState({ [e.target.name]: e.target.value })
} }

View File

@ -4,7 +4,7 @@ const ReadStoryView = props => {
return ( return (
<div> <div>
<h3> {props.eventDate}</h3> <h3> {props.eventDate}</h3>
<p>{props.eventDetails}</p> <p>{props.decryptedStory}</p>
</div> </div>
) )
} }

View File

@ -3,7 +3,7 @@ import React from 'react'
//----------------------------------------------// //----------------------------------------------//
const initialState = { const initialState = {
password: '', encryptionKey: '',
hash: '', hash: '',
} }
@ -21,15 +21,7 @@ class RetrieveStoryForm extends React.Component {
handleSubmit(e) { handleSubmit(e) {
e.preventDefault() e.preventDefault()
try { this.props.fetchStory({ ...this.state })
const encryptedStory = this.props.fetchStory(
this.state.password,
this.state.hash
)
this.props.navigate('read')
} catch (e) {
console.log(e)
}
} }
render() { render() {
return ( return (
@ -38,18 +30,18 @@ class RetrieveStoryForm extends React.Component {
<p>Enter your secret key and your hash below.</p> <p>Enter your secret key and your hash below.</p>
<form onSubmit={this.handleSubmit}> <form onSubmit={this.handleSubmit}>
<div className="form-group row"> <div className="form-group row">
<label htmlFor="password" className="col-sm-3 col-form-label"> <label htmlFor="encryptionKey" className="col-sm-3 col-form-label">
Password Encryption Key:
</label> </label>
<div className="col-sm-7"> <div className="col-sm-7">
<input <input
type="text" type="text"
className="form-control" className="form-control"
id="password" id="encryptionKey"
name="password" name="encryptionKey"
placeholder="Password" placeholder="Encryption Key"
onChange={this.handleChange} onChange={this.handleChange}
value={this.state.password} value={this.state.encryptionKey}
/> />
</div> </div>
</div> </div>
@ -77,6 +69,11 @@ class RetrieveStoryForm extends React.Component {
</div> </div>
</div> </div>
</form> </form>
{this.props.retrievalError ? (
<h5>{this.props.retrievalError}</h5>
) : (
<div />
)}
</div> </div>
) )
} }

View File

@ -3,7 +3,7 @@ import ReadStoryView from './ReadStoryView'
import RetrieveStoryForm from './RetrieveStoryForm' import RetrieveStoryForm from './RetrieveStoryForm'
const ReadStory = props => { const ReadStory = props => {
if (props.retrievedStory) return <ReadStoryView {...props} /> if (props.decryptedStory) return <ReadStoryView {...props} />
return <RetrieveStoryForm {...props} /> return <RetrieveStoryForm {...props} />
} }

View File

@ -1,3 +1,4 @@
const crypto = require('crypto')
const ALGORITHM_NAME = 'aes-128-gcm' const ALGORITHM_NAME = 'aes-128-gcm'
const ALGORITHM_NONCE_SIZE = 12 const ALGORITHM_NONCE_SIZE = 12
const ALGORITHM_TAG_SIZE = 16 const ALGORITHM_TAG_SIZE = 16