authorized-rules
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -5,6 +5,5 @@ export declare type JwtToken = { | ||
}; | ||
export declare type Rule<T> = (token: JwtToken) => Promise<RuleResult<T>>; | ||
export declare type RuleResult<T extends {}> = { | ||
export declare type Rule = (token: JwtToken) => Promise<RuleResult>; | ||
export declare type RuleResult = { | ||
passed: boolean; | ||
data: T[]; | ||
ruleName: string; | ||
@@ -17,4 +16,4 @@ }; | ||
}; | ||
export declare const authorize: <T1 = void, T2 = void, T3 = void, T4 = void>(jwtToken: string, executionRule: Rule<T1 | T2 | T3 | T4>) => Promise<(T1 | T2 | T3 | T4)[]>; | ||
export declare const and: <T extends void | {}>(rules: Rule<T>[]) => Rule<T>; | ||
export declare const or: <T extends void | {}>(rules: Rule<T>[]) => Rule<T>; | ||
export declare const authorize: (jwtToken: string, executionRule: Rule) => Promise<void>; | ||
export declare const and: (rules: Rule[]) => Rule; | ||
export declare const or: (rules: Rule[]) => Rule; |
@@ -38,9 +38,2 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -58,3 +51,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
case 0: | ||
token = getToken(jwtToken); | ||
token = parseToken(jwtToken); | ||
if (!token.iss) { | ||
@@ -69,3 +62,3 @@ throw http_errors_1.default(403, 'invalid token (missing issuer)'); | ||
} | ||
return [2 /*return*/, ruleResult.data]; | ||
return [2 /*return*/]; | ||
} | ||
@@ -76,7 +69,6 @@ }); | ||
var and = function (rules) { return function (token) { return __awaiter(void 0, void 0, void 0, function () { | ||
var data, _i, rules_1, rule, ruleResult; | ||
var _i, rules_1, rule, ruleResult; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
data = []; | ||
_i = 0, rules_1 = rules; | ||
@@ -93,3 +85,2 @@ _a.label = 1; | ||
} | ||
data = __spreadArrays(data, ruleResult.data); | ||
_a.label = 3; | ||
@@ -99,3 +90,3 @@ case 3: | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/, { passed: true, ruleName: 'and', data: data }]; | ||
case 4: return [2 /*return*/, { passed: true, ruleName: 'and' }]; | ||
} | ||
@@ -106,7 +97,6 @@ }); | ||
var or = function (rules) { return function (token) { return __awaiter(void 0, void 0, void 0, function () { | ||
var data, _i, rules_2, rule, ruleResult; | ||
var _i, rules_2, rule, ruleResult; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
data = []; | ||
_i = 0, rules_2 = rules; | ||
@@ -120,5 +110,4 @@ _a.label = 1; | ||
ruleResult = _a.sent(); | ||
data = __spreadArrays(data, ruleResult.data); | ||
if (ruleResult.passed) | ||
return [2 /*return*/, { passed: true, ruleName: 'or', data: data }]; | ||
return [2 /*return*/, { passed: true, ruleName: 'or' }]; | ||
_a.label = 3; | ||
@@ -128,3 +117,3 @@ case 3: | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/, { passed: false, ruleName: 'or', data: [] }]; | ||
case 4: return [2 /*return*/, { passed: false, ruleName: 'or' }]; | ||
} | ||
@@ -134,3 +123,3 @@ }); | ||
exports.or = or; | ||
var getToken = function (token) { return jwt_decode_1.default(token); }; | ||
var parseToken = function (token) { return jwt_decode_1.default(token); }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "authorized-rules", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,7 +9,6 @@ import jwtDecoder from 'jwt-decode'; | ||
export type Rule<T> = (token: JwtToken) => Promise<RuleResult<T>>; | ||
export type Rule = (token: JwtToken) => Promise<RuleResult>; | ||
export type RuleResult<T extends {}> = { | ||
export type RuleResult = { | ||
passed: boolean; | ||
data: T[]; | ||
ruleName: string; | ||
@@ -24,7 +23,7 @@ }; | ||
export const authorize = async <T1 = void, T2 = void, T3 = void, T4 = void>( | ||
export const authorize = async ( | ||
jwtToken: string, | ||
executionRule: Rule<T1 | T2 | T3 | T4> | ||
): Promise<(T1 | T2 | T3 | T4)[]> => { | ||
const token = getToken(jwtToken); | ||
executionRule: Rule | ||
): Promise<void> => { | ||
const token = parseToken(jwtToken); | ||
@@ -39,8 +38,5 @@ if (!token.iss) { | ||
} | ||
return ruleResult.data; | ||
}; | ||
export const and = <T extends {} | void>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
let data: T[] = []; | ||
export const and = (rules: Rule[]): Rule => async (token: JwtToken) => { | ||
for (const rule of rules) { | ||
@@ -51,17 +47,14 @@ const ruleResult = await rule(token); | ||
} | ||
data = [...data, ...ruleResult.data]; | ||
} | ||
return {passed: true, ruleName: 'and', data}; | ||
return {passed: true, ruleName: 'and'}; | ||
}; | ||
export const or = <T extends {} | void>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
let data: T[] = []; | ||
export const or = (rules: Rule[]): Rule => async (token: JwtToken) => { | ||
for (const rule of rules) { | ||
const ruleResult = await rule(token); | ||
data = [...data, ...ruleResult.data]; | ||
if (ruleResult.passed) return {passed: true, ruleName: 'or', data}; | ||
if (ruleResult.passed) return {passed: true, ruleName: 'or'}; | ||
} | ||
return {passed: false, ruleName: 'or', data: []}; | ||
return {passed: false, ruleName: 'or'}; | ||
}; | ||
const getToken = (token: string): JwtToken & {iss: string} => jwtDecoder(token); | ||
const parseToken = (token: string): JwtToken & {iss: string} => jwtDecoder(token); |
import * as jwt from 'jsonwebtoken'; | ||
import {and, authorize, or, Rule, RuleResult} from '../../src/index'; | ||
type Value = { | ||
value: string; | ||
}; | ||
const jwtToken = jwt.sign( | ||
{ | ||
sub: '222222', | ||
client_id: "'some-client-id'", | ||
grant_type: 'client_credentials', | ||
iss: 'some-issuer', | ||
}, | ||
'secret' | ||
); | ||
const jwtToken = | ||
jwt.sign( | ||
{ | ||
sub: "222222", | ||
client_id: "'some-client-id'", | ||
grant_type: 'client_credentials', | ||
iss: 'some-issuer', | ||
}, | ||
'secret' | ||
); | ||
const createRule = (ruleName: string, passed: boolean, data: Value[]) => (): Rule<Value> => async (): Promise< | ||
RuleResult<Value> | ||
> => ({ | ||
const createRule = (ruleName: string, passed: boolean) => (): Rule => async (): Promise<RuleResult> => ({ | ||
ruleName, | ||
passed, | ||
data, | ||
}); | ||
@@ -30,16 +22,7 @@ | ||
it('all rules should pass', async () => { | ||
const rule1 = createRule('rule1', true, []); | ||
const rule2 = createRule('rule2', true, []); | ||
const rule1 = createRule('rule1', true); | ||
const rule2 = createRule('rule2', true); | ||
await authorize(jwtToken, and([rule1(), rule2()])); | ||
}); | ||
it('gathers all data results', async () => { | ||
const rule1 = createRule('rule1', true, [{value: 'value1'}]); | ||
const rule2 = createRule('rule2', true, [{value: 'value2'}]); | ||
const [result1, result2] = await authorize<Value, Value>(jwtToken, and([rule1(), rule2()])); | ||
expect(result1 && result1.value).toBe('value1'); | ||
expect(result2 && result2.value).toBe('value2'); | ||
}); | ||
}); | ||
@@ -49,4 +32,4 @@ | ||
it('one of the rules should pass', async () => { | ||
const rule1 = createRule('rule1', true, []); | ||
const rule2 = createRule('rule2', false, []); | ||
const rule1 = createRule('rule1', true,); | ||
const rule2 = createRule('rule2', false); | ||
@@ -56,15 +39,16 @@ await authorize(jwtToken, or([rule1(), rule2()])); | ||
}); | ||
it('gathers all results until the rule that passed', async () => { | ||
const rule1 = createRule('rule1', true, [{value: 'value1'}]); | ||
const rule2 = createRule('rule2', false, [{value: 'value2'}]); | ||
}); | ||
let [result1, result2] = await authorize(jwtToken, or([rule1(), rule2()])); | ||
expect(result1.value).toBe('value1'); | ||
expect(result2).toBeUndefined(); | ||
describe('authorize', () => { | ||
it('should throw 403 when rule does not pass', async () => { | ||
const rule1 = createRule('rule1', false); | ||
[result2, result1] = await authorize(jwtToken, or([rule2(), rule1()])); | ||
expect(result2.value).toBe('value2'); | ||
expect(result1.value).toBe('value1'); | ||
try { | ||
await authorize(jwtToken, and([rule1()])); | ||
fail(); | ||
} catch (e) { | ||
expect(e.status).toBe(403); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
14518
273