@fabrika/auth-core 0.9.0-dev.23 → 0.9.0-dev.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/contract-canonical.ts +7 -0
- package/src/contract.ts +2 -2
- package/src/permissions.ts +42 -0
- package/types/src/contract.d.ts +2 -2
- package/types/src/permissions.d.ts +12 -0
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type {
|
|
5
5
|
BodyAdmission,
|
|
6
6
|
Caller,
|
|
7
|
+
ColumnPolicyArtifact,
|
|
7
8
|
ColumnTestArtifact,
|
|
8
9
|
ConditionArtifact,
|
|
9
10
|
ConditionLiteralArtifact,
|
|
@@ -160,6 +161,7 @@ function canonicalTable(table: TablePolicyArtifact): TablePolicyArtifact {
|
|
|
160
161
|
scope: table.scope.mode === 'child' ? { ...table.scope, inherit: sorted(table.scope.inherit) } : table.scope,
|
|
161
162
|
insert: sorted(table.insert),
|
|
162
163
|
update: sorted(table.update),
|
|
164
|
+
columns: mapValues(table.columns, canonicalColumn),
|
|
163
165
|
...(table.when === undefined ? {} : { when: mapValues(table.when, canonicalCondition) }),
|
|
164
166
|
...(table.relationWhen === undefined
|
|
165
167
|
? {}
|
|
@@ -170,6 +172,11 @@ function canonicalTable(table: TablePolicyArtifact): TablePolicyArtifact {
|
|
|
170
172
|
}
|
|
171
173
|
}
|
|
172
174
|
|
|
175
|
+
/** A column's list of read actions sorted; one action, and a column without its own read, as they are. */
|
|
176
|
+
function canonicalColumn(column: ColumnPolicyArtifact): ColumnPolicyArtifact {
|
|
177
|
+
return column.read === undefined || typeof column.read === 'string' ? column : { ...column, read: sorted(column.read) }
|
|
178
|
+
}
|
|
179
|
+
|
|
173
180
|
function canonicalConfers(confers: ConfersArtifact): ConfersArtifact {
|
|
174
181
|
return confers.when === undefined ? confers : { ...confers, when: canonicalCondition(confers.when) }
|
|
175
182
|
}
|
package/src/contract.ts
CHANGED
|
@@ -1020,9 +1020,9 @@ export interface ColumnTestArtifact {
|
|
|
1020
1020
|
readonly gte?: ConditionLiteralArtifact | { readonly now: true }
|
|
1021
1021
|
}
|
|
1022
1022
|
|
|
1023
|
-
/** A column's own read
|
|
1023
|
+
/** A column's own read actions (one as its text, several as a list), its own write action, or both; an absent one is left out. */
|
|
1024
1024
|
export interface ColumnPolicyArtifact {
|
|
1025
|
-
readonly read?:
|
|
1025
|
+
readonly read?: TableActionArtifact
|
|
1026
1026
|
readonly write?: string
|
|
1027
1027
|
}
|
|
1028
1028
|
|
package/src/permissions.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { KeyGrant } from './rpc'
|
|
1
2
|
import type { PermissionEntry, Scope } from './types'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -29,6 +30,22 @@ export function matchAction(pattern: string, action: string): boolean {
|
|
|
29
30
|
return pattern === action
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
/**
|
|
34
|
+
* `entries` with each identical action, scope and source once, the first kept in order: how IAM resolves a principal's
|
|
35
|
+
* permissions, so two roles granting one action at one scope put one entry into the token.
|
|
36
|
+
*/
|
|
37
|
+
export function uniquePermissions(entries: readonly PermissionEntry[]): PermissionEntry[] {
|
|
38
|
+
const seen = new Set<string>()
|
|
39
|
+
return entries.filter((entry) => {
|
|
40
|
+
const key = JSON.stringify([entry.action, entry.scope?.type ?? null, entry.scope?.value ?? null, entry.source])
|
|
41
|
+
if (seen.has(key)) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
seen.add(key)
|
|
45
|
+
return true
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
32
49
|
/**
|
|
33
50
|
* Does `entries` grant `action` at the given scope? Mirrors `can()` semantics exactly:
|
|
34
51
|
* - scope === undefined → scope-less: ONLY entries with scope === null satisfy (global perms only).
|
|
@@ -106,3 +123,28 @@ export function isActionAllowed(pattern: string, catalog: readonly string[]): bo
|
|
|
106
123
|
}
|
|
107
124
|
return false
|
|
108
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Intersect live permissions with a ceiling: an OAuth scope's grants, or a terminal's patterns for an application. The
|
|
129
|
+
* ceiling never adds anything: each result is the narrower action and the narrower scope of one held entry and one
|
|
130
|
+
* ceiling entry that overlap, so a held `*` under a ceiling `a.read` yields `a.read`.
|
|
131
|
+
*/
|
|
132
|
+
export function narrowPermissions(live: readonly PermissionEntry[], ceiling: readonly KeyGrant[]): PermissionEntry[] {
|
|
133
|
+
const result: PermissionEntry[] = []
|
|
134
|
+
for (const held of live) {
|
|
135
|
+
for (const limit of ceiling) {
|
|
136
|
+
const action = matchAction(limit.action, held.action) ? held.action : matchAction(held.action, limit.action) ? limit.action : null
|
|
137
|
+
const scope = narrowerScope(held.scope, limit.scope ?? null)
|
|
138
|
+
if (action === null || scope === undefined) continue
|
|
139
|
+
result.push({ action, scope, source: held.source })
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return uniquePermissions(result)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** `null` is global; `undefined` means the two scopes do not overlap. */
|
|
146
|
+
function narrowerScope(held: Scope | null, limit: Scope | null): Scope | null | undefined {
|
|
147
|
+
if (held === null) return limit
|
|
148
|
+
if (limit === null) return held
|
|
149
|
+
return held.type === limit.type && held.value === limit.value ? held : undefined
|
|
150
|
+
}
|
package/types/src/contract.d.ts
CHANGED
|
@@ -937,9 +937,9 @@ export interface ColumnTestArtifact {
|
|
|
937
937
|
readonly now: true;
|
|
938
938
|
};
|
|
939
939
|
}
|
|
940
|
-
/** A column's own read
|
|
940
|
+
/** A column's own read actions (one as its text, several as a list), its own write action, or both; an absent one is left out. */
|
|
941
941
|
export interface ColumnPolicyArtifact {
|
|
942
|
-
readonly read?:
|
|
942
|
+
readonly read?: TableActionArtifact;
|
|
943
943
|
readonly write?: string;
|
|
944
944
|
}
|
|
945
945
|
export type TableScope = {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { KeyGrant } from './rpc';
|
|
1
2
|
import type { PermissionEntry, Scope } from './types';
|
|
2
3
|
/**
|
|
3
4
|
* Wildcard action matching. The pattern is one of:
|
|
@@ -16,6 +17,11 @@ import type { PermissionEntry, Scope } from './types';
|
|
|
16
17
|
* matchAction('project.*', 'projects.read') === false
|
|
17
18
|
*/
|
|
18
19
|
export declare function matchAction(pattern: string, action: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* `entries` with each identical action, scope and source once, the first kept in order: how IAM resolves a principal's
|
|
22
|
+
* permissions, so two roles granting one action at one scope put one entry into the token.
|
|
23
|
+
*/
|
|
24
|
+
export declare function uniquePermissions(entries: readonly PermissionEntry[]): PermissionEntry[];
|
|
19
25
|
/**
|
|
20
26
|
* Does `entries` grant `action` at the given scope? Mirrors `can()` semantics exactly:
|
|
21
27
|
* - scope === undefined → scope-less: ONLY entries with scope === null satisfy (global perms only).
|
|
@@ -48,3 +54,9 @@ export declare function scopedValues(entries: PermissionEntry[], action: string,
|
|
|
48
54
|
* - otherwise → invalid.
|
|
49
55
|
*/
|
|
50
56
|
export declare function isActionAllowed(pattern: string, catalog: readonly string[]): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Intersect live permissions with a ceiling: an OAuth scope's grants, or a terminal's patterns for an application. The
|
|
59
|
+
* ceiling never adds anything: each result is the narrower action and the narrower scope of one held entry and one
|
|
60
|
+
* ceiling entry that overlap, so a held `*` under a ceiling `a.read` yields `a.read`.
|
|
61
|
+
*/
|
|
62
|
+
export declare function narrowPermissions(live: readonly PermissionEntry[], ceiling: readonly KeyGrant[]): PermissionEntry[];
|