-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): implement anonymous user authentication
refs #247
- Loading branch information
1 parent
ce241e1
commit fa9de2f
Showing
27 changed files
with
276 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from "zod"; | ||
|
||
const envSchema = z.object({ | ||
ANONYMOUS_USER_TOKEN_SECRET: z.string() | ||
}); | ||
|
||
export const envConfig = envSchema.parse(process.env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { envConfig } from "./env.config"; | ||
|
||
export const config = envConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { container, inject } from "tsyringe"; | ||
|
||
import { config } from "@src/auth/config"; | ||
|
||
export const AUTH_CONFIG = "AUTH_CONFIG"; | ||
|
||
container.register(AUTH_CONFIG, { useValue: config }); | ||
|
||
export type AuthConfig = typeof config; | ||
|
||
export const InjectAuthConfig = () => inject(AUTH_CONFIG); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./config.provider"; |
37 changes: 37 additions & 0 deletions
37
apps/api/src/auth/services/auth-token/auth-token.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import jwt from "jsonwebtoken"; | ||
import { singleton } from "tsyringe"; | ||
import { z } from "zod"; | ||
|
||
import { AuthConfig, InjectAuthConfig } from "@src/auth/providers/config.provider"; | ||
|
||
@singleton() | ||
export class AuthTokenService { | ||
private readonly PayloadSchema = z.object({ | ||
sub: z.string(), | ||
type: z.literal("ANONYMOUS") | ||
}); | ||
|
||
constructor(@InjectAuthConfig() private readonly config: AuthConfig) {} | ||
|
||
signTokenFor(input: { userId: string }): string { | ||
return jwt.sign({ sub: input.userId, type: "ANONYMOUS" }, this.config.ANONYMOUS_USER_TOKEN_SECRET); | ||
} | ||
|
||
async getValidUserId(bearer: string): Promise<string | undefined> { | ||
const token = bearer.replace(/^Bearer\s+/i, ""); | ||
const payload = await this.decodeToken(token); | ||
|
||
if (payload) { | ||
jwt.verify(token, this.config.ANONYMOUS_USER_TOKEN_SECRET); | ||
|
||
return payload.sub; | ||
} | ||
} | ||
|
||
private async decodeToken(token: string): Promise<z.infer<typeof this.PayloadSchema> | undefined> { | ||
const payload = jwt.decode(token); | ||
const { success, data } = await this.PayloadSchema.safeParseAsync(payload); | ||
|
||
return success ? data : undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.