Skip to content

Commit

Permalink
Removing some closures
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 22, 2019
1 parent 105a74e commit ed8001d
Showing 1 changed file with 121 additions and 133 deletions.
254 changes: 121 additions & 133 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,14 @@ function auth (obj, config) {
config.routes.get[config.auth.uri.root] = authMap;
}

(function () {
let r = `(?!${config.auth.uri.root}/(`;
let r = `(?!${config.auth.uri.root}/(`;

each(authUris, i => {
r += i.replace("_uri", "") + "|";
});
each(authUris, i => {
r += i.replace("_uri", "") + "|";
});

r = r.replace(/\|$/, "") + ")).*$";
obj.always(r, middleware.guard).blacklist(middleware.guard);
}());
r = r.replace(/\|$/, "") + ")).*$";
obj.always(r, middleware.guard).blacklist(middleware.guard);

config.routes.get[config.auth.uri.login] = {
instruction: config.auth.msg.login
Expand All @@ -206,112 +204,106 @@ function auth (obj, config) {
};

if (config.auth.basic.enabled) {
(function () {
let x = {};

function validate (arg, cb) {
if (x[arg] !== void 0) {
cb(null, x[arg]);
} else {
cb(new Error(STATUS_CODES[401]), null);
}
let x = {};

const validate = (arg, cb) => {
if (x[arg] !== void 0) {
cb(null, x[arg]);
} else {
cb(new Error(STATUS_CODES[401]), null);
}
};

each(config.auth.basic.list || [], i => {
let args = i.split(":");
each(config.auth.basic.list || [], i => {
let args = i.split(":");

if (args.length > 0) {
x[args[0]] = {password: args[1]};
}
});
if (args.length > 0) {
x[args[0]] = {password: args[1]};
}
});

passport.use(new BasicStrategy((username, password, done) => {
delay(() => {
validate(username, (err, user) => {
if (err !== null) {
return done(err);
}
passport.use(new BasicStrategy((username, password, done) => {
delay(() => {
validate(username, (err, user) => {
if (err !== null) {
return done(err);
}

if (user === void 0 || user.password !== password) {
return done(null, false);
}
if (user === void 0 || user.password !== password) {
return done(null, false);
}

return done(null, user);
});
}, authDelay);
}));
return done(null, user);
});
}, authDelay);
}));

const passportAuth = passport.authenticate("basic", {session: stateless === false});
const passportAuth = passport.authenticate("basic", {session: stateless === false});

if (async || config.auth.local.enabled) {
obj.get("/auth/basic", passportAuth).blacklist(passportAuth);
obj.get("/auth/basic", redirect);
} else {
obj.always(passportAuth).blacklist(passportAuth);
}
}());
if (async || config.auth.local.enabled) {
obj.get("/auth/basic", passportAuth).blacklist(passportAuth);
obj.get("/auth/basic", redirect);
} else {
obj.always(passportAuth).blacklist(passportAuth);
}
} else if (config.auth.bearer.enabled) {
(function () {
function validate (arg, cb) {
if (obj.config.auth.bearer.tokens.includes(arg)) {
cb(null, arg);
} else {
cb(new Error(STATUS_CODES[401]), null);
}
const validate = (arg, cb) => {
if (obj.config.auth.bearer.tokens.includes(arg)) {
cb(null, arg);
} else {
cb(new Error(STATUS_CODES[401]), null);
}
};

passport.use(new BearerStrategy((token, done) => {
delay(() => {
validate(token, (err, user) => {
if (err !== null) {
done(err);
} else if (user === void 0) {
done(null, false);
} else {
done(null, user, {scope: "read"});
}
});
}, authDelay);
}));
passport.use(new BearerStrategy((token, done) => {
delay(() => {
validate(token, (err, user) => {
if (err !== null) {
done(err);
} else if (user === void 0) {
done(null, false);
} else {
done(null, user, {scope: "read"});
}
});
}, authDelay);
}));

const passportAuth = passport.authenticate("bearer", {session: stateless === false});
const passportAuth = passport.authenticate("bearer", {session: stateless === false});

if (async || config.auth.local.enabled) {
obj.get("/auth/bearer", passportAuth).blacklist(passportAuth);
obj.get("/auth/bearer", redirect);
} else {
obj.always(passportAuth).blacklist(passportAuth);
}
}());
if (async || config.auth.local.enabled) {
obj.get("/auth/bearer", passportAuth).blacklist(passportAuth);
obj.get("/auth/bearer", redirect);
} else {
obj.always(passportAuth).blacklist(passportAuth);
}
} else if (config.auth.jwt.enabled) {
(function () {
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme(config.auth.jwt.scheme),
secretOrKey: config.auth.jwt.secretOrKey,
ignoreExpiration: config.auth.jwt.ignoreExpiration === true
};

each(["algorithms", "audience", "issuer"], i => {
if (config.auth.jwt[i] !== void 0) {
opts[i] = config.auth.jwt[i];
}
});
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme(config.auth.jwt.scheme),
secretOrKey: config.auth.jwt.secretOrKey,
ignoreExpiration: config.auth.jwt.ignoreExpiration === true
};

passport.use(new JWTStrategy(opts, (token, done) => {
delay(() => {
config.auth.jwt.auth(token, (err, user) => {
if (err !== null) {
done(err);
} else {
done(null, user);
}
});
}, authDelay);
}));
each(["algorithms", "audience", "issuer"], i => {
if (config.auth.jwt[i] !== void 0) {
opts[i] = config.auth.jwt[i];
}
});

const passportAuth = passport.authenticate("jwt", {session: false});
obj.always(passportAuth).blacklist(passportAuth);
}());
passport.use(new JWTStrategy(opts, (token, done) => {
delay(() => {
config.auth.jwt.auth(token, (err, user) => {
if (err !== null) {
done(err);
} else {
done(null, user);
}
});
}, authDelay);
}));

const passportAuth = passport.authenticate("jwt", {session: false});
obj.always(passportAuth).blacklist(passportAuth);
} else if (config.auth.local.enabled) {
passport.use(new LocalStrategy((username, password, done) => {
delay(() => {
Expand Down Expand Up @@ -370,25 +362,23 @@ function auth (obj, config) {
obj.get("/auth/oauth2/callback", passport.authenticate("oauth2", {failureRedirect: config.auth.uri.login}));
obj.get("/auth/oauth2/callback", redirect);
} else if (config.auth.saml.enabled) {
(function () {
let arg = config.auth.saml;

arg.callbackURL = realm + "/auth/saml/callback";
delete arg.enabled;
delete arg.path;

passport.use(new SAMLStrategy(arg, (profile, done) => {
delay(() => {
config.auth.saml.auth(profile, (err, user) => {
if (err !== null) {
done(err);
} else {
done(null, user);
}
});
}, authDelay);
}));
}());
let arg = config.auth.saml;

arg.callbackURL = realm + "/auth/saml/callback";
delete arg.enabled;
delete arg.path;

passport.use(new SAMLStrategy(arg, (profile, done) => {
delay(() => {
config.auth.saml.auth(profile, (err, user) => {
if (err !== null) {
done(err);
} else {
done(null, user);
}
});
}, authDelay);
}));

obj.get("/auth/saml", middleware.asyncFlag);
obj.get("/auth/saml", passport.authenticate("saml"));
Expand Down Expand Up @@ -440,17 +430,15 @@ function bootstrap (obj) {
obj.always(middleware.parse).blacklist(middleware.parse);

// Setting 'always' routes before authorization runs
(function () {
const routes = obj.config.routes.always || {};
const routes = obj.config.routes.always || {};

each(Object.keys(routes), i => {
if (typeof routes[i] === "function") {
obj.always(i, routes[i]).blacklist(routes[i]);
}
});
each(Object.keys(routes), i => {
if (typeof routes[i] === "function") {
obj.always(i, routes[i]).blacklist(routes[i]);
}
});

delete obj.config.routes.always;
}());
delete obj.config.routes.always;

if (authorization) {
auth(obj, obj.config);
Expand All @@ -463,13 +451,13 @@ function bootstrap (obj) {

// Setting routes
each(Object.keys(obj.config.routes), method => {
const routes = obj.config.routes[method];
const lroutes = obj.config.routes[method];

each(Object.keys(routes), i => {
if (typeof routes[i] === "function") {
obj[method](i, routes[i]);
each(Object.keys(lroutes), i => {
if (typeof lroutes[i] === "function") {
obj[method](i, lroutes[i]);
} else {
obj[method](i, (req, res) => res.send(routes[i]));
obj[method](i, (req, res) => res.send(lroutes[i]));
}
});
});
Expand All @@ -482,7 +470,7 @@ function id (arg = "") {
}

function scheme (arg = "") {
return arg.includes("://") || arg.startsWith("/");
return arg.includes("://") || arg[0] === "/";
}

function hypermedia (server, req, rep, headers) {
Expand Down Expand Up @@ -519,7 +507,7 @@ function hypermedia (server, req, rep, headers) {
}

if (scheme(lkey) === false) {
uri = `${lcollection.startsWith("/") ? "" : "/"}${lcollection.replace(/\s/g, "%20")}/${lkey.replace(/\s/g, "%20")}`;
uri = `${lcollection[0] === "/" ? "" : "/"}${lcollection.replace(/\s/g, "%20")}/${lkey.replace(/\s/g, "%20")}`;

if (uri !== root && seen.has(uri) === false) {
seen.add(uri);
Expand Down

0 comments on commit ed8001d

Please sign in to comment.