@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.
Files changed (58) hide show
  1. package/README.md +114 -55
  2. package/dist/auth/AuthUtils.d.ts +26 -0
  3. package/dist/auth/AuthUtils.d.ts.map +1 -0
  4. package/dist/auth/AuthUtils.js +109 -0
  5. package/dist/auth/AuthUtils.js.map +1 -0
  6. package/dist/auth/AuthUtils.test.d.ts +2 -0
  7. package/dist/auth/AuthUtils.test.d.ts.map +1 -0
  8. package/dist/auth/AuthUtils.test.js +601 -0
  9. package/dist/auth/AuthUtils.test.js.map +1 -0
  10. package/dist/auth/TokenVerifier.d.ts +31 -0
  11. package/dist/auth/TokenVerifier.d.ts.map +1 -0
  12. package/dist/auth/TokenVerifier.js +127 -0
  13. package/dist/auth/TokenVerifier.js.map +1 -0
  14. package/dist/auth/TokenVerifier.test.d.ts +2 -0
  15. package/dist/auth/TokenVerifier.test.d.ts.map +1 -0
  16. package/dist/auth/TokenVerifier.test.js +125 -0
  17. package/dist/auth/TokenVerifier.test.js.map +1 -0
  18. package/dist/function/GlobalToolFunction.d.ts +27 -0
  19. package/dist/function/GlobalToolFunction.d.ts.map +1 -0
  20. package/dist/function/GlobalToolFunction.js +53 -0
  21. package/dist/function/GlobalToolFunction.js.map +1 -0
  22. package/dist/function/GlobalToolFunction.test.d.ts +2 -0
  23. package/dist/function/GlobalToolFunction.test.d.ts.map +1 -0
  24. package/dist/function/GlobalToolFunction.test.js +425 -0
  25. package/dist/function/GlobalToolFunction.test.js.map +1 -0
  26. package/dist/function/ToolFunction.d.ts +3 -7
  27. package/dist/function/ToolFunction.d.ts.map +1 -1
  28. package/dist/function/ToolFunction.js +6 -10
  29. package/dist/function/ToolFunction.js.map +1 -1
  30. package/dist/function/ToolFunction.test.js +177 -196
  31. package/dist/function/ToolFunction.test.js.map +1 -1
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +2 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/service/Service.d.ts +14 -13
  37. package/dist/service/Service.d.ts.map +1 -1
  38. package/dist/service/Service.js +22 -16
  39. package/dist/service/Service.js.map +1 -1
  40. package/dist/service/Service.test.js +8 -3
  41. package/dist/service/Service.test.js.map +1 -1
  42. package/dist/types/Models.d.ts +5 -5
  43. package/dist/types/Models.d.ts.map +1 -1
  44. package/dist/types/Models.js +9 -9
  45. package/dist/types/Models.js.map +1 -1
  46. package/package.json +4 -3
  47. package/src/auth/AuthUtils.test.ts +729 -0
  48. package/src/auth/AuthUtils.ts +117 -0
  49. package/src/auth/TokenVerifier.test.ts +165 -0
  50. package/src/auth/TokenVerifier.ts +145 -0
  51. package/src/function/GlobalToolFunction.test.ts +505 -0
  52. package/src/function/GlobalToolFunction.ts +56 -0
  53. package/src/function/ToolFunction.test.ts +194 -214
  54. package/src/function/ToolFunction.ts +6 -10
  55. package/src/index.ts +2 -0
  56. package/src/service/Service.test.ts +8 -3
  57. package/src/service/Service.ts +53 -24
  58. package/src/types/Models.ts +4 -4
@@ -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: (functionContext: ToolFunction, data: unknown, authData?: TAuthData) => Promise<InteractionResult>
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 (optional)
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: (functionContext: ToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>,
54
- public authRequirements?: AuthRequirement[]
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
- * Extract Bearer token from Authorization header
83
- * @param headers Request headers (Map-like object or Headers object with get method)
84
- * @returns Bearer token string or undefined
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
- public extractBearerToken(headers: App.Headers): string | undefined {
87
- let bearerToken: string | undefined;
95
+ private enforceOptiIdAuth(authRequirements?: AuthRequirement[]): AuthRequirement[] {
96
+ const hasOptiIdProvider = authRequirements
97
+ && authRequirements.some((auth) => auth.provider.toLowerCase() === 'optiid');
88
98
 
89
- const authHeader = headers ? headers.get('authorization') : undefined;
90
- if (authHeader && authHeader.startsWith('Bearer ')) {
91
- bearerToken = authHeader.substring(7).trim();
99
+ if (hasOptiIdProvider) {
100
+ return authRequirements;
92
101
  }
93
- return bearerToken;
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: (functionContext: ToolFunction, params: unknown, authData?: TAuthData) => Promise<unknown>,
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
- const func = new Tool<TAuthData>(name, description, parameters, endpoint, handler, authRequirements);
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: (functionContext: ToolFunction, data: unknown, authData?: TAuthData) => Promise<InteractionResult>,
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(req: App.Request,
133
- functionContext: ToolFunction): Promise<App.Response> {
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 {
@@ -48,10 +48,10 @@ export class Parameter {
48
48
  export class OptiIdAuthDataCredentials {
49
49
 
50
50
  public constructor(
51
- public customerId: string,
52
- public instanceId: string,
53
- public accessToken: string,
54
- public productSku: string
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