@optimizely-opal/opal-tool-ocp-sdk 0.0.0-beta.7 → 0.0.0-beta.8

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 (38) hide show
  1. package/README.md +9 -43
  2. package/dist/auth/TokenVerifier.d.ts +31 -0
  3. package/dist/auth/TokenVerifier.d.ts.map +1 -0
  4. package/dist/auth/TokenVerifier.js +127 -0
  5. package/dist/auth/TokenVerifier.js.map +1 -0
  6. package/dist/auth/TokenVerifier.test.d.ts +2 -0
  7. package/dist/auth/TokenVerifier.test.d.ts.map +1 -0
  8. package/dist/auth/TokenVerifier.test.js +114 -0
  9. package/dist/auth/TokenVerifier.test.js.map +1 -0
  10. package/dist/function/ToolFunction.d.ts +4 -7
  11. package/dist/function/ToolFunction.d.ts.map +1 -1
  12. package/dist/function/ToolFunction.js +35 -10
  13. package/dist/function/ToolFunction.js.map +1 -1
  14. package/dist/function/ToolFunction.test.js +177 -196
  15. package/dist/function/ToolFunction.test.js.map +1 -1
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/service/Service.d.ts +7 -7
  21. package/dist/service/Service.d.ts.map +1 -1
  22. package/dist/service/Service.js +22 -16
  23. package/dist/service/Service.js.map +1 -1
  24. package/dist/service/Service.test.js +8 -3
  25. package/dist/service/Service.test.js.map +1 -1
  26. package/dist/types/Models.d.ts +5 -5
  27. package/dist/types/Models.d.ts.map +1 -1
  28. package/dist/types/Models.js +9 -9
  29. package/dist/types/Models.js.map +1 -1
  30. package/package.json +5 -3
  31. package/src/auth/TokenVerifier.test.ts +152 -0
  32. package/src/auth/TokenVerifier.ts +145 -0
  33. package/src/function/ToolFunction.test.ts +194 -214
  34. package/src/function/ToolFunction.ts +41 -11
  35. package/src/index.ts +1 -0
  36. package/src/service/Service.test.ts +8 -3
  37. package/src/service/Service.ts +22 -17
  38. package/src/types/Models.ts +4 -4
@@ -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 (optional)
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?: AuthRequirement[]
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
- * 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
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
- public extractBearerToken(headers: App.Headers): string | undefined {
87
- let bearerToken: string | undefined;
88
+ private enforceOptiIdAuth(authRequirements?: AuthRequirement[]): AuthRequirement[] {
89
+ const hasOptiIdProvider = authRequirements
90
+ && authRequirements.some((auth) => auth.provider.toLowerCase() === 'optiid');
88
91
 
89
- const authHeader = headers ? headers.get('authorization') : undefined;
90
- if (authHeader && authHeader.startsWith('Bearer ')) {
91
- bearerToken = authHeader.substring(7).trim();
92
+ if (hasOptiIdProvider) {
93
+ return authRequirements;
92
94
  }
93
- return bearerToken;
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
- const func = new Tool<TAuthData>(name, description, parameters, endpoint, handler, authRequirements);
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
 
@@ -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