Permalink
Browse files

road to 0.1.0 added sha1 and mocha

  • Loading branch information...
1 parent b504b38 commit 93eecd0fb0b4dfb09d96a4b474b3e9252339a973 @twilson63 committed Dec 9, 2011
View
@@ -20,23 +20,40 @@ app.configure ->
#
# # WebSite
app.get '/', (req, resp) ->
- resp.render "index"
+ if req.is('application/json')
+ resp.json welcome: 'Authur 0.1.0'
+ else
+ resp.render "index"
+
app.get '/register', (req, resp) ->
resp.render "register"
app.post '/admins', (req, resp) ->
- admins.register req.body.username, req.body.password, req.body.confirm, (err, admin) ->
- resp.render "success" unless err?
-
+ # TODO: Validate Document
+ if req.body? and req.body.username? and req.body.password? and req.body.confirm?
+ admins.register req.body, (err, admin) ->
+ if req.is('application/json')
+ resp.json success: true unless err?
+ else
+ resp.render "success" unless err?
+ else
+ errors = []
+ errors.push "Admin Object is Required!" if !req.body?
+ errors.push "Attribute: username is required!" if !req.body.username?
+ errors.push "Attribute: password is required!" if !req.body.password?
+ errors.push "Attribute: confirm is required!" if !req.body.confirm?
+ resp.writeHead 400, 'Content-Type': 'application/json'
+ resp.end JSON.stringify(errors: errors)
#
# # Applications
#
# Register Application
app.put '/applications/:name', express.basicAuth(auth), (req, resp) ->
- applications.register req.params.name, express.basicAuth(auth), (err) ->
- resp.json { success: (if err? then false else true) }
+ applications.register req.params.name, req.remoteUser.username, (err) ->
+ resp.json { success: (if err? then false else true) }
+
# Get all apps by owner
app.get '/applications', express.basicAuth(auth), (req, resp) ->
applications.all req.remoteUser.username, (err, applications) ->
@@ -46,8 +63,18 @@ app.get '/applications', express.basicAuth(auth), (req, resp) ->
#
# Register User
app.put '/users/:username', express.basicAuth(auth), (req, resp) ->
- users.register req.params.username, req.body.password, req.body.confirm, (err) ->
- resp.json { success: (if err? then false else true)}
+ if req.body?.password? and req.body?.confirm?
+ users.register req.params.username, req.body.password, req.body.confirm, (err) ->
+ resp.json { success: (if err? then false else true)}
+ else
+ errors = []
+ errors.push "User Object is Required!" if !req.body?
+ errors.push "Attribute: username is required!" if !req.body.username?
+ errors.push "Attribute: password is required!" if !req.body.password?
+ errors.push "Attribute: confirm is required!" if !req.body.confirm?
+ resp.writeHead 400, 'Content-Type': 'application/json'
+ resp.end JSON.stringify(errors)
+
# Attach App
app.post '/users/:username/apps/:app', express.basicAuth(auth), (req, resp) ->
@@ -56,11 +83,18 @@ app.post '/users/:username/apps/:app', express.basicAuth(auth), (req, resp) ->
# Auth
app.post '/auth/:app', express.basicAuth(auth), (req, resp) ->
- users.authenticate req.body.username, req.body.password, (err, apps) ->
- unless err?
- resp.json { success: if req.params.app in apps then true else false }
- else
- resp.json { success: false }
-
+ if req.body? and req.body.username? and req.body.password?
+ users.authenticate req.body.username, req.body.password, (err, apps) ->
+ unless err?
+ resp.json { success: if req.params.app in apps then true else false }
+ else
+ resp.json { success: false }
+ else
+ errors = []
+ errors.push "User Object is Required!" if !req.body?
+ errors.push "Attribute: username is required!" if !req.body.username?
+ errors.push "Attribute: password is required!" if !req.body.password?
+ resp.writeHead 400, 'Content-Type': 'application/json'
+ resp.end JSON.stringify(errors)
app.listen process.env.VMC_APP_PORT or 3000, -> console.log 'Listening...'
View
@@ -2,16 +2,17 @@
request = require 'request'
memcache = require 'memcache'
bcrypt = require 'bcrypt'
+crypto = require 'crypto'
db = new memcache.Client(11211, process.env.DBSERVER or "localhost")
views = require './db'
# Admins
module.exports =
- register: (username, password, confirm, cb) ->
- return cb(new Error('Password and Confirm not equal.')) unless password == confirm
- @_encrypt password, (err, salt, hash) =>
- @_save { username, salt, hash }, (err, result) ->
+ register: (user, cb) ->
+ return cb(new Error('Password and Confirm not equal.')) unless user.password == user.confirm
+ @_encrypt user.password, (err, salt, hash) =>
+ @_save { username: user.username, salt, hash }, (err, result) ->
unless err? then cb(null) else cb(new Error('Unable to create user'))
authenticate: (username, password, cb) ->
@@ -27,8 +28,9 @@ module.exports =
#bolierplate get
_get: (username, cb) ->
+ hash = crypto.createHash('sha1').update(username).digest('hex')
db.connect()
- db.get "admin-#{username}", (err, userDoc) ->
+ db.get "admin-#{hash}", (err, userDoc) ->
db.close()
if err?
cb err, null
@@ -38,7 +40,8 @@ module.exports =
cb new Error('Admin Not Found'), null
_save: (admin, cb) ->
+ hash = crypto.createHash('sha1').update(admin.username).digest('hex')
db.connect()
- db.set "admin-#{admin.username}", JSON.stringify(admin), (err, result) ->
+ db.set "admin-#{hash}", JSON.stringify(admin), (err, result) ->
db.close()
if err? then cb(err, null) else cb(null, result)
View
@@ -1,6 +1,7 @@
# connection info
request = require 'request'
memcache = require 'memcache'
+crypto = require 'crypto'
db = new memcache.Client(11211, process.env.DBSERVER or "localhost")
views = require './db'
@@ -9,8 +10,9 @@ views = require './db'
module.exports =
# register application
register: (name, owner, cb) ->
+ hash = crypto.createHash('sha1').update(name).digest('hex')
db.connect()
- db.set "application-#{name}", JSON.stringify({ name, owner }), (err, result) ->
+ db.set "application-#{hash}", JSON.stringify({ name, owner }), (err, result) ->
db.close()
unless err? then cb(null) else cb(new Error('Unable to save app'))
# all apps by owner
View
@@ -2,6 +2,7 @@
request = require 'request'
memcache = require 'memcache'
bcrypt = require 'bcrypt'
+crypto = require 'crypto'
db = new memcache.Client(11211, process.env.DBSERVER or "localhost")
views = require './db'
@@ -42,8 +43,9 @@ module.exports =
#bolierplate get
_get: (username, cb) ->
+ hash = crypto.createHash('sha1').update(username).digest('hex')
db.connect()
- db.get "user-#{username}", (err, userDoc) ->
+ db.get "user-#{hash}", (err, userDoc) ->
db.close()
if err?
cb err, null
@@ -54,6 +56,7 @@ module.exports =
_save: (user, cb) ->
db.connect()
- db.set "user-#{user.username}", JSON.stringify(user), (err, result) ->
+ hash = crypto.createHash('sha1').update(user.username).digest('hex')
+ db.set "user-#{hash}", JSON.stringify(user), (err, result) ->
db.close()
if err? then cb(err, null) else cb(null, result)
View
@@ -2,29 +2,30 @@
"author": "Tom Wilson <tom@jackhq.com> (http://www.jackhq.com)",
"name": "authur",
"description": "Super Fast Application Authentication Library",
- "version": "0.0.1",
+ "version": "0.1.0",
"homepage": "http://authur.wilbur.io",
"repository": {
"type": "git",
"url": "git://github.com/twilson63/authur.git"
},
"main": "server.js",
"scripts": {
- "test": "jasmine-node spec --coffee"
+ "test": "mocha"
},
"engines": {
- "node": "~> 0.4.x"
+ "node": "~0.4.x"
},
"dependencies": {
- "coffee-script": "1.x.x"
- , "express": "*"
- , "memcache": "*"
- , "request": "*"
- //, "bcrypt": "*" commented out to pull from global npm install bcrypt -g
- , "jade": "*"
- , "markdown": "*"
+ "coffee-script": "~1.x.x"
+ , "express": "~2.5.x"
+ , "memcache": "~0.2.x"
+ , "request": "~2.x.x"
+ , "bcrypt": "~0.4.x"
+ , "jade": "~0.x.x"
+ , "markdown": "~0.x.x"
},
"devDependencies": {
- "jasmine-node": "*"
+ "mocha": "~0.3.x"
+ , "should": "*"
}
}
@@ -1,14 +0,0 @@
-require '../../app'
-request = require 'request'
-
-good_url = 'http://admin:thomas@localhost:3000'
-
-describe 'Applications#list', ->
- it 'should return all owners apps', ->
- request.get
- uri: [good_url, 'applications'].join('/')
- json: true
- (err, resp, body) ->
- expect(body.applications).toBeDefined()
- asyncSpecDone()
- asyncSpecWait()
@@ -1,16 +0,0 @@
-require '../../app'
-request = require 'request'
-
-#good_url = 'http://admin:thomas@localhost:3000'
-good_url = 'http://jackadmin:thomas63@authur.wilbur.io'
-
-
-describe 'Applications#register', ->
- it 'success is true', ->
- request.put
- uri: [good_url, 'applications', 'app1'].join('/')
- json: true
- (err, resp, body) ->
- expect(body.success).toEqual(true)
- asyncSpecDone()
- asyncSpecWait()
View
@@ -1,16 +0,0 @@
-require '../app'
-request = require 'request'
-
-good_url = 'http://jackadmin:thomas63@authur.wilbur.io'
-
-#good_url = 'http://admin:thomas@localhost:3000'
-
-describe 'app#auth', ->
- it 'success is true', ->
- request.post
- uri: [good_url, 'auth', 'app1'].join('/')
- json: { username: 'user', password: 'disel10' }
- (err, resp, body) ->
- expect(body.success).toEqual(true)
- asyncSpecDone()
- asyncSpecWait()
View
@@ -1,25 +0,0 @@
-admins = require [__dirname, '../../lib/admins'].join('/')
-
-describe 'admins', ->
- it '#register', ->
- admins.register 'admin', 'thomas', 'thomas', (err) ->
- expect(err).toBeNull()
- asyncSpecDone()
- asyncSpecWait()
- describe '#authenticate', ->
- it 'should be valid', ->
- admins.authenticate 'admin', 'thomas', (err) ->
- expect(err).toBeNull()
- asyncSpecDone()
- asyncSpecWait()
- it 'should be invalid pasword', ->
- admins.authenticate 'admin', 'disel11', (err) ->
- expect(err.message).toEqual('Password Invalid')
- asyncSpecDone()
- asyncSpecWait()
- it 'should be invalid user', ->
- admins.authenticate 'noname', 'disel11', (err) ->
- expect(err.message).toEqual('Admin Invalid')
- asyncSpecDone()
- asyncSpecWait()
-
@@ -1,8 +0,0 @@
-applications = require [__dirname, '../../lib/applications'].join('/')
-
-describe 'applications', ->
- it '#list', ->
- applications.all 'Admin', (err, applications) ->
- expect(applications).toBeDefined()
- asyncSpecDone()
- asyncSpecWait()
View
@@ -1,30 +0,0 @@
-users = require [__dirname, '../../lib/users'].join('/')
-
-describe 'users', ->
- it '#register', ->
- users.register 'jackhq', 'disel10', 'disel10', (err) ->
- expect(err).toBeNull()
- asyncSpecDone()
- asyncSpecWait()
- describe '#authenticate', ->
- it 'should be valid', ->
- users.authenticate 'jackhq', 'disel10', (err, apps) ->
- expect(err).toBeNull()
- expect(apps).toEqual([])
- asyncSpecDone()
- asyncSpecWait()
- it 'should be invalid pasword', ->
- users.authenticate 'jackhq', 'disel11', (err) ->
- expect(err.message).toEqual('Password Invalid')
- asyncSpecDone()
- asyncSpecWait()
- it 'should be invalid user', ->
- users.authenticate 'noname', 'disel11', (err) ->
- expect(err.message).toEqual('User Invalid')
- asyncSpecDone()
- asyncSpecWait()
- it '#attachApp', ->
- users.attachApp 'jackhq', 'app1', (err, result) ->
- expect(result).toEqual("STORED")
- asyncSpecDone()
- asyncSpecWait()
@@ -1,25 +0,0 @@
-require '../../app'
-request = require 'request'
-
-good_url = 'http://jackadmin:thomas63@authur.wilbur.io'
-
-#good_url = 'http://admin:thomas@localhost:3000'
-
-describe 'Users#attachApp', ->
- it 'success is true', ->
- request.post
- uri: [good_url, 'users', 'user', 'apps', 'app1'].join('/')
- json: true
- (err, resp, body) ->
- expect(body.success).toEqual(true)
- asyncSpecDone()
- asyncSpecWait()
-
- # it 'success is false', ->
- # request.post
- # uri: [good_url, 'users', 'nouser', 'apps', 'app1'].join('/')
- # json: true
- # (err, resp, body) ->
- # expect(body.success).toEqual(false)
- # asyncSpecDone()
- # asyncSpecWait()
@@ -1,16 +0,0 @@
-require '../../app'
-request = require 'request'
-
-good_url = 'http://jackadmin:thomas63@authur.wilbur.io'
-
-#good_url = 'http://admin:thomas@localhost:3000'
-
-describe 'Users#register', ->
- it 'success is true', ->
- request.put
- uri: [good_url, 'users', 'user'].join('/')
- json: { password: 'disel10', confirm: 'disel10' }
- (err, resp, body) ->
- expect(body.success).toEqual(true)
- asyncSpecDone()
- asyncSpecWait()
Oops, something went wrong.

0 comments on commit 93eecd0

Please sign in to comment.