@spidy092/auth-client 2.0.6 → 2.0.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.
- package/config.js +0 -1
- package/core.js +15 -71
- package/package.json +1 -1
package/config.js
CHANGED
package/core.js
CHANGED
|
@@ -12,7 +12,7 @@ import { getConfig, isRouterMode } from './config';
|
|
|
12
12
|
|
|
13
13
|
let callbackProcessed = false;
|
|
14
14
|
|
|
15
|
-
export function login(clientKeyArg, redirectUriArg
|
|
15
|
+
export function login(clientKeyArg, redirectUriArg) {
|
|
16
16
|
// ✅ Reset callback state when starting new login
|
|
17
17
|
resetCallbackState();
|
|
18
18
|
|
|
@@ -25,14 +25,11 @@ export function login(clientKeyArg, redirectUriArg, options = {}) {
|
|
|
25
25
|
|
|
26
26
|
const clientKey = clientKeyArg || defaultClientKey;
|
|
27
27
|
const redirectUri = redirectUriArg || defaultRedirectUri;
|
|
28
|
-
const { codeChallenge, codeChallengeMethod, state } = options;
|
|
29
28
|
|
|
30
29
|
console.log('🔄 Smart Login initiated:', {
|
|
31
30
|
mode: isRouterMode() ? 'ROUTER' : 'CLIENT',
|
|
32
31
|
clientKey,
|
|
33
|
-
redirectUri
|
|
34
|
-
hasPKCE: !!codeChallenge,
|
|
35
|
-
hasState: !!state
|
|
32
|
+
redirectUri
|
|
36
33
|
});
|
|
37
34
|
|
|
38
35
|
if (!clientKey || !redirectUri) {
|
|
@@ -44,39 +41,27 @@ export function login(clientKeyArg, redirectUriArg, options = {}) {
|
|
|
44
41
|
|
|
45
42
|
if (isRouterMode()) {
|
|
46
43
|
// Router mode: Direct backend authentication
|
|
47
|
-
return routerLogin(clientKey, redirectUri
|
|
44
|
+
return routerLogin(clientKey, redirectUri);
|
|
48
45
|
} else {
|
|
49
46
|
// Client mode: Redirect to centralized login
|
|
50
|
-
return clientLogin(clientKey, redirectUri
|
|
47
|
+
return clientLogin(clientKey, redirectUri);
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
// ✅ Router mode: Direct backend call
|
|
55
|
-
function routerLogin(clientKey, redirectUri
|
|
52
|
+
function routerLogin(clientKey, redirectUri) {
|
|
56
53
|
const { authBaseUrl } = getConfig();
|
|
57
|
-
const { codeChallenge, codeChallengeMethod, state } = options;
|
|
58
|
-
|
|
59
|
-
// Build URL with PKCE and state parameters
|
|
60
|
-
const params = new URLSearchParams({
|
|
61
|
-
redirect_uri: redirectUri
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
if (codeChallenge) {
|
|
65
|
-
params.append('code_challenge', codeChallenge);
|
|
66
|
-
params.append('code_challenge_method', codeChallengeMethod || 'S256');
|
|
67
|
-
}
|
|
68
54
|
|
|
69
|
-
|
|
70
|
-
|
|
55
|
+
const params = new URLSearchParams();
|
|
56
|
+
if (redirectUri) {
|
|
57
|
+
params.append('redirect_uri', redirectUri);
|
|
71
58
|
}
|
|
72
|
-
|
|
73
|
-
const backendLoginUrl = `${authBaseUrl}/login/${clientKey}
|
|
59
|
+
const query = params.toString();
|
|
60
|
+
const backendLoginUrl = `${authBaseUrl}/login/${clientKey}${query ? `?${query}` : ''}`;
|
|
74
61
|
|
|
75
62
|
console.log('🏭 Router Login: Direct backend authentication', {
|
|
76
63
|
clientKey,
|
|
77
64
|
redirectUri,
|
|
78
|
-
hasPKCE: !!codeChallenge,
|
|
79
|
-
hasState: !!state,
|
|
80
65
|
backendUrl: backendLoginUrl
|
|
81
66
|
});
|
|
82
67
|
|
|
@@ -84,32 +69,20 @@ function routerLogin(clientKey, redirectUri, options = {}) {
|
|
|
84
69
|
}
|
|
85
70
|
|
|
86
71
|
// ✅ Client mode: Centralized login
|
|
87
|
-
function clientLogin(clientKey, redirectUri
|
|
72
|
+
function clientLogin(clientKey, redirectUri) {
|
|
88
73
|
const { accountUiUrl } = getConfig();
|
|
89
|
-
const { codeChallenge, codeChallengeMethod, state } = options;
|
|
90
74
|
|
|
91
|
-
// Build URL with PKCE and state parameters
|
|
92
75
|
const params = new URLSearchParams({
|
|
93
|
-
client: clientKey
|
|
94
|
-
redirect_uri: redirectUri
|
|
76
|
+
client: clientKey
|
|
95
77
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
params.append('code_challenge', codeChallenge);
|
|
99
|
-
params.append('code_challenge_method', codeChallengeMethod || 'S256');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (state) {
|
|
103
|
-
params.append('state', state);
|
|
78
|
+
if (redirectUri) {
|
|
79
|
+
params.append('redirect_uri', redirectUri);
|
|
104
80
|
}
|
|
105
|
-
|
|
106
81
|
const centralizedLoginUrl = `${accountUiUrl}/login?${params.toString()}`;
|
|
107
82
|
|
|
108
83
|
console.log('🔄 Client Login: Redirecting to centralized login', {
|
|
109
84
|
clientKey,
|
|
110
85
|
redirectUri,
|
|
111
|
-
hasPKCE: !!codeChallenge,
|
|
112
|
-
hasState: !!state,
|
|
113
86
|
centralizedUrl: centralizedLoginUrl
|
|
114
87
|
});
|
|
115
88
|
|
|
@@ -187,40 +160,12 @@ export function handleCallback() {
|
|
|
187
160
|
const params = new URLSearchParams(window.location.search);
|
|
188
161
|
const accessToken = params.get('access_token');
|
|
189
162
|
const error = params.get('error');
|
|
190
|
-
const state = params.get('state');
|
|
191
163
|
|
|
192
164
|
console.log('🔄 Callback handling:', {
|
|
193
165
|
hasAccessToken: !!accessToken,
|
|
194
|
-
error
|
|
195
|
-
hasState: !!state
|
|
166
|
+
error
|
|
196
167
|
});
|
|
197
168
|
|
|
198
|
-
// ✅ Validate state parameter
|
|
199
|
-
if (state) {
|
|
200
|
-
const storedState = sessionStorage.getItem('oauth_state');
|
|
201
|
-
if (storedState && storedState !== state) {
|
|
202
|
-
console.error('❌ State mismatch - possible CSRF attack', {
|
|
203
|
-
received: state.substring(0, 10),
|
|
204
|
-
expected: storedState.substring(0, 10)
|
|
205
|
-
});
|
|
206
|
-
throw new Error('Invalid state parameter - authentication may have been compromised');
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Check state age (prevent replay attacks)
|
|
210
|
-
const stateTimestamp = parseInt(sessionStorage.getItem('pkce_timestamp') || '0', 10);
|
|
211
|
-
const stateAge = Date.now() - stateTimestamp;
|
|
212
|
-
const MAX_STATE_AGE = 10 * 60 * 1000; // 10 minutes
|
|
213
|
-
|
|
214
|
-
if (stateAge > MAX_STATE_AGE) {
|
|
215
|
-
console.error('❌ State expired', { stateAge });
|
|
216
|
-
throw new Error('Authentication state expired - please try again');
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// Clear state after validation
|
|
220
|
-
sessionStorage.removeItem('oauth_state');
|
|
221
|
-
sessionStorage.removeItem('pkce_timestamp');
|
|
222
|
-
}
|
|
223
|
-
|
|
224
169
|
// ✅ Prevent duplicate callback processing
|
|
225
170
|
if (callbackProcessed) {
|
|
226
171
|
const existingToken = getToken();
|
|
@@ -235,7 +180,6 @@ export function handleCallback() {
|
|
|
235
180
|
callbackProcessed = true;
|
|
236
181
|
sessionStorage.removeItem('originalApp');
|
|
237
182
|
sessionStorage.removeItem('returnUrl');
|
|
238
|
-
sessionStorage.removeItem('pkce_code_verifier'); // Clear PKCE verifier after use
|
|
239
183
|
|
|
240
184
|
if (error) {
|
|
241
185
|
const errorDescription = params.get('error_description') || error;
|