Merge pull request #79 from 9p4/disable-auth

feat: disable auth (#59)
This commit is contained in:
dec0dOS 2022-05-24 15:20:27 +04:00 committed by GitHub
commit 9b519079ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 8 deletions

View file

@ -153,6 +153,7 @@ Advanced manual setups are also supported. Check the following environment varia
| ZU_DEFAULT_USERNAME | unset (`docker-compose.yml`: admin) | Default username that will be set on the first run |
| ZU_DEFAULT_PASSWORD | unset (`docker-compose.yml`: zero-ui) | Default password that will be set on the first run |
| ZU_DATAPATH | `data/db.json` | ZeroUI data storage path |
| ZU_DISABLE_AUTH | unset | If set to true, automatically log in all users. This is useful if ZeroUI is protected by an authentication proxy |
ZeroUI could be deployed as a regular nodejs web application, but it requires ZeroTier controller that is installed with `zerotier-one` package. More info about the network controller you could read [here](https://github.com/zerotier/ZeroTierOne/tree/master/controller/#readme).

View file

@ -3,6 +3,14 @@ const router = express.Router();
const auth = require("../services/auth");
router.get("/login", async function (req, res) {
if (process.env.ZU_DISABLE_AUTH === "true") {
res.send({ enabled: false });
} else {
res.send({ enabled: true });
}
});
router.post("/login", async function (req, res) {
if (req.body.username && req.body.password) {
auth.authorize(req.body.username, req.body.password, function (err, user) {

View file

@ -20,14 +20,18 @@ async function authorize(username, password, callback) {
exports.isAuthorized = isAuthorized;
async function isAuthorized(req, res, next) {
if (req.token) {
const user = await db.get("users").find({ token: req.token }).value();
if (user) {
next();
} else {
res.status(403).send({ error: "Invalid token" });
}
if (process.env.ZU_DISABLE_AUTH === "true") {
next();
} else {
res.status(401).send({ error: "Specify token" });
if (req.token) {
const user = await db.get("users").find({ token: req.token }).value();
if (user) {
next();
} else {
res.status(403).send({ error: "Invalid token" });
}
} else {
res.status(401).send({ error: "Specify token" });
}
}
}

View file

@ -1,6 +1,19 @@
import { Grid, Typography } from "@material-ui/core";
import { useLocalStorage } from "react-use";
import axios from "axios";
import { useHistory } from "react-router-dom";
function HomeLoggedOut() {
const [, setLoggedIn] = useLocalStorage("loggedIn", false);
const [, setToken] = useLocalStorage("token", null);
const history = useHistory();
axios.get("/auth/login").then(function (response) {
if (!response.data.enabled) {
setLoggedIn(true);
setToken("");
history.go(0);
}
});
return (
<Grid
container