@oxyhq/core 3.4.17 → 3.4.19
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/HttpService.js +27 -2
- package/dist/cjs/OxyServices.base.js +2 -3
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/HttpService.js +27 -2
- package/dist/esm/OxyServices.base.js +2 -3
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/HttpService.d.ts +4 -0
- package/package.json +1 -1
- package/src/HttpService.ts +34 -2
- package/src/OxyServices.base.ts +2 -3
- package/src/__tests__/httpServiceCsrf.test.ts +68 -0
- package/src/__tests__/linkedClient.test.ts +103 -13
package/dist/cjs/HttpService.js
CHANGED
|
@@ -103,6 +103,7 @@ class HttpService {
|
|
|
103
103
|
this.tokenRefreshPromise = null;
|
|
104
104
|
this.tokenRefreshCooldownUntil = 0;
|
|
105
105
|
this.authRefreshHandler = null;
|
|
106
|
+
this.accessTokenProvider = null;
|
|
106
107
|
/**
|
|
107
108
|
* Fan-out listeners notified on EVERY access-token change on this instance:
|
|
108
109
|
* explicit `setTokens`, `clearTokens`, an AuthManager-owned refresh, and the
|
|
@@ -133,6 +134,24 @@ class HttpService {
|
|
|
133
134
|
this.deduplicator = new requestUtils_1.RequestDeduplicator();
|
|
134
135
|
this.requestQueue = new requestUtils_1.RequestQueue(config.maxConcurrentRequests || 10, config.requestQueueSize || 100);
|
|
135
136
|
}
|
|
137
|
+
syncAccessTokenFromProvider() {
|
|
138
|
+
if (!this.accessTokenProvider) {
|
|
139
|
+
return this.tokenStore.getAccessToken();
|
|
140
|
+
}
|
|
141
|
+
const providedToken = this.accessTokenProvider();
|
|
142
|
+
const currentToken = this.tokenStore.getAccessToken();
|
|
143
|
+
if (providedToken) {
|
|
144
|
+
if (providedToken !== currentToken) {
|
|
145
|
+
this.tokenStore.setTokens(providedToken);
|
|
146
|
+
this.notifyTokenChange();
|
|
147
|
+
}
|
|
148
|
+
return providedToken;
|
|
149
|
+
}
|
|
150
|
+
if (currentToken) {
|
|
151
|
+
this.clearTokens();
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
136
155
|
/**
|
|
137
156
|
* Robust FormData detection that works in browser, React Native, and
|
|
138
157
|
* Node.js polyfill environments.
|
|
@@ -688,7 +707,7 @@ class HttpService {
|
|
|
688
707
|
* Get auth header with automatic token refresh
|
|
689
708
|
*/
|
|
690
709
|
async getAuthHeader() {
|
|
691
|
-
const accessToken = this.
|
|
710
|
+
const accessToken = this.syncAccessTokenFromProvider();
|
|
692
711
|
if (!accessToken) {
|
|
693
712
|
return null;
|
|
694
713
|
}
|
|
@@ -700,7 +719,10 @@ class HttpService {
|
|
|
700
719
|
const refreshed = await this.refreshAccessToken('preflight');
|
|
701
720
|
if (refreshed)
|
|
702
721
|
return `Bearer ${refreshed}`;
|
|
703
|
-
|
|
722
|
+
if (decoded.exp > currentTime) {
|
|
723
|
+
return `Bearer ${accessToken}`;
|
|
724
|
+
}
|
|
725
|
+
// Refresh failed — don't use an expired token (would cause 401 loop)
|
|
704
726
|
return null;
|
|
705
727
|
}
|
|
706
728
|
return `Bearer ${accessToken}`;
|
|
@@ -803,6 +825,9 @@ class HttpService {
|
|
|
803
825
|
setAuthRefreshHandler(handler) {
|
|
804
826
|
this.authRefreshHandler = handler;
|
|
805
827
|
}
|
|
828
|
+
setAccessTokenProvider(provider) {
|
|
829
|
+
this.accessTokenProvider = provider;
|
|
830
|
+
}
|
|
806
831
|
clearTokens() {
|
|
807
832
|
this.tokenStore.clearTokens();
|
|
808
833
|
this.tokenStore.clearCsrfToken();
|
|
@@ -113,12 +113,10 @@ class OxyServicesBase {
|
|
|
113
113
|
};
|
|
114
114
|
syncToken(this.getAccessToken());
|
|
115
115
|
const unsubscribe = this.onTokensChanged(syncToken);
|
|
116
|
+
client.setAccessTokenProvider(() => this.getAccessToken());
|
|
116
117
|
client.setAuthRefreshHandler(async (reason) => {
|
|
117
118
|
const refreshed = await this.httpService.refreshAccessToken(reason);
|
|
118
119
|
if (!refreshed) {
|
|
119
|
-
if (reason === 'response-401') {
|
|
120
|
-
this.clearTokens();
|
|
121
|
-
}
|
|
122
120
|
return null;
|
|
123
121
|
}
|
|
124
122
|
syncToken(refreshed);
|
|
@@ -129,6 +127,7 @@ class OxyServicesBase {
|
|
|
129
127
|
dispose: () => {
|
|
130
128
|
unsubscribe();
|
|
131
129
|
client.setAuthRefreshHandler(null);
|
|
130
|
+
client.setAccessTokenProvider(null);
|
|
132
131
|
client.clearTokens();
|
|
133
132
|
},
|
|
134
133
|
};
|