@flipdish/authorization 0.2.61-rc.1793282230 → 1.0.0-rc.1794579987
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 +146 -159
- package/dist/index.d.mts +3886 -0
- package/dist/index.d.ts +3882 -9
- package/dist/index.js +1950 -30
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1928 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -15
- package/.openapi-generator/FILES +0 -12
- package/.openapi-generator/VERSION +0 -1
- package/.openapi-generator-ignore +0 -23
- package/api.ts +0 -7532
- package/base.ts +0 -86
- package/common.ts +0 -150
- package/configuration.ts +0 -128
- package/dist/api.d.ts +0 -5385
- package/dist/api.js +0 -5452
- package/dist/base.d.ts +0 -66
- package/dist/base.js +0 -86
- package/dist/common.d.ts +0 -65
- package/dist/common.js +0 -253
- package/dist/configuration.d.ts +0 -99
- package/dist/configuration.js +0 -61
- package/git_push.sh +0 -57
- package/index.ts +0 -18
- package/tsconfig.json +0 -21
package/README.md
CHANGED
|
@@ -1,221 +1,197 @@
|
|
|
1
1
|
## @flipdish/authorization
|
|
2
2
|
|
|
3
|
-
This package provides a
|
|
3
|
+
This package provides a TypeScript/JavaScript client for Flipdish's Authorization APIs over HTTP.
|
|
4
4
|
|
|
5
|
-
|
|
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
6
|
|
|
7
7
|
### Example code
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
Configuration,
|
|
13
|
-
ConfigurationDataApi,
|
|
14
|
-
type ErrorResponse,
|
|
11
|
+
createFlipdishClient,
|
|
15
12
|
Permissions,
|
|
16
|
-
|
|
13
|
+
RoleNames,
|
|
17
14
|
} from "@flipdish/authorization";
|
|
18
15
|
import { describe, expect, it, test } from "@jest/globals";
|
|
19
|
-
import axios, { isAxiosError } from "axios";
|
|
20
16
|
|
|
21
|
-
const
|
|
22
|
-
const bearerConfiguration = new Configuration({
|
|
23
|
-
basePath,
|
|
17
|
+
const authorization = createFlipdishClient({
|
|
24
18
|
// to get the API key, you should follow these docs:
|
|
25
19
|
// https://developers.flipdish.com/docs/getting-started
|
|
26
20
|
accessToken: process.env.FLIPDISH_BEARER_TOKEN_PROD,
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
|
|
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",
|
|
30
24
|
});
|
|
31
25
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
undefined,
|
|
41
|
-
axios.create({
|
|
42
|
-
headers: {
|
|
43
|
-
Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD_JM_CLIENT};`,
|
|
44
|
-
},
|
|
45
|
-
}),
|
|
46
|
-
);
|
|
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
|
+
});
|
|
47
34
|
|
|
48
35
|
describe("Authorization Tests", () => {
|
|
49
36
|
describe("Authorization", () => {
|
|
50
37
|
test("List Permissions", async () => {
|
|
51
|
-
const
|
|
52
|
-
expect(
|
|
53
|
-
expect(
|
|
54
|
-
expect(
|
|
55
|
-
expect(
|
|
56
|
-
|
|
57
|
-
);
|
|
58
|
-
expect(
|
|
59
|
-
|
|
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
|
-
);
|
|
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);
|
|
70
47
|
});
|
|
71
48
|
|
|
72
49
|
test("List named roles", async () => {
|
|
73
|
-
const
|
|
74
|
-
expect(
|
|
75
|
-
expect(
|
|
76
|
-
expect(
|
|
77
|
-
expect(
|
|
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);
|
|
78
55
|
});
|
|
79
56
|
|
|
80
57
|
describe("List User Permission Sets", () => {
|
|
81
58
|
it("should list user permission sets", async () => {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
expect(userPermissionSetsResponse.status).toBe(200);
|
|
85
|
-
expect(userPermissionSetsResponse.data.resources).toBeDefined();
|
|
86
|
-
expect(userPermissionSetsResponse.data.resources).toHaveProperty(
|
|
87
|
-
"org42068",
|
|
59
|
+
const { data, response } = await cookieAuthorization.listOwnPermissions(
|
|
60
|
+
{ path: { orgId: "org42068" } },
|
|
88
61
|
);
|
|
89
|
-
expect(
|
|
90
|
-
|
|
91
|
-
).
|
|
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);
|
|
92
66
|
});
|
|
93
67
|
});
|
|
94
68
|
|
|
95
69
|
describe("Authenticate and Authorize", () => {
|
|
96
70
|
it("should authenticate and authorize with a valid FD-Authorization cookie", async () => {
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
},
|
|
106
82
|
},
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
expect(authorizationResponse.status).toBe(200);
|
|
110
|
-
expect(authorizationResponse.data.authentication.authenticated).toBe(
|
|
111
|
-
true,
|
|
112
|
-
);
|
|
113
|
-
expect(authorizationResponse.data.authentication.principal?.type).toBe(
|
|
114
|
-
"User",
|
|
115
|
-
);
|
|
116
|
-
expect(authorizationResponse.data.authentication.principal?.id).toBe(
|
|
117
|
-
"8147747",
|
|
83
|
+
},
|
|
118
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");
|
|
119
90
|
});
|
|
120
91
|
|
|
121
92
|
it("should not authenticate and authorize with an invalid FD-Authorization cookie", async () => {
|
|
122
|
-
|
|
93
|
+
// errors are returned rather than thrown, unless throwOnError: true is set
|
|
94
|
+
const { error, response } =
|
|
123
95
|
await authorization.authenticateAndAuthorize({
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
+
},
|
|
131
105
|
},
|
|
132
106
|
});
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
expect(error.response?.data.message).toBe("Unauthenticated");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
107
|
+
|
|
108
|
+
expect(response?.status).toBe(401);
|
|
109
|
+
expect(error?.message).toBe("Unauthenticated");
|
|
139
110
|
});
|
|
140
111
|
|
|
141
112
|
it("should authenticate and authorize with a valid Bearer token", async () => {
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
+
},
|
|
151
124
|
},
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
expect(authorizationResponse.status).toBe(200);
|
|
155
|
-
expect(authorizationResponse.data.authentication.authenticated).toBe(
|
|
156
|
-
true,
|
|
157
|
-
);
|
|
158
|
-
expect(authorizationResponse.data.authentication.principal?.type).toBe(
|
|
159
|
-
"User",
|
|
160
|
-
);
|
|
161
|
-
expect(authorizationResponse.data.authentication.principal?.id).toBe(
|
|
162
|
-
"8147747",
|
|
125
|
+
},
|
|
163
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");
|
|
164
132
|
});
|
|
165
133
|
});
|
|
166
134
|
|
|
167
135
|
test("Authorize", async () => {
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
+
},
|
|
177
147
|
},
|
|
178
148
|
});
|
|
179
|
-
expect(
|
|
180
|
-
expect(
|
|
181
|
-
expect(
|
|
149
|
+
expect(response?.status).toBe(200);
|
|
150
|
+
expect(data?.allowed).toBe(false);
|
|
151
|
+
expect(data?.decision).toBe("DENY");
|
|
182
152
|
});
|
|
183
153
|
|
|
184
154
|
describe("Check is in role", () => {
|
|
185
155
|
it("should check if a user is in a role", async () => {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
156
|
+
const { data, response } = await authorization.checkIsInRole({
|
|
157
|
+
body: {
|
|
158
|
+
principal: {
|
|
159
|
+
id: "12345",
|
|
160
|
+
type: "User",
|
|
161
|
+
},
|
|
162
|
+
roles: [RoleNames.Admin],
|
|
190
163
|
},
|
|
191
|
-
roles: ["Admin"],
|
|
192
164
|
});
|
|
193
|
-
expect(
|
|
194
|
-
expect(
|
|
165
|
+
expect(response?.status).toBe(200);
|
|
166
|
+
expect(data?.authorized).toBe(false);
|
|
195
167
|
});
|
|
196
168
|
|
|
197
169
|
it("should authenticate and check if a user is in a role with a valid FD-Authorization cookie", async () => {
|
|
198
|
-
const
|
|
170
|
+
const { data, response } =
|
|
199
171
|
await authorization.authenticateAndCheckIsInRole({
|
|
200
|
-
|
|
201
|
-
|
|
172
|
+
body: {
|
|
173
|
+
headers: {
|
|
174
|
+
Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
|
|
175
|
+
},
|
|
176
|
+
roles: [RoleNames.Admin],
|
|
202
177
|
},
|
|
203
|
-
roles: ["Admin"],
|
|
204
178
|
});
|
|
205
|
-
expect(
|
|
206
|
-
expect(
|
|
179
|
+
expect(response?.status).toBe(200);
|
|
180
|
+
expect(data?.authorized).toBe(true);
|
|
207
181
|
});
|
|
208
182
|
|
|
209
183
|
it("should authenticate and check if a user is in a role with a valid Bearer token", async () => {
|
|
210
|
-
const
|
|
184
|
+
const { data, response } =
|
|
211
185
|
await authorization.authenticateAndCheckIsInRole({
|
|
212
|
-
|
|
213
|
-
|
|
186
|
+
body: {
|
|
187
|
+
headers: {
|
|
188
|
+
Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
|
|
189
|
+
},
|
|
190
|
+
roles: [RoleNames.Admin],
|
|
214
191
|
},
|
|
215
|
-
roles: ["Admin"],
|
|
216
192
|
});
|
|
217
|
-
expect(
|
|
218
|
-
expect(
|
|
193
|
+
expect(response?.status).toBe(200);
|
|
194
|
+
expect(data?.authorized).toBe(true);
|
|
219
195
|
});
|
|
220
196
|
});
|
|
221
197
|
});
|
|
@@ -223,19 +199,30 @@ describe("Authorization Tests", () => {
|
|
|
223
199
|
|
|
224
200
|
```
|
|
225
201
|
|
|
226
|
-
|
|
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/1.0.0-rc.1794579987 menu-service/2.3.1 env/production node/22.18.0
|
|
208
|
+
```
|
|
227
209
|
|
|
228
|
-
|
|
229
|
-
* Node.js
|
|
230
|
-
* Webpack
|
|
231
|
-
* Browserify
|
|
210
|
+
Each part can be set in code or with an environment variable. The first one that is set wins, with the option checked first:
|
|
232
211
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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 | `clientEnvironment` | `FLIPDISH_CLIENT_ENVIRONMENT`, `DD_ENV`, `NODE_ENV` |
|
|
217
|
+
| Runtime | `runtime` | `FLIPDISH_CLIENT_RUNTIME`, then the detected Node.js, Bun or Deno version |
|
|
218
|
+
| Suffix | `userAgentSuffix` | `FLIPDISH_CLIENT_USER_AGENT_SUFFIX` |
|
|
236
219
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
220
|
+
Pass the options to `createFlipdishClient`, or call `configureClientIdentity` to set them on the default `client`:
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { configureClientIdentity } from "@flipdish/authorization";
|
|
224
|
+
|
|
225
|
+
configureClientIdentity({ clientName: "menu-service", clientVersion: "2.3.1", clientEnvironment: "production" });
|
|
226
|
+
```
|
|
240
227
|
|
|
241
|
-
|
|
228
|
+
Browsers have no environment variables, so set at least `clientName` in code there. Anything passed in `headers` overrides these values.
|