-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add has permission decorator * Update README * fixed lint and prettier * Remove * permission, and accept authenticated empty
- Loading branch information
Showing
14 changed files
with
202 additions
and
19 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
2 changes: 1 addition & 1 deletion
2
src/decorators/authenticated.decorator.ts → .../authenticated/authenticated.decorator.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
2 changes: 1 addition & 1 deletion
2
src/decorators/token-types.decorator.ts → ...rs/authenticated/token-types.decorator.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { SetMetadata } from "@nestjs/common"; | ||
import { TOKEN_TYPES_METADATA_KEY } from "../constants"; | ||
import { TOKEN_TYPES_METADATA_KEY } from "../../constants"; | ||
|
||
export const TokenTypes = (...tokenTypes: string[]) => | ||
SetMetadata(TOKEN_TYPES_METADATA_KEY, tokenTypes); |
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,16 @@ | ||
import { applyDecorators, UseGuards } from "@nestjs/common"; | ||
import { PermissionsGuard } from "./permissions.guard"; | ||
import { PermissionsTypes } from "./permissions.types"; | ||
import { AuthGuard } from "../auth.guard"; | ||
|
||
/** | ||
* Allows only requests from users with a token in the given types. | ||
* | ||
* @param permission list of allowed token types | ||
*/ | ||
export function HasPermission(permission: string) { | ||
return applyDecorators( | ||
UseGuards(AuthGuard, PermissionsGuard), | ||
PermissionsTypes(permission), | ||
); | ||
} |
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 { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
Dependencies, | ||
} from "@nestjs/common"; | ||
import { Reflector } from "@nestjs/core"; | ||
import { getRequest } from "../../utils"; | ||
import { PERMISSIONS_METADATA_KEY } from "../../constants"; | ||
|
||
@Dependencies(Reflector) | ||
@Injectable() | ||
export class PermissionsGuard implements CanActivate { | ||
private readonly logger = new Logger(PermissionsGuard.name); | ||
|
||
constructor(private readonly reflector: Reflector) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const user = getRequest(context).user; | ||
|
||
if (!user?.permissions) { | ||
return false; | ||
} | ||
|
||
const permission = (this.reflector.get( | ||
PERMISSIONS_METADATA_KEY, | ||
context.getHandler(), | ||
) || | ||
this.reflector.get( | ||
PERMISSIONS_METADATA_KEY, | ||
context.getClass(), | ||
)) as string; | ||
|
||
return user.permissions.includes(permission); | ||
} | ||
} |
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,5 @@ | ||
import { SetMetadata } from "@nestjs/common"; | ||
import { PERMISSIONS_METADATA_KEY } from "../../constants"; | ||
|
||
export const PermissionsTypes = (permission: string) => | ||
SetMetadata(PERMISSIONS_METADATA_KEY, permission); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { AccessToken } from "./access-token.decorator"; | ||
export { Authenticated } from "./authenticated.decorator"; | ||
export { Authenticated } from "./authenticated/authenticated.decorator"; | ||
export { HasPermission } from "./has-permission/has-permission.decorator"; | ||
export { RefreshCookieInterceptor } from "./refresh-cookie.interceptor"; | ||
export { RefreshToken } from "./refresh-token.decorator"; |
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.