Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Aug 24, 2012
0 parents commit d2e0115
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
*.log
*.err
19 changes: 19 additions & 0 deletions LICENCE
@@ -0,0 +1,19 @@
Copyright (c) 2012 Raynos.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file added Makefile
Empty file.
56 changes: 56 additions & 0 deletions README.md
@@ -0,0 +1,56 @@
# auth-strream

Authorize access before exposing a stream

## Example Server

``` js
var Auth = require("auth-stream")
, net = require("net")
, through = require("through")

net.createServer(function (stream) {
var secret = through(function (data) {
console.log("[SERVER]", data)
this.emit("data", "secret")
})
, auth = Auth(secret, function (user, pass) {
if (user === "steve" && pass === "jones") {
return true
}
})

stream.pipe(auth).pipe(stream)
}).listen(8080)
```

## Example Client

``` js
var Auth = require("auth-stream")
, net = require("net")

var stream = net.connect(8080)
, auth = Auth()

stream.pipe(auth).pipe(stream)

stream.on("data", function (data) {
console.log("[CLIENT]", data)
})

stream.write("anything")

// login(user, pass)
auth.login("steve", "jones")
```

## Installation

`npm install auth-strream`

## Contributors

- Raynos

## MIT Licenced
32 changes: 32 additions & 0 deletions example/backwards.js
@@ -0,0 +1,32 @@
var Auth = require("../index")
, net = require("net")
, through = require("through")

// backwards
var server = net.createServer(function (stream) {
stream = Auth("steve", "jones", stream)

stream.on("data", function (data) {
console.log("[SERVER]", data.toString())
stream.end()
server.close()
})

stream.write("anything")
}).listen(8080, function () {
var stream = net.connect(8080)

var secret = through(function (data) {
console.log("[CLIENT]", data.toString())
this.emit("data", "secret")
})
, auth = Auth(secret, function (user, pass) {
if (user === "steve" && pass === "jones") {
return true
}

return "ACCESS DENIED"
})

stream.pipe(auth).pipe(stream)
})
31 changes: 31 additions & 0 deletions example/forward.js
@@ -0,0 +1,31 @@
var Auth = require("../index")
, net = require("net")
, through = require("through")

// forward
var server = net.createServer(function (stream) {
var secret = through(function (data) {
console.log("[SERVER]", data.toString())
this.emit("data", "secret")
})
// Auth(secret, login)
, auth = Auth(secret, function (user, pass) {
if (user === "steve" && pass === "jones") {
return true
}

return "ACCESS DENIED"
})

stream.pipe(auth).pipe(stream)
}).listen(8080, function () {
var stream = Auth("steve", "jones", net.connect(8080))

stream.on("data", function (data) {
console.log("[CLIENT]", data.toString())
stream.end()
server.close()
})

stream.write("anything")
})
75 changes: 75 additions & 0 deletions index.js
@@ -0,0 +1,75 @@
var duplex = require("duplex")
, BufferStream = require("buffer-stream")
, env = process.env

module.exports = Auth

function Auth(first, second, third) {
if (typeof first === "string") {
return AuthClient(first, second, third)
}

return AuthServer(first, second)
}

function AuthClient(user, pass, stream) {
var auth = BufferStream().buffer()
stream.write("steve:jones")

stream.on("data", isOpen)

return auth

function isOpen(data) {
var msg = data.toString()
if (msg === "open") {
return auth.empty(stream)
}

var parts = msg.split(":")

if (parts[0] === "error") {
auth.emit("error", new Error(parts[1]))
}
}
}

function AuthServer(stream, authorize) {
var auth = duplex()
, open = false

authorize = authorize || defaultAuthorize

auth.on("write", onWrite)

return auth

function onWrite(data) {
if (open) {
return stream.write(data)
}

var parts = data.toString().split(":")
, user = parts[0]
, pass = parts[1]

var success = authorize(user, pass)

if (success === true) {
open = true

auth.sendData("open")
stream.on("data", forward)
} else {
auth.sendData("error:" + success)
}
}

function forward(data) {
auth.sendData(data)
}
}

function defaultAuthorize(user, pass) {
return user === env.AUTH_STREAM_USER && pass === env.AUTH_STREAM_PASS
}
33 changes: 33 additions & 0 deletions package.json
@@ -0,0 +1,33 @@
{
"name": "auth-strream",
"version": "0.0.1",
"description": "Authorize access before exposing a stream",
"keywords": [],
"author": "Raynos <raynos2@gmail.com>",
"repository": "git://github.com/Raynos/auth-strream.git",
"main": "index",
"homepage": "https://github.com/Raynos/auth-strream",
"contributors": [
{
"name": "Jake Verbaten"
}
],
"bugs": {
"url": "https://github.com/Raynos/auth-strream/issues",
"email": "raynos2@gmail.com"
},
"dependencies": {
"duplex": "~0.1.2",
"buffer-stream": "0.0.1"
},
"devDependencies": {
"through": "~0.1.4"
},
"licenses": [
{
"type": "MIT",
"url": "http://github.com/Raynos/auth-strream/raw/master/LICENSE"
}
],
"scripts": {}
}

0 comments on commit d2e0115

Please sign in to comment.