-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): implements basic anonymous user auth
refs #247
- Loading branch information
1 parent
fa1f252
commit 339da48
Showing
29 changed files
with
624 additions
and
168 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
Empty file.
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 { Ability } from "@casl/ability"; | ||
import template from "lodash/template"; | ||
import { singleton } from "tsyringe"; | ||
|
||
@singleton() | ||
export class AbilityService { | ||
private readonly createRegularUserRules = template( | ||
JSON.stringify([ | ||
{ action: ["create", "read", "sign"], subject: "UserWallet", conditions: { userId: "${user.id}" } }, | ||
{ action: "read", subject: "User", conditions: { id: "${user.id}" } } | ||
]) | ||
); | ||
private readonly createRegularAnonymousUserRules = template( | ||
JSON.stringify([ | ||
{ action: ["create", "read", "sign"], subject: "UserWallet", conditions: { userId: "${user.id}" } }, | ||
{ action: "read", subject: "User", conditions: { id: "${user.id}", userId: null } } | ||
]) | ||
); | ||
|
||
getAbilityForUser(user: { userId: string }) { | ||
const rules = this.createRegularUserRules({ user }); | ||
return new Ability(JSON.parse(rules)); | ||
} | ||
|
||
getAbilityForAnonymousUser(user: { id: string }) { | ||
const rules = this.createRegularAnonymousUserRules({ user }); | ||
return new Ability(JSON.parse(rules)); | ||
} | ||
|
||
getEmptyAbility() { | ||
return new Ability([]); | ||
} | ||
|
||
getSuperUserAbility() { | ||
return new Ability([{ action: "manage", subject: "all" }]); | ||
} | ||
} |
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,48 @@ | ||
import { Context, Next } from "hono"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { AbilityService } from "@src/auth/services/ability/ability.service"; | ||
import { AuthService } from "@src/auth/services/auth.service"; | ||
import { ExecutionContextService } from "@src/core/services/execution-context/execution-context.service"; | ||
import type { HonoInterceptor } from "@src/core/types/hono-interceptor.type"; | ||
import { getCurrentUserId } from "@src/middlewares/userMiddleware"; | ||
import { UserRepository } from "@src/user/repositories"; | ||
|
||
@singleton() | ||
export class AuthInterceptor implements HonoInterceptor { | ||
constructor( | ||
private readonly abilityService: AbilityService, | ||
private readonly userRepository: UserRepository, | ||
private readonly executionContextService: ExecutionContextService, | ||
private readonly authService: AuthService | ||
) {} | ||
|
||
intercept() { | ||
return async (c: Context, next: Next) => { | ||
const userId = getCurrentUserId(c); | ||
|
||
if (userId) { | ||
const currentUser = await this.userRepository.findByUserId(userId); | ||
|
||
this.authService.currentUser = currentUser; | ||
this.authService.ability = currentUser ? this.abilityService.getAbilityForUser(currentUser) : this.abilityService.getEmptyAbility(); | ||
|
||
return await next(); | ||
} | ||
const anonymousUserId = c.req.header("x-anonymous-user-id"); | ||
|
||
if (anonymousUserId) { | ||
const currentUser = await this.userRepository.findAnonymousById(anonymousUserId); | ||
|
||
this.authService.currentUser = currentUser; | ||
this.authService.ability = currentUser ? this.abilityService.getAbilityForAnonymousUser(currentUser) : this.abilityService.getEmptyAbility(); | ||
|
||
return await next(); | ||
} | ||
|
||
this.authService.ability = this.abilityService.getEmptyAbility(); | ||
|
||
return await next(); | ||
}; | ||
} | ||
} |
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,53 @@ | ||
import { Ability } from "@casl/ability"; | ||
import assert from "http-assert"; | ||
import { container, Lifecycle, scoped } from "tsyringe"; | ||
|
||
import { ExecutionContextService } from "@src/core/services/execution-context/execution-context.service"; | ||
import { UserOutput } from "@src/user/repositories"; | ||
|
||
@scoped(Lifecycle.ResolutionScoped) | ||
export class AuthService { | ||
constructor(private readonly executionContextService: ExecutionContextService) {} | ||
|
||
set currentUser(user: UserOutput) { | ||
this.executionContextService.set("CURRENT_USER", user); | ||
} | ||
|
||
get currentUser(): UserOutput { | ||
return this.executionContextService.get("CURRENT_USER"); | ||
} | ||
|
||
set ability(ability: Ability) { | ||
this.executionContextService.set("ABILITY", ability); | ||
} | ||
|
||
get ability(): Ability { | ||
return this.executionContextService.get("ABILITY"); | ||
} | ||
|
||
get isAuthenticated(): boolean { | ||
return !!this.currentUser; | ||
} | ||
|
||
throwUnlessCan(action: string, subject: string) { | ||
assert(this.ability.can(action, subject), 403); | ||
} | ||
} | ||
|
||
export const Protected = (rules?: { action: string; subject: string }[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { | ||
const originalMethod = descriptor.value; | ||
|
||
descriptor.value = function protectedFunction(...args: any[]) { | ||
const authService = container.resolve(AuthService); | ||
|
||
assert(authService.isAuthenticated, 401); | ||
|
||
if (rules) { | ||
rules.forEach(rule => authService.throwUnlessCan(rule.action, rule.subject)); | ||
} | ||
|
||
return originalMethod.apply(this, args); | ||
}; | ||
|
||
return descriptor; | ||
}; |
18 changes: 11 additions & 7 deletions
18
apps/api/src/billing/controllers/wallet/wallet.controller.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
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.