Skip to content

Commit

Permalink
Refactoring folders...
Browse files Browse the repository at this point in the history
  • Loading branch information
ombr committed Aug 3, 2012
1 parent 996a688 commit fc334bd
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 131 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# User Authentification and role management REST Webservice

## Description

Usr provides you a webservice to authenticate and manage your users with a REST API :

- Using EveryAuth to enable your users to login with any credentials
- Using REST to be shared within multiples service.

## Authentification of a user :

1/ You deploy your service to auth.yourdomain.com
2/ On your application, to authentificate a user, just need to :
- redirect the user to http://auth.yourdomain.com/login/http://mynewapp.com/loguedId/
- The user will come back to http://mynewapp.com/loguedId/SUPERTOKEN with a token
- Send a request to get all the details about your user.

File renamed without changes.
37 changes: 37 additions & 0 deletions examples/myappnewapp.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
express = require 'express'
$ = require 'jquery'

app = express.createServer(
express.bodyParser(),
express.favicon(),
express.cookieParser(),
express.session({ secret: 'supersecret'}),
)

myAppUrl = 'http://127.0.0.1:3001'
usrAppToken = 'lalalal'
usrUrl = "http://local.host:3000"


app.get('/', (req, res)->
if req.session.user
user = req.session.user
res.send("Welcome : #{user.id}, you are in the groups : #{user.groups.join(',')}<a href='/logout/'>logout</a>")
else
res.send("<a href='#{usrUrl}/login/#{myAppUrl}/logguedIn/'>login</a>")
)

app.get('/logout', (req, res)->
delete(req.session.user)
res.redirect(usrUrl+"/logout/#{myAppUrl}")
)

app.get('/logguedIn/:token', (req, res)->
url = usrUrl+"/info/#{req.params.token}/#{usrAppToken}"
$.getJSON(url,(datas)->
req.session.user = datas
res.redirect('/')
)
)

app.listen(3001)
6 changes: 2 additions & 4 deletions lib/exec.coffee → examples/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ Async = require 'async'
Log = require 'log'

log = new Log('warning')
configs = require '../configs'
configs = require './configs'

express = require 'express'
app = express.createServer(
express.bodyParser(),
express.static(__dirname + "/public"),
express.favicon(),
express.cookieParser(),
express.session({ secret: 'supersecret'}),
)
app.log = log

Auth = require './app'
Auth = require '../index'
auth = new Auth(app,configs)

app.get('/', (req, res)->
Expand All @@ -29,4 +28,3 @@ app.configure(()->
app.listen(configs.app.port)
log.info __dirname
log.info 'Application started http://local.host:'+configs.app.port
module.exports = app
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require('coffee-script')
module.exports = require('./lib/exec.coffee');
module.exports = require('./lib/app');
26 changes: 18 additions & 8 deletions lib/app.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

module.exports = class App
constructor : (express,@configs)->
_ = @
Log = require 'log'
@log = new Log()
if not @configs.logger?
Log = require 'log'
@log = new Log("warning")
else
@log = @configs.logger

EventEmitter = require('eventemitter2').EventEmitter2
@_event = new EventEmitter(
wildcard:true
Expand All @@ -14,17 +17,24 @@ module.exports = class App

#!TODO move this function to access...
@_event.once('token/new',(datas)->
console.log "Check INIT ROOT ?"
_.log.debug "First token has been created maybe a root group need to be created ?"
_.stores.group.findGroupByName('root',(err,group)->
if group == null
console.log "ROOT NULL ?"
_.stores.group.addGroup('_root',(err,groupId)->
_.emit('group/new',
groupId : groupId
token : datas.token
)
_.stores.group.addUserToGroup(datas.userId,groupId,(err,res)->
if !res
throw "Error root access granted..."
_.emit('group/addUser',
groupId : groupId
token : datas.token
userId : datas.userId
)
_.stores.group.addUserToGroupCache(datas.userId,groupId,(err,res)->
if !res
throw "Error root access granted..."
# Might be a bit strange, but root seems to proclam himself root
_.emit('root/new',datas)
)
)
Expand All @@ -50,7 +60,7 @@ module.exports = class App
'access' : './access/access'
'event' : './event/event'
for name,file of modules
console.log "Load #{file} as #{name}"
@log.info "Load #{file} as #{name}"
Module = require file
@[name] = new Module(@)

Expand Down
26 changes: 25 additions & 1 deletion lib/auth/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ module.exports = class Auth extends Component
@._everyAuth()
@._routes()

#No Check on addUser, everybody can register or create a new user
addUser : (source='', id='', datas={},cb)->
_ = @
store = @app.stores.user
store.addUser(source,id,datas,(err,userId)->
_.checkErr(err)
cb(null,userId)
_.emit('user/new',
userId:userId
userId : userId
source : source
id : id
)
)

Expand Down Expand Up @@ -56,6 +59,17 @@ module.exports = class Auth extends Component

_routes : ()->
_ = @

#Add a Are you sure on the logout ?
@routeGet('/logout/*', (req, res)->
req.logout()
delete(req.session.token)
if req.params? and req.params[0]? and req.params[0] != ''
res.redirect(req.params[0])
return
#!TODO redirect you from where you are coming ?
res.redirect('/')#!TODO RENDER LOGIN PAGE
)
@routeGet('/login/*', (req, res)->
if req.params? and req.params[0]? and req.params[0] != ''
req.session.url = req.params[0]
Expand All @@ -65,6 +79,16 @@ module.exports = class Auth extends Component
res.redirect('/auth/local')#!TODO RENDER LOGIN PAGE
)


#!TODO Check on AppToken
@routeGet('/info/:token/:appToken', (req, res)->
json = {}
_.app.token.getInfo(req.params.token,req.params.appToken,(err,info)->
_.checkErr(err)
res.json(info)
)

)
@routeGet('/redirect', (req, res)->
if not req.loggedIn
res.redirect('/login/')
Expand Down
2 changes: 2 additions & 0 deletions lib/event/event.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = class Event extends Component
@app = app
@access = app.access
io = require('socket.io').listen(@express())
io.set('log level', 1)

@channel = io.of('/auth')#!TODO Put in configs...
#@channel = io
@_init_socket()
Expand Down
49 changes: 31 additions & 18 deletions lib/group/group.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,55 @@ module.exports = class Group extends Component

add : (groupName, token, cb)->
_ = @
#!TODO Validate Name with Regex
#!TODO Validate Name with Regex and rules

#Add a new user Group
#Add a new group access group

#Add a new Group
_.app.access.check(token, ['_group_add','_root'],(userId)->
#!TODO check group existence
_.store.addGroup(groupName, (groupId)->
cb(groupId)
_.event.emit('group:new',
_.store.addGroup(groupName, (err,groupId)->
cb(err,groupId)
_.emit('group:new',
groupId : groupId
token : token
groupName : groupName
authorId : userId
)
)
)
addUserToGroup : (groupName, userId, token, cb)->
addUserToGroup : (userId, groupName, token, cb)->
_ = @
#!TODO Validate Regex
_.app.access.check(token,
[
'group_'+groupName+"_"+owner,
'group_'+groupName+"_"+add,
'group_'+groupName+"_add",
'_root'
],
(userId)->
@store.findGroupByName(groupName,(group)->
@store.addUserToGroup(group.id,userId,cb)
@store.addUserToGroupCache(group.id,userId,()->
_.event.emit('group:addUser',
groupId : groupId
token : token
)
(authorId)->
_.store.findGroupByName(groupName,(err,group)->
_.checkErr(err)
_.store.addUserToGroup(userId,group.id,cb)
_.emit('group:addUser',
groupId : group.id
token : token
authorId : authorId
userId : userId
groupName : groupName
)
#Add to cache
addUserToGroupCache = (groupId, userId)->
@store.addUserToGroupCache(i,userId,()->
_.event.emit('group:addUser',
groupId : groupId
_.store.addUserToGroupCache(userId, groupId,(err,res)->
_.checkErr(err)
_.emit('group:addUserCache',
groupId : group.id
token : token
userId : userId
groupName : groupName
)
)
addUserToGroupCache(group.id, userId)
for i in group._groups
addUserToGroupCache(i, userId)
)
Expand Down
27 changes: 26 additions & 1 deletion lib/token/token.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = class Token extends Component
)

get : (token, cb)->
@store.get(token, cb)
@store.getToken(token, cb)
add : (userId, options, cb)->
_ = @
_.app.stores.token.addToken(
Expand All @@ -44,3 +44,28 @@ module.exports = class Token extends Component
userId : userId
)
)
#!TODO ADD Check on AppToken
getInfo : (token, appToken, cb)->
_ = @
json = {}
_.get(token,(err,datas)->
if err != null
cb(err,json)
return
_.app.stores.user.findUserById(datas.userId, (err,user)->
if err != null
cb(err,json)
return
json= user
_.app.stores.group.getGroupsUserIsMemberOf(user.id, (err,groups)->
if err != null
cb(err,json)
return
json.groups = []
for k,g of groups
json.groups.push(g.name)
cb(err,json)
return
)
)
)
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
],
"main": "index",
"dependencies": {
"log": "*",
"coffee-script": ">=1.3",
"mongodb": "*",
"everyauth": "*",
"express": "=2.3.10",
"node-promise": "*",
"eventemitter2": "*",
"async": "*",
"socket.io": "~0.9.8"
"express": "~2.5.9",
"everyauth": "~0.2.32",
"log": "~1.3.0",
"mongodb": "~1.1.2",
"node-promise": "~0.5.3",
"eventemitter2": "~0.4.9",
"async": "~0.1.22",
"socket.io": "~0.9.9",
"jade": "~0.27.0"
},
"devDependencies": {
"tobi": "*",
"chai": "*",
"should": "*",
"socket.io-client": "~0.9.8",
"mocha": "*"
"mocha": "*",
"jquery": "~1.7.3"
},
"_id": "@0.0.1",
"engines": {
Expand Down
Loading

0 comments on commit fc334bd

Please sign in to comment.