@flipdish/authorization 0.0.3 → 0.0.6-rc.1764848040

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.
@@ -1,5 +1,6 @@
1
1
  .gitignore
2
2
  .npmignore
3
+ .openapi-generator-ignore
3
4
  README.md
4
5
  api.ts
5
6
  base.ts
package/README.md CHANGED
@@ -7,117 +7,174 @@ 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/authorization';
11
- import { describe, expect, test, it } from '@jest/globals';
10
+ import {
11
+ AuthorizationApi,
12
+ Configuration,
13
+ type ErrorResponse,
14
+ Permissions,
15
+ PermissionsApi,
16
+ UserPermissionsApi,
17
+ } from "@flipdish/authorization";
18
+ import { describe, expect, it, test } from "@jest/globals";
19
+ import axios, { isAxiosError } from "axios";
12
20
 
13
21
  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
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('Authorization Tests', () => {
28
- describe('Authorization', () => {
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
- });
41
-
42
- describe('Authenticate and Authorize', () => {
43
- it('should authenticate and authorize with a valid FD-Authorization cookie', async () => {
44
- const authorizationResponse = await authorization.authenticateAndAuthorize({
45
- headers: {
46
- 'Cookie': `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
47
- },
48
- action: Permissions.AnyAuditLogs,
49
- resource: {
50
- id: "org12345",
51
- type: "Org",
52
- }
53
- });
54
-
55
- expect(authorizationResponse.status).toBe(200);
56
- expect(authorizationResponse.data.authentication.authenticated).toBe(true);
57
- expect(authorizationResponse.data.authentication.principal?.type).toBe("User");
58
- expect(authorizationResponse.data.authentication.principal?.id).toBe("8147747");
59
- });
60
-
61
- it('should not authenticate and authorize with an invalid FD-Authorization cookie', async () => {
62
- try {
63
- await authorization.authenticateAndAuthorize({
64
- headers: {
65
- 'Cookie': `FD-Authorization=not-a-valid-cookie;`,
66
- },
67
- action: Permissions.AnyAuditLogs,
68
- resource: {
69
- id: "org12345",
70
- type: "Org",
71
- },
72
- });
73
- } catch (error: any) {
74
- expect(error.response.status).toBe(401);
75
- expect(error.response.data.message).toBe("Unauthenticated");
76
- }
77
- });
78
-
79
- it('should authenticate and authorize with a valid Bearer token', async () => {
80
- const authorizationResponse = await authorization.authenticateAndAuthorize({
81
- headers: {
82
- 'Authorization': `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
83
- },
84
- action: Permissions.AnyAuditLogs,
85
- resource: {
86
- id: "org12345",
87
- type: "Org",
88
- }
89
- });
90
-
91
- expect(authorizationResponse.status).toBe(200);
92
- expect(authorizationResponse.data.authentication.authenticated).toBe(true);
93
- expect(authorizationResponse.data.authentication.principal?.type).toBe("User");
94
- expect(authorizationResponse.data.authentication.principal?.id).toBe("8147747");
95
- });
96
- });
97
-
98
-
99
- test('Authorize', async () => {
100
- let testPrincipal: any = {
101
- id: "12345",
102
- type: "User",
103
- };
104
-
105
- let testResource: any = {
106
- id: "org12345",
107
- type: "Org",
108
- };
109
-
110
- const authorizationResponse = await authorization.authorize({
111
- principal: testPrincipal,
112
- action: Permissions.AnyAuditLogs,
113
- resource: testResource
114
- });
115
- expect(authorizationResponse.status).toBe(200);
116
- expect(authorizationResponse.data.allowed).toBe(false);
117
- expect(authorizationResponse.data.decision).toBe("DENY");
118
- });
32
+ const authorization = new AuthorizationApi(bearerConfiguration);
33
+ const permissions = new PermissionsApi(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 permissions.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
+ );
119
70
  });
71
+
72
+ describe("List User Permission Sets", () => {
73
+ it("should list user permission sets", async () => {
74
+ const userPermissionSetsResponse =
75
+ await userPermissions.listOwnPermissions("org42068");
76
+ expect(userPermissionSetsResponse.status).toBe(200);
77
+ expect(userPermissionSetsResponse.data.resources).toBeDefined();
78
+ expect(userPermissionSetsResponse.data.resources).toHaveProperty(
79
+ "org42068",
80
+ );
81
+ expect(
82
+ userPermissionSetsResponse.data.resources.org42068.permissions.length,
83
+ ).toBeGreaterThan(0);
84
+ });
85
+ });
86
+
87
+ describe("Authenticate and Authorize", () => {
88
+ it("should authenticate and authorize with a valid FD-Authorization cookie", async () => {
89
+ const authorizationResponse =
90
+ await authorization.authenticateAndAuthorize({
91
+ headers: {
92
+ Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
93
+ },
94
+ action: Permissions.AnyAuditLogs,
95
+ resource: {
96
+ id: "org12345",
97
+ type: "Org",
98
+ },
99
+ });
100
+
101
+ expect(authorizationResponse.status).toBe(200);
102
+ expect(authorizationResponse.data.authentication.authenticated).toBe(
103
+ true,
104
+ );
105
+ expect(authorizationResponse.data.authentication.principal?.type).toBe(
106
+ "User",
107
+ );
108
+ expect(authorizationResponse.data.authentication.principal?.id).toBe(
109
+ "8147747",
110
+ );
111
+ });
112
+
113
+ it("should not authenticate and authorize with an invalid FD-Authorization cookie", async () => {
114
+ try {
115
+ await authorization.authenticateAndAuthorize({
116
+ headers: {
117
+ Cookie: `FD-Authorization=not-a-valid-cookie;`,
118
+ },
119
+ action: Permissions.AnyAuditLogs,
120
+ resource: {
121
+ id: "org12345",
122
+ type: "Org",
123
+ },
124
+ });
125
+ } catch (error) {
126
+ if (isAxiosError<ErrorResponse>(error)) {
127
+ expect(error.response?.status).toBe(401);
128
+ expect(error.response?.data.message).toBe("Unauthenticated");
129
+ }
130
+ }
131
+ });
132
+
133
+ it("should authenticate and authorize with a valid Bearer token", async () => {
134
+ const authorizationResponse =
135
+ await authorization.authenticateAndAuthorize({
136
+ headers: {
137
+ Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
138
+ },
139
+ action: Permissions.AnyAuditLogs,
140
+ resource: {
141
+ id: "org12345",
142
+ type: "Org",
143
+ },
144
+ });
145
+
146
+ expect(authorizationResponse.status).toBe(200);
147
+ expect(authorizationResponse.data.authentication.authenticated).toBe(
148
+ true,
149
+ );
150
+ expect(authorizationResponse.data.authentication.principal?.type).toBe(
151
+ "User",
152
+ );
153
+ expect(authorizationResponse.data.authentication.principal?.id).toBe(
154
+ "8147747",
155
+ );
156
+ });
157
+ });
158
+
159
+ test("Authorize", async () => {
160
+ const authorizationResponse = await authorization.authorize({
161
+ principal: {
162
+ id: "12345",
163
+ type: "User",
164
+ },
165
+ action: Permissions.AnyAuditLogs,
166
+ resource: {
167
+ id: "org12345",
168
+ type: "Org",
169
+ },
170
+ });
171
+ expect(authorizationResponse.status).toBe(200);
172
+ expect(authorizationResponse.data.allowed).toBe(false);
173
+ expect(authorizationResponse.data.decision).toBe("DENY");
174
+ });
175
+ });
120
176
  });
177
+
121
178
  ```
122
179
 
123
180
  The generated Node module can be used in the following environments: