Skip to content

Commit

Permalink
refactor: authentication and authorization method decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokicoule committed Dec 8, 2023
1 parent 29c938d commit 1ea7791
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 42 deletions.
20 changes: 18 additions & 2 deletions packages/auth/lib/authentication/authentication.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import { AuthenticationGuard } from "./authentication.guard";
* Decorator for the AuthenticationGuard
* @returns {ClassDecorator} - The decorator
*/
export function Authentication(): ClassDecorator {
return applyDecorators(UseGuards(AuthenticationGuard));
export function Authentication(): ClassDecorator & MethodDecorator {
return (
target: object,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (propertyKey && descriptor) {
// MethodDecorator
return applyDecorators(UseGuards(AuthenticationGuard))(
target,
propertyKey,
descriptor,
);
} else {
// ClassDecorator
return applyDecorators(UseGuards(AuthenticationGuard))(target);
}
};
}
22 changes: 20 additions & 2 deletions packages/auth/lib/authorization/authorization.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import { AuthorizationOptions } from "./authorization.options";
* @returns {ClassDecorator} - The decorator
* @param {AuthorizationOptions} options - The options
*/
export function Authorization(options: AuthorizationOptions): ClassDecorator {
return applyDecorators(UseGuards(AuthorizationGuard(options)));
export function Authorization(
options: AuthorizationOptions,
): ClassDecorator & MethodDecorator {
return (
target: object,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (propertyKey && descriptor) {
// MethodDecorator
return applyDecorators(UseGuards(AuthorizationGuard(options)))(
target,
propertyKey,
descriptor,
);
} else {
// ClassDecorator
return applyDecorators(UseGuards(AuthorizationGuard(options)))(target);
}
};
}
5 changes: 2 additions & 3 deletions packages/auth/tests/dolphin/dolphin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Authentication, AuthorizationGuard } from "../../lib";
import { Controller, Get, UseGuards } from "@nestjs/common";
import { AuthorizationGuard } from "../../lib";

@Controller("dolphin")
@Authentication()
export class DolphinController {
@Get("flipper")
@UseGuards(AuthorizationGuard(["dolphin"]))
Expand All @@ -16,7 +15,7 @@ export class DolphinController {
@UseGuards(
AuthorizationGuard({
prohibitedGroups: ["shark"],
})
}),
)
getPosition() {
return {
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/tests/manta/manta.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Authorization } from "../../lib";
import { Controller, Get } from "@nestjs/common";

@Controller("manta")
@Authorization({
requiredGroups: ["manta"],
prohibitedGroups: ["dolphin", "shark", "whale"],
})
export class MantaController {
@Get("ray")
@Authorization({
requiredGroups: ["manta"],
prohibitedGroups: ["dolphin", "shark", "whale"],
})
getRay() {
return {
message: "Ray",
Expand Down
29 changes: 18 additions & 11 deletions packages/graphql/lib/authentication/authentication.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import { applyDecorators, UseGuards } from "@nestjs/common";
import { AuthenticationGuard } from "./authentication.guard";

/**
* @deprecated This decorator is deprecated. Use GqlAuthentication instead. This decorator will be removed in the next major release.
* Decorator for the AuthenticationGuard
* @returns {ClassDecorator} - The decorator
*/
export function Authentication(): ClassDecorator {
return applyDecorators(UseGuards(AuthenticationGuard));
}

/**
* Decorator for the AuthenticationGuard
* @returns {ClassDecorator} - The decorator
*/
export function GqlAuthentication(): ClassDecorator {
return applyDecorators(UseGuards(AuthenticationGuard));
export function GqlAuthentication(): ClassDecorator & MethodDecorator {
return (
target: object,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (propertyKey && descriptor) {
// MethodDecorator
return applyDecorators(UseGuards(AuthenticationGuard))(
target,
propertyKey,
descriptor,
);
} else {
// ClassDecorator
return applyDecorators(UseGuards(AuthenticationGuard))(target);
}
};
}
32 changes: 19 additions & 13 deletions packages/graphql/lib/authorization/authorization.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ import { applyDecorators, UseGuards } from "@nestjs/common";
import { AuthorizationGuard } from "./authorization.guard";
import { AuthorizationOptions } from "@nestjs-cognito/auth";

/**
* @deprecated This decorator is deprecated. Use GqlAuthorization instead. This decorator will be removed in the next major release.
* Decorator for the AuthorizationGuard
* @returns {ClassDecorator} - The decorator
* @param {AuthorizationOptions} options - The options
*/
export function Authorization(options: AuthorizationOptions): ClassDecorator {
return applyDecorators(UseGuards(AuthorizationGuard(options)));
}

/**
* Decorator for the AuthorizationGuard
* @returns {ClassDecorator} - The decorator
* @param {AuthorizationOptions} options - The options
*/
export function GqlAuthorization(
options: AuthorizationOptions
): ClassDecorator {
return applyDecorators(UseGuards(AuthorizationGuard(options)));
options: AuthorizationOptions,
): ClassDecorator & MethodDecorator {
return (
target: object,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (propertyKey && descriptor) {
// MethodDecorator
return applyDecorators(UseGuards(AuthorizationGuard(options)))(
target,
propertyKey,
descriptor,
);
} else {
// ClassDecorator
return applyDecorators(UseGuards(AuthorizationGuard(options)))(target);
}
};
}
5 changes: 2 additions & 3 deletions packages/graphql/tests/dolphin/dolphin.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { UseGuards } from "@nestjs/common";
import { Query, Resolver } from "@nestjs/graphql";
import { Authentication, AuthorizationGuard } from "../../lib";
import { AuthorizationGuard } from "../../lib";
import { Response } from "../common/response.dto";

@Resolver(() => Response)
@Authentication()
export class DolphinResolver {
@Query(() => Response)
@UseGuards(AuthorizationGuard(["dolphin"]))
Expand All @@ -18,7 +17,7 @@ export class DolphinResolver {
@UseGuards(
AuthorizationGuard({
prohibitedGroups: ["shark"],
})
}),
)
getPosition(): Response {
return {
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql/tests/manta/manta.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { GqlAuthorization } from "../../lib";
import { Response } from "../common/response.dto";

@Resolver(() => Response)
@GqlAuthorization({
requiredGroups: ["manta"],
prohibitedGroups: ["dolphin", "shark", "whale"],
})
export class MantaResolver {
@GqlAuthorization({
requiredGroups: ["manta"],
prohibitedGroups: ["dolphin", "shark", "whale"],
})
@Query(() => Response)
getRay(): Response {
return {
Expand Down

0 comments on commit 1ea7791

Please sign in to comment.