@warriorteam/redai-zalo-sdk 1.12.0 → 1.12.2

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.
@@ -1,179 +1,179 @@
1
- /**
2
- * Example: Official Account Authentication with PKCE
3
- * Demonstrates how to use the updated createOAAuthUrl method with PKCE support
4
- */
5
-
6
- import { ZaloSDK } from '../src';
7
-
8
- // Initialize SDK
9
- const sdk = new ZaloSDK({
10
- appId: 'your_app_id',
11
- appSecret: 'your_app_secret',
12
- });
13
-
14
- // Example 1: Basic OA Auth without PKCE
15
- async function basicOAAuth() {
16
- console.log('=== Basic OA Auth (without PKCE) ===');
17
-
18
- const redirectUri = 'https://your-app.com/callback';
19
-
20
- // Create auth URL - state will be auto-generated with 'zalo_oa_' prefix
21
- const authResult = sdk.auth.createOAAuthUrl(redirectUri);
22
-
23
- console.log('Authorization URL:', authResult.url);
24
- console.log('Generated State:', authResult.state);
25
-
26
- // You can also provide custom state
27
- const customAuthResult = sdk.auth.createOAAuthUrl(redirectUri, 'my_custom_state');
28
- console.log('Custom State URL:', customAuthResult.url);
29
- console.log('Custom State:', customAuthResult.state);
30
- }
31
-
32
- // Example 2: OA Auth with PKCE for enhanced security (Manual PKCE)
33
- async function oaAuthWithPKCE() {
34
- console.log('\n=== OA Auth with Manual PKCE ===');
35
-
36
- const redirectUri = 'https://your-app.com/callback';
37
-
38
- // Step 1: Generate PKCE configuration
39
- const pkce = sdk.auth.generatePKCE();
40
- console.log('Generated PKCE:');
41
- console.log('- Code Verifier:', pkce.code_verifier);
42
- console.log('- Code Challenge:', pkce.code_challenge);
43
- console.log('- Challenge Method:', pkce.code_challenge_method);
44
-
45
- // Step 2: Create auth URL with manual PKCE
46
- const authResult = sdk.auth.createOAAuthUrl(redirectUri, undefined, pkce, true);
47
-
48
- console.log('\nAuthorization URL with PKCE:', authResult.url);
49
- console.log('Generated State:', authResult.state);
50
- console.log('Used PKCE:', authResult.pkce);
51
-
52
- // IMPORTANT: Store the code_verifier and state for later use
53
- // You'll need these when exchanging the authorization code for access token
54
- console.log('\n⚠️ IMPORTANT: Store these values for token exchange:');
55
- console.log('- Code Verifier:', pkce.code_verifier);
56
- console.log('- State:', authResult.state);
57
-
58
- return { pkce, state: authResult.state };
59
- }
60
-
61
- // Example 2b: OA Auth with Auto-Generated PKCE
62
- async function oaAuthWithAutoPKCE() {
63
- console.log('\n=== OA Auth with Auto-Generated PKCE ===');
64
-
65
- const redirectUri = 'https://your-app.com/callback';
66
-
67
- // Create auth URL with auto-generated PKCE (pkce=undefined, usePkce=true)
68
- const authResult = sdk.auth.createOAAuthUrl(redirectUri, undefined, undefined, true);
69
-
70
- console.log('Authorization URL with Auto PKCE:', authResult.url);
71
- console.log('Generated State:', authResult.state);
72
- console.log('Auto-Generated PKCE:', authResult.pkce);
73
-
74
- // IMPORTANT: Store the auto-generated PKCE and state
75
- console.log('\n⚠️ IMPORTANT: Store these auto-generated values:');
76
- console.log('- Code Verifier:', authResult.pkce?.code_verifier);
77
- console.log('- State:', authResult.state);
78
-
79
- return authResult;
80
- }
81
-
82
- // Example 3: Complete flow - Authorization + Token Exchange
83
- async function completeOAFlow() {
84
- console.log('\n=== Complete OA Flow with PKCE ===');
85
-
86
- const redirectUri = 'https://your-app.com/callback';
87
-
88
- // Step 1: Generate PKCE and create auth URL
89
- const pkce = sdk.auth.generatePKCE();
90
- const authResult = sdk.auth.createOAAuthUrl(redirectUri, 'my_oa_flow', pkce);
91
-
92
- console.log('1. Redirect user to:', authResult.url);
93
- console.log('2. Store state and code_verifier:', {
94
- state: authResult.state,
95
- code_verifier: pkce.code_verifier
96
- });
97
-
98
- // Step 2: After user authorizes and returns with code
99
- // (This would happen in your callback handler)
100
- const simulateCallback = async (authorizationCode: string, returnedState: string) => {
101
- console.log('\n3. User returned with authorization code');
102
-
103
- // Verify state matches
104
- if (returnedState !== authResult.state) {
105
- throw new Error('State mismatch - possible CSRF attack');
106
- }
107
-
108
- // Step 3: Exchange code for access token with PKCE
109
- try {
110
- const tokenResult = await sdk.auth.getOAAccessToken({
111
- app_id: 'your_app_id',
112
- app_secret: 'your_app_secret',
113
- code: authorizationCode,
114
- redirect_uri: redirectUri,
115
- code_verifier: pkce.code_verifier, // Include code_verifier for PKCE
116
- });
117
-
118
- console.log('4. Successfully obtained access token:', {
119
- access_token: tokenResult.access_token.substring(0, 20) + '...',
120
- expires_in: tokenResult.expires_in,
121
- has_refresh_token: !!tokenResult.refresh_token
122
- });
123
-
124
- return tokenResult;
125
- } catch (error) {
126
- console.error('Failed to exchange code for token:', error);
127
- throw error;
128
- }
129
- };
130
-
131
- // Simulate the callback (in real app, this would be handled by your callback endpoint)
132
- console.log('\n--- Simulating callback ---');
133
- // await simulateCallback('simulated_auth_code', authResult.state);
134
- }
135
-
136
- // Example 4: Using getAuthUrls method
137
- async function getAuthUrlsExample() {
138
- console.log('\n=== Get Auth URLs ===');
139
-
140
- const redirectUri = 'https://your-app.com/callback';
141
- const pkce = sdk.auth.generatePKCE();
142
-
143
- const authUrls = sdk.auth.getAuthUrls(redirectUri, pkce);
144
-
145
- console.log('All auth URLs:', {
146
- oa_auth_url: authUrls.oa_auth_url,
147
- social_auth_url: authUrls.social_auth_url,
148
- token_url: authUrls.token_url,
149
- refresh_url: authUrls.refresh_url
150
- });
151
- }
152
-
153
- // Run examples
154
- async function runExamples() {
155
- try {
156
- await basicOAAuth();
157
- await oaAuthWithPKCE();
158
- await oaAuthWithAutoPKCE();
159
- await completeOAFlow();
160
- await getAuthUrlsExample();
161
- } catch (error) {
162
- console.error('Example error:', error);
163
- }
164
- }
165
-
166
- // Export for use in other files
167
- export {
168
- basicOAAuth,
169
- oaAuthWithPKCE,
170
- oaAuthWithAutoPKCE,
171
- completeOAFlow,
172
- getAuthUrlsExample,
173
- runExamples
174
- };
175
-
176
- // Run if this file is executed directly
177
- if (require.main === module) {
178
- runExamples();
179
- }
1
+ /**
2
+ * Example: Official Account Authentication with PKCE
3
+ * Demonstrates how to use the updated createOAAuthUrl method with PKCE support
4
+ */
5
+
6
+ import { ZaloSDK } from '../src';
7
+
8
+ // Initialize SDK
9
+ const sdk = new ZaloSDK({
10
+ appId: 'your_app_id',
11
+ appSecret: 'your_app_secret',
12
+ });
13
+
14
+ // Example 1: Basic OA Auth without PKCE
15
+ async function basicOAAuth() {
16
+ console.log('=== Basic OA Auth (without PKCE) ===');
17
+
18
+ const redirectUri = 'https://your-app.com/callback';
19
+
20
+ // Create auth URL - state will be auto-generated with 'zalo_oa_' prefix
21
+ const authResult = sdk.auth.createOAAuthUrl(redirectUri);
22
+
23
+ console.log('Authorization URL:', authResult.url);
24
+ console.log('Generated State:', authResult.state);
25
+
26
+ // You can also provide custom state
27
+ const customAuthResult = sdk.auth.createOAAuthUrl(redirectUri, 'my_custom_state');
28
+ console.log('Custom State URL:', customAuthResult.url);
29
+ console.log('Custom State:', customAuthResult.state);
30
+ }
31
+
32
+ // Example 2: OA Auth with PKCE for enhanced security (Manual PKCE)
33
+ async function oaAuthWithPKCE() {
34
+ console.log('\n=== OA Auth with Manual PKCE ===');
35
+
36
+ const redirectUri = 'https://your-app.com/callback';
37
+
38
+ // Step 1: Generate PKCE configuration
39
+ const pkce = sdk.auth.generatePKCE();
40
+ console.log('Generated PKCE:');
41
+ console.log('- Code Verifier:', pkce.code_verifier);
42
+ console.log('- Code Challenge:', pkce.code_challenge);
43
+ console.log('- Challenge Method:', pkce.code_challenge_method);
44
+
45
+ // Step 2: Create auth URL with manual PKCE
46
+ const authResult = sdk.auth.createOAAuthUrl(redirectUri, undefined, true, pkce);
47
+
48
+ console.log('\nAuthorization URL with PKCE:', authResult.url);
49
+ console.log('Generated State:', authResult.state);
50
+ console.log('Used PKCE:', authResult.pkce);
51
+
52
+ // IMPORTANT: Store the code_verifier and state for later use
53
+ // You'll need these when exchanging the authorization code for access token
54
+ console.log('\n⚠️ IMPORTANT: Store these values for token exchange:');
55
+ console.log('- Code Verifier:', pkce.code_verifier);
56
+ console.log('- State:', authResult.state);
57
+
58
+ return { pkce, state: authResult.state };
59
+ }
60
+
61
+ // Example 2b: OA Auth with Auto-Generated PKCE
62
+ async function oaAuthWithAutoPKCE() {
63
+ console.log('\n=== OA Auth with Auto-Generated PKCE ===');
64
+
65
+ const redirectUri = 'https://your-app.com/callback';
66
+
67
+ // Create auth URL with auto-generated PKCE (usePkce=true, pkce=undefined)
68
+ const authResult = sdk.auth.createOAAuthUrl(redirectUri, undefined, true);
69
+
70
+ console.log('Authorization URL with Auto PKCE:', authResult.url);
71
+ console.log('Generated State:', authResult.state);
72
+ console.log('Auto-Generated PKCE:', authResult.pkce);
73
+
74
+ // IMPORTANT: Store the auto-generated PKCE and state
75
+ console.log('\n⚠️ IMPORTANT: Store these auto-generated values:');
76
+ console.log('- Code Verifier:', authResult.pkce?.code_verifier);
77
+ console.log('- State:', authResult.state);
78
+
79
+ return authResult;
80
+ }
81
+
82
+ // Example 3: Complete flow - Authorization + Token Exchange
83
+ async function completeOAFlow() {
84
+ console.log('\n=== Complete OA Flow with PKCE ===');
85
+
86
+ const redirectUri = 'https://your-app.com/callback';
87
+
88
+ // Step 1: Generate PKCE and create auth URL
89
+ const pkce = sdk.auth.generatePKCE();
90
+ const authResult = sdk.auth.createOAAuthUrl(redirectUri, 'my_oa_flow', pkce);
91
+
92
+ console.log('1. Redirect user to:', authResult.url);
93
+ console.log('2. Store state and code_verifier:', {
94
+ state: authResult.state,
95
+ code_verifier: pkce.code_verifier
96
+ });
97
+
98
+ // Step 2: After user authorizes and returns with code
99
+ // (This would happen in your callback handler)
100
+ const simulateCallback = async (authorizationCode: string, returnedState: string) => {
101
+ console.log('\n3. User returned with authorization code');
102
+
103
+ // Verify state matches
104
+ if (returnedState !== authResult.state) {
105
+ throw new Error('State mismatch - possible CSRF attack');
106
+ }
107
+
108
+ // Step 3: Exchange code for access token with PKCE
109
+ try {
110
+ const tokenResult = await sdk.auth.getOAAccessToken({
111
+ app_id: 'your_app_id',
112
+ app_secret: 'your_app_secret',
113
+ code: authorizationCode,
114
+ redirect_uri: redirectUri,
115
+ code_verifier: pkce.code_verifier, // Include code_verifier for PKCE
116
+ });
117
+
118
+ console.log('4. Successfully obtained access token:', {
119
+ access_token: tokenResult.access_token.substring(0, 20) + '...',
120
+ expires_in: tokenResult.expires_in,
121
+ has_refresh_token: !!tokenResult.refresh_token
122
+ });
123
+
124
+ return tokenResult;
125
+ } catch (error) {
126
+ console.error('Failed to exchange code for token:', error);
127
+ throw error;
128
+ }
129
+ };
130
+
131
+ // Simulate the callback (in real app, this would be handled by your callback endpoint)
132
+ console.log('\n--- Simulating callback ---');
133
+ // await simulateCallback('simulated_auth_code', authResult.state);
134
+ }
135
+
136
+ // Example 4: Using getAuthUrls method
137
+ async function getAuthUrlsExample() {
138
+ console.log('\n=== Get Auth URLs ===');
139
+
140
+ const redirectUri = 'https://your-app.com/callback';
141
+ const pkce = sdk.auth.generatePKCE();
142
+
143
+ const authUrls = sdk.auth.getAuthUrls(redirectUri, pkce);
144
+
145
+ console.log('All auth URLs:', {
146
+ oa_auth_url: authUrls.oa_auth_url,
147
+ social_auth_url: authUrls.social_auth_url,
148
+ token_url: authUrls.token_url,
149
+ refresh_url: authUrls.refresh_url
150
+ });
151
+ }
152
+
153
+ // Run examples
154
+ async function runExamples() {
155
+ try {
156
+ await basicOAAuth();
157
+ await oaAuthWithPKCE();
158
+ await oaAuthWithAutoPKCE();
159
+ await completeOAFlow();
160
+ await getAuthUrlsExample();
161
+ } catch (error) {
162
+ console.error('Example error:', error);
163
+ }
164
+ }
165
+
166
+ // Export for use in other files
167
+ export {
168
+ basicOAAuth,
169
+ oaAuthWithPKCE,
170
+ oaAuthWithAutoPKCE,
171
+ completeOAFlow,
172
+ getAuthUrlsExample,
173
+ runExamples
174
+ };
175
+
176
+ // Run if this file is executed directly
177
+ if (require.main === module) {
178
+ runExamples();
179
+ }