Skip to content

Commit

Permalink
Add access-token decorator, resolves #145
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrerojosh committed Oct 18, 2024
1 parent d7bd9b5 commit 8de426d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ Here is the decorators you can use in your controllers.
| Decorator | Description |
| ---------------- | --------------------------------------------------------------------------------------------------------- |
| @KeycloakUser | Retrieves the current Keycloak logged-in user. (must be per method, unless controller is request scoped.) |
| @AccessToken | Retrieves the access token used in the request |
| @EnforcerOptions | Keycloak enforcer options. |
| @Public | Allow any user to use the route. |
| @Resource | Keycloak application resource name. |
Expand Down
13 changes: 13 additions & 0 deletions src/decorators/access-token.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { extractRequest } from '../util';

/**
* Retrieves the currently used access token
* @since 2.0.0
*/
export const AccessToken = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const [req] = extractRequest(ctx);
return req.accessToken;
},
);
3 changes: 2 additions & 1 deletion src/keycloak-connect.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
import { KeycloakMultiTenantService } from './services/keycloak-multitenant.service';

export * from './constants';
export * from './decorators/keycloak-user.decorator';
export * from './decorators/access-token.decorator';
export * from './decorators/enforcer-options.decorator';
export * from './decorators/keycloak-user.decorator';
export * from './decorators/public.decorator';
export * from './decorators/resource.decorator';
export * from './decorators/roles.decorator';
Expand Down

0 comments on commit 8de426d

Please sign in to comment.