Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: improvements on securing Edge Functions guide (#41727)

Kalleby Santos037e5f90a568
diff --git a/apps/docs/content/guides/functions/auth.mdx b/apps/docs/content/guides/functions/auth.mdx
index c8589c79997..47f7be07c0f 100644
--- a/apps/docs/content/guides/functions/auth.mdx
+++ b/apps/docs/content/guides/functions/auth.mdx
@@ -30,33 +30,9 @@ Following the [upcoming API key changes](https://github.com/orgs/supabase/discus
</Admonition>
-## Verifying JWT
-
-### Using Supabase template
-
-You can see [a custom JWT verification example on GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/custom-jwt-validation) and a variety of [auth function templates](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/_shared/jwt) also on GitHub.
-
-To verify incoming requests, you can download the specified template and start using it:
-
-```bash
-curl -O https://raw.githubusercontent.com/supabase/supabase/refs/heads/master/examples/edge-functions/supabase/functions/_shared/jwt/default.ts
-```
-
-```ts
-import { AuthMiddleware } from '../_shared/jwt/default.ts'
-
-Deno.serve((r) =>
- AuthMiddleware(r, async (req) => {
- const { name } = await req.json()
-
- return Response.json({ message: `hello ${name}` })
- })
-)
-```
+## Integrating with Supabase Auth
-### Integrating with Supabase Auth
-
-You can also use the Supabase Auth to verify users
+The simplest way to secure your endpoints is by using Supabase Auth to verify users.
<$Partial path="api_settings.mdx" variables={{ "framework": "", "tab": "" }} />
@@ -94,66 +70,28 @@ Deno.serve(async (req) => {
})
```
-### Using JWT library
+## Verifying JWT
-You can use the popular [`jose`](https://www.npmjs.com/package/jose/v/4.10.0) library can be used to verify received JWTs, like the following example:
+### Using Supabase template
-```ts
-import { createRemoteJWKSet, jwtVerify } from 'https://deno.land/x/[email protected]/index.ts'
+You can see [a custom JWT verification example on GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/custom-jwt-validation) and a variety of [auth function templates](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/_shared/jwt) also on GitHub.
-// While developing you may populate the 'SB_JWT_ISSUER' to match the same issuer from your local environment
-// The issuer can also be customised from [auth.jwt_issuer] property
-const SUPABASE_JWT_ISSUER =
- Deno.env.get('SB_JWT_ISSUER') ?? Deno.env.get('SUPABASE_URL') + '/auth/v1'
+To verify incoming requests, you can copy/download the specified template and start using it:
-const SUPABASE_JWT_KEYS = createRemoteJWKSet(
- new URL(Deno.env.get('SUPABASE_URL')! + '/auth/v1/.well-known/jwks.json')
-)
+<Admonition type="tip">
-function getAuthToken(req: Request) {
- const authHeader = req.headers.get('authorization')
- if (!authHeader) {
- throw new Error('Missing authorization header')
- }
- const [bearer, token] = authHeader.split(' ')
- if (bearer !== 'Bearer') {
- throw new Error(`Auth header is not 'Bearer {token}'`)
- }
- return token
-}
-
-function verifySupabaseJWT(jwt: string) {
- return jwtVerify(jwt, SUPABASE_JWT_KEYS, {
- issuer: SUPABASE_JWT_ISSUER,
- })
-}
-
-Deno.serve(async (req: Request) => {
- if (req.method !== 'OPTIONS') {
- try {
- const token = getAuthToken(req)
- const isValidJWT = await verifySupabaseJWT(token)
-
- if (!isValidJWT) {
- return Response.json(
- { msg: 'Invalid JWT' },
- {
- status: 401,
- }
- )
- }
- } catch (e) {
- console.error(e)
- return Response.json(
- { msg: e.toString() },
- {
- status: 401,
- }
- )
- }
- }
+The following example uses [`jose`](https://jsr.io/@panva/jose) library to verify received JWTs.
- const { name } = await req.json()
- return Response.json({ message: `hello ${name}` })
-})
-```
+</Admonition>
+{/* prettier-ignore */}
+<$CodeSample
+path="/edge-functions/supabase/functions/_shared/jwt/default.ts"
+meta="name=_shared/jwt/default.ts"
+lines={[[3, -1]]}
+/>
+
+<$CodeSample
+path="/edge-functions/supabase/functions/custom-jwt-validation/index.ts"
+meta="name=hello/index.ts"
+lines={[[4, -1]]}
+/>
diff --git a/examples/edge-functions/supabase/functions/_shared/jwt/clerk.ts b/examples/edge-functions/supabase/functions/_shared/jwt/clerk.ts
index 8c170902010..bed6a0b90e3 100644
--- a/examples/edge-functions/supabase/functions/_shared/jwt/clerk.ts
+++ b/examples/edge-functions/supabase/functions/_shared/jwt/clerk.ts
@@ -1,6 +1,6 @@
// Clerk as a third-party provider alongside Supabase Auth.
// Use this template to validate tokens issued by Clerk integration
-import * as jose from "https://deno.land/x/[email protected]/index.ts";
+import * as jose from "jsr:@panva/jose@6";
// Obtain from https://clerk.com/setup/supabase
// Must supply this value from function env
diff --git a/examples/edge-functions/supabase/functions/_shared/jwt/default.ts b/examples/edge-functions/supabase/functions/_shared/jwt/default.ts
index 14928ef881f..06129fd6504 100644
--- a/examples/edge-functions/supabase/functions/_shared/jwt/default.ts
+++ b/examples/edge-functions/supabase/functions/_shared/jwt/default.ts
@@ -1,6 +1,6 @@
// Default supabase JWT verification
// Use this template to validate tokens issued by Supabase default auth
-import * as jose from "https://deno.land/x/[email protected]/index.ts";
+import * as jose from "jsr:@panva/jose@6";
const SUPABASE_JWT_ISSUER = Deno.env.get("SB_JWT_ISSUER") ??
Deno.env.get("SUPABASE_URL") + "/auth/v1";
diff --git a/examples/edge-functions/supabase/functions/_shared/jwt/legacy-jwt.ts b/examples/edge-functions/supabase/functions/_shared/jwt/legacy-jwt.ts
index 5266fd5b385..fda0cb17f52 100644
--- a/examples/edge-functions/supabase/functions/_shared/jwt/legacy-jwt.ts
+++ b/examples/edge-functions/supabase/functions/_shared/jwt/legacy-jwt.ts
@@ -1,7 +1,6 @@
// Legacy supabase JWT verification
// Use this template to validate tokens using the legacy symmetric JWT secret
-
-import * as jose from "https://deno.land/x/[email protected]/index.ts";
+import * as jose from "jsr:@panva/jose@6";
// Automatically supplied by Supabase
const JWT_SECRET = Deno.env.get("JWT_SECRET");