@zakyyudha/node-authzkit 1.0.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.
Files changed (120) hide show
  1. package/.eslintrc.cjs +17 -0
  2. package/.prettierrc.json +10 -0
  3. package/.release-it.json +24 -0
  4. package/README.md +221 -0
  5. package/dist/src/classes/Authzkit.d.ts +110 -0
  6. package/dist/src/classes/Authzkit.js +189 -0
  7. package/dist/src/classes/Authzkit.js.map +1 -0
  8. package/dist/src/dashboard/router.d.ts +19 -0
  9. package/dist/src/dashboard/router.js +89 -0
  10. package/dist/src/dashboard/router.js.map +1 -0
  11. package/dist/src/dashboard/routes/permissions.d.ts +3 -0
  12. package/dist/src/dashboard/routes/permissions.js +39 -0
  13. package/dist/src/dashboard/routes/permissions.js.map +1 -0
  14. package/dist/src/dashboard/routes/roles.d.ts +3 -0
  15. package/dist/src/dashboard/routes/roles.js +39 -0
  16. package/dist/src/dashboard/routes/roles.js.map +1 -0
  17. package/dist/src/dashboard/routes/users.d.ts +3 -0
  18. package/dist/src/dashboard/routes/users.js +81 -0
  19. package/dist/src/dashboard/routes/users.js.map +1 -0
  20. package/dist/src/drivers/mongodb/mongo-connection.d.ts +15 -0
  21. package/dist/src/drivers/mongodb/mongo-connection.js +89 -0
  22. package/dist/src/drivers/mongodb/mongo-connection.js.map +1 -0
  23. package/dist/src/drivers/postgres/pg-connection.d.ts +17 -0
  24. package/dist/src/drivers/postgres/pg-connection.js +145 -0
  25. package/dist/src/drivers/postgres/pg-connection.js.map +1 -0
  26. package/dist/src/index.d.ts +19 -0
  27. package/dist/src/index.js +36 -0
  28. package/dist/src/index.js.map +1 -0
  29. package/dist/src/interfaces/Authorizable.d.ts +7 -0
  30. package/dist/src/interfaces/Authorizable.js +3 -0
  31. package/dist/src/interfaces/Authorizable.js.map +1 -0
  32. package/dist/src/interfaces/IAuthzkitConfig.d.ts +18 -0
  33. package/dist/src/interfaces/IAuthzkitConfig.js +3 -0
  34. package/dist/src/interfaces/IAuthzkitConfig.js.map +1 -0
  35. package/dist/src/interfaces/Permission.d.ts +4 -0
  36. package/dist/src/interfaces/Permission.js +3 -0
  37. package/dist/src/interfaces/Permission.js.map +1 -0
  38. package/dist/src/interfaces/Role.d.ts +5 -0
  39. package/dist/src/interfaces/Role.js +3 -0
  40. package/dist/src/interfaces/Role.js.map +1 -0
  41. package/dist/src/middleware/authzMiddleware.d.ts +17 -0
  42. package/dist/src/middleware/authzMiddleware.js +52 -0
  43. package/dist/src/middleware/authzMiddleware.js.map +1 -0
  44. package/dist/src/stores/IAuthzkitStore.d.ts +23 -0
  45. package/dist/src/stores/IAuthzkitStore.js +3 -0
  46. package/dist/src/stores/IAuthzkitStore.js.map +1 -0
  47. package/dist/src/stores/InMemoryAuthzkitStore.d.ts +28 -0
  48. package/dist/src/stores/InMemoryAuthzkitStore.js +83 -0
  49. package/dist/src/stores/InMemoryAuthzkitStore.js.map +1 -0
  50. package/dist/src/stores/MongoAuthzkitStore.d.ts +31 -0
  51. package/dist/src/stores/MongoAuthzkitStore.js +127 -0
  52. package/dist/src/stores/MongoAuthzkitStore.js.map +1 -0
  53. package/dist/src/stores/PgAuthzkitStore.d.ts +31 -0
  54. package/dist/src/stores/PgAuthzkitStore.js +133 -0
  55. package/dist/src/stores/PgAuthzkitStore.js.map +1 -0
  56. package/dist/src/utils/envConfig.d.ts +2 -0
  57. package/dist/src/utils/envConfig.js +68 -0
  58. package/dist/src/utils/envConfig.js.map +1 -0
  59. package/dist/tests/Authzkit.test.d.ts +1 -0
  60. package/dist/tests/Authzkit.test.js +126 -0
  61. package/dist/tests/Authzkit.test.js.map +1 -0
  62. package/dist/tests/MongoAuthzkitStore.test.d.ts +1 -0
  63. package/dist/tests/MongoAuthzkitStore.test.js +161 -0
  64. package/dist/tests/MongoAuthzkitStore.test.js.map +1 -0
  65. package/dist/tests/MongoAuthzkitStoreCustom.test.d.ts +1 -0
  66. package/dist/tests/MongoAuthzkitStoreCustom.test.js +65 -0
  67. package/dist/tests/MongoAuthzkitStoreCustom.test.js.map +1 -0
  68. package/dist/tests/PgAuthzkitStore.test.d.ts +1 -0
  69. package/dist/tests/PgAuthzkitStore.test.js +163 -0
  70. package/dist/tests/PgAuthzkitStore.test.js.map +1 -0
  71. package/dist/tests/PgAuthzkitStoreCustom.test.d.ts +1 -0
  72. package/dist/tests/PgAuthzkitStoreCustom.test.js +74 -0
  73. package/dist/tests/PgAuthzkitStoreCustom.test.js.map +1 -0
  74. package/examples/express-app.ts +65 -0
  75. package/jest.config.js +9 -0
  76. package/package.json +57 -0
  77. package/src/classes/Authzkit.ts +214 -0
  78. package/src/dashboard/router.ts +79 -0
  79. package/src/dashboard/routes/permissions.ts +38 -0
  80. package/src/dashboard/routes/roles.ts +38 -0
  81. package/src/dashboard/routes/users.ts +81 -0
  82. package/src/dashboard/web/README.md +73 -0
  83. package/src/dashboard/web/eslint.config.js +23 -0
  84. package/src/dashboard/web/index.html +13 -0
  85. package/src/dashboard/web/package.json +31 -0
  86. package/src/dashboard/web/pnpm-lock.yaml +2094 -0
  87. package/src/dashboard/web/public/vite.svg +1 -0
  88. package/src/dashboard/web/src/App.css +42 -0
  89. package/src/dashboard/web/src/App.tsx +26 -0
  90. package/src/dashboard/web/src/assets/react.svg +1 -0
  91. package/src/dashboard/web/src/components/Navbar.tsx +53 -0
  92. package/src/dashboard/web/src/index.css +138 -0
  93. package/src/dashboard/web/src/main.tsx +10 -0
  94. package/src/dashboard/web/src/pages/PermissionsPage.tsx +87 -0
  95. package/src/dashboard/web/src/pages/RolesPage.tsx +98 -0
  96. package/src/dashboard/web/src/pages/UsersPage.tsx +146 -0
  97. package/src/dashboard/web/src/services/api.ts +59 -0
  98. package/src/dashboard/web/tsconfig.app.json +28 -0
  99. package/src/dashboard/web/tsconfig.json +7 -0
  100. package/src/dashboard/web/tsconfig.node.json +26 -0
  101. package/src/dashboard/web/vite.config.ts +8 -0
  102. package/src/drivers/mongodb/mongo-connection.ts +98 -0
  103. package/src/drivers/postgres/pg-connection.ts +159 -0
  104. package/src/index.ts +19 -0
  105. package/src/interfaces/Authorizable.ts +8 -0
  106. package/src/interfaces/IAuthzkitConfig.ts +19 -0
  107. package/src/interfaces/Permission.ts +4 -0
  108. package/src/interfaces/Role.ts +5 -0
  109. package/src/middleware/authzMiddleware.ts +60 -0
  110. package/src/stores/IAuthzkitStore.ts +33 -0
  111. package/src/stores/InMemoryAuthzkitStore.ts +101 -0
  112. package/src/stores/MongoAuthzkitStore.ts +171 -0
  113. package/src/stores/PgAuthzkitStore.ts +191 -0
  114. package/src/utils/envConfig.ts +70 -0
  115. package/tests/Authzkit.test.ts +157 -0
  116. package/tests/MongoAuthzkitStore.test.ts +204 -0
  117. package/tests/MongoAuthzkitStoreCustom.test.ts +75 -0
  118. package/tests/PgAuthzkitStore.test.ts +207 -0
  119. package/tests/PgAuthzkitStoreCustom.test.ts +90 -0
  120. package/tsconfig.json +37 -0
@@ -0,0 +1,79 @@
1
+ import { Router } from 'express';
2
+ import * as express from 'express';
3
+ import * as path from 'path';
4
+ import * as fs from 'fs';
5
+ import basicAuth from 'basic-auth';
6
+ import rolesRouter from './routes/roles';
7
+ import permissionsRouter from './routes/permissions';
8
+ import usersRouter from './routes/users';
9
+ import { Authzkit } from '../classes/Authzkit';
10
+
11
+ export interface DashboardOptions {
12
+ /**
13
+ * Authzkit instance to use. If not provided, uses the singleton.
14
+ */
15
+ authzkit?: Authzkit;
16
+ /**
17
+ * Secret password for Basic Auth.
18
+ * If not provided, checks AUTHZKIT_DASHBOARD_SECRET env var.
19
+ * If neither is present, dashboard will not be accessible (returns 500 configuration error or 401).
20
+ */
21
+ secret?: string;
22
+ /**
23
+ * Usersname for Basic Auth. Defaults to 'admin'.
24
+ */
25
+ username?: string;
26
+ }
27
+
28
+ export function createDashboardRouter(options: DashboardOptions = {}): Router {
29
+ const router = Router();
30
+ const username = options.username || process.env.AUTHZKIT_DASHBOARD_USERNAME || 'admin';
31
+ const secret = options.secret || process.env.AUTHZKIT_DASHBOARD_SECRET;
32
+
33
+ // Trailing slash redirection for proper relative path resolution in frontend
34
+ router.use((req, res, next) => {
35
+ if (req.path === '/' && !req.originalUrl.endsWith('/')) {
36
+ return res.redirect(req.originalUrl + '/');
37
+ }
38
+ next();
39
+ });
40
+
41
+ // Basic Authentication Middleware
42
+ router.use((req, res, next) => {
43
+ if (!secret) {
44
+ console.warn('Authzkit Dashboard: No secret provided. Dashboard is disabled.');
45
+ return res.status(500).send('Dashboard configuration error: No secret provided.');
46
+ }
47
+
48
+ const user = basicAuth(req);
49
+ if (!user || user.name !== username || user.pass !== secret) {
50
+ res.set('WWW-Authenticate', 'Basic realm="Authzkit Dashboard"');
51
+ return res.status(401).send('Authentication required.');
52
+ }
53
+ next();
54
+ });
55
+
56
+ // API Routes
57
+ router.use('/api/roles', rolesRouter);
58
+ router.use('/api/permissions', permissionsRouter);
59
+ router.use('/api/users', usersRouter);
60
+
61
+ // Static Assets
62
+ // In src environment: ./web/dist (relative to this file)
63
+ const staticPath = path.join(__dirname, 'web/dist');
64
+
65
+ // In dist environment, this file is in dist/src/dashboard/router.js
66
+ // And web assets are in dist/src/dashboard/web/dist (copied by build script)
67
+ // So relative path is still 'web/dist'
68
+
69
+ // Check if dist exists (maybe we are in dev and it hasn't been built yet, or path is wrong)
70
+ if (!fs.existsSync(staticPath)) {
71
+ console.warn(
72
+ `Authzkit Dashboard: Static assets not found at ${staticPath}. Did you run 'pnpm build:web'?`
73
+ );
74
+ }
75
+
76
+ router.use(express.static(staticPath));
77
+
78
+ return router;
79
+ }
@@ -0,0 +1,38 @@
1
+ import { Router } from 'express';
2
+ import { Authzkit } from '../../classes/Authzkit';
3
+
4
+ const router: Router = Router();
5
+
6
+ router.get('/', async (req, res) => {
7
+ try {
8
+ const authzkit = Authzkit.getInstance();
9
+ const permissions = await authzkit.getPermissions();
10
+ res.json(permissions);
11
+ } catch (err: any) {
12
+ res.status(500).json({ error: err.message });
13
+ }
14
+ });
15
+
16
+ router.post('/', async (req, res) => {
17
+ try {
18
+ const { name, guard_name } = req.body;
19
+ const authzkit = Authzkit.getInstance();
20
+ const permission = await authzkit.definePermission(name, guard_name);
21
+ res.status(201).json(permission);
22
+ } catch (err: any) {
23
+ res.status(400).json({ error: err.message });
24
+ }
25
+ });
26
+
27
+ router.delete('/:name', async (req, res) => {
28
+ try {
29
+ const { name } = req.params;
30
+ const authzkit = Authzkit.getInstance();
31
+ await authzkit.deletePermission(name);
32
+ res.status(204).send();
33
+ } catch (err: any) {
34
+ res.status(500).json({ error: err.message });
35
+ }
36
+ });
37
+
38
+ export default router;
@@ -0,0 +1,38 @@
1
+ import { Router } from 'express';
2
+ import { Authzkit } from '../../classes/Authzkit';
3
+
4
+ const router: Router = Router();
5
+
6
+ router.get('/', async (req, res) => {
7
+ try {
8
+ const authzkit = Authzkit.getInstance();
9
+ const roles = await authzkit.getRoles();
10
+ res.json(roles);
11
+ } catch (err: any) {
12
+ res.status(500).json({ error: err.message });
13
+ }
14
+ });
15
+
16
+ router.post('/', async (req, res) => {
17
+ try {
18
+ const { name, permissions, guard_name } = req.body;
19
+ const authzkit = Authzkit.getInstance();
20
+ const role = await authzkit.defineRole(name, permissions, guard_name);
21
+ res.status(201).json(role);
22
+ } catch (err: any) {
23
+ res.status(400).json({ error: err.message });
24
+ }
25
+ });
26
+
27
+ router.delete('/:name', async (req, res) => {
28
+ try {
29
+ const { name } = req.params;
30
+ const authzkit = Authzkit.getInstance();
31
+ await authzkit.deleteRole(name);
32
+ res.status(204).send();
33
+ } catch (err: any) {
34
+ res.status(500).json({ error: err.message });
35
+ }
36
+ });
37
+
38
+ export default router;
@@ -0,0 +1,81 @@
1
+ import { Router } from 'express';
2
+ import { Authzkit } from '../../classes/Authzkit';
3
+ import { Authorizable } from '../../interfaces/Authorizable';
4
+
5
+ const router: Router = Router();
6
+
7
+ router.get('/:userId/roles', async (req, res) => {
8
+ try {
9
+ const { userId } = req.params;
10
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
11
+ const authzkit = Authzkit.getInstance();
12
+ const roles = await authzkit.getUserRoles(user);
13
+ res.json(Array.from(roles));
14
+ } catch (err: any) {
15
+ res.status(500).json({ error: err.message });
16
+ }
17
+ });
18
+
19
+ router.post('/:userId/roles', async (req, res) => {
20
+ try {
21
+ const { userId } = req.params;
22
+ const { roleName } = req.body;
23
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
24
+ const authzkit = Authzkit.getInstance();
25
+ await authzkit.assignRole(user, roleName);
26
+ res.status(201).send();
27
+ } catch (err: any) {
28
+ res.status(400).json({ error: err.message });
29
+ }
30
+ });
31
+
32
+ router.delete('/:userId/roles/:roleName', async (req, res) => {
33
+ try {
34
+ const { userId, roleName } = req.params;
35
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
36
+ const authzkit = Authzkit.getInstance();
37
+ await authzkit.revokeRole(user, roleName);
38
+ res.status(204).send();
39
+ } catch (err: any) {
40
+ res.status(500).json({ error: err.message });
41
+ }
42
+ });
43
+
44
+ router.get('/:userId/permissions', async (req, res) => {
45
+ try {
46
+ const { userId } = req.params;
47
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
48
+ const authzkit = Authzkit.getInstance();
49
+ const permissions = await authzkit.getUserPermissions(user);
50
+ res.json(Array.from(permissions));
51
+ } catch (err: any) {
52
+ res.status(500).json({ error: err.message });
53
+ }
54
+ });
55
+
56
+ router.post('/:userId/permissions', async (req, res) => {
57
+ try {
58
+ const { userId } = req.params;
59
+ const { permissionName } = req.body;
60
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
61
+ const authzkit = Authzkit.getInstance();
62
+ await authzkit.assignPermission(user, permissionName);
63
+ res.status(201).send();
64
+ } catch (err: any) {
65
+ res.status(400).json({ error: err.message });
66
+ }
67
+ });
68
+
69
+ router.delete('/:userId/permissions/:permissionName', async (req, res) => {
70
+ try {
71
+ const { userId, permissionName } = req.params;
72
+ const user: Authorizable = { id: userId, roles: [], permissions: [] };
73
+ const authzkit = Authzkit.getInstance();
74
+ await authzkit.revokePermission(user, permissionName);
75
+ res.status(204).send();
76
+ } catch (err: any) {
77
+ res.status(500).json({ error: err.message });
78
+ }
79
+ });
80
+
81
+ export default router;
@@ -0,0 +1,73 @@
1
+ # React + TypeScript + Vite
2
+
3
+ This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4
+
5
+ Currently, two official plugins are available:
6
+
7
+ - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
8
+ - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9
+
10
+ ## React Compiler
11
+
12
+ The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
13
+
14
+ ## Expanding the ESLint configuration
15
+
16
+ If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
17
+
18
+ ```js
19
+ export default defineConfig([
20
+ globalIgnores(['dist']),
21
+ {
22
+ files: ['**/*.{ts,tsx}'],
23
+ extends: [
24
+ // Other configs...
25
+
26
+ // Remove tseslint.configs.recommended and replace with this
27
+ tseslint.configs.recommendedTypeChecked,
28
+ // Alternatively, use this for stricter rules
29
+ tseslint.configs.strictTypeChecked,
30
+ // Optionally, add this for stylistic rules
31
+ tseslint.configs.stylisticTypeChecked,
32
+
33
+ // Other configs...
34
+ ],
35
+ languageOptions: {
36
+ parserOptions: {
37
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
38
+ tsconfigRootDir: import.meta.dirname,
39
+ },
40
+ // other options...
41
+ },
42
+ },
43
+ ])
44
+ ```
45
+
46
+ You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
47
+
48
+ ```js
49
+ // eslint.config.js
50
+ import reactX from 'eslint-plugin-react-x'
51
+ import reactDom from 'eslint-plugin-react-dom'
52
+
53
+ export default defineConfig([
54
+ globalIgnores(['dist']),
55
+ {
56
+ files: ['**/*.{ts,tsx}'],
57
+ extends: [
58
+ // Other configs...
59
+ // Enable lint rules for React
60
+ reactX.configs['recommended-typescript'],
61
+ // Enable lint rules for React DOM
62
+ reactDom.configs.recommended,
63
+ ],
64
+ languageOptions: {
65
+ parserOptions: {
66
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
67
+ tsconfigRootDir: import.meta.dirname,
68
+ },
69
+ // other options...
70
+ },
71
+ },
72
+ ])
73
+ ```
@@ -0,0 +1,23 @@
1
+ import js from '@eslint/js'
2
+ import globals from 'globals'
3
+ import reactHooks from 'eslint-plugin-react-hooks'
4
+ import reactRefresh from 'eslint-plugin-react-refresh'
5
+ import tseslint from 'typescript-eslint'
6
+ import { defineConfig, globalIgnores } from 'eslint/config'
7
+
8
+ export default defineConfig([
9
+ globalIgnores(['dist']),
10
+ {
11
+ files: ['**/*.{ts,tsx}'],
12
+ extends: [
13
+ js.configs.recommended,
14
+ tseslint.configs.recommended,
15
+ reactHooks.configs.flat.recommended,
16
+ reactRefresh.configs.vite,
17
+ ],
18
+ languageOptions: {
19
+ ecmaVersion: 2020,
20
+ globals: globals.browser,
21
+ },
22
+ },
23
+ ])
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>web</title>
8
+ </head>
9
+ <body>
10
+ <div id="root"></div>
11
+ <script type="module" src="/src/main.tsx"></script>
12
+ </body>
13
+ </html>
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "web",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc -b && vite build",
9
+ "lint": "eslint .",
10
+ "preview": "vite preview"
11
+ },
12
+ "dependencies": {
13
+ "react": "^19.2.0",
14
+ "react-dom": "^19.2.0",
15
+ "react-router-dom": "^7.13.0"
16
+ },
17
+ "devDependencies": {
18
+ "@eslint/js": "^9.39.1",
19
+ "@types/node": "^24.10.1",
20
+ "@types/react": "^19.2.5",
21
+ "@types/react-dom": "^19.2.3",
22
+ "@vitejs/plugin-react": "^5.1.1",
23
+ "eslint": "^9.39.1",
24
+ "eslint-plugin-react-hooks": "^7.0.1",
25
+ "eslint-plugin-react-refresh": "^0.4.24",
26
+ "globals": "^16.5.0",
27
+ "typescript": "~5.9.3",
28
+ "typescript-eslint": "^8.46.4",
29
+ "vite": "^7.2.4"
30
+ }
31
+ }