Skip to content

Commit

Permalink
refactor: channels -> sesisons
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Mar 25, 2024
1 parent 4fc586c commit 65f2bd5
Show file tree
Hide file tree
Showing 159 changed files with 787 additions and 817 deletions.
12 changes: 7 additions & 5 deletions apps/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "@farcaster/relay",
"version": "0.0.9",
"description": "Farcaster Auth relay server",
"private": true,
"type": "commonjs",
"type": "module",
"main": "./dist/esm/exports/index.js",
"types": "./dist/types/exports/index.d.ts",
"typings": "./dist/types/exports/index.d.ts",
"scripts": {
"dev": "dotenv -e .env -c development tsx watch src/index.ts",
"build": "pnpm run clean && pnpm run build:esm+types",
Expand All @@ -22,16 +24,16 @@
"@farcaster/core": "^0.13.8",
"@fastify/rate-limit": "^9.1.0",
"ethers": "^6.11.1",
"hono": "^4.1.3",
"hono": "^4.1.4",
"hono-rate-limiter": "^0.2.2",
"ioredis": "^5.3.2",
"siwe": "^2.1.4",
"viem": "^2.8.18",
"viem": "^2.9.0",
"zod": "^3.22.4",
"zod-validation-error": "^3.0.3"
},
"devDependencies": {
"dotenv-cli": "^7.1.0",
"dotenv-cli": "^7.4.1",
"jest": "^29.7.0",
"tsx": "^4.7.1"
}
Expand Down
65 changes: 0 additions & 65 deletions apps/relay/src/channels.ts

This file was deleted.

97 changes: 0 additions & 97 deletions apps/relay/src/connect.ts

This file was deleted.

2 changes: 2 additions & 0 deletions apps/relay/src/exports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "../types/actions.js";
export * from "../types/session.js";
12 changes: 5 additions & 7 deletions apps/relay/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { channels } from "./channels";
import { healthcheck } from "./healthcheck";
import { getConfig } from "./utils/getConfig";
import { connect } from "./connect";
import { session } from "./session.js";
import { healthcheck } from "./healthcheck.js";
import { getConfig } from "./utils/getConfig.js";

const config = getConfig();

export const app = new Hono().basePath("/v1");
export const app = new Hono().basePath("/v2");

app.use(cors({ origin: config.corsOrigin }));

app.route("/healthcheck", healthcheck);
app.route("/channels", channels);
app.route("/connect", connect);
app.route("/session", session);
8 changes: 4 additions & 4 deletions apps/relay/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { createMiddleware } from "hono/factory";

export type AuthVariables = {
channelToken: string;
sessionToken: string;
};

export const auth = createMiddleware<{ Variables: AuthVariables }>(async (c, next) => {
const auth = c.req.header("authorization");

const channelToken = auth?.split(" ")[1];
if (!channelToken) {
const sessionToken = auth?.split(" ")[1];
if (!sessionToken) {
c.status(401);
return c.json({ error: "Unauthorized" });
}
c.set("channelToken", channelToken);
c.set("sessionToken", sessionToken);
return next();
});
76 changes: 0 additions & 76 deletions apps/relay/src/middlewares/channels.ts

This file was deleted.

58 changes: 58 additions & 0 deletions apps/relay/src/middlewares/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createMiddleware } from "hono/factory";
import { Redis } from "ioredis";
import { getConfig } from "../utils/getConfig.js";
import { RelayError } from "../utils/errors.js";
import { type Session } from "../types/session.js";

const config = getConfig();

export type SessionVariables = {
session: {
get: (token: string) => Promise<Session>;
update: (token: string, channels: Session) => Promise<void>;
create: (token: string) => Promise<void>;
delete: (token: string) => Promise<void>;
};
};

const { sessionTtl } = getConfig();

export const session = createMiddleware<{ Variables: SessionVariables }>(async (c, next) => {
const redis = new Redis(config.redisUrl);

c.set("session", {
get: async (token) => {
try {
const serializedSession = await redis.get(token);
if (!serializedSession) throw new RelayError("not_found", "Session not found");
return JSON.parse(serializedSession);
} catch (e) {
if (e instanceof RelayError) throw e;

throw new RelayError("unknown", e as Error);
}
},
create: async (token) => {
try {
await redis.set(token, JSON.stringify({}), "EX", sessionTtl);
} catch (e) {
throw new RelayError("unknown", e as Error);
}
},
update: async (token, channels) => {
try {
await redis.set(token, JSON.stringify(channels), "EX", sessionTtl);
} catch (e) {
throw new RelayError("unknown", e as Error);
}
},
delete: async (token) => {
try {
await redis.del(token);
} catch (e) {
throw new RelayError("unknown", e as Error);
}
},
});
await next();
});
13 changes: 13 additions & 0 deletions apps/relay/src/schemas/sessionAuthenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Hex } from "viem";
import { z } from "zod";

export const sessionAuthenticateSchema = z.object({
message: z.string() /*.url()?*/,
signature: z.custom<Hex>((val) => /^0x[a-fA-F0-9]{130}$/.test(val as string)),
fid: z.number(),
username: z.string().regex(/^[a-z0-9][a-z0-9-]{0,15}$|^[a-z0-9][a-z0-9-]{0,15}\\.eth$/),
bio: z.string(),
displayName: z.string(),
pfpUrl: z.string().url(),
});
export type SessionAuthenticateParameters = z.infer<typeof sessionAuthenticateSchema>;
Loading

0 comments on commit 65f2bd5

Please sign in to comment.