Skip to content

Commit

Permalink
start on user lookup middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bradcypert committed Dec 5, 2024
1 parent 34af3a1 commit f1c6217
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/users/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,27 @@ import { InvalidCredentialsError, UserService } from "./user-service.ts";
import { dustService } from "../../main.ts";
import { validateSignup } from "./validators/signup-validator.ts";
import { validateSignIn } from "./validators/signin-validator.ts";
import { UserWithId } from "./user.ts";

interface Request {
user: UserWithId
}

export const registerRoutes = (router: Router) => {

router.use("/", async (ctx, next) => {
const userService = new UserService(dustService.database);

const bearer = ctx.request.headers.get("authorization");
const token = bearer?.split(" ")?.[1];
if (token != null) {
const payload = await userService.validateJWT(token);
ctx.state.user = payload.user;
}

next();
});

router.post("/auth", async (ctx) => {
const userService = new UserService(dustService.database);
try {
Expand Down
15 changes: 8 additions & 7 deletions src/users/user-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Database } from "../../database.ts";
import { addUser, getUserByEmail, createSession } from "./data.ts";
import type { User } from "./user.ts";
import type { User, UserWithId } from "./user.ts";
import { hash, verify } from "@ts-rex/bcrypt";
import * as jose from "https://deno.land/x/[email protected]/index.ts";

Expand Down Expand Up @@ -48,14 +48,15 @@ export class UserService {
this.jwtSecretKey,
{ name: "HMAC", hash: "SHA-256" },
true,
["sign"]
["sign", "verify"]
);
}

private async createJWTForUser(user: Omit<User, "password">): Promise<SignedJWTToken> {
private async createJWTForUser(user: Omit<UserWithId, "password">): Promise<SignedJWTToken> {
const key = await this.getJWTSecret();
const token = await new jose.SignJWT({
user: {
id: user.id,
email: user.email,
displayName: user.displayName,
},
Expand All @@ -70,13 +71,13 @@ export class UserService {
return token;
}

async validateJWT(token: SignedJWTToken): Promise<{user: {email: string, displayName: string}}> {
async validateJWT(token: SignedJWTToken): Promise<{user: {id: number, email: string, displayName: string}}> {
const key = await this.getJWTSecret();
try {
// verify token
const { payload, protectedHeader } = await jose.jwtVerify<{user: {email: string, displayName: string}}>(token, key, {
issuer: "dust-server", // issuer
audience: "dust-client", // audience
const { payload, protectedHeader } = await jose.jwtVerify<{user: {id: number, email: string, displayName: string}}>(token, key, {
issuer: "urn:dust:server", // issuer
audience: "urn:dust:client", // audience
});

return payload;
Expand Down

0 comments on commit f1c6217

Please sign in to comment.