Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrerojosh committed Dec 2, 2020
1 parent f722694 commit 0bba058
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 47 deletions.
118 changes: 87 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "nest-keycloak-connect",
"version": "1.3.0-alpha.03",
"version": "1.3.0",
"description": "keycloak-nodejs-connect module for Nest",
"author": "John Joshua Ferrer <johnjoshuaferrer@disroot.org>",
"contributors": [
"IERomanov <i.e.romanov1997@gmail.com>",
"Jeff Tian <jeff.tian@outlook.com>",
"EFritzsche90"
"EFritzsche90",
"abiodunsulaiman694 <hello@abiodun.dev>"
],
"license": "MIT",
"scripts": {
Expand Down Expand Up @@ -35,7 +36,7 @@
"rxjs": "^6.0.0"
},
"dependencies": {
"keycloak-connect": "^10.0.0"
"keycloak-connect": "^11.0.0"
},
"devDependencies": {
"@nestjs/common": "^7.0.3",
Expand Down
12 changes: 6 additions & 6 deletions src/decorators/unprotected.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export const META_SKIP_AUTH = 'skip-auth';
* @param skipAuth attaches authorization header to user object when `false`, defaults to `true`
*/
export const Unprotected = (skipAuth = true) => {
SetMetadata(META_UNPROTECTED, true);
SetMetadata(META_SKIP_AUTH, skipAuth);
}
SetMetadata(META_UNPROTECTED, true);
SetMetadata(META_SKIP_AUTH, skipAuth);
};

/**
* Alias for `@Unprotected`.
* @since 1.2.0
* @param skipAuth attaches authorization header to user object when `false`, defaults to `true`
*/
export const Public = (skipAuth = true) => {
SetMetadata(META_UNPROTECTED, true);
SetMetadata(META_SKIP_AUTH, skipAuth);
}
SetMetadata(META_UNPROTECTED, true);
SetMetadata(META_SKIP_AUTH, skipAuth);
};
17 changes: 10 additions & 7 deletions src/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import * as KeycloakConnect from 'keycloak-connect';
import { KEYCLOAK_INSTANCE, KEYCLOAK_CONNECT_OPTIONS } from '../constants';
import { KeycloakConnectOptions } from '../interface/keycloak-connect-options.interface';
import { Reflector } from '@nestjs/core';
import { META_SKIP_AUTH, META_UNPROTECTED } from '../decorators/unprotected.decorator';
import {
META_SKIP_AUTH,
META_UNPROTECTED,
} from '../decorators/unprotected.decorator';

/**
* An authentication guard. Will return a 401 unauthorized when it is unable to
Expand All @@ -30,10 +33,10 @@ export class AuthGuard implements CanActivate {
META_UNPROTECTED,
[context.getClass(), context.getHandler()],
);
const skipAuth = this.reflector.getAllAndOverride<boolean>(
META_SKIP_AUTH,
[context.getClass(), context.getHandler()],
);
const skipAuth = this.reflector.getAllAndOverride<boolean>(META_SKIP_AUTH, [
context.getClass(),
context.getHandler(),
]);

// If unprotected is set skip Keycloak authentication
if (isUnprotected && skipAuth) {
Expand All @@ -44,10 +47,10 @@ export class AuthGuard implements CanActivate {
const jwt =
this.extractJwtFromCookie(request.cookies) ??
this.extractJwt(request.headers);
const isInvalidJwt = (jwt === null || jwt === undefined);
const isInvalidJwt = jwt === null || jwt === undefined;

// Auth is not skipped, but no jwt token given, immediate return
if(isUnprotected && isInvalidJwt) {
if (isUnprotected && isInvalidJwt) {
return true;
}

Expand Down

0 comments on commit 0bba058

Please sign in to comment.