@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.
- package/.openapi-generator/FILES +1 -0
- package/README.md +162 -105
- package/api.ts +2690 -538
- package/base.ts +1 -1
- package/configuration.ts +1 -1
- package/dist/api.d.ts +1887 -243
- package/dist/api.js +2425 -431
- package/dist/base.js +1 -1
- package/dist/common.d.ts +1 -1
- package/dist/common.js +2 -2
- package/dist/configuration.js +1 -1
- package/package.json +1 -1
package/.openapi-generator/FILES
CHANGED
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 {
|
|
11
|
-
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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(
|
|
25
|
-
const permissions = new PermissionsApi(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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:
|