@optimizely-opal/opal-tool-ocp-sdk 0.0.0-beta.7 → 0.0.0-beta.9
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 +9 -43
- 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 +114 -0
- package/dist/auth/TokenVerifier.test.js.map +1 -0
- package/dist/function/ToolFunction.d.ts +4 -7
- package/dist/function/ToolFunction.d.ts.map +1 -1
- package/dist/function/ToolFunction.js +38 -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 +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 +7 -7
- 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 +5 -3
- package/src/auth/TokenVerifier.test.ts +152 -0
- package/src/auth/TokenVerifier.ts +145 -0
- package/src/function/ToolFunction.test.ts +194 -214
- package/src/function/ToolFunction.ts +45 -11
- package/src/index.ts +1 -0
- package/src/service/Service.test.ts +8 -3
- package/src/service/Service.ts +22 -17
- package/src/types/Models.ts +4 -4
package/src/service/Service.ts
CHANGED
|
@@ -4,6 +4,11 @@ import * as App from '@zaiusinc/app-sdk';
|
|
|
4
4
|
import { logger } from '@zaiusinc/app-sdk';
|
|
5
5
|
import { ToolFunction } from '../function/ToolFunction';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Default OptiID authentication requirement that will be enforced for all tools
|
|
9
|
+
*/
|
|
10
|
+
const DEFAULT_OPTIID_AUTH = new AuthRequirement('OptiID', 'default', true);
|
|
11
|
+
|
|
7
12
|
|
|
8
13
|
|
|
9
14
|
/**
|
|
@@ -43,7 +48,7 @@ export class Tool<TAuthData> {
|
|
|
43
48
|
* @param parameters Function parameters
|
|
44
49
|
* @param endpoint API endpoint
|
|
45
50
|
* @param handler Function implementing the tool
|
|
46
|
-
* @param authRequirements Authentication requirements (
|
|
51
|
+
* @param authRequirements Authentication requirements (mandatory - OptiID enforced)
|
|
47
52
|
*/
|
|
48
53
|
public constructor(
|
|
49
54
|
public name: string,
|
|
@@ -51,7 +56,7 @@ export class Tool<TAuthData> {
|
|
|
51
56
|
public parameters: Parameter[],
|
|
52
57
|
public endpoint: string,
|
|
53
58
|
public handler: (functionContext: ToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>,
|
|
54
|
-
public authRequirements
|
|
59
|
+
public authRequirements: AuthRequirement[] = [DEFAULT_OPTIID_AUTH]
|
|
55
60
|
) {}
|
|
56
61
|
|
|
57
62
|
/**
|
|
@@ -63,13 +68,10 @@ export class Tool<TAuthData> {
|
|
|
63
68
|
description: this.description,
|
|
64
69
|
parameters: this.parameters.map((p) => p.toJSON()),
|
|
65
70
|
endpoint: this.endpoint,
|
|
66
|
-
http_method: this.httpMethod
|
|
71
|
+
http_method: this.httpMethod,
|
|
72
|
+
auth_requirements: this.authRequirements.map((auth) => auth.toJSON())
|
|
67
73
|
};
|
|
68
74
|
|
|
69
|
-
if (this.authRequirements && this.authRequirements.length > 0) {
|
|
70
|
-
result.auth_requirements = this.authRequirements.map((auth) => auth.toJSON());
|
|
71
|
-
}
|
|
72
|
-
|
|
73
75
|
return result;
|
|
74
76
|
}
|
|
75
77
|
}
|
|
@@ -79,18 +81,19 @@ export class ToolsService {
|
|
|
79
81
|
private interactions: Map<string, Interaction<any>> = new Map();
|
|
80
82
|
|
|
81
83
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param
|
|
84
|
-
* @returns
|
|
84
|
+
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
85
|
+
* @param authRequirements Original authentication requirements
|
|
86
|
+
* @returns Enforced authentication requirements with OptiID
|
|
85
87
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
private enforceOptiIdAuth(authRequirements?: AuthRequirement[]): AuthRequirement[] {
|
|
89
|
+
const hasOptiIdProvider = authRequirements
|
|
90
|
+
&& authRequirements.some((auth) => auth.provider.toLowerCase() === 'optiid');
|
|
88
91
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
bearerToken = authHeader.substring(7).trim();
|
|
92
|
+
if (hasOptiIdProvider) {
|
|
93
|
+
return authRequirements;
|
|
92
94
|
}
|
|
93
|
-
|
|
95
|
+
|
|
96
|
+
return [...(authRequirements || []), DEFAULT_OPTIID_AUTH];
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
/**
|
|
@@ -110,7 +113,9 @@ export class ToolsService {
|
|
|
110
113
|
endpoint: string,
|
|
111
114
|
authRequirements?: AuthRequirement[]
|
|
112
115
|
): void {
|
|
113
|
-
|
|
116
|
+
// Enforce OptiID authentication for all tools
|
|
117
|
+
const enforcedAuthRequirements = this.enforceOptiIdAuth(authRequirements);
|
|
118
|
+
const func = new Tool<TAuthData>(name, description, parameters, endpoint, handler, enforcedAuthRequirements);
|
|
114
119
|
this.functions.set(endpoint, func);
|
|
115
120
|
}
|
|
116
121
|
|
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
|
|