Comparing version 0.2.1 to 0.3.0
interface IPrincipal { | ||
id: string; | ||
policyVersion?: any; | ||
policyVersion?: unknown; | ||
roles: string[]; | ||
attr?: { | ||
[key: string]: any; | ||
[key: string]: unknown; | ||
}; | ||
@@ -19,3 +19,3 @@ } | ||
resource: { | ||
policyVersion?: any; | ||
policyVersion?: unknown; | ||
kind: string; | ||
@@ -25,3 +25,3 @@ instances: { | ||
attr?: { | ||
[key: string]: any; | ||
[key: string]: unknown; | ||
}; | ||
@@ -34,16 +34,6 @@ }; | ||
} | ||
export interface ICerbosBatchAuthorizeResource { | ||
actions: string[]; | ||
resource: { | ||
policyVersion?: any; | ||
kind: string; | ||
id: string; | ||
attr: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
export declare enum ValidationErrorSource { | ||
SOURCE_RESOURCE = "SOURCE_RESOURCE", | ||
SOURCE_PRINCIPAL = "SOURCE_PRINCIPAL" | ||
} | ||
export interface ICerbosBatchAuthorizeResult { | ||
[key: string]: AuthorizeEffect; | ||
} | ||
export declare enum AuthorizeEffect { | ||
@@ -61,2 +51,3 @@ ALLOW = "EFFECT_ALLOW", | ||
logLevel?: "fatal" | "error" | "warn" | "info" | "debug"; | ||
handleValidationErrors?: "error" | "log" | false; | ||
playgroundInstance?: string; | ||
@@ -68,5 +59,6 @@ } | ||
private playgroundInstance?; | ||
constructor({ hostname, logLevel, playgroundInstance }: CerbosOptions); | ||
private handleValidationErrors?; | ||
constructor({ hostname, logLevel, playgroundInstance, handleValidationErrors, }: CerbosOptions); | ||
check(data: IAuthorize): Promise<ICerbosResponse>; | ||
} | ||
export {}; |
@@ -34,6 +34,42 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Cerbos = exports.AuthorizationError = exports.AuthorizeEffect = void 0; | ||
exports.Cerbos = exports.AuthorizationError = exports.AuthorizeEffect = exports.ValidationErrorSource = void 0; | ||
const isomorphic_unfetch_1 = __importDefault(require("isomorphic-unfetch")); | ||
const uuid_1 = require("uuid"); | ||
const winston = __importStar(require("winston")); | ||
var ValidationErrorSource; | ||
(function (ValidationErrorSource) { | ||
ValidationErrorSource["SOURCE_RESOURCE"] = "SOURCE_RESOURCE"; | ||
ValidationErrorSource["SOURCE_PRINCIPAL"] = "SOURCE_PRINCIPAL"; | ||
})(ValidationErrorSource = exports.ValidationErrorSource || (exports.ValidationErrorSource = {})); | ||
// export interface ICerbosBatchAuthorizeResource { | ||
// actions: string[]; | ||
// resource: { | ||
// policyVersion?: unknown; | ||
// kind: string; | ||
// id: string; | ||
// attr: { | ||
// [key: string]: unknown; | ||
// }; | ||
// }; | ||
// } | ||
// export interface ICerbosBatchAuthorizeResult { | ||
// [key: string]: AuthorizeEffect; | ||
// } | ||
// interface IBatchAuthorize { | ||
// principal: IPrincipal; | ||
// resources: ICerbosBatchAuthorizeResource[]; | ||
// } | ||
// interface IAuthorizeBatchResponse { | ||
// requestID: string; | ||
// results: { | ||
// resourceId: string; | ||
// actions: ICerbosBatchAuthorizeResult; | ||
// }[]; | ||
// } | ||
// interface ICerbosBatchResponse { | ||
// resourceId: string; | ||
// actions: { | ||
// [action: string]: AuthorizeEffect; | ||
// }; | ||
// } | ||
var AuthorizeEffect; | ||
@@ -64,5 +100,6 @@ (function (AuthorizeEffect) { | ||
class Cerbos { | ||
constructor({ hostname, logLevel, playgroundInstance }) { | ||
constructor({ hostname, logLevel, playgroundInstance, handleValidationErrors, }) { | ||
this.host = hostname; | ||
this.playgroundInstance = playgroundInstance; | ||
this.handleValidationErrors = handleValidationErrors; | ||
this.log = winston.createLogger({ | ||
@@ -83,4 +120,2 @@ level: logLevel, | ||
this.log.debug("Cerbos.check Payload", payload); | ||
// eslint-disable-next-line prefer-const | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let headers = { | ||
@@ -92,2 +127,4 @@ "Content-Type": "application/json", | ||
} | ||
let resp; | ||
// Fetch Data | ||
try { | ||
@@ -99,5 +136,4 @@ const response = yield (0, isomorphic_unfetch_1.default)(`${this.host}/api/check`, { | ||
}); | ||
const data = yield response.json(); | ||
this.log.info("Cerbos.check: Response", data); | ||
return new CerbosResponseWrapper(data); | ||
resp = yield response.json(); | ||
this.log.info("Cerbos.check: Response", resp); | ||
} | ||
@@ -108,2 +144,19 @@ catch (e) { | ||
} | ||
// Handle Validation Errors | ||
if (this.handleValidationErrors) { | ||
const validationErrors = resp.resourceInstances | ||
? Object.values(resp.resourceInstances) | ||
.map((resource) => resource.validationErrors) | ||
.flat() | ||
: []; | ||
if (validationErrors.length > 0) { | ||
if (this.handleValidationErrors === "error") { | ||
throw new AuthorizationError(`Validation Error: ${JSON.stringify(validationErrors)}`); | ||
} | ||
else { | ||
this.log.error("Cerbos.check: Validation Errors", validationErrors); | ||
} | ||
} | ||
} | ||
return new CerbosResponseWrapper(resp); | ||
}); | ||
@@ -110,0 +163,0 @@ } |
interface IPrincipal { | ||
id: string; | ||
policyVersion?: any; | ||
policyVersion?: unknown; | ||
roles: string[]; | ||
attr?: { | ||
[key: string]: any; | ||
[key: string]: unknown; | ||
}; | ||
@@ -19,3 +19,3 @@ } | ||
resource: { | ||
policyVersion?: any; | ||
policyVersion?: unknown; | ||
kind: string; | ||
@@ -25,3 +25,3 @@ instances: { | ||
attr?: { | ||
[key: string]: any; | ||
[key: string]: unknown; | ||
}; | ||
@@ -34,16 +34,6 @@ }; | ||
} | ||
export interface ICerbosBatchAuthorizeResource { | ||
actions: string[]; | ||
resource: { | ||
policyVersion?: any; | ||
kind: string; | ||
id: string; | ||
attr: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
export declare enum ValidationErrorSource { | ||
SOURCE_RESOURCE = "SOURCE_RESOURCE", | ||
SOURCE_PRINCIPAL = "SOURCE_PRINCIPAL" | ||
} | ||
export interface ICerbosBatchAuthorizeResult { | ||
[key: string]: AuthorizeEffect; | ||
} | ||
export declare enum AuthorizeEffect { | ||
@@ -61,2 +51,3 @@ ALLOW = "EFFECT_ALLOW", | ||
logLevel?: "fatal" | "error" | "warn" | "info" | "debug"; | ||
handleValidationErrors?: "error" | "log" | false; | ||
playgroundInstance?: string; | ||
@@ -68,5 +59,6 @@ } | ||
private playgroundInstance?; | ||
constructor({ hostname, logLevel, playgroundInstance }: CerbosOptions); | ||
private handleValidationErrors?; | ||
constructor({ hostname, logLevel, playgroundInstance, handleValidationErrors, }: CerbosOptions); | ||
check(data: IAuthorize): Promise<ICerbosResponse>; | ||
} | ||
export {}; |
@@ -13,2 +13,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import * as winston from "winston"; | ||
export var ValidationErrorSource; | ||
(function (ValidationErrorSource) { | ||
ValidationErrorSource["SOURCE_RESOURCE"] = "SOURCE_RESOURCE"; | ||
ValidationErrorSource["SOURCE_PRINCIPAL"] = "SOURCE_PRINCIPAL"; | ||
})(ValidationErrorSource || (ValidationErrorSource = {})); | ||
// export interface ICerbosBatchAuthorizeResource { | ||
// actions: string[]; | ||
// resource: { | ||
// policyVersion?: unknown; | ||
// kind: string; | ||
// id: string; | ||
// attr: { | ||
// [key: string]: unknown; | ||
// }; | ||
// }; | ||
// } | ||
// export interface ICerbosBatchAuthorizeResult { | ||
// [key: string]: AuthorizeEffect; | ||
// } | ||
// interface IBatchAuthorize { | ||
// principal: IPrincipal; | ||
// resources: ICerbosBatchAuthorizeResource[]; | ||
// } | ||
// interface IAuthorizeBatchResponse { | ||
// requestID: string; | ||
// results: { | ||
// resourceId: string; | ||
// actions: ICerbosBatchAuthorizeResult; | ||
// }[]; | ||
// } | ||
// interface ICerbosBatchResponse { | ||
// resourceId: string; | ||
// actions: { | ||
// [action: string]: AuthorizeEffect; | ||
// }; | ||
// } | ||
export var AuthorizeEffect; | ||
@@ -38,5 +74,6 @@ (function (AuthorizeEffect) { | ||
export class Cerbos { | ||
constructor({ hostname, logLevel, playgroundInstance }) { | ||
constructor({ hostname, logLevel, playgroundInstance, handleValidationErrors, }) { | ||
this.host = hostname; | ||
this.playgroundInstance = playgroundInstance; | ||
this.handleValidationErrors = handleValidationErrors; | ||
this.log = winston.createLogger({ | ||
@@ -57,4 +94,2 @@ level: logLevel, | ||
this.log.debug("Cerbos.check Payload", payload); | ||
// eslint-disable-next-line prefer-const | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let headers = { | ||
@@ -66,2 +101,4 @@ "Content-Type": "application/json", | ||
} | ||
let resp; | ||
// Fetch Data | ||
try { | ||
@@ -73,5 +110,4 @@ const response = yield fetch(`${this.host}/api/check`, { | ||
}); | ||
const data = yield response.json(); | ||
this.log.info("Cerbos.check: Response", data); | ||
return new CerbosResponseWrapper(data); | ||
resp = yield response.json(); | ||
this.log.info("Cerbos.check: Response", resp); | ||
} | ||
@@ -82,4 +118,21 @@ catch (e) { | ||
} | ||
// Handle Validation Errors | ||
if (this.handleValidationErrors) { | ||
const validationErrors = resp.resourceInstances | ||
? Object.values(resp.resourceInstances) | ||
.map((resource) => resource.validationErrors) | ||
.flat() | ||
: []; | ||
if (validationErrors.length > 0) { | ||
if (this.handleValidationErrors === "error") { | ||
throw new AuthorizationError(`Validation Error: ${JSON.stringify(validationErrors)}`); | ||
} | ||
else { | ||
this.log.error("Cerbos.check: Validation Errors", validationErrors); | ||
} | ||
} | ||
} | ||
return new CerbosResponseWrapper(resp); | ||
}); | ||
} | ||
} |
{ | ||
"name": "cerbos", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://cerbos.dev", | ||
@@ -5,0 +5,0 @@ "description": "Cerbos helps you super-charge your authorization implementation by writing context-aware access control policies for your application resources.", |
@@ -17,2 +17,3 @@ # Cerbos Node SDK | ||
- [Hostname (required)](#hostname-required) | ||
- [Validation Errors](#validation-errors) | ||
- [Logging](#logging) | ||
@@ -111,2 +112,14 @@ - [Documentation](#documentation) | ||
### Validation Errors | ||
If you have [schema support](https://docs.cerbos.dev/cerbos/latest/policies/schemas.html) enabled on your Cerbos PDP then validation errors can be surfaced in the SDK also. To do this set the `handleValidationErrors` configuration value to either `log` for validation errors to be logged to console or `error` for an exception to be thrown should any validation errors occur. | ||
```js | ||
const cerbos = new Cerbos({ | ||
hostname: "http://localhost:9090", // The Cerbos PDP instance | ||
handleValidationErrors: 'log', // or 'error' | ||
logLevel: "error", | ||
}); | ||
``` | ||
### Logging | ||
@@ -113,0 +126,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31388
411
137
1