-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from UNIZAR-30226-2024-03/auth
Auth
- Loading branch information
Showing
12 changed files
with
379 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const PUBLIC_ROUTES = ["/", "/login", "/register"]; | ||
export const TOKEN = "token"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export const Global ={ | ||
url: "https://playbeat.uksouth.cloudapp.azure.com:3000/" | ||
url: "http://playbeat.uksouth.cloudapp.azure.com:3000/" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { errors, jwtVerify } from "jose"; | ||
import { defineMiddleware } from "astro/middleware"; | ||
import { TOKEN, PUBLIC_ROUTES } from "./constants"; | ||
|
||
// The JWT secret | ||
const secret = new TextEncoder().encode(import.meta.env.JWT_SECRET_KEY); | ||
|
||
/** | ||
* Verify if the client token is valid. | ||
*/ | ||
const verifyAuth = async (token?: string) => { | ||
if (!token) { | ||
return { | ||
status: "unauthorized", | ||
msg: "Please pass a request token", | ||
} as const; | ||
} | ||
|
||
try { | ||
const jwtVerifyResult = await jwtVerify(token, secret); | ||
|
||
return { | ||
status: "authorized", | ||
payload: jwtVerifyResult.payload, | ||
msg: "successfully verified auth token", | ||
} as const; | ||
} catch (err) { | ||
if (err instanceof errors.JOSEError) { | ||
return { status: "error", msg: err.message } as const; | ||
} | ||
|
||
console.debug(err); | ||
return { status: "error", msg: "could not validate auth token" } as const; | ||
} | ||
}; | ||
|
||
export const onRequest = defineMiddleware(async (context, next) => { | ||
// Ignore auth validation for public routes | ||
if (PUBLIC_ROUTES.includes(context.url.pathname)) { | ||
// Respond as usual | ||
return next(); | ||
} | ||
|
||
// Get the token from cookies | ||
const token = context.cookies.get(TOKEN)?.value; | ||
// Verify the token | ||
const validationResult = await verifyAuth(token); | ||
|
||
console.log(validationResult); | ||
|
||
// Handle the validation result | ||
switch (validationResult.status) { | ||
case "authorized": | ||
// Respond as usual if the user is authorised | ||
return next(); | ||
|
||
case "error": | ||
case "unauthorized": | ||
// If an API endpoint, return a JSON response | ||
if (context.url.pathname.startsWith("/api/")) { | ||
return new Response(JSON.stringify({ message: validationResult.msg }), { | ||
status: 401, | ||
}); | ||
} | ||
// Otherwise, this is a standard page. Redirect to the root page for the user to login | ||
else { | ||
return Response.redirect(new URL("/", context.url)); | ||
} | ||
|
||
default: | ||
return Response.redirect(new URL("/", context.url)); | ||
} | ||
}); |
Oops, something went wrong.