@optimizely-opal/opal-tool-ocp-sdk 0.0.0-beta.11 → 0.0.0-beta.13
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 +114 -55
- package/dist/auth/AuthUtils.d.ts +26 -0
- package/dist/auth/AuthUtils.d.ts.map +1 -0
- package/dist/auth/AuthUtils.js +109 -0
- package/dist/auth/AuthUtils.js.map +1 -0
- package/dist/auth/AuthUtils.test.d.ts +2 -0
- package/dist/auth/AuthUtils.test.d.ts.map +1 -0
- package/dist/auth/AuthUtils.test.js +601 -0
- package/dist/auth/AuthUtils.test.js.map +1 -0
- package/dist/auth/TokenVerifier.d.ts +31 -0
- package/dist/auth/TokenVerifier.d.ts.map +1 -0
- package/dist/auth/TokenVerifier.js +127 -0
- package/dist/auth/TokenVerifier.js.map +1 -0
- package/dist/auth/TokenVerifier.test.d.ts +2 -0
- package/dist/auth/TokenVerifier.test.d.ts.map +1 -0
- package/dist/auth/TokenVerifier.test.js +125 -0
- package/dist/auth/TokenVerifier.test.js.map +1 -0
- package/dist/function/GlobalToolFunction.d.ts +27 -0
- package/dist/function/GlobalToolFunction.d.ts.map +1 -0
- package/dist/function/GlobalToolFunction.js +53 -0
- package/dist/function/GlobalToolFunction.js.map +1 -0
- package/dist/function/GlobalToolFunction.test.d.ts +2 -0
- package/dist/function/GlobalToolFunction.test.d.ts.map +1 -0
- package/dist/function/GlobalToolFunction.test.js +425 -0
- package/dist/function/GlobalToolFunction.test.js.map +1 -0
- package/dist/function/ToolFunction.d.ts +3 -7
- package/dist/function/ToolFunction.d.ts.map +1 -1
- package/dist/function/ToolFunction.js +6 -10
- package/dist/function/ToolFunction.js.map +1 -1
- package/dist/function/ToolFunction.test.js +177 -196
- package/dist/function/ToolFunction.test.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/service/Service.d.ts +14 -13
- package/dist/service/Service.d.ts.map +1 -1
- package/dist/service/Service.js +22 -16
- package/dist/service/Service.js.map +1 -1
- package/dist/service/Service.test.js +8 -3
- package/dist/service/Service.test.js.map +1 -1
- package/dist/types/Models.d.ts +5 -5
- package/dist/types/Models.d.ts.map +1 -1
- package/dist/types/Models.js +9 -9
- package/dist/types/Models.js.map +1 -1
- package/package.json +4 -3
- package/src/auth/AuthUtils.test.ts +729 -0
- package/src/auth/AuthUtils.ts +117 -0
- package/src/auth/TokenVerifier.test.ts +165 -0
- package/src/auth/TokenVerifier.ts +145 -0
- package/src/function/GlobalToolFunction.test.ts +505 -0
- package/src/function/GlobalToolFunction.ts +56 -0
- package/src/function/ToolFunction.test.ts +194 -214
- package/src/function/ToolFunction.ts +6 -10
- package/src/index.ts +2 -0
- package/src/service/Service.test.ts +8 -3
- package/src/service/Service.ts +53 -24
- package/src/types/Models.ts +4 -4
package/src/service/Service.ts
CHANGED
|
@@ -3,8 +3,12 @@ import { AuthRequirement, Parameter } from '../types/Models';
|
|
|
3
3
|
import * as App from '@zaiusinc/app-sdk';
|
|
4
4
|
import { logger } from '@zaiusinc/app-sdk';
|
|
5
5
|
import { ToolFunction } from '../function/ToolFunction';
|
|
6
|
+
import { GlobalToolFunction } from '../function/GlobalToolFunction';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Default OptiID authentication requirement that will be enforced for all tools
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_OPTIID_AUTH = new AuthRequirement('OptiID', 'default', true);
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* Result type for interaction handlers
|
|
@@ -23,7 +27,11 @@ export class Interaction<TAuthData> {
|
|
|
23
27
|
public constructor(
|
|
24
28
|
public name: string,
|
|
25
29
|
public endpoint: string,
|
|
26
|
-
public handler: (
|
|
30
|
+
public handler: (
|
|
31
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
32
|
+
data: unknown,
|
|
33
|
+
authData?: TAuthData
|
|
34
|
+
) => Promise<InteractionResult>
|
|
27
35
|
) {}
|
|
28
36
|
}
|
|
29
37
|
|
|
@@ -43,15 +51,19 @@ export class Tool<TAuthData> {
|
|
|
43
51
|
* @param parameters Function parameters
|
|
44
52
|
* @param endpoint API endpoint
|
|
45
53
|
* @param handler Function implementing the tool
|
|
46
|
-
* @param authRequirements Authentication requirements (
|
|
54
|
+
* @param authRequirements Authentication requirements (mandatory - OptiID enforced)
|
|
47
55
|
*/
|
|
48
56
|
public constructor(
|
|
49
57
|
public name: string,
|
|
50
58
|
public description: string,
|
|
51
59
|
public parameters: Parameter[],
|
|
52
60
|
public endpoint: string,
|
|
53
|
-
public handler: (
|
|
54
|
-
|
|
61
|
+
public handler: (
|
|
62
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
63
|
+
params: unknown,
|
|
64
|
+
authData?: TAuthData
|
|
65
|
+
) => Promise<unknown>,
|
|
66
|
+
public authRequirements: AuthRequirement[] = [DEFAULT_OPTIID_AUTH]
|
|
55
67
|
) {}
|
|
56
68
|
|
|
57
69
|
/**
|
|
@@ -63,13 +75,10 @@ export class Tool<TAuthData> {
|
|
|
63
75
|
description: this.description,
|
|
64
76
|
parameters: this.parameters.map((p) => p.toJSON()),
|
|
65
77
|
endpoint: this.endpoint,
|
|
66
|
-
http_method: this.httpMethod
|
|
78
|
+
http_method: this.httpMethod,
|
|
79
|
+
auth_requirements: this.authRequirements.map((auth) => auth.toJSON())
|
|
67
80
|
};
|
|
68
81
|
|
|
69
|
-
if (this.authRequirements && this.authRequirements.length > 0) {
|
|
70
|
-
result.auth_requirements = this.authRequirements.map((auth) => auth.toJSON());
|
|
71
|
-
}
|
|
72
|
-
|
|
73
82
|
return result;
|
|
74
83
|
}
|
|
75
84
|
}
|
|
@@ -79,18 +88,19 @@ export class ToolsService {
|
|
|
79
88
|
private interactions: Map<string, Interaction<any>> = new Map();
|
|
80
89
|
|
|
81
90
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param
|
|
84
|
-
* @returns
|
|
91
|
+
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
92
|
+
* @param authRequirements Original authentication requirements
|
|
93
|
+
* @returns Enforced authentication requirements with OptiID
|
|
85
94
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
private enforceOptiIdAuth(authRequirements?: AuthRequirement[]): AuthRequirement[] {
|
|
96
|
+
const hasOptiIdProvider = authRequirements
|
|
97
|
+
&& authRequirements.some((auth) => auth.provider.toLowerCase() === 'optiid');
|
|
88
98
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
bearerToken = authHeader.substring(7).trim();
|
|
99
|
+
if (hasOptiIdProvider) {
|
|
100
|
+
return authRequirements;
|
|
92
101
|
}
|
|
93
|
-
|
|
102
|
+
|
|
103
|
+
return [...(authRequirements || []), DEFAULT_OPTIID_AUTH];
|
|
94
104
|
}
|
|
95
105
|
|
|
96
106
|
/**
|
|
@@ -105,12 +115,25 @@ export class ToolsService {
|
|
|
105
115
|
public registerTool<TAuthData>(
|
|
106
116
|
name: string,
|
|
107
117
|
description: string,
|
|
108
|
-
handler: (
|
|
118
|
+
handler: (
|
|
119
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
120
|
+
params: unknown,
|
|
121
|
+
authData?: TAuthData
|
|
122
|
+
) => Promise<unknown>,
|
|
109
123
|
parameters: Parameter[],
|
|
110
124
|
endpoint: string,
|
|
111
125
|
authRequirements?: AuthRequirement[]
|
|
112
126
|
): void {
|
|
113
|
-
|
|
127
|
+
// Enforce OptiID authentication for all tools
|
|
128
|
+
const enforcedAuthRequirements = this.enforceOptiIdAuth(authRequirements);
|
|
129
|
+
const func = new Tool<TAuthData>(
|
|
130
|
+
name,
|
|
131
|
+
description,
|
|
132
|
+
parameters,
|
|
133
|
+
endpoint,
|
|
134
|
+
handler,
|
|
135
|
+
enforcedAuthRequirements
|
|
136
|
+
);
|
|
114
137
|
this.functions.set(endpoint, func);
|
|
115
138
|
}
|
|
116
139
|
|
|
@@ -122,15 +145,21 @@ export class ToolsService {
|
|
|
122
145
|
*/
|
|
123
146
|
public registerInteraction<TAuthData>(
|
|
124
147
|
name: string,
|
|
125
|
-
handler: (
|
|
148
|
+
handler: (
|
|
149
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
150
|
+
data: unknown,
|
|
151
|
+
authData?: TAuthData
|
|
152
|
+
) => Promise<InteractionResult>,
|
|
126
153
|
endpoint: string
|
|
127
154
|
): void {
|
|
128
155
|
const func = new Interaction<TAuthData>(name, endpoint, handler);
|
|
129
156
|
this.interactions.set(endpoint, func);
|
|
130
157
|
}
|
|
131
158
|
|
|
132
|
-
public async processRequest(
|
|
133
|
-
|
|
159
|
+
public async processRequest(
|
|
160
|
+
req: App.Request,
|
|
161
|
+
functionContext: ToolFunction | GlobalToolFunction
|
|
162
|
+
): Promise<App.Response> {
|
|
134
163
|
if (req.path === '/discovery') {
|
|
135
164
|
return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
|
|
136
165
|
} else {
|
package/src/types/Models.ts
CHANGED
|
@@ -48,10 +48,10 @@ export class Parameter {
|
|
|
48
48
|
export class OptiIdAuthDataCredentials {
|
|
49
49
|
|
|
50
50
|
public constructor(
|
|
51
|
-
public
|
|
52
|
-
public
|
|
53
|
-
public
|
|
54
|
-
public
|
|
51
|
+
public customer_id: string,
|
|
52
|
+
public instance_id: string,
|
|
53
|
+
public access_token: string,
|
|
54
|
+
public product_sku: string
|
|
55
55
|
) {}
|
|
56
56
|
}
|
|
57
57
|
|