Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lahirumaramba/edge_token_verifier
Experimental token verifier that works on the Edge
https://github.com/lahirumaramba/edge_token_verifier
Last synced: 27 days ago
JSON representation
Experimental token verifier that works on the Edge
- Host: GitHub
- URL: https://github.com/lahirumaramba/edge_token_verifier
- Owner: lahirumaramba
- License: apache-2.0
- Created: 2023-01-25T02:46:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-29T17:50:45.000Z (almost 2 years ago)
- Last Synced: 2024-12-10T02:36:27.586Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 135 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# edge_token_verifier
**Experimental token verifier that works on the Edge.**
Hello there! This is an early stage experimental Deno module to verify tokens on
edge runtimes. We are also experimenting with
[`dnt`](https://github.com/denoland/dnt) to create the the Node.js compatible
module.We are currently testing the module on different edge runtimes. The current
releases are only for testing purposes.```js
import { AppCheckTokenVerifier } from 'https://deno.land/x/[email protected]/mod.ts';
```## Node.js
```js
import { AppCheckTokenVerifier } from '@lahirumaramba/edge-token-verifier';
```# Examples on the Edge
## Netlify Edge Functions
```js
// examples/netlify-edge/netlify/edge-functions/hello.ts
import { AppCheckTokenVerifier } from 'https://deno.land/x/[email protected]/mod.ts';export default async (request: Request) => {
const appCheckToken = request.headers.get('X-Firebase-AppCheck');
const appCheckClaims = await verifyAppCheckToken(appCheckToken);if (!appCheckClaims) {
return Response.json(
{ message: 'Unauthorized access. Invalid App Check token.' },
{ status: 401, headers: { "content-type": "application/json" } },
);
}
return new Response(`Hello world Netlify Edge: App:${appCheckClaims.app_id}`);
};const tokenVerifier = new AppCheckTokenVerifier();
const verifyAppCheckToken = async (appCheckToken: string | null) => {
if (!appCheckToken) {
return null;
}
try {
return await tokenVerifier.verify(appCheckToken, 'project-id');
} catch (_err) {
return null;
}
};export const config = { path: '/api' };
```## Vercel Edge Functions (Next.js Middleware)
```js
// examples/edge-token-vc/middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { AppCheckTokenVerifier } from '@lahirumaramba/edge-token-verifier';export async function middleware(request: NextRequest) {
const verifyAppCheckToken = async (appCheckToken: string | null) => {
if (!appCheckToken) {
return null;
}
const tokenVerifier = new AppCheckTokenVerifier();
try {
return await tokenVerifier.verify(appCheckToken, 'project-id');
} catch (_err) {
return null;
}
};
const appCheckToken = request.headers.get('X-Firebase-AppCheck');
const appCheckClaims = await verifyAppCheckToken(appCheckToken);if (!appCheckClaims) {
return NextResponse.json(
{ message: 'Unauthorized access. Invalid App Check token.' },
{ status: 401, headers: { 'content-type': 'application/json' } },
);
}
return NextResponse.next();
}export const config = {
matcher: '/api/hello/:path*',
};
```