Proper auth
This commit is contained in:
@@ -103,28 +103,6 @@ export default function OverviewPage() {
|
|||||||
fetchNodes();
|
fetchNodes();
|
||||||
const interval = setInterval(fetchNodes, 3000);
|
const interval = setInterval(fetchNodes, 3000);
|
||||||
|
|
||||||
const test = async () => {
|
|
||||||
const sessionToken = await getSession();
|
|
||||||
console.log(sessionToken);
|
|
||||||
// console.log(
|
|
||||||
// (
|
|
||||||
// await axios.get("https://api.github.com/user/repos", {
|
|
||||||
// headers: { Authorization: `Bearer ${sessionToken}` },
|
|
||||||
// })
|
|
||||||
// ).data
|
|
||||||
// );
|
|
||||||
|
|
||||||
// console.log(
|
|
||||||
// (
|
|
||||||
// await axios.get("https://api.github.com/orgs/orus-dev/repos", {
|
|
||||||
// headers: { Authorization: `Bearer ${sessionToken}` },
|
|
||||||
// })
|
|
||||||
// ).data
|
|
||||||
// );
|
|
||||||
};
|
|
||||||
|
|
||||||
test();
|
|
||||||
|
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
import { read, write } from "@/app/api/file";
|
||||||
|
import axios from "axios";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
export async function getSession() {
|
type Users = Record<string, { roles: string[] }>;
|
||||||
|
|
||||||
|
export async function getSession(): Promise<
|
||||||
|
undefined | { user: { id: number }; accessToken: string }
|
||||||
|
> {
|
||||||
const c = await cookies();
|
const c = await cookies();
|
||||||
const accessToken = c.get("github_access")?.value;
|
const accessToken = c.get("github_access")?.value;
|
||||||
|
|
||||||
@@ -10,5 +16,36 @@ export async function getSession() {
|
|||||||
redirect("/");
|
redirect("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
return accessToken;
|
// Test the token
|
||||||
|
try {
|
||||||
|
const user = (
|
||||||
|
await axios.get("https://api.github.com/user", {
|
||||||
|
headers: { Authorization: `Bearer ${accessToken}` },
|
||||||
|
})
|
||||||
|
).data;
|
||||||
|
|
||||||
|
const users = Object.keys(
|
||||||
|
await read<Users>(() => ({}), "data", "users.json")
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!users.includes(String(user.id))) {
|
||||||
|
if (users.length === 0) {
|
||||||
|
await write(
|
||||||
|
{ [String(user.id)]: { roles: ["admin"] } } as Users, // First user is always an admin
|
||||||
|
"data",
|
||||||
|
"users.json"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new Error("Invalid user");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { user, accessToken };
|
||||||
|
} catch {
|
||||||
|
redirect("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function verifySession(): Promise<boolean> {
|
||||||
|
return !(await getSession())?.accessToken;
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-14
@@ -3,35 +3,42 @@
|
|||||||
import { read, write } from "@/app/api/file";
|
import { read, write } from "@/app/api/file";
|
||||||
import { Docker, Node, NodeLiveData } from "@/lib/types";
|
import { Docker, Node, NodeLiveData } from "@/lib/types";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { verifySession } from "./auth/session";
|
||||||
|
|
||||||
|
export async function getNodes(): Promise<Node[] | undefined> {
|
||||||
|
if (await verifySession()) return;
|
||||||
|
|
||||||
export async function getNodes(): Promise<Node[]> {
|
|
||||||
return await read(() => [], "data", "nodes.json");
|
return await read(() => [], "data", "nodes.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setNodes(nodes: Node[]) {
|
export async function setNodes(nodes: Node[]) {
|
||||||
|
if (await verifySession()) return;
|
||||||
|
|
||||||
await write(nodes, "data", "nodes.json");
|
await write(nodes, "data", "nodes.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getNode(node_id: string): Promise<Node | undefined> {
|
export async function getLiveNodes(): Promise<
|
||||||
const nodes = await read<Node[]>(() => [], "data", "nodes.json");
|
(NodeLiveData & Node)[] | undefined
|
||||||
return nodes.find((v) => v.node_id === node_id);
|
> {
|
||||||
}
|
if (await verifySession()) return;
|
||||||
|
|
||||||
export async function getLiveNode(node: Node): Promise<NodeLiveData & Node> {
|
const nodes: Node[] = await read(() => [], "data", "nodes.json");
|
||||||
const data = await (
|
|
||||||
|
return await Promise.all(
|
||||||
|
nodes.map(async (node) => ({
|
||||||
|
...node,
|
||||||
|
liveData: await (
|
||||||
await axios.get(`http://${node.ip}:3000/api/status`, {
|
await axios.get(`http://${node.ip}:3000/api/status`, {
|
||||||
headers: { Authorization: `ApiKey ${node.key}` },
|
headers: { Authorization: `ApiKey ${node.key}` },
|
||||||
})
|
})
|
||||||
).data;
|
).data.liveData,
|
||||||
return { ...node, liveData: data.liveData };
|
}))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLiveNodes(): Promise<(NodeLiveData & Node)[]> {
|
export async function getDockerNode(node: Node): Promise<Docker | undefined> {
|
||||||
const nodes = await read(() => [], "data", "nodes.json");
|
if (await verifySession()) return;
|
||||||
return await Promise.all(nodes.map(getLiveNode));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getDockerNode(node: Node): Promise<Docker> {
|
|
||||||
const data = await (
|
const data = await (
|
||||||
await axios.get(`http://${node.ip}:3000/api/get-docker`, {
|
await axios.get(`http://${node.ip}:3000/api/get-docker`, {
|
||||||
headers: { Authorization: `ApiKey ${node.key}` },
|
headers: { Authorization: `ApiKey ${node.key}` },
|
||||||
@@ -41,6 +48,8 @@ export async function getDockerNode(node: Node): Promise<Docker> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function authNode(ip: string, key: string) {
|
export async function authNode(ip: string, key: string) {
|
||||||
|
if (await verifySession()) return;
|
||||||
|
|
||||||
const data = await (
|
const data = await (
|
||||||
await axios.get(`http://${ip}:3000/api/auth`, {
|
await axios.get(`http://${ip}:3000/api/auth`, {
|
||||||
headers: { Authorization: `ApiKey ${key}` },
|
headers: { Authorization: `ApiKey ${key}` },
|
||||||
|
|||||||
Reference in New Issue
Block a user