@flipdish/authorization 0.0.0-preview.184.g0a86e1e
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 +218 -0
- package/dist/index.d.mts +3880 -0
- package/dist/index.d.ts +3880 -0
- package/dist/index.js +1901 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1878 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
## @flipdish/authorization
|
|
2
|
+
|
|
3
|
+
This package provides a TypeScript/JavaScript client for Flipdish's Authorization APIs over HTTP.
|
|
4
|
+
|
|
5
|
+
It is generated with [Hey API](https://heyapi.dev) and uses the native `fetch` API, so it has no runtime dependencies. It works in Node.js 18+, Bun, Deno and browsers, and ships both ES modules and CommonJS.
|
|
6
|
+
|
|
7
|
+
### Example code
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import {
|
|
11
|
+
createFlipdishClient,
|
|
12
|
+
Permissions,
|
|
13
|
+
RoleNames,
|
|
14
|
+
} from "@flipdish/authorization";
|
|
15
|
+
import { describe, expect, it, test } from "@jest/globals";
|
|
16
|
+
|
|
17
|
+
const authorization = createFlipdishClient({
|
|
18
|
+
// to get the API key, you should follow these docs:
|
|
19
|
+
// https://developers.flipdish.com/docs/getting-started
|
|
20
|
+
accessToken: process.env.FLIPDISH_BEARER_TOKEN_PROD,
|
|
21
|
+
// optional: identifies your app in the X-Flipdish-Client header,
|
|
22
|
+
// and in User-Agent outside browsers. Defaults to env vars such as DD_SERVICE.
|
|
23
|
+
clientName: "sdk-test-app",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// mimic browser config where cookies will be sent automatically
|
|
27
|
+
// you shouldn't need to set the Cookie header in a browser as the
|
|
28
|
+
// cookies will be sent automatically by the browser
|
|
29
|
+
const cookieAuthorization = createFlipdishClient({
|
|
30
|
+
headers: {
|
|
31
|
+
Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD_JM_CLIENT};`,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("Authorization Tests", () => {
|
|
36
|
+
describe("Authorization", () => {
|
|
37
|
+
test("List Permissions", async () => {
|
|
38
|
+
const { data, response } = await authorization.listPermissions();
|
|
39
|
+
expect(response?.status).toBe(200);
|
|
40
|
+
expect(data?.permissions).toBeDefined();
|
|
41
|
+
expect(data?.permissions.length).toBeGreaterThan(0);
|
|
42
|
+
expect(data?.permissions).toContain(Permissions.ViewApp);
|
|
43
|
+
expect(data?.permissions).toContain(Permissions.CreateApp);
|
|
44
|
+
expect(data?.permissions).toContain(Permissions.UpdateApp);
|
|
45
|
+
expect(data?.permissions).toContain(Permissions.ViewAppName);
|
|
46
|
+
expect(data?.permissions).toContain(Permissions.EditAppAssets);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("List named roles", async () => {
|
|
50
|
+
const { data, response } = await authorization.listRoles();
|
|
51
|
+
expect(response?.status).toBe(200);
|
|
52
|
+
expect(data?.roles).toBeDefined();
|
|
53
|
+
expect(data?.roles.length).toBeGreaterThan(0);
|
|
54
|
+
expect(data?.roles).toContainEqual(RoleNames.Admin);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("List User Permission Sets", () => {
|
|
58
|
+
it("should list user permission sets", async () => {
|
|
59
|
+
const { data, response } = await cookieAuthorization.listOwnPermissions(
|
|
60
|
+
{ path: { orgId: "org42068" } },
|
|
61
|
+
);
|
|
62
|
+
expect(response?.status).toBe(200);
|
|
63
|
+
expect(data?.resources).toBeDefined();
|
|
64
|
+
expect(data?.resources).toHaveProperty("org42068");
|
|
65
|
+
expect(data?.resources.org42068.permissions.length).toBeGreaterThan(0);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("Authenticate and Authorize", () => {
|
|
70
|
+
it("should authenticate and authorize with a valid FD-Authorization cookie", async () => {
|
|
71
|
+
const { data, response } = await authorization.authenticateAndAuthorize(
|
|
72
|
+
{
|
|
73
|
+
body: {
|
|
74
|
+
headers: {
|
|
75
|
+
Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
|
|
76
|
+
},
|
|
77
|
+
action: Permissions.AnyAuditLogs,
|
|
78
|
+
resource: {
|
|
79
|
+
id: "org12345",
|
|
80
|
+
type: "Org",
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(response?.status).toBe(200);
|
|
87
|
+
expect(data?.authentication.authenticated).toBe(true);
|
|
88
|
+
expect(data?.authentication.principal?.type).toBe("User");
|
|
89
|
+
expect(data?.authentication.principal?.id).toBe("8147747");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should not authenticate and authorize with an invalid FD-Authorization cookie", async () => {
|
|
93
|
+
// errors are returned rather than thrown, unless throwOnError: true is set
|
|
94
|
+
const { error, response } =
|
|
95
|
+
await authorization.authenticateAndAuthorize({
|
|
96
|
+
body: {
|
|
97
|
+
headers: {
|
|
98
|
+
Cookie: `FD-Authorization=not-a-valid-cookie;`,
|
|
99
|
+
},
|
|
100
|
+
action: Permissions.AnyAuditLogs,
|
|
101
|
+
resource: {
|
|
102
|
+
id: "org12345",
|
|
103
|
+
type: "Org",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
expect(response?.status).toBe(401);
|
|
109
|
+
expect(error?.message).toBe("Unauthenticated");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should authenticate and authorize with a valid Bearer token", async () => {
|
|
113
|
+
const { data, response } = await authorization.authenticateAndAuthorize(
|
|
114
|
+
{
|
|
115
|
+
body: {
|
|
116
|
+
headers: {
|
|
117
|
+
Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
|
|
118
|
+
},
|
|
119
|
+
action: Permissions.AnyAuditLogs,
|
|
120
|
+
resource: {
|
|
121
|
+
id: "org12345",
|
|
122
|
+
type: "Org",
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(response?.status).toBe(200);
|
|
129
|
+
expect(data?.authentication.authenticated).toBe(true);
|
|
130
|
+
expect(data?.authentication.principal?.type).toBe("User");
|
|
131
|
+
expect(data?.authentication.principal?.id).toBe("8147747");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("Authorize", async () => {
|
|
136
|
+
const { data, response } = await authorization.authorize({
|
|
137
|
+
body: {
|
|
138
|
+
principal: {
|
|
139
|
+
id: "12345",
|
|
140
|
+
type: "User",
|
|
141
|
+
},
|
|
142
|
+
action: Permissions.AnyAuditLogs,
|
|
143
|
+
resource: {
|
|
144
|
+
id: "org12345",
|
|
145
|
+
type: "Org",
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
expect(response?.status).toBe(200);
|
|
150
|
+
expect(data?.allowed).toBe(false);
|
|
151
|
+
expect(data?.decision).toBe("DENY");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("Check is in role", () => {
|
|
155
|
+
it("should check if a user is in a role", async () => {
|
|
156
|
+
const { data, response } = await authorization.checkIsInRole({
|
|
157
|
+
body: {
|
|
158
|
+
principal: {
|
|
159
|
+
id: "12345",
|
|
160
|
+
type: "User",
|
|
161
|
+
},
|
|
162
|
+
roles: [RoleNames.Admin],
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
expect(response?.status).toBe(200);
|
|
166
|
+
expect(data?.authorized).toBe(false);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should authenticate and check if a user is in a role with a valid FD-Authorization cookie", async () => {
|
|
170
|
+
const { data, response } =
|
|
171
|
+
await authorization.authenticateAndCheckIsInRole({
|
|
172
|
+
body: {
|
|
173
|
+
headers: {
|
|
174
|
+
Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
|
|
175
|
+
},
|
|
176
|
+
roles: [RoleNames.Admin],
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
expect(response?.status).toBe(200);
|
|
180
|
+
expect(data?.authorized).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("should authenticate and check if a user is in a role with a valid Bearer token", async () => {
|
|
184
|
+
const { data, response } =
|
|
185
|
+
await authorization.authenticateAndCheckIsInRole({
|
|
186
|
+
body: {
|
|
187
|
+
headers: {
|
|
188
|
+
Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
|
|
189
|
+
},
|
|
190
|
+
roles: [RoleNames.Admin],
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
expect(response?.status).toBe(200);
|
|
194
|
+
expect(data?.authorized).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Client identity
|
|
203
|
+
|
|
204
|
+
Every request carries an `X-Flipdish-Client` header identifying the SDK and the app using it. Outside browsers the same value is also sent as `User-Agent`, for example:
|
|
205
|
+
|
|
206
|
+
```
|
|
207
|
+
Flipdish-authorization-ts/0.0.0-preview.184.g0a86e1e menu-service/2.3.1 env/production node/22.18.0
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
The app name and version are read from the first of these that is set:
|
|
211
|
+
|
|
212
|
+
| Part | Option | Environment variables |
|
|
213
|
+
|------|--------|-----------------------|
|
|
214
|
+
| App name | `clientName` | `FLIPDISH_CLIENT_NAME`, `DD_SERVICE`, `AWS_LAMBDA_FUNCTION_NAME`, `npm_package_name` |
|
|
215
|
+
| App version | `clientVersion` | `FLIPDISH_CLIENT_VERSION`, `DD_VERSION`, `npm_package_version` |
|
|
216
|
+
| Environment | | `DD_ENV`, `NODE_ENV` |
|
|
217
|
+
|
|
218
|
+
Browsers have no environment variables, so pass `clientName` to `createFlipdishClient` there. Anything passed in `headers` overrides these values.
|