nest-keycloak-connect
Advanced tools
Comparing version 1.8.3-alpha.0 to 1.8.3
@@ -10,5 +10,5 @@ "use strict"; | ||
*/ | ||
exports.AuthenticatedUser = (0, common_1.createParamDecorator)((data, ctx) => { | ||
const [req] = (0, util_1.extractRequest)(ctx); | ||
exports.AuthenticatedUser = common_1.createParamDecorator((data, ctx) => { | ||
const [req] = util_1.extractRequest(ctx); | ||
return req.user; | ||
}); |
@@ -11,3 +11,3 @@ "use strict"; | ||
*/ | ||
const EnforcerOptions = (opts) => (0, common_1.SetMetadata)(exports.META_ENFORCER_OPTIONS, opts); | ||
const EnforcerOptions = (opts) => common_1.SetMetadata(exports.META_ENFORCER_OPTIONS, opts); | ||
exports.EnforcerOptions = EnforcerOptions; |
@@ -12,3 +12,3 @@ "use strict"; | ||
*/ | ||
const Unprotected = (skipAuth = true) => (0, common_1.applyDecorators)((0, common_1.SetMetadata)(exports.META_UNPROTECTED, true), (0, common_1.SetMetadata)(exports.META_SKIP_AUTH, skipAuth)); | ||
const Unprotected = (skipAuth = true) => common_1.applyDecorators(common_1.SetMetadata(exports.META_UNPROTECTED, true), common_1.SetMetadata(exports.META_SKIP_AUTH, skipAuth)); | ||
exports.Unprotected = Unprotected; | ||
@@ -20,3 +20,3 @@ /** | ||
*/ | ||
const Public = (skipAuth = true) => (0, common_1.applyDecorators)((0, common_1.SetMetadata)(exports.META_UNPROTECTED, true), (0, common_1.SetMetadata)(exports.META_SKIP_AUTH, skipAuth)); | ||
const Public = (skipAuth = true) => common_1.applyDecorators(common_1.SetMetadata(exports.META_UNPROTECTED, true), common_1.SetMetadata(exports.META_SKIP_AUTH, skipAuth)); | ||
exports.Public = Public; |
@@ -10,3 +10,3 @@ "use strict"; | ||
*/ | ||
const Resource = (resource) => (0, common_1.SetMetadata)(exports.META_RESOURCE, resource); | ||
const Resource = (resource) => common_1.SetMetadata(exports.META_RESOURCE, resource); | ||
exports.Resource = Resource; |
@@ -11,3 +11,3 @@ "use strict"; | ||
*/ | ||
const Roles = (roleMetaData) => (0, common_1.SetMetadata)(exports.META_ROLES, roleMetaData); | ||
const Roles = (roleMetaData) => common_1.SetMetadata(exports.META_ROLES, roleMetaData); | ||
exports.Roles = Roles; |
@@ -10,3 +10,3 @@ "use strict"; | ||
*/ | ||
const Scopes = (...scopes) => (0, common_1.SetMetadata)(exports.META_SCOPES, scopes); | ||
const Scopes = (...scopes) => common_1.SetMetadata(exports.META_SCOPES, scopes); | ||
exports.Scopes = Scopes; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -80,3 +76,3 @@ if (k2 === undefined) k2 = k; | ||
// Extract request/response | ||
const [request] = (0, util_1.extractRequest)(context); | ||
const [request] = util_1.extractRequest(context); | ||
// if is not an HTTP request ignore this guard | ||
@@ -99,7 +95,7 @@ if (!request) { | ||
this.logger.verbose(`User JWT: ${jwt}`); | ||
const keycloak = (0, util_1.useKeycloak)(request, jwt, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const keycloak = yield util_1.useKeycloak(request, jwt, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const isValidToken = yield this.validateToken(keycloak, jwt); | ||
if (isValidToken) { | ||
// Attach user info object | ||
request.user = (0, util_1.parseToken)(jwt); | ||
request.user = util_1.parseToken(jwt); | ||
// Attach raw access token JWT extracted from bearer/cookie | ||
@@ -169,6 +165,6 @@ request.accessTokenJWT = jwt; | ||
AuthGuard = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__param(0, (0, common_1.Inject)(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, (0, common_1.Inject)(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, (0, common_1.Inject)(constants_1.KEYCLOAK_LOGGER)), | ||
common_1.Injectable(), | ||
__param(0, common_1.Inject(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, common_1.Inject(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, common_1.Inject(constants_1.KEYCLOAK_LOGGER)), | ||
__metadata("design:paramtypes", [Object, Object, common_1.Logger, | ||
@@ -175,0 +171,0 @@ keycloak_multitenant_service_1.KeycloakMultiTenantService, |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -105,3 +101,3 @@ if (k2 === undefined) k2 = k; | ||
// Extract request/response | ||
const [request, response] = (0, util_1.extractRequest)(context); | ||
const [request, response] = util_1.extractRequest(context); | ||
// if is not an HTTP request ignore this guard | ||
@@ -117,3 +113,3 @@ if (!request) { | ||
const enforcerFn = createEnforcerContext(request, response, enforcerOpts); | ||
const keycloak = (0, util_1.useKeycloak)(request, request.accessTokenJWT, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const keycloak = yield util_1.useKeycloak(request, request.accessTokenJWT, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const isAllowed = yield enforcerFn(keycloak, permissions); | ||
@@ -132,6 +128,6 @@ // If statement for verbose logging only | ||
ResourceGuard = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__param(0, (0, common_1.Inject)(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, (0, common_1.Inject)(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, (0, common_1.Inject)(constants_1.KEYCLOAK_LOGGER)), | ||
common_1.Injectable(), | ||
__param(0, common_1.Inject(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, common_1.Inject(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, common_1.Inject(constants_1.KEYCLOAK_LOGGER)), | ||
__metadata("design:paramtypes", [Object, Object, common_1.Logger, | ||
@@ -138,0 +134,0 @@ keycloak_multitenant_service_1.KeycloakMultiTenantService, |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -79,3 +75,3 @@ if (k2 === undefined) k2 = k; | ||
// Extract request | ||
const [request] = (0, util_1.extractRequest)(context); | ||
const [request] = util_1.extractRequest(context); | ||
const { accessTokenJWT } = request; | ||
@@ -92,3 +88,3 @@ // if is not an HTTP request ignore this guard | ||
// Create grant | ||
const keycloak = (0, util_1.useKeycloak)(request, request.accessTokenJWT, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const keycloak = yield util_1.useKeycloak(request, request.accessTokenJWT, this.singleTenant, this.multiTenant, this.keycloakOpts); | ||
const grant = yield keycloak.grantManager.createGrant({ | ||
@@ -114,6 +110,6 @@ access_token: accessTokenJWT, | ||
RoleGuard = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__param(0, (0, common_1.Inject)(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, (0, common_1.Inject)(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, (0, common_1.Inject)(constants_1.KEYCLOAK_LOGGER)), | ||
common_1.Injectable(), | ||
__param(0, common_1.Inject(constants_1.KEYCLOAK_INSTANCE)), | ||
__param(1, common_1.Inject(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__param(2, common_1.Inject(constants_1.KEYCLOAK_LOGGER)), | ||
__metadata("design:paramtypes", [Object, Object, common_1.Logger, | ||
@@ -120,0 +116,0 @@ keycloak_multitenant_service_1.KeycloakMultiTenantService, |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,0 +6,0 @@ if (k2 === undefined) k2 = k; |
@@ -8,4 +8,4 @@ import { LogLevel } from '@nestjs/common'; | ||
export interface MultiTenantOptions { | ||
realmResolver: (request: any) => string; | ||
realmSecretResolver?: (realm: string) => string; | ||
realmResolver: (request: any) => Promise<string> | string; | ||
realmSecretResolver?: (realm: string) => Promise<string> | string; | ||
} | ||
@@ -12,0 +12,0 @@ /** |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -60,3 +56,3 @@ if (k2 === undefined) k2 = k; | ||
const keycloakConnectProviders = [ | ||
(0, keycloak_connect_providers_1.createKeycloakConnectOptionProvider)(opts, config), | ||
keycloak_connect_providers_1.createKeycloakConnectOptionProvider(opts, config), | ||
keycloak_connect_providers_1.loggerProvider, | ||
@@ -116,4 +112,4 @@ keycloak_connect_providers_1.keycloakProvider, | ||
KeycloakConnectModule = KeycloakConnectModule_1 = __decorate([ | ||
(0, common_1.Module)({}) | ||
common_1.Module({}) | ||
], KeycloakConnectModule); | ||
exports.KeycloakConnectModule = KeycloakConnectModule; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,0 +6,0 @@ if (k2 === undefined) k2 = k; |
{ | ||
"name": "nest-keycloak-connect", | ||
"version": "1.8.3-alpha.0", | ||
"version": "1.8.3", | ||
"description": "keycloak-nodejs-connect module for Nest", | ||
@@ -5,0 +5,0 @@ "author": "John Joshua Ferrer <johnjoshuaferrer@disroot.org>", |
@@ -19,3 +19,3 @@ import KeycloakConnect from 'keycloak-connect'; | ||
*/ | ||
get(realm: string): KeycloakConnect.Keycloak; | ||
get(realm: string): Promise<KeycloakConnect.Keycloak>; | ||
} |
@@ -14,2 +14,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -43,37 +52,45 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
get(realm) { | ||
var _a, _b; | ||
if (this.instances.has(realm)) { | ||
return this.instances.get(realm); | ||
} | ||
else { | ||
if (typeof this.keycloakOpts === 'string') { | ||
throw new Error('Keycloak configuration is a configuration path. This should not happen after module load.'); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.instances.has(realm)) { | ||
return this.instances.get(realm); | ||
} | ||
// Resolve realm secret | ||
const realmSecret = (_b = (_a = this.keycloakOpts.multiTenant) === null || _a === void 0 ? void 0 : _a.realmSecretResolver) === null || _b === void 0 ? void 0 : _b.call(_a, realm); | ||
// Override secret | ||
// Order of priority: resolved realm secret > default global secret | ||
const secret = realmSecret || this.keycloakOpts.secret; | ||
// TODO: Repeating code from provider, will need to rework this in 2.0 | ||
// Override realm and secret | ||
const keycloakOpts = Object.assign(this.keycloakOpts, { | ||
realm, | ||
secret, | ||
}); | ||
const keycloak = new keycloak_connect_1.default({}, keycloakOpts); | ||
// The most important part | ||
keycloak.accessDenied = (req, res, next) => { | ||
req.resourceDenied = true; | ||
next(); | ||
}; | ||
this.instances.set(realm, keycloak); | ||
return keycloak; | ||
} | ||
else { | ||
if (typeof this.keycloakOpts === 'string') { | ||
throw new Error('Keycloak configuration is a configuration path. This should not happen after module load.'); | ||
} | ||
if (this.keycloakOpts.multiTenant === null || | ||
this.keycloakOpts.multiTenant === undefined) { | ||
throw new Error('Multi tenant is not defined yet multi tenant service is being called.'); | ||
} | ||
// Resolve realm secret | ||
const resolvedRealmSecret = this.keycloakOpts.multiTenant.realmSecretResolver(realm); | ||
const realmSecret = resolvedRealmSecret || resolvedRealmSecret instanceof Promise | ||
? yield resolvedRealmSecret | ||
: resolvedRealmSecret; | ||
// Override secret | ||
// Order of priority: resolved realm secret > default global secret | ||
const secret = realmSecret || this.keycloakOpts.secret; | ||
// TODO: Repeating code from provider, will need to rework this in 2.0 | ||
// Override realm and secret | ||
const keycloakOpts = Object.assign(this.keycloakOpts, { | ||
realm, | ||
secret, | ||
}); | ||
const keycloak = new keycloak_connect_1.default({}, keycloakOpts); | ||
// The most important part | ||
keycloak.accessDenied = (req, res, next) => { | ||
req.resourceDenied = true; | ||
next(); | ||
}; | ||
this.instances.set(realm, keycloak); | ||
return keycloak; | ||
} | ||
}); | ||
} | ||
}; | ||
KeycloakMultiTenantService = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__param(0, (0, common_1.Inject)(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
common_1.Injectable(), | ||
__param(0, common_1.Inject(constants_1.KEYCLOAK_CONNECT_OPTIONS)), | ||
__metadata("design:paramtypes", [Object]) | ||
], KeycloakMultiTenantService); | ||
exports.KeycloakMultiTenantService = KeycloakMultiTenantService; |
@@ -5,4 +5,4 @@ import { ExecutionContext } from '@nestjs/common'; | ||
import { KeycloakMultiTenantService } from './services/keycloak-multitenant.service'; | ||
export declare const useKeycloak: (request: any, jwt: string, singleTenant: KeycloakConnect.Keycloak, multiTenant: KeycloakMultiTenantService, opts: KeycloakConnectConfig) => KeycloakConnect.Keycloak; | ||
export declare const useKeycloak: (request: any, jwt: string, singleTenant: KeycloakConnect.Keycloak, multiTenant: KeycloakMultiTenantService, opts: KeycloakConnectConfig) => Promise<KeycloakConnect.Keycloak>; | ||
export declare const extractRequest: (context: ExecutionContext) => [any, any]; | ||
export declare const parseToken: (token: string) => any; |
20
util.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,14 +15,15 @@ exports.parseToken = exports.extractRequest = exports.useKeycloak = void 0; | ||
// TODO: Rework in 2.0 | ||
const useKeycloak = (request, jwt, singleTenant, multiTenant, opts) => { | ||
const useKeycloak = (request, jwt, singleTenant, multiTenant, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (opts.multiTenant && opts.multiTenant.realmResolver) { | ||
const resolvedRealm = opts.multiTenant.realmResolver(request); | ||
return multiTenant.get(resolvedRealm); | ||
const realm = resolvedRealm instanceof Promise ? yield resolvedRealm : resolvedRealm; | ||
return yield multiTenant.get(realm); | ||
} | ||
else if (!opts.realm) { | ||
const payload = (0, exports.parseToken)(jwt); | ||
const payload = exports.parseToken(jwt); | ||
const issuerRealm = payload.iss.split('/').pop(); | ||
return multiTenant.get(issuerRealm); | ||
return yield multiTenant.get(issuerRealm); | ||
} | ||
return singleTenant; | ||
}; | ||
}); | ||
exports.useKeycloak = useKeycloak; | ||
@@ -20,0 +30,0 @@ const extractRequest = (context) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
77602
1445
0
1