Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor authentication and demo-mode to use middleware #120

Open
gcschmit opened this issue Jun 18, 2024 · 3 comments
Open

refactor authentication and demo-mode to use middleware #120

gcschmit opened this issue Jun 18, 2024 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gcschmit
Copy link
Contributor

gcschmit commented Jun 18, 2024

Currently, every admin and setup endpoint checks for authentication. It would be cleaner if authentication was checked once with a middleware approach and all endpoints wouldn't have to worry about it. Similarly, demo-mode is checked throughout admin and scouting endpoints and that could be done in a single place using middleware.

@gcschmit gcschmit added enhancement New feature or request help wanted Extra attention is needed labels Jun 18, 2024
@gcschmit gcschmit changed the title refactor authentication to use middleware refactor authentication and demo-mode to use middleware Jun 18, 2024
@Allybe
Copy link
Contributor

Allybe commented Jun 19, 2024

When you say "authentication" you mean having it check for the admin password once and that'd generate a token to be used across other endpoints, right? Could also mean using something like Google OAuth.

@Allybe
Copy link
Contributor

Allybe commented Jun 19, 2024

And in that case we could use something like Passport.js which has token based authentication.

@gcschmit
Copy link
Contributor Author

gcschmit commented Jun 20, 2024

Good question; I didn't make this issue very clear. I think the access code-based authentication is fine, at least for now. While it isn't very secure, it guards against non-malicious mistakes. What I was trying to capture is that most of the routes in the admin and scouting endpoints check for authentication. For example:

router.get("/scouters", (req, res) => {
  if (!DEMO) {
    if (req.headers.authorization === config.secrets.ACCESS_CODE) {
      res.json(ScoutingSync.getScouters());
    } else {
      res.json({ error: "Not Authorized" });
    }
  } else {
    res.json(ScoutingSync.getScouters());
  }
});

Rather than duplicating the code to perform this check in each route, a middleware approach can be used instead. For example, something like this:

router.use((req, res, next) => {
  if (!ScoutingSync.initialized) {
    res.status(503).send("Scouting Sync not initialized yet!");
  }  else if (req.headers.authorization !== config.secrets.ACCESS_CODE && !req.path.startsWith("/auth")) {
   res.json({ error: "Not Authorized" });
}
else {
    next();
  }
});

Demo-mode could be handled in a similar fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants