@dynamic-labs/utils 3.0.0-alpha.43 → 3.0.0-alpha.44
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/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
|
|
2
|
+
## [3.0.0-alpha.44](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.43...v3.0.0-alpha.44) (2024-08-15)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### Bug Fixes
|
|
6
|
+
|
|
7
|
+
* populate wallet new isPrimary flag ([#6597](https://github.com/dynamic-labs/DynamicAuth/issues/6597)) ([7809f81](https://github.com/dynamic-labs/DynamicAuth/commit/7809f81c9743b8bf7bc27dc34cf45923cf9baf99))
|
|
8
|
+
* twitter should no longer rely polling for oauth flow ([#6603](https://github.com/dynamic-labs/DynamicAuth/issues/6603)) ([09aea8b](https://github.com/dynamic-labs/DynamicAuth/commit/09aea8b3f197c25dcfb5bc72049b90859df246f3))
|
|
9
|
+
|
|
2
10
|
## [3.0.0-alpha.43](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.42...v3.0.0-alpha.43) (2024-08-15)
|
|
3
11
|
|
|
4
12
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/utils",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.44",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/dynamic-labs/dynamic-auth.git",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@dynamic-labs/sdk-api-core": "0.0.516",
|
|
30
30
|
"tldts": "6.0.16",
|
|
31
|
-
"@dynamic-labs/logger": "3.0.0-alpha.
|
|
32
|
-
"@dynamic-labs/types": "3.0.0-alpha.
|
|
31
|
+
"@dynamic-labs/logger": "3.0.0-alpha.44",
|
|
32
|
+
"@dynamic-labs/types": "3.0.0-alpha.44",
|
|
33
33
|
"buffer": "6.0.3",
|
|
34
34
|
"stream": "0.0.2"
|
|
35
35
|
},
|
|
@@ -5,13 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
|
|
6
6
|
var _tslib = require('../../../../_virtual/_tslib.cjs');
|
|
7
7
|
var types = require('@dynamic-labs/types');
|
|
8
|
-
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
|
|
9
8
|
var logger = require('../../../logger/logger.cjs');
|
|
10
9
|
|
|
11
|
-
const providersWithoutWindowOpenerReference = ['twitter'];
|
|
12
10
|
let authWindowInterval;
|
|
13
11
|
const createWindowOauth2Service = () => ({
|
|
14
|
-
getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl,
|
|
12
|
+
getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl, isMobile, initWebAuth, }) => new Promise((resolve, _reject) => {
|
|
15
13
|
/**
|
|
16
14
|
* Use redirect flow on mobile for all providers except Telegram
|
|
17
15
|
*/
|
|
@@ -27,7 +25,6 @@ const createWindowOauth2Service = () => ({
|
|
|
27
25
|
// Clear any potential pending timeouts and intervals
|
|
28
26
|
clearInterval(authWindowInterval);
|
|
29
27
|
const providersWaitingOauthMessage = {};
|
|
30
|
-
let shouldPool = false;
|
|
31
28
|
const authWindow = window.open('', '_blank', 'width=500,height=600');
|
|
32
29
|
const clearListeners = () => {
|
|
33
30
|
window.removeEventListener('message', handleWindowMessage);
|
|
@@ -35,7 +32,7 @@ const createWindowOauth2Service = () => ({
|
|
|
35
32
|
};
|
|
36
33
|
const handleWindowMessage = (event) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
37
34
|
const message = event.data;
|
|
38
|
-
const expectedOrigin = getExpectedOrigin(apiProvider
|
|
35
|
+
const expectedOrigin = getExpectedOrigin(apiProvider);
|
|
39
36
|
if (!expectedOrigin) {
|
|
40
37
|
return;
|
|
41
38
|
}
|
|
@@ -75,24 +72,20 @@ const createWindowOauth2Service = () => ({
|
|
|
75
72
|
}
|
|
76
73
|
handleAuthorizationMessage(message, provider, state);
|
|
77
74
|
});
|
|
78
|
-
const getExpectedOrigin = (apiProvider
|
|
75
|
+
const getExpectedOrigin = (apiProvider) => {
|
|
79
76
|
if (!(apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.redirectUrl)) {
|
|
80
77
|
return;
|
|
81
78
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
});
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
79
|
+
try {
|
|
80
|
+
const redirectUri = new URL(apiProvider.redirectUrl);
|
|
81
|
+
return redirectUri.origin;
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
logger.logger.error('Failed to parse social provider redirect url', {
|
|
85
|
+
error: e,
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
94
88
|
}
|
|
95
|
-
return expectedOrigin;
|
|
96
89
|
};
|
|
97
90
|
const handleTelegramCompletionMessage = (message) => {
|
|
98
91
|
logger.logger.debug('Telegram completion message received', {
|
|
@@ -113,8 +106,7 @@ const createWindowOauth2Service = () => ({
|
|
|
113
106
|
// check that the state we receive from message is the same state we calculated earlier
|
|
114
107
|
// this could be an attack
|
|
115
108
|
// this state check is used only by providers with an open window opener reference (eg, not twitter)
|
|
116
|
-
if (
|
|
117
|
-
state !== authState) {
|
|
109
|
+
if (state !== authState) {
|
|
118
110
|
typedReject({
|
|
119
111
|
code: types.SocialOAuthErrorCode.OAUTH_ERROR,
|
|
120
112
|
message: `Failed to connect ${provider} social account: Invalid random state`,
|
|
@@ -138,64 +130,15 @@ const createWindowOauth2Service = () => ({
|
|
|
138
130
|
// First we store the state in backend
|
|
139
131
|
initWebAuth().then(() => {
|
|
140
132
|
authWindow === null || authWindow === void 0 ? void 0 : authWindow.location.assign(oauthLoginUrl);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
authWindowInterval = setInterval(() => {
|
|
144
|
-
if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
|
|
145
|
-
return;
|
|
146
|
-
clearInterval(authWindowInterval);
|
|
147
|
-
setIsProcessing(false);
|
|
148
|
-
// user didn't complete oauth
|
|
149
|
-
if (providersWaitingOauthMessage[provider])
|
|
150
|
-
typedReject('user-cancelled');
|
|
151
|
-
}, 2000);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
// For provider that don't support window.opener, we need to use a timeout to pool the oauth result
|
|
155
|
-
// If we don't get a valid result in {async sessionTimeout} ms, we'll assume the user closed the window
|
|
156
|
-
// and we'll clear all states/listeners
|
|
157
|
-
const poolOauthResult = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
-
if (!shouldPool)
|
|
159
|
-
return;
|
|
160
|
-
const result = yield getOAuthResultFromApi();
|
|
161
|
-
if (!shouldPool)
|
|
162
|
-
return;
|
|
163
|
-
if ((result === null || result === void 0 ? void 0 : result.status) !== sdkApiCore.OauthResultStatus.Completed) {
|
|
164
|
-
authWindowInterval = setTimeout(() => {
|
|
165
|
-
poolOauthResult();
|
|
166
|
-
}, 1000);
|
|
133
|
+
authWindowInterval = setInterval(() => {
|
|
134
|
+
if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
|
|
167
135
|
return;
|
|
168
|
-
|
|
169
|
-
shouldPool = false;
|
|
170
|
-
const authMessage = {
|
|
171
|
-
code: result === null || result === void 0 ? void 0 : result.code,
|
|
172
|
-
error: result === null || result === void 0 ? void 0 : result.error,
|
|
173
|
-
provider,
|
|
174
|
-
type: 'authorization_response',
|
|
175
|
-
};
|
|
176
|
-
window.postMessage(authMessage, '*');
|
|
177
|
-
});
|
|
178
|
-
// start pooling oauth result
|
|
179
|
-
shouldPool = true;
|
|
180
|
-
poolOauthResult();
|
|
181
|
-
// if this is mobile, set a longer timeout to allow the user to login to the provider in the browser
|
|
182
|
-
let authWindowTimeout = sessionTimeout;
|
|
183
|
-
if (isMobile) {
|
|
184
|
-
authWindowTimeout = authWindowTimeout * 3;
|
|
185
|
-
}
|
|
186
|
-
authWindowInterval = setTimeout(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
187
|
-
shouldPool = false;
|
|
188
|
-
// clear all states/listeners, assuming user closed the window before completing oauth
|
|
189
|
-
if (providersWaitingOauthMessage[provider]) {
|
|
190
|
-
clearListeners();
|
|
191
|
-
typedReject({
|
|
192
|
-
code: types.SocialOAuthErrorCode.OAUTH_WINDOW_TIMEOUT,
|
|
193
|
-
message: `Connecting ${provider} account window timeout.`,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
136
|
+
clearInterval(authWindowInterval);
|
|
196
137
|
setIsProcessing(false);
|
|
197
|
-
|
|
198
|
-
|
|
138
|
+
// user didn't complete oauth
|
|
139
|
+
if (providersWaitingOauthMessage[provider])
|
|
140
|
+
typedReject('user-cancelled');
|
|
141
|
+
}, 2000);
|
|
199
142
|
});
|
|
200
143
|
}),
|
|
201
144
|
});
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { __awaiter } from '../../../../_virtual/_tslib.js';
|
|
3
3
|
import { ProviderEnum, SocialOAuthErrorCode } from '@dynamic-labs/types';
|
|
4
|
-
import { OauthResultStatus } from '@dynamic-labs/sdk-api-core';
|
|
5
4
|
import { logger } from '../../../logger/logger.js';
|
|
6
5
|
|
|
7
|
-
const providersWithoutWindowOpenerReference = ['twitter'];
|
|
8
6
|
let authWindowInterval;
|
|
9
7
|
const createWindowOauth2Service = () => ({
|
|
10
|
-
getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl,
|
|
8
|
+
getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl, isMobile, initWebAuth, }) => new Promise((resolve, _reject) => {
|
|
11
9
|
/**
|
|
12
10
|
* Use redirect flow on mobile for all providers except Telegram
|
|
13
11
|
*/
|
|
@@ -23,7 +21,6 @@ const createWindowOauth2Service = () => ({
|
|
|
23
21
|
// Clear any potential pending timeouts and intervals
|
|
24
22
|
clearInterval(authWindowInterval);
|
|
25
23
|
const providersWaitingOauthMessage = {};
|
|
26
|
-
let shouldPool = false;
|
|
27
24
|
const authWindow = window.open('', '_blank', 'width=500,height=600');
|
|
28
25
|
const clearListeners = () => {
|
|
29
26
|
window.removeEventListener('message', handleWindowMessage);
|
|
@@ -31,7 +28,7 @@ const createWindowOauth2Service = () => ({
|
|
|
31
28
|
};
|
|
32
29
|
const handleWindowMessage = (event) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
30
|
const message = event.data;
|
|
34
|
-
const expectedOrigin = getExpectedOrigin(apiProvider
|
|
31
|
+
const expectedOrigin = getExpectedOrigin(apiProvider);
|
|
35
32
|
if (!expectedOrigin) {
|
|
36
33
|
return;
|
|
37
34
|
}
|
|
@@ -71,24 +68,20 @@ const createWindowOauth2Service = () => ({
|
|
|
71
68
|
}
|
|
72
69
|
handleAuthorizationMessage(message, provider, state);
|
|
73
70
|
});
|
|
74
|
-
const getExpectedOrigin = (apiProvider
|
|
71
|
+
const getExpectedOrigin = (apiProvider) => {
|
|
75
72
|
if (!(apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.redirectUrl)) {
|
|
76
73
|
return;
|
|
77
74
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
});
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
75
|
+
try {
|
|
76
|
+
const redirectUri = new URL(apiProvider.redirectUrl);
|
|
77
|
+
return redirectUri.origin;
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
logger.error('Failed to parse social provider redirect url', {
|
|
81
|
+
error: e,
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
90
84
|
}
|
|
91
|
-
return expectedOrigin;
|
|
92
85
|
};
|
|
93
86
|
const handleTelegramCompletionMessage = (message) => {
|
|
94
87
|
logger.debug('Telegram completion message received', {
|
|
@@ -109,8 +102,7 @@ const createWindowOauth2Service = () => ({
|
|
|
109
102
|
// check that the state we receive from message is the same state we calculated earlier
|
|
110
103
|
// this could be an attack
|
|
111
104
|
// this state check is used only by providers with an open window opener reference (eg, not twitter)
|
|
112
|
-
if (
|
|
113
|
-
state !== authState) {
|
|
105
|
+
if (state !== authState) {
|
|
114
106
|
typedReject({
|
|
115
107
|
code: SocialOAuthErrorCode.OAUTH_ERROR,
|
|
116
108
|
message: `Failed to connect ${provider} social account: Invalid random state`,
|
|
@@ -134,64 +126,15 @@ const createWindowOauth2Service = () => ({
|
|
|
134
126
|
// First we store the state in backend
|
|
135
127
|
initWebAuth().then(() => {
|
|
136
128
|
authWindow === null || authWindow === void 0 ? void 0 : authWindow.location.assign(oauthLoginUrl);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
authWindowInterval = setInterval(() => {
|
|
140
|
-
if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
|
|
141
|
-
return;
|
|
142
|
-
clearInterval(authWindowInterval);
|
|
143
|
-
setIsProcessing(false);
|
|
144
|
-
// user didn't complete oauth
|
|
145
|
-
if (providersWaitingOauthMessage[provider])
|
|
146
|
-
typedReject('user-cancelled');
|
|
147
|
-
}, 2000);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
// For provider that don't support window.opener, we need to use a timeout to pool the oauth result
|
|
151
|
-
// If we don't get a valid result in {async sessionTimeout} ms, we'll assume the user closed the window
|
|
152
|
-
// and we'll clear all states/listeners
|
|
153
|
-
const poolOauthResult = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
154
|
-
if (!shouldPool)
|
|
155
|
-
return;
|
|
156
|
-
const result = yield getOAuthResultFromApi();
|
|
157
|
-
if (!shouldPool)
|
|
158
|
-
return;
|
|
159
|
-
if ((result === null || result === void 0 ? void 0 : result.status) !== OauthResultStatus.Completed) {
|
|
160
|
-
authWindowInterval = setTimeout(() => {
|
|
161
|
-
poolOauthResult();
|
|
162
|
-
}, 1000);
|
|
129
|
+
authWindowInterval = setInterval(() => {
|
|
130
|
+
if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
|
|
163
131
|
return;
|
|
164
|
-
|
|
165
|
-
shouldPool = false;
|
|
166
|
-
const authMessage = {
|
|
167
|
-
code: result === null || result === void 0 ? void 0 : result.code,
|
|
168
|
-
error: result === null || result === void 0 ? void 0 : result.error,
|
|
169
|
-
provider,
|
|
170
|
-
type: 'authorization_response',
|
|
171
|
-
};
|
|
172
|
-
window.postMessage(authMessage, '*');
|
|
173
|
-
});
|
|
174
|
-
// start pooling oauth result
|
|
175
|
-
shouldPool = true;
|
|
176
|
-
poolOauthResult();
|
|
177
|
-
// if this is mobile, set a longer timeout to allow the user to login to the provider in the browser
|
|
178
|
-
let authWindowTimeout = sessionTimeout;
|
|
179
|
-
if (isMobile) {
|
|
180
|
-
authWindowTimeout = authWindowTimeout * 3;
|
|
181
|
-
}
|
|
182
|
-
authWindowInterval = setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
183
|
-
shouldPool = false;
|
|
184
|
-
// clear all states/listeners, assuming user closed the window before completing oauth
|
|
185
|
-
if (providersWaitingOauthMessage[provider]) {
|
|
186
|
-
clearListeners();
|
|
187
|
-
typedReject({
|
|
188
|
-
code: SocialOAuthErrorCode.OAUTH_WINDOW_TIMEOUT,
|
|
189
|
-
message: `Connecting ${provider} account window timeout.`,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
132
|
+
clearInterval(authWindowInterval);
|
|
192
133
|
setIsProcessing(false);
|
|
193
|
-
|
|
194
|
-
|
|
134
|
+
// user didn't complete oauth
|
|
135
|
+
if (providersWaitingOauthMessage[provider])
|
|
136
|
+
typedReject('user-cancelled');
|
|
137
|
+
}, 2000);
|
|
195
138
|
});
|
|
196
139
|
}),
|
|
197
140
|
});
|