Skip to content

Commit

Permalink
renamed unauthenticated to setunauthenticatedcode
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgramCpp committed Sep 30, 2017
1 parent acfd5ef commit b3ed0f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var express = require('express'), app = express(), cs = require('cansecurity'),

// only authorized if logged in, or as certain roles, or some combination
app.get("/secure/loggedin",cansec.restrictToLoggedIn,send200);
app.get("/secure/customloggedin",cansec.unauthenticated({code:302,location:"/login"}),cansec.restrictToLoggedIn,send200);
app.get("/secure/customloggedin",cansec.setUnauthenticatedCode({code:302,location:"/login"}),cansec.restrictToLoggedIn,send200);
app.get("/secure/user/:user",cansec.restrictToSelf,send200);
app.get("/secure/roles/admin",cansec.restrictToRoles("admin"),send200);
app.get("/secure/roles/adminOrSuper",cansec.restrictToRoles(["admin","super"]),send200);
Expand Down
15 changes: 10 additions & 5 deletions lib/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const errors = require('./errors'), rparams = require('./param'), sender = requi
csauth = constants.header.AUTH,
fields = {}, params = {};

const checkLoggedIn = (req, res, next, unauthCode = HttpStatus.UNAUTHORIZED, unauthLocation = null) => {
const checkLoggedIn = (req, res, next) => {
// If our user is authenticated
// then everything is fine :)
let logged = true;
let logged = true,
unauthenticatedResponse = req.unauthenticatedResponse || {}
unauthCode = unauthenticatedResponse.code || HttpStatus.UNAUTHORIZED;
unauthLocation = unauthenticatedResponse.location || null;

rparams(req);
if (!req[csauth]) {
if (unauthLocation != null) {
Expand Down Expand Up @@ -104,9 +108,10 @@ const checkLoggedIn = (req, res, next, unauthCode = HttpStatus.UNAUTHORIZED, una
}
},
indirect: {
unauthenticated: (unauthenticatedResponse) => {
return (req, res, next) => {
checkLoggedIn(req, res, next, unauthenticatedResponse.code, unauthenticatedResponse.location) && next();
setUnauthenticatedCode: (unauthenticatedResponse) => {
return (req, res, next) => {
req.unauthenticatedResponse = unauthenticatedResponse;
next()
};
},
// valid if user is logged in *and* the logged-in user has at least one of the given roles
Expand Down
2 changes: 1 addition & 1 deletion test/test-authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ alltests = function () {
setpaths = function () {
app.get('/secure/fieldOrRole',cansec.restrictToFieldOrRoles("owner","admin",getCheckObject),send200);
app.get("/secure/loggedin",cansec.restrictToLoggedIn,send200);
app.get("/secure/customloggedin",cansec.unauthenticated({code:302,location:"/login"}),cansec.restrictToLoggedIn,send200);
app.get("/secure/customloggedin",cansec.setUnauthenticatedCode({code:302,location:"/login"}),cansec.restrictToLoggedIn,send200);
app.get("/secure/user/:user",cansec.restrictToSelf,send200);
app.get("/secure/roles/admin",cansec.restrictToRoles("admin"),send200);
app.get("/secure/roles/adminOrSuper",cansec.restrictToRoles(["admin","super"]),send200);
Expand Down

0 comments on commit b3ed0f3

Please sign in to comment.