Skip to content

Instantly share code, notes, and snippets.

@ohmpzz
Created April 29, 2018 16:02
Show Gist options
  • Save ohmpzz/390658db710e4b9c7cfc3eb7e0f5a3d5 to your computer and use it in GitHub Desktop.
Save ohmpzz/390658db710e4b9c7cfc3eb7e0f5a3d5 to your computer and use it in GitHub Desktop.

Revisions

  1. ohmpzz created this gist Apr 29, 2018.
    40 changes: 40 additions & 0 deletions secure-demo-1.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const admin = require('firebase-admin');
    const CryptoJS = require("crypto-js")
    const firebaseConfig = require('./config/firebase')
    const express = require('express')
    const cors = require('cors')
    const bodyParser = require('body-parser')

    const app = express()

    app.use(cors())
    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: true}))

    app.post('/secure', (req, res) => {
    console.log('[starting secure-chat]: ..')
    const secretKey = 'this is a secret key'
    const ciphertext = req.body.payload
    const bytes = CryptoJS.AES.decrypt(ciphertext, secretKey)
    const plaintext = bytes.toString(CryptoJS.enc.Utf8)
    console.log('[secure-chat]: plaintext => ', plaintext)
    admin.firestore()
    .collection('secure-chat').add({msg: ciphertext})
    .then(ref => {
    console.log('[secure-chat(added)]: ', ciphertext)
    res.json({cb: ref})
    })
    .catch(err => res.json({cb: 'err'}))
    })

    app.post('/unsecure', (req, res) => {
    console.log('[starting unsecure-chat]: ..')
    const plaintext = req.body.payload
    admin.firestore()
    .collection('unsecure-chat').add({msg: plaintext})
    .then(ref => {
    console.log('[unsecure-chat(added)]: ', plaintext)
    res.json({cb: ref})
    })
    .catch(err => res.json({cb: 'err'}))
    })