Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Last active February 13, 2017 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okunishinishi/cb8cccfb35878cb920baf7dfe3feb48d to your computer and use it in GitHub Desktop.
Save okunishinishi/cb8cccfb35878cb920baf7dfe3feb48d to your computer and use it in GitHub Desktop.
[SUGOS] チュートリアル05 - ActorやCallerを認証する ref: http://qiita.com/okunishinishi@github/items/b981528e91b31d4c42dc
mkdir sugos-tutorial-05
cd sugos-tutorial-05
npm init -y
npm install sugo-actor sugo-caller sugo-hub co debug asleep -S
#!/usr/bin/env node
'use strict'
const sugoHub = require('sugo-hub')
const co = require('co')
const crypto = require('crypto')
// Password hash function
const toHash = (password) => crypto.createHash('sha512').update(password).digest('hex')
co(function * () {
/**
* Password hashes for auth
*/
const passwordHashes = {
actors: {
'the-actor-for-tutorial-05': toHash('BigBigApplePie')
},
callers: {
'the-caller-for-tutorial-05': toHash('LittleLittleOrange')
}
}
let hub = sugoHub({
/**
* Custom auth function.
* @param {Object} socket - A socket connecting
* @param {Object} data - Socket auth data
* @returns {Promise.<boolean>} - OK or not
*/
authenticate (socket, data) {
let { id, type, hash } = data
switch (type) {
case 'actor': {
// Check actor hash
let ok = passwordHashes.actors[ id ] === hash
if (ok) {
console.log(`Auth succeeded with actor: ${id}`)
}
return Promise.resolve(ok)
}
case 'caller': {
// Check caller hash
let ok = passwordHashes.callers[ id ] === hash
if (ok) {
console.log(`Auth succeeded with caller: ${id}`)
}
return Promise.resolve(ok)
}
default:
console.warn(`Unknown type: ${type}`)
return false
}
}
})
yield hub.listen(3000)
console.log(`SUGO Cloud started at port: ${hub.port}`)
}).catch((err) => {
console.error(err)
process.exit(1)
})
DEBUG=sugos:tutorial:* node ./hub.js
#!/usr/bin/env node
'use strict'
const sugoActor = require('sugo-actor')
const { Module } = sugoActor
const co = require('co')
const asleep = require('asleep')
const crypto = require('crypto')
// Password hash function
const toHash = (password) => crypto.createHash('sha512').update(password).digest('hex')
co(function * () {
let actor = sugoActor({
host: 'localhost:3000',
key: 'my-actor-05',
/** Modules to load */
modules: {
tableTennis: new Module({
ping (message = '') {
// Just wait 500ms and return pong.
return co(function * () {
yield asleep(500)
return `pong! ${message}`
})
}
})
},
/**
* Authenticate data of this actor
*/
auth: {
id: 'the-actor-for-tutorial-05',
type: 'actor',
hash: toHash('BigBigApplePie')
}
})
// Connect to hub
yield actor.connect()
console.log(`Actor connected to: ${actor.socket.io.uri}`)
}).catch((err) => console.error(err))
node ./actor.js
#!/usr/bin/env node
/**
* Use sugo caller
* @see https://github.com/realglobe-Inc/sugo-caller#readme
*/
'use strict'
const sugoCaller = require('sugo-caller')
const co = require('co')
const crypto = require('crypto')
// Password hash function
const toHash = (password) => crypto.createHash('sha512').update(password).digest('hex')
co(function * () {
let caller = sugoCaller({
host: 'localhost:3000',
/**
* Authenticate data of this caller
*/
auth: {
id: 'the-caller-for-tutorial-05',
type: 'caller',
hash: toHash('LittleLittleOrange')
}
})
// Connect to actor
let myActor05 = yield caller.connect('my-actor-05')
let tableTennis = myActor05.get('tableTennis')
let pong = yield tableTennis.ping('hello world!')
console.log(`Pong from myActor05/tableTennis: "${pong}"`)
}).catch((err) => console.error(err))
node ./caller.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment