better-auth 0.2.3-beta.7 → 0.2.3

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/access.js CHANGED
@@ -1 +1,125 @@
1
- var a=class extends Error{path;constructor(e,n){super(e),this.path=n}},c=class{constructor(e){this.s=e;this.statements=e}statements;newRole(e){return new i(e)}},i=class s{statements;constructor(e){this.statements=e}authorize(e,n){for(let[t,o]of Object.entries(e)){let r=this.statements[t];if(!r)return{success:!1,error:`You are not allowed to access resource: ${t}`};let p=n==="OR"?o.some(m=>r.includes(m)):o.every(m=>r.includes(m));return p?{success:p}:{success:!1,error:`unauthorized to access resource "${t}"`}}return{success:!1,error:"Not authorized"}}static fromString(e){let n=JSON.parse(e);if(typeof n!="object")throw new a("statements is not an object",".");for(let[t,o]of Object.entries(n)){if(typeof t!="string")throw new a("invalid resource identifier",t);if(!Array.isArray(o))throw new a("actions is not an array",t);for(let r=0;r<o.length;r++)if(typeof o[r]!="string")throw new a("action is not a string",`${t}[${r}]`)}return new s(n)}toString(){return JSON.stringify(this.statements)}};var l=s=>new c(s),d={organization:["update","delete"],member:["create","update","delete"],invitation:["create","cancel"]},u=l(d),S=u.newRole({organization:["update"],invitation:["create","cancel"],member:["create","update","delete"]}),f=u.newRole({organization:["update","delete"],member:["create","update","delete"],invitation:["create","cancel"]}),x=u.newRole({organization:[],member:[],invitation:[]}),b={admin:S,owner:f,member:x};var T=s=>i.fromString(s??"");export{c as AccessControl,a as ParsingError,i as Role,S as adminAc,l as createAccessControl,u as defaultAc,b as defaultRoles,d as defaultStatements,x as memberAc,f as ownerAc,T as permissionFromString};
1
+ // src/plugins/organization/access/src/access.ts
2
+ var ParsingError = class extends Error {
3
+ path;
4
+ constructor(message, path) {
5
+ super(message);
6
+ this.path = path;
7
+ }
8
+ };
9
+ var AccessControl = class {
10
+ constructor(s) {
11
+ this.s = s;
12
+ this.statements = s;
13
+ }
14
+ statements;
15
+ newRole(statements) {
16
+ return new Role(statements);
17
+ }
18
+ };
19
+ var Role = class _Role {
20
+ statements;
21
+ constructor(statements) {
22
+ this.statements = statements;
23
+ }
24
+ authorize(request, connector) {
25
+ for (const [requestedResource, requestedActions] of Object.entries(
26
+ request
27
+ )) {
28
+ const allowedActions = this.statements[requestedResource];
29
+ if (!allowedActions) {
30
+ return {
31
+ success: false,
32
+ error: `You are not allowed to access resource: ${requestedResource}`
33
+ };
34
+ }
35
+ const success = connector === "OR" ? requestedActions.some(
36
+ (requestedAction) => allowedActions.includes(requestedAction)
37
+ ) : requestedActions.every(
38
+ (requestedAction) => allowedActions.includes(requestedAction)
39
+ );
40
+ if (success) {
41
+ return { success };
42
+ }
43
+ return {
44
+ success: false,
45
+ error: `unauthorized to access resource "${requestedResource}"`
46
+ };
47
+ }
48
+ return {
49
+ success: false,
50
+ error: "Not authorized"
51
+ };
52
+ }
53
+ static fromString(s) {
54
+ const statements = JSON.parse(s);
55
+ if (typeof statements !== "object") {
56
+ throw new ParsingError("statements is not an object", ".");
57
+ }
58
+ for (const [resource, actions] of Object.entries(statements)) {
59
+ if (typeof resource !== "string") {
60
+ throw new ParsingError("invalid resource identifier", resource);
61
+ }
62
+ if (!Array.isArray(actions)) {
63
+ throw new ParsingError("actions is not an array", resource);
64
+ }
65
+ for (let i = 0; i < actions.length; i++) {
66
+ if (typeof actions[i] !== "string") {
67
+ throw new ParsingError("action is not a string", `${resource}[${i}]`);
68
+ }
69
+ }
70
+ }
71
+ return new _Role(statements);
72
+ }
73
+ toString() {
74
+ return JSON.stringify(this.statements);
75
+ }
76
+ };
77
+
78
+ // src/plugins/organization/access/statement.ts
79
+ var createAccessControl = (statements) => {
80
+ return new AccessControl(statements);
81
+ };
82
+ var defaultStatements = {
83
+ organization: ["update", "delete"],
84
+ member: ["create", "update", "delete"],
85
+ invitation: ["create", "cancel"]
86
+ };
87
+ var defaultAc = createAccessControl(defaultStatements);
88
+ var adminAc = defaultAc.newRole({
89
+ organization: ["update"],
90
+ invitation: ["create", "cancel"],
91
+ member: ["create", "update", "delete"]
92
+ });
93
+ var ownerAc = defaultAc.newRole({
94
+ organization: ["update", "delete"],
95
+ member: ["create", "update", "delete"],
96
+ invitation: ["create", "cancel"]
97
+ });
98
+ var memberAc = defaultAc.newRole({
99
+ organization: [],
100
+ member: [],
101
+ invitation: []
102
+ });
103
+ var defaultRoles = {
104
+ admin: adminAc,
105
+ owner: ownerAc,
106
+ member: memberAc
107
+ };
108
+
109
+ // src/plugins/organization/access/utils.ts
110
+ var permissionFromString = (permission) => {
111
+ return Role.fromString(permission ?? "");
112
+ };
113
+ export {
114
+ AccessControl,
115
+ ParsingError,
116
+ Role,
117
+ adminAc,
118
+ createAccessControl,
119
+ defaultAc,
120
+ defaultRoles,
121
+ defaultStatements,
122
+ memberAc,
123
+ ownerAc,
124
+ permissionFromString
125
+ };