Skip to content

Commit

Permalink
Updates README. Adds localStorage.clear() (patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Jan 5, 2018
1 parent 9015962 commit 2e6c2b2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# gun-restrict-examples

Examples of how to restrict put with [GUN](https://github.com/amark/gun)
Examples of how to restrict put and get with [GUN](https://github.com/amark/gun)

The solutions are based on an answer from [stackoverflow](https://stackoverflow.com/questions/38598391/jwt-authentication-with-gundb)

## Setup

Expand Down
47 changes: 47 additions & 0 deletions gun-servers/server-gun-restricted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const port = 8000
const Gun = require('gun')

function hasValidToken (msg) {
return msg && msg.headers && msg.headers.token && msg.headers.token === 'thisIsTheTokenForReals'
}

// Add listener
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
// Check all incoming traffic
ctx.on('in', function (msg) {
var to = this.to
// restrict everything with invalid tokens
if (msg.put || msg.get) {
if (hasValidToken(msg)) {
console.log('okay')
to.next(msg)
} else {
console.log('not okay')
console.log(msg)
}
} else {
console.log(msg)
to.next(msg)
}
})
})

const server = require('http').createServer((req, res) => {
// filters gun requests!
if (Gun.serve(req, res)) {
return
}
})

Gun({
file: 'data.json',
web: server
})

server.listen(port)

console.log('Server started on port ' + port + ' with /gun')
console.log('Use CTRL + C to stop it')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "standard",
"restricted-put": "node gun-servers/server-gun-restricted-put",
"unrestricted": "node gun-servers/server-gun-unrestricted",
"restricted": "node gun-servers/server-gun-restricted",
"tunnel": "node tunnel",
"start": "node www/server",
"start-unauth": "node www/server-unauthenticated"
Expand Down
2 changes: 2 additions & 0 deletions www/authenticated.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
localStorage.clear()

function init () {
addListener(document.getElementById('gunForm'), 'submit', addLine)
}
Expand Down
2 changes: 2 additions & 0 deletions www/not-authenticated.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
localStorage.clear()

function init () {
addListener(document.getElementById('gunForm'), 'submit', addLine)
}
Expand Down
2 changes: 1 addition & 1 deletion www/tunnel-url.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const tunnelUrl = 'https://3fa07bc7.ngrok.io'
const tunnelUrl = 'https://4f9d3630.ngrok.io'

0 comments on commit 2e6c2b2

Please sign in to comment.