@optimizely-opal/opal-tool-ocp-sdk 0.0.0-devmg.13 → 1.0.0-beta.1
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 +108 -15
- 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.map +1 -1
- package/dist/auth/TokenVerifier.js +0 -1
- package/dist/auth/TokenVerifier.js.map +1 -1
- package/dist/auth/TokenVerifier.test.js +9 -0
- package/dist/auth/TokenVerifier.test.js.map +1 -1
- 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 +1 -2
- package/dist/function/ToolFunction.d.ts.map +1 -1
- package/dist/function/ToolFunction.js +3 -35
- package/dist/function/ToolFunction.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/service/Service.d.ts +8 -7
- package/dist/service/Service.d.ts.map +1 -1
- package/dist/service/Service.js.map +1 -1
- package/package.json +3 -4
- package/src/auth/AuthUtils.test.ts +729 -0
- package/src/auth/AuthUtils.ts +117 -0
- package/src/auth/TokenVerifier.test.ts +11 -0
- package/src/auth/TokenVerifier.ts +0 -1
- package/src/function/GlobalToolFunction.test.ts +505 -0
- package/src/function/GlobalToolFunction.ts +56 -0
- package/src/function/ToolFunction.ts +4 -41
- package/src/index.ts +1 -0
- package/src/service/Service.ts +33 -9
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Function, Response, amendLogContext
|
|
1
|
+
import { Function, Response, amendLogContext } from '@zaiusinc/app-sdk';
|
|
2
|
+
import { authenticateRegularRequest } from '../auth/AuthUtils';
|
|
2
3
|
import { toolsService } from '../service/Service';
|
|
3
|
-
import { getTokenVerifier } from '../auth/TokenVerifier';
|
|
4
|
-
import { OptiIdAuthData } from '../types/Models';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Abstract base class for tool-based function execution
|
|
@@ -41,45 +40,9 @@ export abstract class ToolFunction extends Function {
|
|
|
41
40
|
/**
|
|
42
41
|
* Authenticate the incoming request by validating the OptiID token and organization ID
|
|
43
42
|
*
|
|
44
|
-
* @
|
|
43
|
+
* @returns true if authentication succeeds
|
|
45
44
|
*/
|
|
46
45
|
private async authorizeRequest(): Promise<boolean> {
|
|
47
|
-
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
const authData = this.request.bodyJSON?.auth as OptiIdAuthData;
|
|
51
|
-
const accessToken = authData?.credentials?.access_token;
|
|
52
|
-
if (!accessToken || authData?.provider?.toLowerCase() !== 'optiid') {
|
|
53
|
-
logger.error('OptiID token is required but not provided');
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const customerId = authData.credentials?.customer_id;
|
|
58
|
-
if (!customerId) {
|
|
59
|
-
logger.error('Organisation ID is required but not provided');
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const appOrganisationId = getAppContext().account?.organizationId;
|
|
64
|
-
if (customerId !== appOrganisationId) {
|
|
65
|
-
logger.error(`Invalid organisation ID: expected ${appOrganisationId}, received ${customerId}`);
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return await this.validateAccessToken(accessToken);
|
|
46
|
+
return await authenticateRegularRequest(this.request);
|
|
70
47
|
}
|
|
71
|
-
|
|
72
|
-
private async validateAccessToken(accessToken: string | undefined): Promise<boolean> {
|
|
73
|
-
try {
|
|
74
|
-
if (!accessToken) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
const tokenVerifier = await getTokenVerifier();
|
|
78
|
-
return await tokenVerifier.verify(accessToken);
|
|
79
|
-
} catch (error) {
|
|
80
|
-
logger.error('OptiID token validation failed:', error);
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
48
|
}
|
package/src/index.ts
CHANGED
package/src/service/Service.ts
CHANGED
|
@@ -3,14 +3,13 @@ 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
|
/**
|
|
8
9
|
* Default OptiID authentication requirement that will be enforced for all tools
|
|
9
10
|
*/
|
|
10
11
|
const DEFAULT_OPTIID_AUTH = new AuthRequirement('OptiID', 'default', true);
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
13
|
/**
|
|
15
14
|
* Result type for interaction handlers
|
|
16
15
|
*/
|
|
@@ -28,7 +27,11 @@ export class Interaction<TAuthData> {
|
|
|
28
27
|
public constructor(
|
|
29
28
|
public name: string,
|
|
30
29
|
public endpoint: string,
|
|
31
|
-
public handler: (
|
|
30
|
+
public handler: (
|
|
31
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
32
|
+
data: unknown,
|
|
33
|
+
authData?: TAuthData
|
|
34
|
+
) => Promise<InteractionResult>
|
|
32
35
|
) {}
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -55,7 +58,11 @@ export class Tool<TAuthData> {
|
|
|
55
58
|
public description: string,
|
|
56
59
|
public parameters: Parameter[],
|
|
57
60
|
public endpoint: string,
|
|
58
|
-
public handler: (
|
|
61
|
+
public handler: (
|
|
62
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
63
|
+
params: unknown,
|
|
64
|
+
authData?: TAuthData
|
|
65
|
+
) => Promise<unknown>,
|
|
59
66
|
public authRequirements: AuthRequirement[] = [DEFAULT_OPTIID_AUTH]
|
|
60
67
|
) {}
|
|
61
68
|
|
|
@@ -108,14 +115,25 @@ export class ToolsService {
|
|
|
108
115
|
public registerTool<TAuthData>(
|
|
109
116
|
name: string,
|
|
110
117
|
description: string,
|
|
111
|
-
handler: (
|
|
118
|
+
handler: (
|
|
119
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
120
|
+
params: unknown,
|
|
121
|
+
authData?: TAuthData
|
|
122
|
+
) => Promise<unknown>,
|
|
112
123
|
parameters: Parameter[],
|
|
113
124
|
endpoint: string,
|
|
114
125
|
authRequirements?: AuthRequirement[]
|
|
115
126
|
): void {
|
|
116
127
|
// Enforce OptiID authentication for all tools
|
|
117
128
|
const enforcedAuthRequirements = this.enforceOptiIdAuth(authRequirements);
|
|
118
|
-
const func = new Tool<TAuthData>(
|
|
129
|
+
const func = new Tool<TAuthData>(
|
|
130
|
+
name,
|
|
131
|
+
description,
|
|
132
|
+
parameters,
|
|
133
|
+
endpoint,
|
|
134
|
+
handler,
|
|
135
|
+
enforcedAuthRequirements
|
|
136
|
+
);
|
|
119
137
|
this.functions.set(endpoint, func);
|
|
120
138
|
}
|
|
121
139
|
|
|
@@ -127,15 +145,21 @@ export class ToolsService {
|
|
|
127
145
|
*/
|
|
128
146
|
public registerInteraction<TAuthData>(
|
|
129
147
|
name: string,
|
|
130
|
-
handler: (
|
|
148
|
+
handler: (
|
|
149
|
+
functionContext: ToolFunction | GlobalToolFunction,
|
|
150
|
+
data: unknown,
|
|
151
|
+
authData?: TAuthData
|
|
152
|
+
) => Promise<InteractionResult>,
|
|
131
153
|
endpoint: string
|
|
132
154
|
): void {
|
|
133
155
|
const func = new Interaction<TAuthData>(name, endpoint, handler);
|
|
134
156
|
this.interactions.set(endpoint, func);
|
|
135
157
|
}
|
|
136
158
|
|
|
137
|
-
public async processRequest(
|
|
138
|
-
|
|
159
|
+
public async processRequest(
|
|
160
|
+
req: App.Request,
|
|
161
|
+
functionContext: ToolFunction | GlobalToolFunction
|
|
162
|
+
): Promise<App.Response> {
|
|
139
163
|
if (req.path === '/discovery') {
|
|
140
164
|
return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
|
|
141
165
|
} else {
|