Skip to content

Instantly share code, notes, and snippets.

@davidguttman
Last active November 13, 2015 22:58
Show Gist options
  • Save davidguttman/7a5fdacd9f950e94ec6d to your computer and use it in GitHub Desktop.
Save davidguttman/7a5fdacd9f950e94ec6d to your computer and use it in GitHub Desktop.
dry.ly/authentic examples
...
"main": "index.js",
"scripts": {
"start": "wzrd index.js"
},
"keywords": [],
...
{
"success": true,
"message": "User created. Check email for confirmation link.",
"data": {
"email": "chet@scalehaus.io",
"createdDate": "2015-11-12T21:36:20.021Z"
}
}
openssl genrsa 4096 > rsa-private.pem
openssl rsa -in rsa-private.pem -pubout > rsa-public.pem
mkdir scale-admin
cd scale-admin
npm init -y
touch index.js
mkdir scale-auth
cd scale-auth
npm init -y
npm install --save authentic-server
mkdir scale-reports
cd scale-reports
npm init -y
npm install --save authentic-service
touch server.js
{ type: 'signup',
email: 'chet@scalehaus.io',
confirmUrl: 'http://localhost:9966/?confirmToken=4d10ed4dd4321b265a2186694a3d786bba4b62ff01dd61b2311e17c46a40&email=chet%40scalehaus.io#/confirm',
confirmToken: '4d10ed4dd4321b265a2186694a3d786bba4b62ff01dd61b2311e17c46a40' }
// server.js
var fs = require('fs')
var http = require('http')
var Authentic = require('authentic-server')
var auth = Authentic({
db: __dirname + '/db',
publicKey: fs.readFileSync(__dirname + '/rsa-public.pem'),
privateKey: fs.readFileSync(__dirname + '/rsa-private.pem'),
sendEmail: function (email, cb) {
console.log(email)
setImmediate(cb)
}
})
http.createServer(auth).listen(1337)
console.log('Authentic enabled server listening on port', 1337)
var http = require('http')
var Authentic = require('authentic-service')
var auth = Authentic({
server: 'http://localhost:1337'
})
var authorized = [ 'your@email.com' ]
http.createServer(function (req, res) {
auth(req, res, function (err, authData) {
if (err) return console.error(err)
if (!authData) return respond(res, 401, {error: 'Not authenticated.'})
if (authorized.indexOf(authData.email) >= 0) {
return respond(res, 200, {
success: true, message: authData.email + ' is on the list.'
})
}
respond(res, 403, {error: 'Not on the list.'})
})
}).listen(1338)
console.log('Protected microservice listening on port', 1338)
function respond (res, status, obj) {
res.writeHead(status, {'content-type': 'application/json'})
res.end(JSON.stringify(obj))
}
curl 'http://localhost:1337/auth/signup' -X POST -d '{
"email": "chet@scalehaus.io",
"password": "notswordfish",
"confirmUrl": "http://localhost:9966/#/confirm"
}' -H 'Content-Type: application/json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment