@flipdish/authorization 0.0.671-rc.1794575868 → 0.0.672-rc.1794583352

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
@@ -1,221 +1,197 @@
1
1
  ## @flipdish/authorization
2
2
 
3
- This package provides a Typescript/JavaScript client for interacting with Flipdish's Authorization APIs over HTTP.
3
+ This package provides a TypeScript/JavaScript client for Flipdish's Authorization APIs over HTTP.
4
4
 
5
- Internally the package utilizes the [axios](https://github.com/axios/axios) as its HTTP client.
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
- AuthorizationApi,
12
- Configuration,
13
- ConfigurationDataApi,
14
- type ErrorResponse,
11
+ createFlipdishClient,
15
12
  Permissions,
16
- UserPermissionsApi,
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 basePath = "https://api.flipdish.co/auth/";
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
- // if using in a browser set useDefaultUserAgent
28
- // to true to prevent errors
29
- // useDefaultUserAgent: true
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
- 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
- );
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 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
- );
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 namedRolesResponse = await configurationData.listRoles();
74
- expect(namedRolesResponse.status).toBe(200);
75
- expect(namedRolesResponse.data.roles).toBeDefined();
76
- expect(namedRolesResponse.data.roles.length).toBeGreaterThan(0);
77
- expect(namedRolesResponse.data.roles).toContainEqual("Admin");
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 userPermissionSetsResponse =
83
- await userPermissions.listOwnPermissions("org42068");
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
- userPermissionSetsResponse.data.resources.org42068.permissions.length,
91
- ).toBeGreaterThan(0);
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 authorizationResponse =
98
- await authorization.authenticateAndAuthorize({
99
- headers: {
100
- Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
101
- },
102
- action: Permissions.AnyAuditLogs,
103
- resource: {
104
- id: "org12345",
105
- type: "Org",
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
- try {
93
+ // errors are returned rather than thrown, unless throwOnError: true is set
94
+ const { error, response } =
123
95
  await authorization.authenticateAndAuthorize({
124
- headers: {
125
- Cookie: `FD-Authorization=not-a-valid-cookie;`,
126
- },
127
- action: Permissions.AnyAuditLogs,
128
- resource: {
129
- id: "org12345",
130
- type: "Org",
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
- } catch (error) {
134
- if (isAxiosError<ErrorResponse>(error)) {
135
- expect(error.response?.status).toBe(401);
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 authorizationResponse =
143
- await authorization.authenticateAndAuthorize({
144
- headers: {
145
- Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
146
- },
147
- action: Permissions.AnyAuditLogs,
148
- resource: {
149
- id: "org12345",
150
- type: "Org",
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 authorizationResponse = await authorization.authorize({
169
- principal: {
170
- id: "12345",
171
- type: "User",
172
- },
173
- action: Permissions.AnyAuditLogs,
174
- resource: {
175
- id: "org12345",
176
- type: "Org",
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(authorizationResponse.status).toBe(200);
180
- expect(authorizationResponse.data.allowed).toBe(false);
181
- expect(authorizationResponse.data.decision).toBe("DENY");
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 isInRoleResponse = await authorization.checkIsInRole({
187
- principal: {
188
- id: "12345",
189
- type: "User",
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(isInRoleResponse.status).toBe(200);
194
- expect(isInRoleResponse.data.authorized).toBe(false);
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 isInRoleResponse =
170
+ const { data, response } =
199
171
  await authorization.authenticateAndCheckIsInRole({
200
- headers: {
201
- Cookie: `FD-Authorization=${process.env.FD_AUTH_COOKIE_PROD};`,
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(isInRoleResponse.status).toBe(200);
206
- expect(isInRoleResponse.data.authorized).toBe(true);
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 isInRoleResponse =
184
+ const { data, response } =
211
185
  await authorization.authenticateAndCheckIsInRole({
212
- headers: {
213
- Authorization: `Bearer ${process.env.FLIPDISH_BEARER_TOKEN_PROD}`,
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(isInRoleResponse.status).toBe(200);
218
- expect(isInRoleResponse.data.authorized).toBe(true);
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
- The generated Node module can be used in the following environments:
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.672-rc.1794583352 menu-service/2.3.1 env/production node/22.18.0
208
+ ```
227
209
 
228
- Environment
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
- Language level
234
- * ES5 - you must have a Promises/A+ library installed
235
- * ES6
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
- Module system
238
- * CommonJS
239
- * ES6 module system
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
- It can be used in both TypeScript and JavaScript. In TypeScript, the definition will be automatically resolved via `package.json`. ([Reference](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html))
228
+ Browsers have no environment variables, so set at least `clientName` in code there. Anything passed in `headers` overrides these values.