Skip to content

Commit

Permalink
Merge pull request #2 from Chalin-Shi/master
Browse files Browse the repository at this point in the history
add test and integrate travis ci
  • Loading branch information
hsluoyz authored Aug 20, 2018
2 parents c098fc2 + 9b825b2 commit b47d0e2
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 148 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js

sudo: false

node_js:
- 'stable'
- '8'

script: echo "Running tests against $(node -v)..."

jobs:
include:
- stage: Produce Coverage
node_js: node
script: jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
62 changes: 31 additions & 31 deletions authz.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ const { Enforcer } = require('casbin')
// authz returns the authorizer, uses a Casbin enforcer as input
module.exports = function authz (newEnforcer) {
return async (req, res, next) => {
const enforcer = await newEnforcer()
if (!(enforcer instanceof Enforcer)) {
res.status(500).json({500: 'Invalid enforcer'})
return
}
const authzorizer = new BasicAuthorizer(req, enforcer)
if (!authzorizer.checkPermission()) {
res.status(403).json({403: 'Forbidden'})
return
}
next()
const enforcer = await newEnforcer()
if (!(enforcer instanceof Enforcer)) {
res.status(500).json({500: 'Invalid enforcer'})
return
}
const authzorizer = new BasicAuthorizer(req, enforcer)
if (!authzorizer.checkPermission()) {
res.status(403).json({403: 'Forbidden'})
return
}
next()
}
}

// BasicAuthorizer class stores the casbin handler
class BasicAuthorizer {
constructor(req, enforcer) {
this.req = req
this.enforcer = enforcer
}
constructor (req, enforcer) {
this.req = req
this.enforcer = enforcer
}

// getUserName gets the user name from the request.
// Currently, only HTTP basic authentication is supported
getUserName() {
// customize to get username from context
const {user} = this.req
const {username} = user
return username
}
// getUserName gets the user name from the request.
// Currently, only HTTP basic authentication is supported
getUserName () {
// customize to get username from context
const {user} = this.req
const {username} = user
return username
}

// checkPermission checks the user/method/path combination from the request.
// Returns true (permission granted) or false (permission forbidden)
checkPermission() {
const {req, enforcer} = this
const {originalUrl: path, method} = req
const user = this.getUserName()
return enforcer.enforce(user, path, method)
}
// checkPermission checks the user/method/path combination from the request.
// Returns true (permission granted) or false (permission forbidden)
checkPermission () {
const {req, enforcer} = this
const {originalUrl: path, method} = req
const user = this.getUserName()
return enforcer.enforce(user, path, method)
}
}
File renamed without changes.
File renamed without changes.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "express-authz is an authorization middleware for Express",
"main": "authz.js",
"scripts": {
"test": "nyc npm run test-only",
"test-only": "mocha --reporter spec test/test.js"
"prepublish": "npm run lint && npm run test",
"lint": "eslint \"authz.js\"",
"fix": "eslint \"authz.js\" --fix",
"test": "jest"
},
"keywords": [
"node-casbin",
Expand All @@ -23,13 +25,6 @@
"name": "Chalin-Shi",
"email": "[email protected]"
},
"nyc": {
"reporter": [
"lcov",
"text-summary"
],
"report-dir": "./coverage"
},
"engines": {
"node": ">= 7.6.0"
},
Expand All @@ -39,12 +34,17 @@
},
"homepage": "https://github.com/node-casbin/express-authz#readme",
"dependencies": {
"casbin": "^1.0.7"
"casbin": "^1.1.0"
},
"devDependencies": {
"eslint": "^5.4.0",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^3.1.0",
"express": "^4.16.3",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"jest": "^23.5.0",
"supertest": "^3.1.0"
}
}
6 changes: 2 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ app.use((req, res, next) => {
})

// use authz middleware
app.use(authz(async() => {
app.use(authz(async () => {
// load the casbin model and policy from files, database is also supported.
const enforcer = await Enforcer.newEnforcer("authz_model.conf", "authz_policy.csv")
const enforcer = await Enforcer.newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv')
return enforcer
}))

Expand All @@ -37,6 +37,4 @@ app.use((req, res, next) => {
res.status(200).json({200: 'OK'})
})

// app.listen(3000)

module.exports = app
Loading

0 comments on commit b47d0e2

Please sign in to comment.