Comparing version 0.0.1 to 0.0.2-alpha
@@ -52,3 +52,3 @@ interface IPrincipal { | ||
} | ||
export default class Cerbos { | ||
export declare class Cerbos { | ||
private host; | ||
@@ -55,0 +55,0 @@ private log; |
152
lib/index.js
@@ -1,28 +0,1 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -37,62 +10,25 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthorizationError = exports.AuthorizeEffect = void 0; | ||
var node_fetch_1 = require("node-fetch"); | ||
var uuid_1 = require("uuid"); | ||
var winston = require("winston"); | ||
var AuthorizeEffect; | ||
import fetch from "node-fetch"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import * as winston from "winston"; | ||
export var AuthorizeEffect; | ||
(function (AuthorizeEffect) { | ||
AuthorizeEffect["ALLOW"] = "EFFECT_ALLOW"; | ||
AuthorizeEffect["DENY"] = "EFFECT_DENY"; | ||
})(AuthorizeEffect = exports.AuthorizeEffect || (exports.AuthorizeEffect = {})); | ||
var AuthorizationError = /** @class */ (function (_super) { | ||
__extends(AuthorizationError, _super); | ||
function AuthorizationError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return AuthorizationError; | ||
}(Error)); | ||
exports.AuthorizationError = AuthorizationError; | ||
var CerbosResponseWrapper = /** @class */ (function () { | ||
function CerbosResponseWrapper(resp) { | ||
})(AuthorizeEffect || (AuthorizeEffect = {})); | ||
export class AuthorizationError extends Error { | ||
} | ||
class CerbosResponseWrapper { | ||
constructor(resp) { | ||
this.resp = resp; | ||
} | ||
CerbosResponseWrapper.prototype.isAuthorized = function (resourceKey, action) { | ||
isAuthorized(resourceKey, action) { | ||
var _a; | ||
var allowed = ((_a = this.resp.resourceInstances[resourceKey]) === null || _a === void 0 ? void 0 : _a.actions[action]) == | ||
const allowed = ((_a = this.resp.resourceInstances[resourceKey]) === null || _a === void 0 ? void 0 : _a.actions[action]) == | ||
AuthorizeEffect.ALLOW; | ||
return allowed !== null && allowed !== void 0 ? allowed : false; | ||
}; | ||
return CerbosResponseWrapper; | ||
}()); | ||
var Cerbos = /** @class */ (function () { | ||
function Cerbos(_a) { | ||
var hostname = _a.hostname, logLevel = _a.logLevel, _b = _a.timeout, timeout = _b === void 0 ? 0 : _b; | ||
} | ||
} | ||
export class Cerbos { | ||
constructor({ hostname, logLevel, timeout = 0 }) { | ||
this.host = hostname; | ||
@@ -110,38 +46,24 @@ this.timeout = timeout; | ||
} | ||
Cerbos.prototype.check = function (data) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var payload, response, data_1, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.log.info("Cerbos.check", data); | ||
payload = __assign(__assign({ requestId: uuid_1.v4() }, data), { resource: __assign({ policyVersion: data.resource.policyVersion || "default" }, data.resource), principal: __assign({ policyVersion: data.principal.policyVersion || "default" }, data.principal) }); | ||
this.log.debug("Cerbos.check Payload", payload); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 4, , 5]); | ||
return [4 /*yield*/, node_fetch_1.default(this.host + "/api/check", { | ||
method: "post", | ||
body: JSON.stringify(payload), | ||
headers: { "Content-Type": "application/json" }, | ||
timeout: this.timeout, | ||
})]; | ||
case 2: | ||
response = _a.sent(); | ||
return [4 /*yield*/, response.json()]; | ||
case 3: | ||
data_1 = _a.sent(); | ||
this.log.info("Cerbos.check: Response", data_1); | ||
return [2 /*return*/, new CerbosResponseWrapper(data_1)]; | ||
case 4: | ||
e_1 = _a.sent(); | ||
this.log.error("Cerbos.check Error", e_1); | ||
throw new AuthorizationError("Could not connect to Cerbos PDP at " + this.host); | ||
case 5: return [2 /*return*/]; | ||
} | ||
}); | ||
check(data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.log.info("Cerbos.check", data); | ||
const payload = Object.assign(Object.assign({ requestId: uuidv4() }, data), { resource: Object.assign({ policyVersion: data.resource.policyVersion || "default" }, data.resource), principal: Object.assign({ policyVersion: data.principal.policyVersion || "default" }, data.principal) }); | ||
this.log.debug("Cerbos.check Payload", payload); | ||
try { | ||
const response = yield fetch(`${this.host}/api/check`, { | ||
method: "post", | ||
body: JSON.stringify(payload), | ||
headers: { "Content-Type": "application/json" }, | ||
timeout: this.timeout, | ||
}); | ||
const data = yield response.json(); | ||
this.log.info("Cerbos.check: Response", data); | ||
return new CerbosResponseWrapper(data); | ||
} | ||
catch (e) { | ||
this.log.error("Cerbos.check Error", e); | ||
throw new AuthorizationError(`Could not connect to Cerbos PDP at ${this.host}`); | ||
} | ||
}); | ||
}; | ||
return Cerbos; | ||
}()); | ||
exports.default = Cerbos; | ||
} | ||
} |
{ | ||
"name": "cerbos", | ||
"version": "0.0.1", | ||
"version": "0.0.2-alpha", | ||
"homepage": "https://cerbos.dev", | ||
"description": "Cerbos helps you super-charge your authorization implementation by writing context-aware access control policies for your application resources.", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"main": "./lib/cjs/index.js", | ||
"module": "./lib/esm/index.js", | ||
"files": [ | ||
@@ -12,3 +12,3 @@ "lib/**/*" | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json", | ||
"test": "jest --config jestconfig.json", | ||
@@ -15,0 +15,0 @@ "prepare": "npm run build", |
@@ -39,3 +39,3 @@ # Cerbos Node SDK | ||
```js | ||
import Cerbos from "cerbos"; | ||
import { Cerbos } from "cerbos"; | ||
@@ -42,0 +42,0 @@ const cerbos = new Cerbos({ |
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
Network access
Supply chain riskThis module accesses the network.
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
30101
9
408
3