@flipdish/authorization 0.0.1-rc.1756734343

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.
@@ -0,0 +1,12 @@
1
+ .gitignore
2
+ .npmignore
3
+ .openapi-generator-ignore
4
+ README.md
5
+ api.ts
6
+ base.ts
7
+ common.ts
8
+ configuration.ts
9
+ git_push.sh
10
+ index.ts
11
+ package.json
12
+ tsconfig.json
@@ -0,0 +1 @@
1
+ 7.8.0
@@ -0,0 +1,23 @@
1
+ # OpenAPI Generator Ignore
2
+ # Generated by openapi-generator https://github.com/openapitools/openapi-generator
3
+
4
+ # Use this file to prevent files from being overwritten by the generator.
5
+ # The patterns follow closely to .gitignore or .dockerignore.
6
+
7
+ # As an example, the C# client generator defines ApiClient.cs.
8
+ # You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9
+ #ApiClient.cs
10
+
11
+ # You can match any string of characters against a directory, file or extension with a single asterisk (*):
12
+ #foo/*/qux
13
+ # The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14
+
15
+ # You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16
+ #foo/**/qux
17
+ # This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18
+
19
+ # You can also negate patterns with an exclamation (!).
20
+ # For example, you can ignore all files in a docs folder with the file extension .md:
21
+ #docs/*.md
22
+ # Then explicitly reverse the ignore rule for a single file:
23
+ #!docs/README.md
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ ## @flipdish/authorization
2
+
3
+ This package provides a Typescript/JavaScript client for interacting with Flipdish's Authorization APIs over HTTP.
4
+
5
+ Internally the package utilizes the [axios](https://github.com/axios/axios) as its HTTP client.
6
+
7
+ ### Example code
8
+
9
+ ```typescript
10
+ import { AuthorizationApi, Configuration, PermissionsApi, Permissions } from '@flipdish/authorization';
11
+ import { describe, expect, test, it } from '@jest/globals';
12
+
13
+ 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
+ });
23
+
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
+ });
119
+ });
120
+ });
121
+ ```
122
+
123
+ The generated Node module can be used in the following environments:
124
+
125
+ Environment
126
+ * Node.js
127
+ * Webpack
128
+ * Browserify
129
+
130
+ Language level
131
+ * ES5 - you must have a Promises/A+ library installed
132
+ * ES6
133
+
134
+ Module system
135
+ * CommonJS
136
+ * ES6 module system
137
+
138
+ 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))