Skip to content

Commit

Permalink
Add auth0 middleware (#21)
Browse files Browse the repository at this point in the history
* Add auth0 middleware

* Remove dev details

---------

Co-authored-by: Jon Breen <[email protected]>
  • Loading branch information
cultpodcasts and cultpodcasts authored May 3, 2024
1 parent 3e10c8b commit 82afd4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ podcasts.sqlite
podcasts.sql
/wrangler.toml
/queries.cmd
.dev.vars
47 changes: 31 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { parseJwt } from '@cfworker/jwt';
import { Hono } from 'hono';
import { cors } from 'hono/cors'
import { stream } from 'hono/streaming'
import { createMiddleware } from 'hono/factory'
import { Bindings } from 'hono/types';

type Env = {
Content: R2Bucket;
Expand All @@ -11,6 +13,8 @@ type Env = {
apikey: string;
apihost: string;
gatewayKey: string;
auth0Issuer: string;
auth0Audience: string;
}

const allowedOrigins: Array<string> = [
Expand All @@ -30,6 +34,27 @@ function getOrigin(origin: string | null | undefined) {

const app = new Hono<{ Bindings: Env }>();

const auth0Middleware = createMiddleware<{
Bindings: Env,
Variables: {
auth0: (payload: any) => any
}
}>(async (c, next) => {
const authorization = c.req.header('Authorization');
const bearer = "Bearer ";
c.set('auth0', (payload) => {})
if (authorization && authorization.startsWith(bearer)) {
const token = authorization.slice(bearer.length);
const result = await parseJwt(token, c.env.auth0Issuer, c.env.auth0Audience);
if (result.valid) {
c.set('auth0', (payload) => result.payload)
} else {
console.log(result.reason);
}
}
await next()
})

app.use('/*', cors({
origin: (origin, c) => {
return getOrigin(origin);
Expand Down Expand Up @@ -58,10 +83,10 @@ app.get('/homepage', async (c) => {

return stream(c, async (stream) => {
stream.onAbort(() => {
console.log('Aborted!')
console.log('Aborted!')
})
await stream.pipe(object.body)
})
})
});

app.post("/search", async (c) => {
Expand Down Expand Up @@ -247,7 +272,8 @@ app.get("/submit", async (c) => {
}
});

app.post("/submit", async (c) => {
app.post("/submit", auth0Middleware, async (c) => {
const auth0Payload = c.var.auth0('payload');
c.header("Cache-Control", "max-age=600");
c.header("Content-Type", "application/json");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
Expand Down Expand Up @@ -281,17 +307,6 @@ app.post("/submit", async (c) => {

export default app;

async function auth(request: Request) {
const jwt = request.headers.get('Authorization');
if (jwt) {
const issuer = 'https://dev-q3x2z6aofdzbjkkf.us.auth0.com/';
const audience = 'https://api.cultpodcasts.com/';

const result = await parseJwt(jwt.slice(7), issuer, audience);
if (!result.valid) {
console.log(result.reason);
} else {
console.log(result.payload); // { iss, sub, aud, iat, exp, ...claims }
}
}
}


0 comments on commit 82afd4c

Please sign in to comment.