@entur-partner/permission-client-node
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -41,14 +41,14 @@ "use strict"; | ||
var ValueObjects_1 = require("../ValueObjects"); | ||
var CacheStorage_1 = require("./CacheStorage"); | ||
var InMemoryCache = /** @class */ (function () { | ||
function InMemoryCache(applicationPermission, permissionDeliverRepository) { | ||
this.isInitialized = false; | ||
this.isRegistered = false; | ||
this.lastRefresh = null; | ||
this.businessCapabilityMap = new Map(); | ||
this.responsibilityMap = new Map(); | ||
this.permissionRepository = permissionDeliverRepository; | ||
this.applicationPermission = applicationPermission; | ||
this.cacheStorage = new CacheStorage_1.CacheStorage(this.permissionRepository.getRefreshRate()); | ||
this.scheduleRefresh(); | ||
} | ||
InMemoryCache.prototype.checkBusinessCapabilityPermission = function (authoritySubject, businessCapability) { | ||
var tenantPermissions = Array.from(this.findBusinessCapabilityPermissions(authoritySubject)); | ||
var tenantPermissions = Array.from(this.cacheStorage.getBusinessCapabilityPermissions(authoritySubject)); | ||
for (var _i = 0, tenantPermissions_1 = tenantPermissions; _i < tenantPermissions_1.length; _i++) { | ||
@@ -63,3 +63,3 @@ var tenantPermission = tenantPermissions_1[_i]; | ||
InMemoryCache.prototype.checkResponsibilitySetPermission = function (authoritySubject, responsibilitySet) { | ||
var tenantPermissions = Array.from(this.findBusinessCapabilityPermissions(authoritySubject)); | ||
var tenantPermissions = Array.from(this.cacheStorage.getBusinessCapabilityPermissions(authoritySubject)); | ||
for (var _i = 0, tenantPermissions_2 = tenantPermissions; _i < tenantPermissions_2.length; _i++) { | ||
@@ -78,3 +78,3 @@ var tenantPermission = tenantPermissions_2[_i]; | ||
var permissions = []; | ||
var tenantPermissions = this.findBusinessCapabilityPermissions(authoritySubject); | ||
var tenantPermissions = this.cacheStorage.getBusinessCapabilityPermissions(authoritySubject); | ||
tenantPermissions.forEach(function (tenantPermission) { | ||
@@ -89,3 +89,3 @@ permissions.push({ | ||
}); | ||
tenantPermissions = this.findResponsibilityPermissions(authoritySubject); | ||
tenantPermissions = this.cacheStorage.getResponsibilityPermissions(authoritySubject); | ||
tenantPermissions.forEach(function (tenantPermission) { | ||
@@ -104,15 +104,12 @@ permissions.push({ | ||
return __awaiter(this, void 0, void 0, function () { | ||
var businessCapabilityMap, responsibilityMap, lastChanged, refresh, tenantPermissions; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var lastChanged, refresh, _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
businessCapabilityMap = new Map(); | ||
responsibilityMap = new Map(); | ||
if (!!this.isInitialized) return [3 /*break*/, 2]; | ||
if (!!this.isRegistered) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.permissionRepository.registerPermissions(this.applicationPermission)]; | ||
case 1: | ||
_a.sent(); | ||
this.isInitialized = true; | ||
_a.label = 2; | ||
_d.sent(); | ||
this.isRegistered = true; | ||
_d.label = 2; | ||
case 2: | ||
@@ -122,36 +119,14 @@ if (!(this.lastRefresh != null)) return [3 /*break*/, 4]; | ||
case 3: | ||
lastChanged = _a.sent(); | ||
lastChanged = _d.sent(); | ||
if (lastChanged.getTime() < this.lastRefresh) { | ||
return [2 /*return*/]; | ||
} | ||
_a.label = 4; | ||
_d.label = 4; | ||
case 4: | ||
refresh = Date.now(); | ||
_b = (_a = this.cacheStorage).set; | ||
_c = [this.cacheStorage.getCurrentEpochSecond()]; | ||
return [4 /*yield*/, this.permissionRepository.getPermissions()]; | ||
case 5: | ||
tenantPermissions = _a.sent(); | ||
tenantPermissions.forEach(function (tenantPermission) { | ||
var cacheMapKey = _this.getCacheMapKey({ | ||
subject: tenantPermission.subject, | ||
authority: tenantPermission.authority, | ||
}); | ||
if (tenantPermission.responsibilityType == null) { | ||
var cacheMapElement = businessCapabilityMap.get(cacheMapKey); | ||
if (cacheMapElement == null) { | ||
cacheMapElement = new Set(); | ||
businessCapabilityMap.set(cacheMapKey, cacheMapElement); | ||
} | ||
cacheMapElement.add(tenantPermission); | ||
} | ||
else { | ||
var cacheMapElement = responsibilityMap.get(cacheMapKey); | ||
if (cacheMapElement == null) { | ||
cacheMapElement = new Set(); | ||
responsibilityMap.set(cacheMapKey, cacheMapElement); | ||
} | ||
cacheMapElement.add(tenantPermission); | ||
} | ||
}); | ||
this.businessCapabilityMap = businessCapabilityMap; | ||
this.responsibilityMap = responsibilityMap; | ||
_b.apply(_a, _c.concat([_d.sent()])); | ||
this.lastRefresh = refresh; | ||
@@ -163,19 +138,2 @@ return [2 /*return*/]; | ||
}; | ||
InMemoryCache.prototype.getCacheMapKey = function (authoritySubject) { | ||
return authoritySubject.subject + '^' + authoritySubject.authority; | ||
}; | ||
InMemoryCache.prototype.findBusinessCapabilityPermissions = function (authoritySubject) { | ||
if (!this.isInitialized) { | ||
throw new Error('In Memory Cache is not initialized. Please make sure that refresh is called before use.'); | ||
} | ||
var permissions = this.businessCapabilityMap.get(this.getCacheMapKey(authoritySubject)); | ||
return permissions == null ? new Set() : permissions; | ||
}; | ||
InMemoryCache.prototype.findResponsibilityPermissions = function (authoritySubject) { | ||
if (!this.isInitialized) { | ||
throw new Error('In Memory Cache is not initialized. Please make sure that refresh is called before use.'); | ||
} | ||
var permissions = this.responsibilityMap.get(this.getCacheMapKey(authoritySubject)); | ||
return permissions == null ? new Set() : permissions; | ||
}; | ||
InMemoryCache.prototype.scheduleRefresh = function () { | ||
@@ -182,0 +140,0 @@ var _this = this; |
@@ -42,2 +42,25 @@ declare module '@entur-partner/permission-client-node/ValueObjects' { | ||
} | ||
declare module '@entur-partner/permission-client-node/cache/CacheStorage' { | ||
import { AuthoritySubject } from '@entur-partner/permission-client-node/jwt/JwtDecoder'; | ||
import { TenantPermission } from '@entur-partner/permission-client-node/ValueObjects'; | ||
export class CacheStorage { | ||
private refreshRate; | ||
private startEpochSecond; | ||
private nextOperationPermissionsMap; | ||
private nextObjectPermissionsMap; | ||
private operationPermissionsMap; | ||
private objectPermissionsMap; | ||
constructor(refreshRate: number); | ||
isPreparing(): boolean; | ||
getCurrentEpochSecond(): number; | ||
getBusinessCapabilityPermissions(authoritySubject: AuthoritySubject): Set<TenantPermission>; | ||
getResponsibilityPermissions(authoritySubject: AuthoritySubject): Set<TenantPermission>; | ||
set(currentEpochSecond: number, tenantPermissions: TenantPermission[]): void; | ||
private put; | ||
private nextCacheMap; | ||
private getPermissionSet; | ||
private getCacheMapKey; | ||
} | ||
} | ||
declare module '@entur-partner/permission-client-node/cache/InMemoryCache' { | ||
@@ -54,8 +77,7 @@ import { AuthoritySubject } from '@entur-partner/permission-client-node/jwt/JwtDecoder'; | ||
export class InMemoryCache implements AuthorizeCache { | ||
private isInitialized; | ||
private isRegistered; | ||
private lastRefresh; | ||
private businessCapabilityMap; | ||
private responsibilityMap; | ||
private readonly permissionRepository; | ||
private readonly applicationPermission; | ||
private cacheStorage; | ||
constructor(applicationPermission: ApplicationPermission[], permissionDeliverRepository: PermissionDeliverRepository); | ||
@@ -66,5 +88,2 @@ checkBusinessCapabilityPermission(authoritySubject: AuthoritySubject, businessCapability: BusinessCapability): boolean; | ||
refresh(): Promise<void>; | ||
private getCacheMapKey; | ||
private findBusinessCapabilityPermissions; | ||
private findResponsibilityPermissions; | ||
private scheduleRefresh; | ||
@@ -71,0 +90,0 @@ } |
{ | ||
"info": "This file is automatically generated by utils/UpdateVersionFile.js", | ||
"version": "1.1.3" | ||
"version": "1.2.0" | ||
} |
{ | ||
"name": "@entur-partner/permission-client-node", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"author": "Entur AS", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
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
55108
13
984
406
24
1
1
7