|
| 1 | +import { |
| 2 | + createApp, |
| 3 | + ErrorResponse, |
| 4 | + ForbiddenHttpError, |
| 5 | + HttpStatus, |
| 6 | + NoResponse, |
| 7 | + UnauthorizedHttpError, |
| 8 | +} from "@aklinker1/zeta"; |
| 9 | +import * as cookie from "cookie"; |
| 10 | +import z from "zod"; |
| 11 | +import type { Setter } from "../../src/types"; |
| 12 | + |
| 13 | +// Models |
| 14 | + |
| 15 | +enum UserPermission { |
| 16 | + ListUsers, |
| 17 | +} |
| 18 | + |
| 19 | +const User = z.object({ |
| 20 | + id: z.string(), |
| 21 | + username: z.string().min(2).max(100), |
| 22 | + permissions: z.array(z.enum(UserPermission)), |
| 23 | +}); |
| 24 | +type User = z.infer<typeof User>; |
| 25 | + |
| 26 | +// Hard-code a list of users for the example |
| 27 | + |
| 28 | +const USERS: (User & { password: string })[] = [ |
| 29 | + { |
| 30 | + id: "1", |
| 31 | + username: "aaron", |
| 32 | + permissions: [UserPermission.ListUsers], |
| 33 | + password: "aarons-password", |
| 34 | + }, |
| 35 | + { |
| 36 | + id: "2", |
| 37 | + username: "tom", |
| 38 | + permissions: [], |
| 39 | + password: "toms-password", |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +// Auth plugin |
| 44 | + |
| 45 | +/** |
| 46 | + * Personally, I'm not a fan of classes in JS, but since this will be created |
| 47 | + * every request, it's a better choice compared to a factory function. |
| 48 | + * |
| 49 | + * In JS, creating class instances is faster than calling a factory function |
| 50 | + * that creates an object. |
| 51 | + */ |
| 52 | +class Auth { |
| 53 | + // Store session tokens in memory for the example |
| 54 | + private static sessionIdUserMap: Record<string, User> = Object.create(null); |
| 55 | + |
| 56 | + private SESSION_COOKIE_NAME = "example_session"; |
| 57 | + |
| 58 | + private _sessionId: string | undefined | null = null; |
| 59 | + |
| 60 | + constructor(private ctx: { request: Request; set: Setter }) {} |
| 61 | + |
| 62 | + /** |
| 63 | + * The session ID provided in the cookie. `undefined` if not provided. |
| 64 | + */ |
| 65 | + get sessionId(): string | undefined { |
| 66 | + if (this._sessionId !== null) return this._sessionId; |
| 67 | + |
| 68 | + const cookieString = this.ctx.request.headers.get("Cookie"); |
| 69 | + if (!cookieString) return; |
| 70 | + |
| 71 | + return (this._sessionId = |
| 72 | + cookie.parse(cookieString)[this.SESSION_COOKIE_NAME]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the user if logged in via cookies. |
| 77 | + */ |
| 78 | + getUser(): User | undefined { |
| 79 | + if (!this.sessionId) return; |
| 80 | + return Auth.sessionIdUserMap[this.sessionId]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Require the request be authenticated and return the user. Throws a |
| 85 | + * {@link UnauthorizedHttpError} if the session cookie is missing or invalid. |
| 86 | + */ |
| 87 | + requireUser(): User { |
| 88 | + if (!this.sessionId) throw new UnauthorizedHttpError("Session missing"); |
| 89 | + |
| 90 | + const user = Auth.sessionIdUserMap[this.sessionId]; |
| 91 | + if (!user) { |
| 92 | + this.clearSession(); |
| 93 | + throw new UnauthorizedHttpError("Invalid session"); |
| 94 | + } |
| 95 | + |
| 96 | + return user; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Require the request be authenticated AND the authenticated user have the |
| 101 | + * required permission. |
| 102 | + */ |
| 103 | + requirePermission(permission: UserPermission): User { |
| 104 | + const user = this.requireUser(); |
| 105 | + if (!user.permissions.includes(permission)) throw new ForbiddenHttpError(); |
| 106 | + |
| 107 | + return user; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Create a session, adds the cookie, and returns the session ID. |
| 112 | + */ |
| 113 | + createSession(user: User): string { |
| 114 | + const sessionId = crypto.randomUUID(); |
| 115 | + Auth.sessionIdUserMap[sessionId] = user; |
| 116 | + |
| 117 | + this.ctx.set.headers["Set-Cookie"] = cookie.serialize( |
| 118 | + this.SESSION_COOKIE_NAME, |
| 119 | + sessionId, |
| 120 | + { |
| 121 | + httpOnly: true, |
| 122 | + secure: true, |
| 123 | + sameSite: "strict", |
| 124 | + maxAge: 60 * 60 * 24 * 7, // 7 days |
| 125 | + }, |
| 126 | + ); |
| 127 | + |
| 128 | + return sessionId; |
| 129 | + } |
| 130 | + |
| 131 | + clearSession(): void { |
| 132 | + if (!this.sessionId) return; |
| 133 | + |
| 134 | + delete Auth.sessionIdUserMap[this.sessionId]; |
| 135 | + this.ctx.set.headers["Set-Cookie"] = cookie.serialize( |
| 136 | + this.SESSION_COOKIE_NAME, |
| 137 | + "", |
| 138 | + { |
| 139 | + httpOnly: true, |
| 140 | + secure: true, |
| 141 | + sameSite: "strict", |
| 142 | + maxAge: 0, |
| 143 | + }, |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +const authPlugin = createApp() |
| 149 | + .onTransform((ctx) => ({ |
| 150 | + // Create an auth object for every request containing helper functions for |
| 151 | + // ensuring the request is authorized. |
| 152 | + auth: new Auth(ctx), |
| 153 | + })) |
| 154 | + .export(); |
| 155 | + |
| 156 | +const app = createApp() |
| 157 | + .use(authPlugin) |
| 158 | + .get( |
| 159 | + "/", |
| 160 | + { |
| 161 | + responses: User, |
| 162 | + }, |
| 163 | + ({ auth }) => auth.requireUser(), |
| 164 | + ) |
| 165 | + .get( |
| 166 | + "/users", |
| 167 | + { |
| 168 | + responses: { |
| 169 | + [HttpStatus.Ok]: User.array(), |
| 170 | + [HttpStatus.Forbidden]: ErrorResponse, |
| 171 | + [HttpStatus.Unauthorized]: ErrorResponse, |
| 172 | + }, |
| 173 | + }, |
| 174 | + ({ auth, status }) => { |
| 175 | + auth.requirePermission(UserPermission.ListUsers); |
| 176 | + return status(HttpStatus.Ok, USERS); |
| 177 | + }, |
| 178 | + ) |
| 179 | + .get( |
| 180 | + "/login", |
| 181 | + { |
| 182 | + query: z.object({ |
| 183 | + username: z.string(), |
| 184 | + password: z.string(), |
| 185 | + }), |
| 186 | + responses: { |
| 187 | + [HttpStatus.Found]: NoResponse, |
| 188 | + [HttpStatus.Unauthorized]: ErrorResponse, |
| 189 | + }, |
| 190 | + }, |
| 191 | + ({ query, set, status, auth }) => { |
| 192 | + const user = USERS.find((user) => user.username === query.username); |
| 193 | + |
| 194 | + if (!user) throw new UnauthorizedHttpError("Username not found"); |
| 195 | + if (user.password !== query.password) |
| 196 | + throw new UnauthorizedHttpError("Incorrect password"); |
| 197 | + |
| 198 | + auth.createSession(user); |
| 199 | + set.headers["Location"] = "/"; |
| 200 | + return status(HttpStatus.Found, undefined); |
| 201 | + }, |
| 202 | + ) |
| 203 | + .get( |
| 204 | + "/logout", |
| 205 | + { |
| 206 | + responses: { |
| 207 | + [HttpStatus.Found]: NoResponse, |
| 208 | + }, |
| 209 | + }, |
| 210 | + ({ auth, set, status }) => { |
| 211 | + auth.clearSession(); |
| 212 | + set.headers["Location"] = "/"; |
| 213 | + return status(HttpStatus.Found, undefined); |
| 214 | + }, |
| 215 | + ); |
| 216 | + |
| 217 | +app.listen(3000, () => { |
| 218 | + console.log("Example server started!"); |
| 219 | + console.log(""); |
| 220 | + console.log("Try out using cookies for auth:"); |
| 221 | + console.log( |
| 222 | + "1. Visit http://localhost:3000 and get a 401 since you're not logged in.", |
| 223 | + ); |
| 224 | + console.log( |
| 225 | + "2. Log in with tom and you will be redirected to the homepage to see the user details: http://localhost:3000/login?username=tom&password=toms-password", |
| 226 | + ); |
| 227 | + console.log( |
| 228 | + "3. Try to get the list of users, but see you get a 403 because tom is missing the required permission: http://localhost:3000/users", |
| 229 | + ); |
| 230 | + console.log( |
| 231 | + "4. Log out of Tom's account at http://localhost:3000/logout - You will be redirected back to the homepage with a 401", |
| 232 | + ); |
| 233 | + console.log( |
| 234 | + "5. Log in as aaron: http://localhost:3000/login?username=aaron&password=aarons-password", |
| 235 | + ); |
| 236 | + console.log( |
| 237 | + "6. Now you can access the list of users: http://localhost:3000/users", |
| 238 | + ); |
| 239 | +}); |
0 commit comments