@murumets-ee/auth 0.1.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/LICENSE +94 -0
- package/README.md +15 -0
- package/dist/admin/index.d.ts +72 -0
- package/dist/admin/index.js +1 -0
- package/dist/chunk-2GGGFSMD.js +1 -0
- package/dist/chunk-NH6AU5Z6.js +1 -0
- package/dist/client.d.ts +2064 -0
- package/dist/client.js +1 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.js +1 -0
- package/dist/plugin-C-MA3A5U.d.ts +993 -0
- package/dist/plugin.d.ts +6 -0
- package/dist/plugin.js +1 -0
- package/dist/server-GEU5KK6Y.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject
|
|
14
|
+
to the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set
|
|
20
|
+
of the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other
|
|
27
|
+
notices of the licensor in the software. Any use of the licensor's trademarks
|
|
28
|
+
is subject to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license
|
|
39
|
+
for the software granted under these terms ends immediately. If your company
|
|
40
|
+
makes such a claim, your patent license ends immediately for work on behalf
|
|
41
|
+
of your company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from
|
|
46
|
+
you also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license
|
|
61
|
+
no later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your
|
|
64
|
+
licenses to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is
|
|
76
|
+
the software the licensor makes available under these terms, including any
|
|
77
|
+
portion of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that organization.
|
|
84
|
+
**control** means ownership of substantially all the assets of an entity, or
|
|
85
|
+
the power to direct the management and policies of an entity (for example, by
|
|
86
|
+
voting right, contract, or otherwise). Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your
|
|
92
|
+
licenses.
|
|
93
|
+
|
|
94
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @murumets-ee/auth
|
|
2
|
+
|
|
3
|
+
Authentication layer — wraps better-auth with role-based access, session management, and audit hooks.
|
|
4
|
+
|
|
5
|
+
Part of [Lumi CMS](https://github.com/murumets-ee/lumi-cms) — a modular, type-safe CMS toolkit for Node.js.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @murumets-ee/auth
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
[Elastic License 2.0 (ELv2)](../../LICENSE)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Permission management admin routes for the centralized admin API handler.
|
|
3
|
+
*
|
|
4
|
+
* Provides CRUD for role definitions and permission assignments.
|
|
5
|
+
* Admin-only by default (resource: 'permissions', only admin has access).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createAdminApiHandler } from '@murumets-ee/admin-ui/server'
|
|
10
|
+
* import { permissionRoutes } from '@murumets-ee/auth/admin'
|
|
11
|
+
*
|
|
12
|
+
* const handler = createAdminApiHandler({
|
|
13
|
+
* authenticate: async (req) => { ... },
|
|
14
|
+
* entities: [...],
|
|
15
|
+
* routes: [
|
|
16
|
+
* permissionRoutes({
|
|
17
|
+
* getStatements: () => catalog,
|
|
18
|
+
* loadRoles: () => client.get('roles'),
|
|
19
|
+
* saveRoles: (roles) => client.set('roles', roles),
|
|
20
|
+
* onSave: () => { cachedChecker = null },
|
|
21
|
+
* }),
|
|
22
|
+
* ],
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
/** Fire-and-forget audit log function — structurally matches AuditLogFn from @murumets-ee/core */
|
|
27
|
+
type AuditLogFn = (entry: {
|
|
28
|
+
action: string;
|
|
29
|
+
entityType?: string;
|
|
30
|
+
entityId?: string;
|
|
31
|
+
userId?: string;
|
|
32
|
+
userName?: string;
|
|
33
|
+
changes?: Record<string, unknown>;
|
|
34
|
+
metadata?: Record<string, unknown>;
|
|
35
|
+
}) => void;
|
|
36
|
+
interface AdminRoute {
|
|
37
|
+
prefix: string;
|
|
38
|
+
resource?: string;
|
|
39
|
+
actions?: readonly string[];
|
|
40
|
+
handlers: Partial<Record<string, (req: Request, ctx: {
|
|
41
|
+
segments: string[];
|
|
42
|
+
user: {
|
|
43
|
+
id: string;
|
|
44
|
+
role?: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
};
|
|
47
|
+
audit?: AuditLogFn;
|
|
48
|
+
checkPermission: (resource: string, action: string) => boolean;
|
|
49
|
+
}) => Promise<Response>>>;
|
|
50
|
+
}
|
|
51
|
+
interface PermissionRoutesConfig {
|
|
52
|
+
/** Returns the resource catalog: resource → available actions */
|
|
53
|
+
getStatements: () => Record<string, readonly string[]>;
|
|
54
|
+
/** Load saved role definitions from settings (null if first run) */
|
|
55
|
+
loadRoles: () => Promise<Record<string, Record<string, string[]>> | null>;
|
|
56
|
+
/** Save role definitions to settings */
|
|
57
|
+
saveRoles: (roles: Record<string, Record<string, string[]>>) => Promise<void>;
|
|
58
|
+
/** Called after save to invalidate cached permission checker */
|
|
59
|
+
onSave?: () => void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create admin API routes for permission management.
|
|
63
|
+
*
|
|
64
|
+
* Routes (all prefixed with `/api/admin/permissions`):
|
|
65
|
+
* - `GET /permissions` — Get statements + roles + builtInRoles
|
|
66
|
+
* - `PATCH /permissions` — Update role permissions
|
|
67
|
+
* - `POST /permissions/roles` — Create a custom role
|
|
68
|
+
* - `DELETE /permissions/roles/:name` — Delete a custom role
|
|
69
|
+
*/
|
|
70
|
+
declare function permissionRoutes(config: PermissionRoutesConfig): AdminRoute;
|
|
71
|
+
|
|
72
|
+
export { permissionRoutes };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{d as f}from"../chunk-NH6AU5Z6.js";var P=/^[a-z][a-z0-9-]{0,49}$/;function u(d,c=200){return new Response(JSON.stringify(d),{status:c,headers:{"Content-Type":"application/json"}})}function r(d,c){return u({error:d},c)}function T(d){let{getStatements:c,loadRoles:g,saveRoles:R,onSave:b}=d;return{prefix:"permissions",resource:"permissions",actions:["view","create","update","delete"],handlers:{GET:async(m,{segments:s})=>{let t=c(),i=await g()??{};return s.length===1&&s[0]==="roles"?u({roles:Object.keys(i),builtInRoles:[...f]}):u({statements:t,roles:i,builtInRoles:[...f]})},PATCH:async(m,{user:s,audit:t})=>{let i=await m.json(),{roles:e}=i;if(!e||typeof e!="object")return r('Body must contain "roles" object',400);if("admin"in e)return r("Cannot modify admin role permissions (admin always has full access)",400);if(s.role&&s.role!=="admin"&&s.role in e)return r("Cannot modify permissions for your own role",403);let n=await g()??{};for(let o of Object.keys(e))if(!(o in n))return r(`Role '${o}' does not exist. Create it first via POST /permissions/roles`,400);let a=c();for(let[o,l]of Object.entries(e)){if(typeof o!="string"||!o)return r("Role names must be non-empty strings",400);if(typeof l!="object"||l===null||Array.isArray(l))return r(`Permissions for role '${o}' must be an object`,400);for(let[p,w]of Object.entries(l)){if(!Array.isArray(w)||!w.every(y=>typeof y=="string"))return r(`Actions for '${p}' in role '${o}' must be a string array`,400);let h=a[p];if(!h)return r(`Unknown resource: ${p}`,400);for(let y of w)if(!h.includes(y))return r(`Invalid action '${y}' for resource '${p}'. Valid: ${h.join(", ")}`,400)}}let j={...n};for(let[o,l]of Object.entries(e))j[o]=l;return await R(j),b?.(),t?.({action:"permissions.update",entityType:"permissions",userId:s.id,userName:s.name,changes:{roles:e},metadata:{rolesModified:Object.keys(e)}}),u({ok:!0})},POST:async(m,{segments:s,user:t,audit:i})=>{if(s.length!==1||s[0]!=="roles")return r("POST only supported at /permissions/roles",400);let e=await m.json(),{name:n}=e;if(!n||typeof n!="string")return r('Body must contain "name" string',400);if(!P.test(n))return r("Role name must be lowercase alphanumeric with hyphens, start with a letter, max 50 chars",400);if(f.includes(n))return r(`Cannot create role with built-in name: ${n}`,400);let a=await g()??{};return n in a?r(`Role already exists: ${n}`,409):(a[n]={},await R(a),b?.(),i?.({action:"permissions.role.create",entityType:"permissions",userId:t.id,userName:t.name,changes:{roleName:n}}),u({name:n,permissions:{}},201))},DELETE:async(m,{segments:s,user:t,audit:i})=>{if(s.length!==2||s[0]!=="roles")return r("DELETE only supported at /permissions/roles/:name",400);let e=s[1];if(f.includes(e))return r(`Cannot delete built-in role: ${e}`,400);let n=await g()??{};if(!(e in n))return r(`Role not found: ${e}`,404);let a=n[e];return delete n[e],await R(n),b?.(),i?.({action:"permissions.role.delete",entityType:"permissions",userId:t.id,userName:t.name,changes:{roleName:e,permissions:a}}),u({deleted:e})}}}}export{T as permissionRoutes};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=null;function a(){if(!e)throw new Error("@murumets-ee/auth not initialized. Add auth() to your toolkit config plugins.");return e}function m(r={}){return{name:"@murumets-ee/auth",init:async t=>{let{createAuthServer:u}=await import("./server-GEU5KK6Y.js"),i;if(r.audit!==!1){let{createAuditLogger:o,createAuditDbWriter:n,createLogger:g}=await import("@murumets-ee/logging");i=o({logger:g({name:"auth-audit"}),dbWriter:n(t.db.readWrite)})}e=u(r,t,i),t.logger.info("Auth plugin initialized")}}}export{a,m as b};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createAccessControl as p}from"better-auth/plugins/access";var u=["view","create","update","delete"],d=["view","create","update","delete","publish"];var g=["admin","public","authenticated"],m={GET:"view",POST:"create",PATCH:"update",DELETE:"delete"};function f(){return{public:{},authenticated:{}}}function b(n){let r=new Map;for(let[e,t]of Object.entries(n)){let s=new Map;for(let[c,a]of Object.entries(t))s.set(c,new Set(a));r.set(e,s)}return(e,t,s)=>e==="admin"?!0:r.get(e)?.get(t)?.has(s)??!1}function i(n){return n.behaviors?.some(r=>r.name==="publishable")??!1}function R(n,r){let e={};for(let t of n)e[t.name]=i(t)?["view","create","update","delete","publish"]:["view","create","update","delete"];if(r)for(let t of r)t.resource&&t.actions&&!(t.resource in e)&&(e[t.resource]=[...t.actions]);return e}var o={user:["create","list","set-role","ban","impersonate","delete","set-password","get","update"],session:["list","revoke","delete"]};function h(n){let r={...o};for(let e of n)r[e.name]=i(e)?d:u;return r}function T(n,r){let e={},t={};e.user=[...o.user],e.session=[...o.session],t.user=[],t.session=[];for(let s of r)e[s.name]=i(s)?["view","create","update","delete","publish"]:["view","create","update","delete"],t[s.name]=["view"];return{admin:n.newRole(e),authenticated:n.newRole(t)}}export{p as a,u as b,d as c,g as d,m as e,f,b as g,R as h,h as i,T as j};
|