Skip to content

Commit

Permalink
feat(sid): generating uuid to set sid in statements as default
Browse files Browse the repository at this point in the history
re #27
  • Loading branch information
roggervalf committed Oct 10, 2020
1 parent 9e83b7c commit c23d6e6
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/ActionBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('ActionBasedPolicy Class', () => {
}
];
const policy = new ActionBasedPolicy(statements);
expect(policy.getStatements()).toEqual(statements);
const exportedStatements = policy.getStatements();
expect(exportedStatements).toMatchObject(statements);
expect(exportedStatements[0].sid).not.toBeFalsy();
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/ActionBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export class ActionBasedPolicy {
config: ActionBasedType[],
conditionResolver?: ConditionResolver
) {
const statementInstances = config.map(s => new ActionBased(s));
const statementInstances = config.map(
statement => new ActionBased(statement)
);
this.allowStatements = statementInstances.filter(s => s.effect === 'allow');
this.denyStatements = statementInstances.filter(s => s.effect === 'deny');
this.conditionResolver = conditionResolver;
this.statements = config;
this.statements = this.statements = statementInstances.map(statement =>
statement.getStatement()
);
}
getStatements(): ActionBasedType[] {
return this.statements;
Expand Down
2 changes: 1 addition & 1 deletion src/ActionBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ActionBased extends Statement {
? [action.notAction]
: action.notAction;
}
this.statement = action;
this.statement = { ...action, sid: this.sid };
}

getStatement(): ActionBasedType {
Expand Down
4 changes: 3 additions & 1 deletion src/IdentityBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe('IdentityBasedPolicy Class', () => {
}
];
const policy = new IdentityBasedPolicy(statements);
expect(policy.getStatements()).toEqual(statements);
const exportedStatements = policy.getStatements();
expect(exportedStatements).toMatchObject(statements);
expect(exportedStatements[0].sid).not.toBeFalsy();
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/IdentityBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export class IdentityBasedPolicy {
config: IdentityBasedType[],
conditionResolver?: ConditionResolver
) {
const statementInstances = config.map(s => new IdentityBased(s));
const statementInstances = config.map(
statement => new IdentityBased(statement)
);
this.allowStatements = statementInstances.filter(s => s.effect === 'allow');
this.denyStatements = statementInstances.filter(s => s.effect === 'deny');
this.conditionResolver = conditionResolver;
this.statements = config;
this.statements = statementInstances.map(statement =>
statement.getStatement()
);
}
getStatements(): IdentityBasedType[] {
return this.statements;
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class IdentityBased extends Statement {
? [identity.notAction]
: identity.notAction;
}
this.statement = identity;
this.statement = { ...identity, sid: this.sid };
}

getStatement(): IdentityBasedType {
Expand Down
4 changes: 3 additions & 1 deletion src/ResourceBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('ResourceBasedPolicy Class', () => {
}
];
const policy = new ResourceBasedPolicy(statements);
expect(policy.getStatements()).toEqual(statements);
const exportedStatements = policy.getStatements();
expect(exportedStatements).toMatchObject(statements);
expect(exportedStatements[0].sid).not.toBeFalsy();
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/ResourceBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export class ResourceBasedPolicy {
config: ResourceBasedType[],
conditionResolver?: ConditionResolver
) {
const statementInstances = config.map(s => new ResourceBased(s));
const statementInstances = config.map(
statement => new ResourceBased(statement)
);
this.allowStatements = statementInstances.filter(s => s.effect === 'allow');
this.denyStatements = statementInstances.filter(s => s.effect === 'deny');
this.conditionResolver = conditionResolver;
this.statements = config;
this.statements = statementInstances.map(statement =>
statement.getStatement()
);
}
getStatements(): ResourceBasedType[] {
return this.statements;
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ResourceBased extends Statement {
? [identity.notPrincipal]
: identity.notPrincipal;
}
this.statement = identity;
this.statement = { ...identity, sid: this.sid };
}

getStatement(): ResourceBasedType {
Expand Down
11 changes: 9 additions & 2 deletions src/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MatchConditionInterface
} from './types';
import { getValueFromPath } from './utils/getValueFromPath';
import { generateUUID } from './utils/generateUUID';

const reDelimiters = /\${([^}]*)}/g;
const trim = / +(?= )|^\s+|\s+$/g;
Expand All @@ -27,10 +28,16 @@ export function applyContext(str: string, context?: Context): string {
}

class Statement {
effect: EffectBlock;
protected sid: string;
protected readonly condition?: ConditionBlock;
effect: EffectBlock;

constructor({ effect = 'allow', condition }: StatementInterface) {
constructor({ sid, effect = 'allow', condition }: StatementInterface) {
if (!sid) {
this.sid = generateUUID();
} else {
this.sid = sid;
}
this.effect = effect;
this.condition = condition;
}
Expand Down
15 changes: 8 additions & 7 deletions src/utils/generateUUID.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MersenneTwister } from './mersenneTwister';

const replacePlaceholders = (placeholder: string): string => {
const mersenne = new MersenneTwister();
const random = Math.floor(mersenne.randomInt32() * (1.0 / 4294967296.0) * 15);
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
const replacePlaceholders = (mersenne: MersenneTwister) => (
placeholder: string
): string => {
const random = Math.floor(mersenne.randomReal2() * 16);

const value = placeholder === 'x' ? random : (random & 0x3) | 0x8;
return value.toString(16);
Expand All @@ -18,14 +18,15 @@ const replacePlaceholders = (placeholder: string): string => {
* @example
* ```javascript
* generateUUID()
* // => 84cc8800-6000-4555-844b-bbb333888888
* // => 49e71c40-9b21-4371-9699-2def33f62e66
*
* generateUUID()
* // => 3dd002aa-2111-4000-b333-777666666666
* // => da94f128-4247-48e3-bc73-d0cae46b5093
* ```
*/
export function generateUUID(): string {
const mersenne = new MersenneTwister();
const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';

return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders(mersenne));
}
3 changes: 2 additions & 1 deletion src/utils/isSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { getTag } from './getTag';
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
* @example
*
* ```javascript
* isSymbol(Symbol())
* // => true
*
* isSymbol('abc')
* // => false
* ```
*/
export function isSymbol(value?: any): boolean {
const type = typeof value;
Expand Down

0 comments on commit c23d6e6

Please sign in to comment.