Email: {user.email}
- {/*Expertise level: {level}
- OAuth accounts: -
-
- {profile.oauthAccounts.map((a) => (
-
- - {a.oauthName}: {a.accountEmail} - - ))} -
-
+ {user.roles.map((role) => (
+
- {role.name} + ))} +
Login expires: {new Date(expireDate).toISOString()}
*/} Logout diff --git a/app/routes/register.tsx b/app/routes/register.tsx index 9143bebf..dcf1278b 100644 --- a/app/routes/register.tsx +++ b/app/routes/register.tsx @@ -6,8 +6,8 @@ import { } from "@remix-run/node"; import { Form, Link } from "@remix-run/react"; import { authenticator } from "~/auth.server"; -import { localLogin, register } from "~/models/user.server"; -import { commitSession, getSession, setSession } from "~/session.server"; +import { register } from "~/models/user.server"; +import { commitSession, getSession } from "~/session.server"; export async function loader({ request }: LoaderArgs) { // TODO check already logged in @@ -32,7 +32,7 @@ export async function action({ request }: ActionArgs) { const user = await register(username, password); // Make just registered user logged in const session = await getSession(request.headers.get("cookie")); - session.set(authenticator.sessionKey, user); + session.set(authenticator.sessionKey, user.id); let headers = new Headers({ "Set-Cookie": await commitSession(session) }); return redirect("/", { headers }); } diff --git a/app/routes/upload.tsx b/app/routes/upload.tsx index 9b10eddd..ef88de9d 100644 --- a/app/routes/upload.tsx +++ b/app/routes/upload.tsx @@ -8,21 +8,20 @@ import { Form } from "@remix-run/react"; import { submitJob } from "~/models/applicaton.server"; import { - checkAuthenticated, getLevel, isSubmitAllowed, } from "~/models/user.server"; -import { getSession } from "~/session.server"; import { WORKFLOW_CONFIG_FILENAME } from "~/models/constants"; -import { authenticator } from "~/auth.server"; +import { getUser } from "~/auth.server"; +import { getAccessToken } from "~/bartender_token.server"; export const loader = async ({ request }: LoaderArgs) => { - let user = await authenticator.isAuthenticated(request); + const user = await getUser(request); if (!user) { return redirect("/login"); } // TODO get roles of current user - const level = await getLevel(session.data.roles); + const level = await getLevel(user ? user.roles.map(r => r.name) : undefined); if (!isSubmitAllowed(level)) { throw new Error("Forbidden"); } @@ -36,14 +35,8 @@ export const action = async ({ request }: ActionArgs) => { if (typeof upload === "string" || upload === null) { throw new Error("Bad upload"); } - const session = await getSession(request); - // TODO fetch token for user - const accessToken = session.data.bartenderToken; - checkAuthenticated(accessToken); - const level = await getLevel(session.data.roles); - if (!isSubmitAllowed(level)) { - throw new Error("Forbidden"); - } + + const accessToken = await getAccessToken(request) const job = await submitJob(upload, accessToken!); const job_url = `/jobs/${job.id}`; return redirect(job_url); diff --git a/app/session.server.ts b/app/session.server.ts index 07635720..63903afa 100644 --- a/app/session.server.ts +++ b/app/session.server.ts @@ -1,11 +1,8 @@ -import { - createCookieSessionStorage, -} from "@remix-run/node"; +import { createCookieSessionStorage } from "@remix-run/node"; const COOKIE_NAME = "haddock3_webapp_session"; export const sessionStorage = createCookieSessionStorage({ - // TODO add secret + domain + path cookie: { name: COOKIE_NAME, httpOnly: true, diff --git a/app/session.ts b/app/session.ts deleted file mode 100644 index c24d117c..00000000 --- a/app/session.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { useRouteLoaderData } from "@remix-run/react"; - -export function useIsAuthenticated() { - const { isAuthenticated } = useRouteLoaderData("root") as { - isAuthenticated?: boolean; - }; - return isAuthenticated; -} - -export function useIsSuperUser() { - const { isSuperUser } = useRouteLoaderData("root") as { - isSuperUser?: boolean; - }; - return isSuperUser; -} diff --git a/app/token.server.ts b/app/token.server.ts deleted file mode 100644 index 275e5743..00000000 --- a/app/token.server.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Functions dealing with access token of bartender web service. - */ -import { decodeJwt } from "jose"; -import { getSession } from "./session.server"; - -export async function getAccessToken(request: Request) { - const session = await getSession(request); - return session.data.bartenderToken; -} - -export function isExpired(accessToken: string | undefined) { - const payload = getTokenPayload(accessToken); - const now = Date.now() / 1000; - return payload.exp !== undefined && payload.exp <= now; -} - -export function getTokenPayload(accessToken: string | undefined) { - if (accessToken === undefined) { - return {}; - } - // TODO verify token by using HS256 algorithm, - // see https://github.com/i-VRESSE/bartender/issues/58 - return decodeJwt(accessToken); -} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4fe0483f..9929cb57 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -18,20 +18,9 @@ model User { // User can have no password if they use OAuth passwordHash String? roles Role[] - oauthAccounts OAUthAccount[] } model Role { name String @id users User[] } - -model OAUthAccount { - id String @id @default(uuid()) - provider String - accessToken String - refreshToken String - expiresAt DateTime? - user User @relation(fields: [userId], references: [id]) - userId String -} \ No newline at end of file diff --git a/prisma/seed.mts b/prisma/seed.mts index e629f271..e5252ffe 100644 --- a/prisma/seed.mts +++ b/prisma/seed.mts @@ -2,15 +2,14 @@ import { PrismaClient } from "@prisma/client"; const db = new PrismaClient(); async function seed() { - await Promise.all( // TODO use createMany when postgresql is used getRoles().map(async (role) => { - return db.role.create({ - data: { - name: role, - }, - }); + return db.role.create({ + data: { + name: role, + }, + }); }) ); } @@ -18,5 +17,5 @@ async function seed() { seed(); function getRoles() { - return ["guru", "expert", "easy"]; + return ["admin", "guru", "expert", "easy"]; } From 1f49596074f02ab4a3932ba4c30b3a1c9f7238a8 Mon Sep 17 00:00:00 2001 From: Stefan Verhoeven-
- {roles.map((role) => {
+ {expertiseLevels.map((expertiseLevel) => {
return (
-
- +
-
Roles | +Administrator? | +Expertise levels | +Actions |
---|