@insaeng/auth 0.4.0 → 0.5.0
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/dist/app/cli/types-command.d.ts +11 -0
- package/dist/app/cli/types-command.d.ts.map +1 -0
- package/dist/app/cli/types-command.js +45 -0
- package/dist/app/http/auth-error-handler.d.ts +3 -0
- package/dist/app/http/auth-error-handler.d.ts.map +1 -0
- package/dist/app/http/auth-error-handler.js +29 -0
- package/dist/app/http/auth-middleware.d.ts +16 -0
- package/dist/app/http/auth-middleware.d.ts.map +1 -0
- package/dist/app/http/auth-middleware.js +73 -0
- package/dist/data/auth/auth-repo.d.ts +8 -1
- package/dist/data/auth/auth-repo.d.ts.map +1 -1
- package/dist/data/auth/auth-repo.js +23 -1
- package/dist/data/sources/cedar/cedar-schema-source.d.ts +3 -0
- package/dist/data/sources/cedar/cedar-schema-source.d.ts.map +1 -0
- package/dist/data/sources/cedar/cedar-schema-source.js +67 -0
- package/dist/data/sources/cedar/cedar-source.d.ts +17 -0
- package/dist/data/sources/cedar/cedar-source.d.ts.map +1 -0
- package/dist/data/sources/cedar/cedar-source.js +75 -0
- package/dist/dependencies.d.ts +10 -5
- package/dist/dependencies.d.ts.map +1 -1
- package/dist/dependencies.js +7 -4
- package/dist/domain/authz-types/authz-types-entity.d.ts +37 -0
- package/dist/domain/authz-types/authz-types-entity.d.ts.map +1 -0
- package/dist/domain/authz-types/authz-types-entity.js +1 -0
- package/dist/domain/authz-types/use-cases/generate-authz-types-use-case.d.ts +4 -0
- package/dist/domain/authz-types/use-cases/generate-authz-types-use-case.d.ts.map +1 -0
- package/dist/domain/authz-types/use-cases/generate-authz-types-use-case.js +64 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +11 -0
- package/package.json +25 -2
- package/src/app/cli/types-command.ts +62 -0
- package/src/app/http/auth-error-handler.ts +39 -0
- package/src/app/http/auth-middleware.ts +104 -0
- package/src/data/auth/auth-repo.ts +43 -1
- package/src/data/sources/cedar/cedar-schema-source.ts +92 -0
- package/src/data/sources/cedar/cedar-source.ts +116 -0
- package/src/dependencies.ts +16 -7
- package/src/domain/authz-types/authz-types-entity.ts +29 -0
- package/src/domain/authz-types/use-cases/generate-authz-types-use-case.ts +85 -0
- package/src/main.ts +12 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuthzAttribute,
|
|
3
|
+
AuthzSchema,
|
|
4
|
+
AuthzType,
|
|
5
|
+
} from "../authz-types-entity.js";
|
|
6
|
+
|
|
7
|
+
export type GenerateAuthzTypes = (schema: AuthzSchema) => string;
|
|
8
|
+
|
|
9
|
+
const maxLineLength = 80;
|
|
10
|
+
|
|
11
|
+
function byName(a: { name: string }, b: { name: string }): number {
|
|
12
|
+
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function propertyName(name: string): string {
|
|
16
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function renderAttributes(attributes: AuthzAttribute[], indent: string) {
|
|
20
|
+
return [...attributes]
|
|
21
|
+
.sort(byName)
|
|
22
|
+
.map(
|
|
23
|
+
({ name, type, required }) =>
|
|
24
|
+
`${indent} ${propertyName(name)}${required ? "" : "?"}: ${renderType(type, `${indent} `)};\n`,
|
|
25
|
+
)
|
|
26
|
+
.join("");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function renderType(type: AuthzType, indent: string): string {
|
|
30
|
+
switch (type.kind) {
|
|
31
|
+
case "string":
|
|
32
|
+
case "extension":
|
|
33
|
+
return "string";
|
|
34
|
+
case "long":
|
|
35
|
+
return "number";
|
|
36
|
+
case "boolean":
|
|
37
|
+
return "boolean";
|
|
38
|
+
case "set":
|
|
39
|
+
return `${renderType(type.element, indent)}[]`;
|
|
40
|
+
case "entity":
|
|
41
|
+
return `EntityUid<${JSON.stringify(type.name)}>`;
|
|
42
|
+
case "record":
|
|
43
|
+
return type.attributes.length === 0
|
|
44
|
+
? "Record<string, never>"
|
|
45
|
+
: `{\n${renderAttributes(type.attributes, indent)}${indent}}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function renderAction(actions: string[]): string {
|
|
50
|
+
if (actions.length === 0) {
|
|
51
|
+
return "export type Action = never;\n";
|
|
52
|
+
}
|
|
53
|
+
const union = `${actions.map((action) => JSON.stringify(action)).join(" | ")};`;
|
|
54
|
+
if (`export type Action = ${union}`.length <= maxLineLength) {
|
|
55
|
+
return `export type Action = ${union}\n`;
|
|
56
|
+
}
|
|
57
|
+
if (` ${union}`.length <= maxLineLength) {
|
|
58
|
+
return `export type Action =\n ${union}\n`;
|
|
59
|
+
}
|
|
60
|
+
return `export type Action =\n${actions.map((action) => ` | ${JSON.stringify(action)}`).join("\n")};\n`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function generateAuthzTypesUseCase(schema: AuthzSchema): string {
|
|
64
|
+
const actions = [
|
|
65
|
+
...new Set(schema.namespaces.flatMap(({ actions }) => actions)),
|
|
66
|
+
].sort();
|
|
67
|
+
const entities = schema.namespaces
|
|
68
|
+
.flatMap(({ name: namespace, entityTypes }) =>
|
|
69
|
+
entityTypes.map(({ name, attributes }) => ({
|
|
70
|
+
name: [...namespace.split("::"), name].join(""),
|
|
71
|
+
attributes,
|
|
72
|
+
})),
|
|
73
|
+
)
|
|
74
|
+
.sort(byName)
|
|
75
|
+
.map(({ name, attributes }) =>
|
|
76
|
+
attributes.length === 0
|
|
77
|
+
? `export type ${name} = Record<string, never>;\n`
|
|
78
|
+
: `export interface ${name} {\n${renderAttributes(attributes, "")}}\n`,
|
|
79
|
+
);
|
|
80
|
+
return [
|
|
81
|
+
renderAction(actions),
|
|
82
|
+
"export interface EntityUid<Type extends string> {\n type: Type;\n id: string;\n}\n",
|
|
83
|
+
...entities,
|
|
84
|
+
].join("\n");
|
|
85
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { typesCommand } from "./app/cli/types-command.js";
|
|
3
|
+
import { readCedarSchemaSource } from "./data/sources/cedar/cedar-schema-source.js";
|
|
4
|
+
import { generateAuthzTypesUseCase } from "./domain/authz-types/use-cases/generate-authz-types-use-case.js";
|
|
5
|
+
|
|
6
|
+
process.exitCode = typesCommand(process.argv.slice(2), {
|
|
7
|
+
readAuthzSchema: readCedarSchemaSource,
|
|
8
|
+
generateAuthzTypes: generateAuthzTypesUseCase,
|
|
9
|
+
cwd: process.cwd(),
|
|
10
|
+
log: console.log,
|
|
11
|
+
error: console.error,
|
|
12
|
+
});
|