class

NestjsAuthTagger

Inherits FrameworkTagger < FileHelper < Tagger < Reference < Object

Constants

APPLY_DECORATORS = /\bapplyDecorators\s*\(/

A project decorator that bundles a guard, as the Nest docs' Auth() recipe does: export function Auth() { return applyDecorators( UseGuards(AuthGuard), ApiBearerAuth()) }.

AUTH_DECORATORS = [{/\A@ApiBearerAuth\s*\(/, "NestJS @ApiBearerAuth (Swagger)"}, {/\A@ApiBasicAuth\s*\(/, "NestJS @ApiBasicAuth (Swagger)"}, {/\A@ApiOAuth2\s*\(/, "NestJS @ApiOAuth2 (Swagger)"}, {/\A@ApiSecurity\s*\(/, "NestJS @ApiSecurity (Swagger)"}]

NestJS auth-related decorators

AUTH_GUARD_NAME = /Auth|Jwt|JWT|Session|AccessToken/

Global guards are as often throttling or role checks; only a guard named for authentication counts.

AUTH_GUARD_REF = /\b\w*[Aa]uth\w*Guard\b/
AUTH_START = /switchToHttp|getRequest|super\s*\.\s*canActivate|GqlExecutionContext|getArgs/

Authentication starts once the guard touches the request or hands over to Passport; a return true after that (if (!roles) return true) admits only callers who already logged in.

AUTHZ_KEY = /admin|role|permission|scope|polic/i

ponytail: a name heuristic for what the guard hands on to the auth service (admin, permission, roles are checked after login); reading that service is the upgrade.

BARE_USE_GUARDS = /\bUseGuards\s*\(/
CAN_ACTIVATE = /\bcanActivate\s*\(/

What a canActivate does with a caller: throws 401 outright; throws 403 or returns false (Nest answers 403) on what it read from the request; or only ever returns true.

CLASS_DECL = /\bclass\s+(\w+)\b/
FLAG_PAIR = /(\w+)\s*:\s*([^,}]+)/
GLOBAL_GUARD_PATTERNS = [/provide:\s*APP_GUARD\s*,\s*use(?:Class|Existing):\s*(\w+)/, /use(?:Class|Existing):\s*(\w+)\s*,\s*provide:\s*APP_GUARD/, /useGlobalGuards\s*\(\s*new\s+(\w+)/]

A guard registered for every route: { provide: APP_GUARD, useClass: JwtAuthGuard } in a module, or app.useGlobalGuards(new JwtAuthGuard()). The Nest docs' authentication recipe does exactly this and opts routes out with @Public().

GUARD_CLASS_DECL = /\bclass\s+(\w+)\b([^{]*)\{/
GUARD_PATTERNS = [{/\A@UseGuards\s*\(\s*AuthGuard/, "NestJS @UseGuards(AuthGuard)"}, {/\A@UseGuards\s*\(\s*JwtAuthGuard/, "NestJS @UseGuards(JwtAuthGuard)"}, {/\A@UseGuards\s*\(\s*LocalAuthGuard/, "NestJS @UseGuards(LocalAuthGuard)"}, {/\A@UseGuards\s*\(\s*AuthenticationGuard/, "NestJS @UseGuards(AuthenticationGuard)"}, {/\A@UseGuards\s*\([^)]*?\b\w*[Aa]uth\w*Guard/, "NestJS auth guard"}, {/\A@UseGuards\s*\(\s*GqlAuthGuard/, "NestJS GraphQL auth guard"}, {/\A@Security\s*\(/, "@Security decorator"}, {/\A@Authorized\s*\(/, "@Authorized decorator"}, {/\A@(?:Authenticate|UseAuth)\s*\(/, "@Authenticate decorator"}]

NestJS authentication guards — verify who the caller is.

HANDLE_REQUEST = /\bhandleRequest\s*(?:<[^>]*>)?\s*\([^)]*\)[^{]*\{/

A Passport guard's handleRequest override that hands back no user (return user || null) where the stock one throws Unauthorized: the guard authenticates whoever presents credentials and lets everyone else through, so it does not guard the route.

NAMED_IMPORT = /\bimport\s+(?:type\s+)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/
NEXT_TOP_LEVEL = /\n(?=[^\s})\]])/

The start of the next top-level statement after a declaration: a line that starts in column 0 with anything but a closing bracket.

NO_USER_RETURN = /\breturn\s+(?:null|undefined)\b|(?:\|\||\?\?)\s*(?:null|undefined)\b/
PACKAGE_JSON = /\Apackage\.json\z/
PASSPORT_CALL = /\bAuthGuard\s*\(/

Its strategy list: AuthGuard('google'), AuthGuard(['jwt', 'api-key']).

PASSPORT_GUARD = /\AAuthGuard\s*\(|\bextends\s+AuthGuard\s*\(/

Passport's AuthGuard(...) (called inline, or extended) refuses a caller its strategy cannot authenticate with 401.

PASSPORT_LOGIN_STRATEGY = /\A(?:local|google|google-oauth20?|github2?|gitlab|microsoft|azuread-openidconnect|facebook|twitter|linkedin|apple|discord|slack|saml|oauth2?|openidconnect|oidc)\z/i

A strategy that signs a caller in rather than checking one: a password form or an OAuth/OIDC/SAML provider's redirect. Exact names: local_apikey checks a key, google-id-token, auth0 and magic-link verify a token.

PUBLIC_PATTERNS = [/\A@Public\s*\(\)/, /\A@SkipAuth\s*\(\)/, /\A@AllowAnonymous\s*\(\)/, /\A@NoSecurity\s*\(\)/, /\A@SetMetadata\s*\(\s*['"]isPublic['"]/]

Public/skip auth markers (negative signal)

READS_REQUEST = /getRequest|switchToHttp|\bheaders\b|\bcookies\b|\.user\b|\bsession\b/
REFLECTOR_READ = /reflector\s*\.\s*get(?:AllAndOverride|AllAndMerge)?\s*(?:<[^(]*?>)?\s*\(\s*([\w.$'"]+)/

What a global guard does with route metadata, read from its source: the metadata keys it reads through Reflector, and the if (isPublic) return true / if (options.public) return true / if (!options) return true branches that let a request through before any authentication runs.

RETURN_STMT = /\breturn\b\s*([^;\n]*)/
RETURNS_REFUSE = /\breturn\s+(?:false\b|!|[^;\n]*?(?:===|!==|==|!=))/
ROLE_PATTERNS = [{/\A@UseGuards\s*\([\s\S]*\bRolesGuard\b/, "NestJS @UseGuards(RolesGuard)"}, {/\A@Roles\s*\(/, "NestJS @Roles decorator"}, {/\A@Permissions\s*\(/, "NestJS @Permissions decorator"}, {/\A@RequirePermissions\s*\(/, "NestJS @RequirePermissions decorator"}, {/\A@SetMetadata\s*\(\s*['"]roles['"]/, "NestJS role metadata"}]

NestJS authorization decorators/guards — verify what the caller may do. Distinct from authentication so JwtAuthGuard + @Roles stack as auth_guard + authz_guard rather than collapsing into a single auth tag.

SET_METADATA = /SetMetadata\s*\(\s*([\w.$'"]+)\s*,\s*([^)]*?)\s*\)/

export const Public = () => SetMetadata(IS_PUBLIC_KEY, true), or a decorator that hands its own argument over, as Authenticated = (options = {}) => applyDecorators(SetMetadata(KEY, options), ...).

SKIP_BRANCH = /if\s*\(\s*(!?)\s*(\w+)\??(?:\.(\w+))?\s*\)\s*\{?\s*return\s+true\b/
STRATEGY_LITERAL = /\A['"`]([^'"`$]+)['"`]\z/

One literal strategy name.

TEST_PATH = /\/(?:tests?|__tests__|e2e)\/|[.-](?:spec|test)\.[cm]?[jt]sx?\z/

Test harnesses (test/app.e2e-spec.ts, *.spec.ts) bootstrap the app with their own guards, which must not leak onto real routes.

THROWS_401 = /\bUnauthorizedException\b|\bHttpException\s*\([^;]*(?:\b401\b|UNAUTHORIZED)/
THROWS_403 = /\bForbiddenException\b|\bHttpException\s*\([^;]*(?:\b403\b|FORBIDDEN)/
TOP_LEVEL_DECL = /^(?:export\s+)?(?:const|function)\s+(\w+)\s*(?::[^=(]*)?(?:=\s*(?:async\s*)?)?\(\s*(?:\.\.\.)?(\w*)(?:\s*:[^=,)]*)?(?:\s*=\s*([^,)]+))?/m
USE_GUARDS_CALL = /\A@UseGuards\s*\(/

Constructors

new(options : Hash(String, YAML::Any))
Source

Class methods

target_techs
Source

Instance methods

perform(endpoints : Array(Endpoint)) : Array(Endpoint)

The per-endpoint shape: look at each endpoint, tag in place, hand the array back. Taggers declare only check_endpoint; those that need a pass over the whole set first (a project pre-scan, mcp's legacy prefixes) override perform and call super or loop themselves.

Source

Nested types