@optimizely-opal/opal-tool-ocp-sdk 0.0.0-OCP-1487.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.
Files changed (72) hide show
  1. package/README.md +631 -0
  2. package/dist/auth/AuthUtils.d.ts +31 -0
  3. package/dist/auth/AuthUtils.d.ts.map +1 -0
  4. package/dist/auth/AuthUtils.js +64 -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 +469 -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/decorator/Decorator.d.ts +48 -0
  19. package/dist/decorator/Decorator.d.ts.map +1 -0
  20. package/dist/decorator/Decorator.js +53 -0
  21. package/dist/decorator/Decorator.js.map +1 -0
  22. package/dist/decorator/Decorator.test.d.ts +2 -0
  23. package/dist/decorator/Decorator.test.d.ts.map +1 -0
  24. package/dist/decorator/Decorator.test.js +528 -0
  25. package/dist/decorator/Decorator.test.js.map +1 -0
  26. package/dist/function/GlobalToolFunction.d.ts +28 -0
  27. package/dist/function/GlobalToolFunction.d.ts.map +1 -0
  28. package/dist/function/GlobalToolFunction.js +56 -0
  29. package/dist/function/GlobalToolFunction.js.map +1 -0
  30. package/dist/function/GlobalToolFunction.test.d.ts +2 -0
  31. package/dist/function/GlobalToolFunction.test.d.ts.map +1 -0
  32. package/dist/function/GlobalToolFunction.test.js +425 -0
  33. package/dist/function/GlobalToolFunction.test.js.map +1 -0
  34. package/dist/function/ToolFunction.d.ts +28 -0
  35. package/dist/function/ToolFunction.d.ts.map +1 -0
  36. package/dist/function/ToolFunction.js +60 -0
  37. package/dist/function/ToolFunction.js.map +1 -0
  38. package/dist/function/ToolFunction.test.d.ts +2 -0
  39. package/dist/function/ToolFunction.test.d.ts.map +1 -0
  40. package/dist/function/ToolFunction.test.js +314 -0
  41. package/dist/function/ToolFunction.test.js.map +1 -0
  42. package/dist/index.d.ts +6 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +26 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/service/Service.d.ts +80 -0
  47. package/dist/service/Service.d.ts.map +1 -0
  48. package/dist/service/Service.js +210 -0
  49. package/dist/service/Service.js.map +1 -0
  50. package/dist/service/Service.test.d.ts +2 -0
  51. package/dist/service/Service.test.d.ts.map +1 -0
  52. package/dist/service/Service.test.js +427 -0
  53. package/dist/service/Service.test.js.map +1 -0
  54. package/dist/types/Models.d.ts +126 -0
  55. package/dist/types/Models.d.ts.map +1 -0
  56. package/dist/types/Models.js +181 -0
  57. package/dist/types/Models.js.map +1 -0
  58. package/package.json +64 -0
  59. package/src/auth/AuthUtils.test.ts +586 -0
  60. package/src/auth/AuthUtils.ts +66 -0
  61. package/src/auth/TokenVerifier.test.ts +165 -0
  62. package/src/auth/TokenVerifier.ts +145 -0
  63. package/src/decorator/Decorator.test.ts +649 -0
  64. package/src/decorator/Decorator.ts +111 -0
  65. package/src/function/GlobalToolFunction.test.ts +505 -0
  66. package/src/function/GlobalToolFunction.ts +61 -0
  67. package/src/function/ToolFunction.test.ts +374 -0
  68. package/src/function/ToolFunction.ts +64 -0
  69. package/src/index.ts +5 -0
  70. package/src/service/Service.test.ts +661 -0
  71. package/src/service/Service.ts +213 -0
  72. package/src/types/Models.ts +163 -0
@@ -0,0 +1,165 @@
1
+
2
+ // Mock the app-sdk module
3
+ jest.mock('@zaiusinc/app-sdk', () => ({
4
+ logger: {
5
+ info: jest.fn(),
6
+ error: jest.fn(),
7
+ warn: jest.fn(),
8
+ debug: jest.fn(),
9
+ },
10
+ }));
11
+
12
+ import { TokenVerifier } from './TokenVerifier';
13
+
14
+ // Test constants
15
+ const TEST_ISSUER = 'https://prep.login.optimizely.com/oauth2/default';
16
+ const TEST_JWKS_URI = 'https://prep.login.optimizely.com/oauth2/v1/keys';
17
+
18
+ // Mock fetch globally
19
+ global.fetch = jest.fn();
20
+
21
+ describe('TokenVerifier', () => {
22
+ beforeEach(() => {
23
+ // Reset fetch mock
24
+ (global.fetch as jest.Mock).mockReset();
25
+
26
+ // Reset singleton instance for each test
27
+ (TokenVerifier as any).instance = null;
28
+ });
29
+
30
+ describe('getInitializedInstance', () => {
31
+ it('should initialize successfully', async () => {
32
+ // Mock fetch for OAuth2 authorization server discovery
33
+ (global.fetch as jest.Mock).mockResolvedValue({
34
+ ok: true,
35
+ json: jest.fn().mockResolvedValue({
36
+ issuer: TEST_ISSUER,
37
+ jwks_uri: TEST_JWKS_URI
38
+ })
39
+ });
40
+
41
+ const tokenVerifier = await TokenVerifier.getInitializedInstance();
42
+ expect(tokenVerifier).toBeInstanceOf(TokenVerifier);
43
+ });
44
+
45
+ it('should throw error when discovery document is invalid', async () => {
46
+ (global.fetch as jest.Mock).mockResolvedValue({
47
+ ok: true,
48
+ json: jest.fn().mockResolvedValue({
49
+ // Missing issuer and jwks_uri
50
+ })
51
+ });
52
+
53
+ await expect(TokenVerifier.getInitializedInstance()).rejects.toThrow(
54
+ 'Invalid discovery document: missing issuer or jwks_uri'
55
+ );
56
+ });
57
+
58
+ it('should throw error when discovery endpoint is unreachable', async () => {
59
+ (global.fetch as jest.Mock).mockResolvedValue({
60
+ ok: false,
61
+ status: 404,
62
+ statusText: 'Not Found'
63
+ });
64
+
65
+ await expect(TokenVerifier.getInitializedInstance()).rejects.toThrow(
66
+ 'Failed to fetch discovery document: 404 Not Found'
67
+ );
68
+ });
69
+ });
70
+
71
+ describe('token verification', () => {
72
+ let tokenVerifier: TokenVerifier;
73
+
74
+ beforeEach(async () => {
75
+ // Mock successful initialization
76
+ (global.fetch as jest.Mock).mockResolvedValue({
77
+ ok: true,
78
+ json: jest.fn().mockResolvedValue({
79
+ issuer: TEST_ISSUER,
80
+ jwks_uri: TEST_JWKS_URI
81
+ })
82
+ });
83
+
84
+ tokenVerifier = await TokenVerifier.getInitializedInstance();
85
+ });
86
+
87
+ it('should throw error for empty token', async () => {
88
+ await expect(tokenVerifier.verify('')).rejects.toThrow(
89
+ 'Token cannot be null or empty'
90
+ );
91
+ });
92
+
93
+ it('should throw error for undefined token', async () => {
94
+ await expect(tokenVerifier.verify(undefined)).rejects.toThrow(
95
+ 'Token cannot be null or empty'
96
+ );
97
+ });
98
+
99
+ it('should throw error for whitespace-only token', async () => {
100
+ await expect(tokenVerifier.verify(' ')).rejects.toThrow(
101
+ 'Token cannot be null or empty'
102
+ );
103
+ });
104
+
105
+ it('should return false for invalid token format', async () => {
106
+ const result = await tokenVerifier.verify('invalid.jwt.token');
107
+ expect(result).toBe(false);
108
+ });
109
+ });
110
+
111
+ describe('singleton pattern', () => {
112
+ it('should return same instance when called multiple times', async () => {
113
+ // Mock successful initialization
114
+ (global.fetch as jest.Mock).mockResolvedValue({
115
+ ok: true,
116
+ json: jest.fn().mockResolvedValue({
117
+ issuer: TEST_ISSUER,
118
+ jwks_uri: TEST_JWKS_URI
119
+ })
120
+ });
121
+
122
+ const instance1 = await TokenVerifier.getInitializedInstance();
123
+ const instance2 = await TokenVerifier.getInitializedInstance();
124
+ expect(instance1).toBe(instance2);
125
+ });
126
+ it('should call correct prod OAuth2 authorization server discovery URL', async () => {
127
+ const fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValue({
128
+ ok: true,
129
+ json: jest.fn().mockResolvedValue({
130
+ issuer: TEST_ISSUER,
131
+ jwks_uri: TEST_JWKS_URI
132
+ })
133
+ } as unknown as Response);
134
+
135
+ await TokenVerifier.getInitializedInstance();
136
+
137
+ expect(fetchSpy).toHaveBeenCalledWith(
138
+ 'https://login.optimizely.com/oauth2/default/.well-known/oauth-authorization-server'
139
+ );
140
+ });
141
+
142
+ it('should call correct prep OAuth2 authorization server discovery URL', async () => {
143
+ // Set environment variable to staging
144
+ process.env.ENVIRONMENT = 'staging';
145
+
146
+ const fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValue({
147
+ ok: true,
148
+ json: jest.fn().mockResolvedValue({
149
+ issuer: TEST_ISSUER,
150
+ jwks_uri: TEST_JWKS_URI
151
+ })
152
+ } as unknown as Response);
153
+
154
+ await TokenVerifier.getInitializedInstance();
155
+
156
+ expect(fetchSpy).toHaveBeenCalledWith(
157
+ 'https://prep.login.optimizely.com/oauth2/default/.well-known/oauth-authorization-server'
158
+ );
159
+
160
+ // Clean up environment variable
161
+ delete process.env.ENVIRONMENT;
162
+ });
163
+ });
164
+
165
+ });
@@ -0,0 +1,145 @@
1
+ import { jwtVerify, createRemoteJWKSet } from 'jose';
2
+ import { logger } from '@zaiusinc/app-sdk';
3
+
4
+ /**
5
+ * Default JWKS cache expiration time in milliseconds (1 hour)
6
+ */
7
+ const DEFAULT_JWKS_EXPIRES_IN = 60 * 60 * 1000;
8
+
9
+ /**
10
+ * Default clock skew tolerance in seconds
11
+ */
12
+ const DEFAULT_LEEWAY = 30;
13
+
14
+ /**
15
+ * Expected JWT audience for token validation
16
+ */
17
+ const AUDIENCE = 'api://default';
18
+
19
+ /**
20
+ * Prep Base URL for Optimizely OAuth2 endpoints
21
+ */
22
+ const PREP_BASE_URL = 'https://prep.login.optimizely.com/oauth2/default';
23
+
24
+ /**
25
+ * Prod Base URL for Optimizely OAuth2 endpoints
26
+ */
27
+ const PROD_BASE_URL = 'https://login.optimizely.com/oauth2/default';
28
+
29
+ interface DiscoveryDocument {
30
+ issuer: string;
31
+ jwks_uri: string;
32
+ [key: string]: any;
33
+ }
34
+
35
+ export class TokenVerifier {
36
+ private static instance: TokenVerifier | null = null;
37
+ private jwksUri?: string;
38
+ private issuer?: string;
39
+ private jwks?: ReturnType<typeof createRemoteJWKSet>;
40
+ private initialized: boolean = false;
41
+
42
+ /**
43
+ * Verify the provided Optimizely JWT token string
44
+ * @param token JWT token string to verify
45
+ * @returns boolean true if verification successful, false otherwise
46
+ * @throws Error if token is null, empty, or verifier is not properly configured
47
+ */
48
+ public async verify(token: string | undefined): Promise<boolean> {
49
+ if (!token || token.trim().length === 0) {
50
+ throw new Error('Token cannot be null or empty');
51
+ }
52
+
53
+ return this.verifyToken(token);
54
+ }
55
+
56
+ private static getInstance(): TokenVerifier {
57
+ if (!TokenVerifier.instance) {
58
+ TokenVerifier.instance = new TokenVerifier();
59
+ }
60
+ return TokenVerifier.instance;
61
+ }
62
+
63
+ /**
64
+ * Get singleton instance of TokenVerifier and ensure it's initialized
65
+ * @returns Promise<TokenVerifier> - initialized singleton instance
66
+ */
67
+ public static async getInitializedInstance(): Promise<TokenVerifier> {
68
+ const instance = TokenVerifier.getInstance();
69
+ if (!instance.initialized) {
70
+ await instance.initialize();
71
+ }
72
+ return instance;
73
+ }
74
+
75
+ /**
76
+ * Initialize the TokenVerifier with discovery document from well-known endpoint
77
+ */
78
+ private async initialize(): Promise<void> {
79
+ if (this.initialized) {
80
+ return;
81
+ }
82
+
83
+ try {
84
+ // Use prep URL when environment variable is set to 'staging', otherwise use prod
85
+ const environment = process.env.ENVIRONMENT || 'production';
86
+ const baseUrl = environment === 'staging' ? PREP_BASE_URL : PROD_BASE_URL;
87
+ const discoveryDocument = await this.fetchDiscoveryDocument(baseUrl);
88
+ this.issuer = discoveryDocument.issuer;
89
+ this.jwksUri = discoveryDocument.jwks_uri;
90
+ this.jwks = createRemoteJWKSet(new URL(this.jwksUri), {
91
+ cacheMaxAge: DEFAULT_JWKS_EXPIRES_IN,
92
+ cooldownDuration: DEFAULT_JWKS_EXPIRES_IN
93
+ });
94
+ this.initialized = true;
95
+ logger.info(`TokenVerifier initialized with issuer: ${this.issuer} (environment: ${environment})`);
96
+ } catch (error) {
97
+ logger.error('Failed to initialize TokenVerifier', error);
98
+ // Re-throw the original error to preserve specific error messages for tests
99
+ throw error;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Fetch discovery document from well-known endpoint
105
+ */
106
+ private async fetchDiscoveryDocument(baseUrl: string): Promise<DiscoveryDocument> {
107
+ const wellKnownUrl = `${baseUrl}/.well-known/oauth-authorization-server`;
108
+
109
+ const response = await fetch(wellKnownUrl);
110
+ if (!response.ok) {
111
+ throw new Error(`Failed to fetch discovery document: ${response.status} ${response.statusText}`);
112
+ }
113
+ const discoveryDocument = await response.json() as DiscoveryDocument;
114
+ if (!discoveryDocument.issuer || !discoveryDocument.jwks_uri) {
115
+ throw new Error('Invalid discovery document: missing issuer or jwks_uri');
116
+ }
117
+
118
+ return discoveryDocument;
119
+ }
120
+
121
+ private async verifyToken(token: string): Promise<boolean> {
122
+ if (!this.initialized) {
123
+ throw new Error('TokenVerifier not initialized. Call initialize() first.');
124
+ }
125
+
126
+ if (!this.jwks || !this.issuer) {
127
+ throw new Error('TokenVerifier not properly configured.');
128
+ }
129
+
130
+ try {
131
+ await jwtVerify(token, this.jwks, {
132
+ issuer: this.issuer,
133
+ audience: AUDIENCE,
134
+ clockTolerance: DEFAULT_LEEWAY,
135
+ });
136
+ return true;
137
+ } catch (error) {
138
+ logger.error('Token verification failed:', error);
139
+ return false;
140
+ }
141
+ }
142
+
143
+ }
144
+
145
+ export const getTokenVerifier = async (): Promise<TokenVerifier> => TokenVerifier.getInitializedInstance();