-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseOAuthCallback.ts
More file actions
169 lines (151 loc) · 5.13 KB
/
Copy pathuseOAuthCallback.ts
File metadata and controls
169 lines (151 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { useCallback, useEffect, useState } from 'react';
import { clearCSRFToken, validateCSRFToken } from '@/components/shared/utils/config/config';
import { clearAuthData } from '@/utils/auth-utils';
/**
* OAuth callback parameters extracted from URL
*/
export interface OAuthCallbackParams {
code: string | null;
state: string | null;
error: string | null;
error_description: string | null;
}
/**
* OAuth callback processing result
*/
export interface OAuthCallbackResult {
isProcessing: boolean;
isValid: boolean;
params: OAuthCallbackParams;
error: string | null;
cleanupURL: () => void;
}
/**
* Custom hook to handle OAuth callback flow
*
* This hook:
* 1. Extracts OAuth parameters (code, state, error) from URL
* 2. Validates CSRF token (state parameter)
* 3. Returns the authorization code and a cleanup function
*
* Note: Call cleanupURL() after you've processed the authorization code
*
* @returns OAuth callback processing result with cleanupURL function
*
* @example
* ```tsx
* const { isProcessing, isValid, params, error, cleanupURL } = useOAuthCallback();
*
* useEffect(() => {
* if (!isProcessing && isValid && params.code) {
* // Exchange code for tokens
* exchangeCodeForTokens(params.code).then(() => {
* cleanupURL(); // Clean up after processing
* });
* }
* }, [isProcessing, isValid, params.code]);
* ```
*/
export const useOAuthCallback = (): OAuthCallbackResult => {
const [result, setResult] = useState<Omit<OAuthCallbackResult, 'cleanupURL'>>({
isProcessing: true,
isValid: false,
params: {
code: null,
state: null,
error: null,
error_description: null,
},
error: null,
});
// Cleanup function that can be called by the consuming component
const cleanupURL = useCallback(() => {
const url = new URL(window.location.href);
url.searchParams.delete('code');
url.searchParams.delete('state');
url.searchParams.delete('scope');
url.searchParams.delete('error');
url.searchParams.delete('error_description');
window.history.replaceState({}, '', url.toString());
}, []);
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
// Extract OAuth parameters
const code = urlParams.get('code');
const state = urlParams.get('state');
const error = urlParams.get('error');
const error_description = urlParams.get('error_description');
// Check if this is an OAuth callback (has code or error parameter)
const isOAuthCallback = code !== null || error !== null || state !== null;
if (!isOAuthCallback) {
// Not an OAuth callback, mark as complete
setResult({
isProcessing: false,
isValid: false,
params: { code: null, state: null, error: null, error_description: null },
error: null,
});
return;
}
// Handle OAuth error response
if (error) {
console.error('OAuth error:', error, error_description);
setResult({
isProcessing: false,
isValid: false,
params: { code, state, error, error_description },
error: error_description || error,
});
cleanupURL();
return;
}
// Validate CSRF token (state parameter)
if (!state) {
console.error('[DEBUG] Missing state parameter in OAuth callback');
clearAuthData();
setResult({
isProcessing: false,
isValid: false,
params: { code, state, error, error_description },
error: 'Missing state parameter - potential security threat',
});
window.location.replace(window.location.origin);
return;
}
if (!validateCSRFToken(state)) {
console.error('[DEBUG] CSRF token validation failed - potential security threat');
clearAuthData();
setResult({
isProcessing: false,
isValid: false,
params: { code, state, error, error_description },
error: 'CSRF token validation failed',
});
return;
}
// CSRF validation passed
clearCSRFToken();
// Validate that we have the authorization code
if (!code) {
console.error('Missing authorization code in OAuth callback');
setResult({
isProcessing: false,
isValid: false,
params: { code, state, error, error_description },
error: 'Missing authorization code',
});
cleanupURL();
return;
}
setResult({
isProcessing: false,
isValid: true,
params: { code, state, error, error_description },
error: null,
});
}, [cleanupURL]); // Run only once on mount
return {
...result,
cleanupURL,
};
};