@flipdish/authorization 0.0.3-rc.1756733706 → 0.0.3-rc.1766093275

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/README.md CHANGED
@@ -7,125 +7,232 @@ Internally the package utilizes the [axios](https://github.com/axios/axios) as i
7
7
  ### Example code
8
8
 
9
9
  ```typescript
10
- import { AuthorizationApi, Configuration, PermissionsApi, Permissions } from '@flipdish/authorisation';
11
- import { describe, expect, test, it } from '@jest/globals';
12
-
13
- const basePath = "https://api.flipdish.co/auth/";
14
- const configuration = new Configuration({
15
- basePath,
16
- // to get the API key, you should follow these docs:
17
- // https://developers.flipdish.com/docs/getting-started
18
- accessToken: process.env.FLIPDISH_BEARER_TOKEN_PROD,
19
- // if using in a browser set useDefaultUserAgent
20
- // to true to prevent errors
21
- // useDefaultUserAgent: true
10
+ import {
11
+ AuthorizationApi,
12
+ Configuration,
13
+ ConfigurationDataApi,
14
+ type ErrorResponse,
15
+ Permissions,
16
+ UserPermissionsApi,
17
+ } from "@flipdish/authorization";
18
+ import { describe, expect, it, test } from "@jest/globals";
19
+ import axios, { isAxiosError } from "axios";
20
+
21
+ const basePath = "https://api.flipdish.co/auth/";
22
+ const bearerConfiguration = new Configuration({
23
+ basePath,
24
+ // to get the API key, you should follow these docs:
25
+ // https://developers.flipdish.com/docs/getting-started
26
+ accessToken: process.env.FLIPDISH_BEARER_TOKEN_PROD,
27
+ // if using in a browser set useDefaultUserAgent
28
+ // to true to prevent errors
29
+ // useDefaultUserAgent: true
22
30
  });
23
31
 
24
- const authorization = new AuthorizationApi(configuration);
25
- const permissions = new PermissionsApi(configuration);
26
-
27
- describe('Authorisation Tests', () => {
28
- describe('Authorisation', () => {
29
-
30
- test('List Permissions', async () => {
31
- const permissionsResponse = await permissions.listPermissions();
32
- expect(permissionsResponse.status).toBe(200);
33
- expect(permissionsResponse.data.permissions).toBeDefined();
34
- expect(permissionsResponse.data.permissions.length).toBeGreaterThan(0);
35
- expect(permissionsResponse.data.permissions).toContain(Permissions.ViewApp);
36
- expect(permissionsResponse.data.permissions).toContain(Permissions.CreateApp);
37
- expect(permissionsResponse.data.permissions).toContain(Permissions.UpdateApp);
38
- expect(permissionsResponse.data.permissions).toContain(Permissions.ViewAppName);
39
- expect(permissionsResponse.data.permissions).toContain(Permissions.EditAppAssets);
40
- });
32
+ const authorization = new AuthorizationApi(bearerConfiguration);
33
+ const configurationData = new ConfigurationDataApi(bearerConfiguration);
34
+
35
+ // mimic brower config where cookies will be sent automatically
36
+ // you shouldn't need to pass an axios instance as the cookies will be
37
+ // sent automatically by the browser
38
+ const userPermissions = new UserPermissionsApi(
39
+ new Configuration({ basePath }),
40
+ undefined,
41
+ axios.create({
42
+ headers: {
43
+ Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD_JM_CLIENT};`,
44
+ },
45
+ }),
46
+ );
47
+
48
+ describe("Authorization Tests", () => {
49
+ describe("Authorization", () => {
50
+ test("List Permissions", async () => {
51
+ const permissionsResponse = await configurationData.listPermissions();
52
+ expect(permissionsResponse.status).toBe(200);
53
+ expect(permissionsResponse.data.permissions).toBeDefined();
54
+ expect(permissionsResponse.data.permissions.length).toBeGreaterThan(0);
55
+ expect(permissionsResponse.data.permissions).toContain(
56
+ Permissions.ViewApp,
57
+ );
58
+ expect(permissionsResponse.data.permissions).toContain(
59
+ Permissions.CreateApp,
60
+ );
61
+ expect(permissionsResponse.data.permissions).toContain(
62
+ Permissions.UpdateApp,
63
+ );
64
+ expect(permissionsResponse.data.permissions).toContain(
65
+ Permissions.ViewAppName,
66
+ );
67
+ expect(permissionsResponse.data.permissions).toContain(
68
+ Permissions.EditAppAssets,
69
+ );
70
+ });
41
71
 
42
- describe('Authenticate and Authorise', () => {
43
- it('should authenticate and authorise with a valid FD-Authorization cookie', async () => {
44
- const authorizationResponse = await authorization.authenticateAndAuthorize({
45
- authenticateAndAuthorizeRequest: {
46
- headers: {
47
- 'Cookie': `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
48
- },
49
- action: Permissions.AnyAuditLogs,
50
- resource: {
51
- id: "org12345",
52
- type: "Org",
53
- }
54
- }
55
- });
56
-
57
- expect(authorizationResponse.status).toBe(200);
58
- expect(authorizationResponse.data.authentication.authenticated).toBe(true);
59
- expect(authorizationResponse.data.authentication.principal?.type).toBe("User");
60
- expect(authorizationResponse.data.authentication.principal?.id).toBe("8147747");
61
- });
62
-
63
- it('should not authenticate and authorise with an invalid FD-Authorization cookie', async () => {
64
- try {
65
- await authorization.authenticateAndAuthorize({
66
- authenticateAndAuthorizeRequest: {
67
- headers: {
68
- 'Cookie': `FD-Authorization=not-a-valid-cookie;`,
69
- },
70
- action: Permissions.AnyAuditLogs,
71
- resource: {
72
- id: "org12345",
73
- type: "Org",
74
- },
75
- }
76
- });
77
- } catch (error: any) {
78
- expect(error.response.status).toBe(401);
79
- expect(error.response.data.message).toBe("Unauthenticated");
80
- }
81
- });
82
-
83
- it('should authenticate and authorise with a valid Bearer token', async () => {
84
- const authorizationResponse = await authorization.authenticateAndAuthorize({
85
- authenticateAndAuthorizeRequest: {
86
- headers: {
87
- 'Authorization': `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
88
- },
89
- action: Permissions.AnyAuditLogs,
90
- resource: {
91
- id: "org12345",
92
- type: "Org",
93
- }
94
- }
95
- });
96
-
97
- expect(authorizationResponse.status).toBe(200);
98
- expect(authorizationResponse.data.authentication.authenticated).toBe(true);
99
- expect(authorizationResponse.data.authentication.principal?.type).toBe("User");
100
- expect(authorizationResponse.data.authentication.principal?.id).toBe("8147747");
101
- });
102
- });
72
+ test("List Feature Based Roles", async () => {
73
+ const featureBasedRolesResponse =
74
+ await configurationData.listFeatureBasedRoles();
75
+ expect(featureBasedRolesResponse.status).toBe(200);
76
+ expect(featureBasedRolesResponse.data.roles).toBeDefined();
77
+ expect(featureBasedRolesResponse.data.roles.length).toBeGreaterThan(0);
78
+ expect(featureBasedRolesResponse.data.roles).toContainEqual({
79
+ name: "OrgViewer",
80
+ permissions: ["ViewOrg"],
81
+ });
82
+ });
83
+
84
+ test("List named roles", async () => {
85
+ const namedRolesResponse = await configurationData.listRoles();
86
+ expect(namedRolesResponse.status).toBe(200);
87
+ expect(namedRolesResponse.data.roles).toBeDefined();
88
+ expect(namedRolesResponse.data.roles.length).toBeGreaterThan(0);
89
+ expect(namedRolesResponse.data.roles).toContainEqual("Admin");
90
+ });
103
91
 
92
+ describe("List User Permission Sets", () => {
93
+ it("should list user permission sets", async () => {
94
+ const userPermissionSetsResponse =
95
+ await userPermissions.listOwnPermissions("org42068");
96
+ expect(userPermissionSetsResponse.status).toBe(200);
97
+ expect(userPermissionSetsResponse.data.resources).toBeDefined();
98
+ expect(userPermissionSetsResponse.data.resources).toHaveProperty(
99
+ "org42068",
100
+ );
101
+ expect(
102
+ userPermissionSetsResponse.data.resources.org42068.permissions.length,
103
+ ).toBeGreaterThan(0);
104
+ });
105
+ });
106
+
107
+ describe("Authenticate and Authorize", () => {
108
+ it("should authenticate and authorize with a valid FD-Authorization cookie", async () => {
109
+ const authorizationResponse =
110
+ await authorization.authenticateAndAuthorize({
111
+ headers: {
112
+ Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
113
+ },
114
+ action: Permissions.AnyAuditLogs,
115
+ resource: {
116
+ id: "org12345",
117
+ type: "Org",
118
+ },
119
+ });
120
+
121
+ expect(authorizationResponse.status).toBe(200);
122
+ expect(authorizationResponse.data.authentication.authenticated).toBe(
123
+ true,
124
+ );
125
+ expect(authorizationResponse.data.authentication.principal?.type).toBe(
126
+ "User",
127
+ );
128
+ expect(authorizationResponse.data.authentication.principal?.id).toBe(
129
+ "8147747",
130
+ );
131
+ });
132
+
133
+ it("should not authenticate and authorize with an invalid FD-Authorization cookie", async () => {
134
+ try {
135
+ await authorization.authenticateAndAuthorize({
136
+ headers: {
137
+ Cookie: `FD-Authorization=not-a-valid-cookie;`,
138
+ },
139
+ action: Permissions.AnyAuditLogs,
140
+ resource: {
141
+ id: "org12345",
142
+ type: "Org",
143
+ },
144
+ });
145
+ } catch (error) {
146
+ if (isAxiosError<ErrorResponse>(error)) {
147
+ expect(error.response?.status).toBe(401);
148
+ expect(error.response?.data.message).toBe("Unauthenticated");
149
+ }
150
+ }
151
+ });
152
+
153
+ it("should authenticate and authorize with a valid Bearer token", async () => {
154
+ const authorizationResponse =
155
+ await authorization.authenticateAndAuthorize({
156
+ headers: {
157
+ Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
158
+ },
159
+ action: Permissions.AnyAuditLogs,
160
+ resource: {
161
+ id: "org12345",
162
+ type: "Org",
163
+ },
164
+ });
165
+
166
+ expect(authorizationResponse.status).toBe(200);
167
+ expect(authorizationResponse.data.authentication.authenticated).toBe(
168
+ true,
169
+ );
170
+ expect(authorizationResponse.data.authentication.principal?.type).toBe(
171
+ "User",
172
+ );
173
+ expect(authorizationResponse.data.authentication.principal?.id).toBe(
174
+ "8147747",
175
+ );
176
+ });
177
+ });
104
178
 
105
- test(&#39;Authorise&#39;, async () &#x3D;&gt; {
106
- let testPrincipal: any &#x3D; {
107
- id: &quot;12345&quot;,
108
- type: &quot;User&quot;,
109
- };
110
-
111
- let testResource: any &#x3D; {
112
- id: &quot;org12345&quot;,
113
- type: &quot;Org&quot;,
114
- };
115
-
116
- const authorizationResponse &#x3D; await authorization.authorize({
117
- authorizationRequest: {
118
- principal: testPrincipal,
119
- action: Permissions.AnyAuditLogs,
120
- resource: testResource
121
- }
122
- });
123
- expect(authorizationResponse.status).toBe(200);
124
- expect(authorizationResponse.data.allowed).toBe(false);
125
- expect(authorizationResponse.data.decision).toBe(&quot;DENY&quot;);
179
+ test("Authorize", async () => {
180
+ const authorizationResponse = await authorization.authorize({
181
+ principal: {
182
+ id: "12345",
183
+ type: "User",
184
+ },
185
+ action: Permissions.AnyAuditLogs,
186
+ resource: {
187
+ id: "org12345",
188
+ type: "Org",
189
+ },
190
+ });
191
+ expect(authorizationResponse.status).toBe(200);
192
+ expect(authorizationResponse.data.allowed).toBe(false);
193
+ expect(authorizationResponse.data.decision).toBe("DENY");
194
+ });
195
+
196
+ describe("Check is in role", () => {
197
+ it("should check if a user is in a role", async () => {
198
+ const isInRoleResponse = await authorization.checkIsInRole({
199
+ principal: {
200
+ id: "12345",
201
+ type: "User",
202
+ },
203
+ roles: ["Admin"],
126
204
  });
205
+ expect(isInRoleResponse.status).toBe(200);
206
+ expect(isInRoleResponse.data.authorized).toBe(false);
207
+ });
208
+
209
+ it("should authenticate and check if a user is in a role with a valid FD-Authorization cookie", async () => {
210
+ const isInRoleResponse =
211
+ await authorization.authenticateAndCheckIsInRole({
212
+ headers: {
213
+ Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
214
+ },
215
+ roles: ["Admin"],
216
+ });
217
+ expect(isInRoleResponse.status).toBe(200);
218
+ expect(isInRoleResponse.data.authorized).toBe(false);
219
+ });
220
+
221
+ it("should authenticate and check if a user is in a role with a valid Bearer token", async () => {
222
+ const isInRoleResponse =
223
+ await authorization.authenticateAndCheckIsInRole({
224
+ headers: {
225
+ Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
226
+ },
227
+ roles: ["Admin"],
228
+ });
229
+ expect(isInRoleResponse.status).toBe(200);
230
+ expect(isInRoleResponse.data.authorized).toBe(false);
231
+ });
127
232
  });
233
+ });
128
234
  });
235
+
129
236
  ```
130
237
 
131
238
  The generated Node module can be used in the following environments: