Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 03:15 UTC
fix(sdk): auto-detect OAuth tokens in AuthScheme helpers to prevent stuck INITIATED state
jkomyno committed
Mar 13, 2026 at 02:04 UTC
b4d82be00c2d2f8a16c7eb4ea2aa8d470431a001
5 files changed
+202
-17
python/composio/core/models/connected_accounts.py
+15
-3
@@ -94,13 +94,22 @@ class AuthScheme:
94
"""
95
Create a new connected account using OAuth 1.0.
96
"""
97
+ oauth_token = options.get("oauth_token") # type: ignore[union-attr]
98
+ oauth_token_secret = options.get("oauth_token_secret") # type: ignore[union-attr]
99
+ has_tokens = (
100
+ isinstance(oauth_token, str)
101
+ and len(oauth_token) > 0
102
+ and isinstance(oauth_token_secret, str)
103
+ and len(oauth_token_secret) > 0
104
+ )
105
+ status = "ACTIVE" if has_tokens else "INITIALIZING"
106
return {
107
"auth_scheme": "OAUTH1",
108
"val": t.cast(
109
connected_account_create_params.ConnectionStateUnionMember0Val,
110
{
111
+ "status": status,
112
**options,
103
- "status": "INITIALIZING",
113
},
114
),
115
}
@@ -109,15 +118,18 @@ class AuthScheme:
118
self, options: connected_account_create_params.ConnectionStateUnionMember1Val
119
) -> connected_account_create_params.ConnectionState:
120
"""
112
- Create a new connected account using OAuth 1.0.
121
+ Create a new connected account using OAuth 2.0.
122
"""
123
+ access_token = options.get("access_token") # type: ignore[union-attr]
124
+ has_token = isinstance(access_token, str) and len(access_token) > 0
125
+ status = "ACTIVE" if has_token else "INITIALIZING"
126
return {
127
"auth_scheme": "OAUTH2",
128
"val": t.cast(
129
connected_account_create_params.ConnectionStateUnionMember1Val,
130
{
131
+ "status": status,
132
**options,
120
- "status": "INITIALIZING",
133
},
134
),
135
}
python/tests/test_connected_accounts.py
+73
-1
@@ -13,7 +13,18 @@ from composio.core.models.connected_accounts import (
13
14
15
class TestAuthScheme:
16
- def test_oauth2_sets_initializing_status(self):
16
+ def test_oauth2_with_access_token_sets_active_status(self):
17
+ scheme = AuthScheme()
18
+ options = {"access_token": "test_token", "refresh_token": "test_refresh"}
19
+
20
+ state = scheme.oauth2(options)
21
+
22
+ assert state["auth_scheme"] == "OAUTH2"
23
+ assert state["val"]["access_token"] == "test_token"
24
+ assert state["val"]["refresh_token"] == "test_refresh"
25
+ assert state["val"]["status"] == "ACTIVE"
26
+
27
+ def test_oauth2_without_access_token_sets_initializing_status(self):
28
scheme = AuthScheme()
29
options = {"client_id": "id", "client_secret": "secret"}
30
@@ -24,6 +35,40 @@ class TestAuthScheme:
35
assert state["val"]["client_secret"] == "secret"
36
assert state["val"]["status"] == "INITIALIZING"
37
38
+ def test_oauth2_with_empty_access_token_sets_initializing_status(self):
39
+ scheme = AuthScheme()
40
+ state = scheme.oauth2({"access_token": ""})
41
+
42
+ assert state["val"]["status"] == "INITIALIZING"
43
+
44
+ def test_oauth2_honors_explicit_status_override(self):
45
+ scheme = AuthScheme()
46
+ state = scheme.oauth2({"access_token": "test_token", "status": "INITIALIZING"})
47
+
48
+ assert state["val"]["status"] == "INITIALIZING"
49
+
50
+ def test_oauth1_with_both_tokens_sets_active_status(self):
51
+ scheme = AuthScheme()
52
+ state = scheme.oauth1({"oauth_token": "tok", "oauth_token_secret": "secret"})
53
+
54
+ assert state["auth_scheme"] == "OAUTH1"
55
+ assert state["val"]["oauth_token"] == "tok"
56
+ assert state["val"]["oauth_token_secret"] == "secret"
57
+ assert state["val"]["status"] == "ACTIVE"
58
+
59
+ def test_oauth1_without_secret_sets_initializing_status(self):
60
+ scheme = AuthScheme()
61
+ state = scheme.oauth1({"oauth_token": "tok"})
62
+
63
+ assert state["auth_scheme"] == "OAUTH1"
64
+ assert state["val"]["status"] == "INITIALIZING"
65
+
66
+ def test_oauth1_with_empty_token_sets_initializing_status(self):
67
+ scheme = AuthScheme()
68
+ state = scheme.oauth1({"oauth_token": "", "oauth_token_secret": "secret"})
69
+
70
+ assert state["val"]["status"] == "INITIALIZING"
71
+
72
@pytest.mark.parametrize(
73
"method_name, expected_auth_scheme, expected_status",
74
[
@@ -288,6 +333,33 @@ class TestConnectedAccounts:
333
assert call_kwargs["user_id"] == "user-1"
334
assert "callback_url" not in call_kwargs
335
336
+ def test_initiate_with_oauth2_tokens_returns_active_connection_request(
337
+ self, connected_accounts, mock_client
338
+ ):
339
+ mock_accounts = Mock()
340
+ mock_accounts.items = []
341
+ mock_client.connected_accounts.list.return_value = mock_accounts
342
+
343
+ mock_response = Mock()
344
+ mock_response.id = "conn-active"
345
+ mock_response.connection_data.val.status = "ACTIVE"
346
+ mock_response.connection_data.val.redirect_url = None
347
+ mock_client.connected_accounts.create.return_value = mock_response
348
+
349
+ scheme = AuthScheme()
350
+ config = scheme.oauth2(
351
+ {"access_token": "tok", "refresh_token": "ref", "expires_in": 3600}
352
+ )
353
+
354
+ result = connected_accounts.initiate(
355
+ user_id="user-1", auth_config_id="auth-1", config=config
356
+ )
357
+
358
+ assert isinstance(result, ConnectionRequest)
359
+ assert result.id == "conn-active"
360
+ assert result.status == "ACTIVE"
361
+ assert result.redirect_url is None
362
+
363
def test_wait_for_connection_delegates_to_connection_request(self, mock_client):
364
connected_accounts = ConnectedAccounts(client=mock_client)
365
ts/packages/core/src/models/AuthScheme.ts
+9
-2
@@ -33,10 +33,11 @@ export class AuthScheme {
33
expired_at?: string;
34
}
35
): ConnectionData {
36
+ const hasToken = typeof params.access_token === 'string' && params.access_token.length > 0;
37
return {
38
authScheme: AuthSchemeTypes.OAUTH2,
39
val: {
39
- status: ConnectionStatuses.INITIALIZING,
40
+ status: hasToken ? ConnectionStatuses.ACTIVE : ConnectionStatuses.INITIALIZING,
41
...params,
42
},
43
};
@@ -50,6 +51,7 @@ export class AuthScheme {
51
static OAuth1(
52
params: BaseConnectionFields & {
53
oauth_token?: string;
54
+ oauth_token_secret?: string;
55
consumer_key?: string;
56
redirectUrl?: string;
57
callback_url?: string;
@@ -58,10 +60,15 @@ export class AuthScheme {
60
expired_at?: string;
61
}
62
): ConnectionData {
63
+ const hasTokens =
64
+ typeof params.oauth_token === 'string' &&
65
+ params.oauth_token.length > 0 &&
66
+ typeof params.oauth_token_secret === 'string' &&
67
+ params.oauth_token_secret.length > 0;
68
return {
69
authScheme: AuthSchemeTypes.OAUTH1,
70
val: {
64
- status: ConnectionStatuses.INITIALIZING,
71
+ status: hasTokens ? ConnectionStatuses.ACTIVE : ConnectionStatuses.INITIALIZING,
72
...params,
73
},
74
};
ts/packages/core/test/AuthConfigs/AuthScheme.test.ts
+71
-6
@@ -8,7 +8,7 @@ import {
8
9
describe('AuthScheme', () => {
10
describe('OAuth2', () => {
11
- it('should create OAuth2 connection data with required fields', () => {
11
+ it('should set ACTIVE status when access_token is provided', () => {
12
const params = {
13
access_token: 'test_token',
14
token_type: 'Bearer',
@@ -19,7 +19,7 @@ describe('AuthScheme', () => {
19
expect(result).toEqual({
20
authScheme: AuthSchemeTypes.OAUTH2,
21
val: {
22
- status: ConnectionStatuses.INITIALIZING,
22
+ status: ConnectionStatuses.ACTIVE,
23
access_token: 'test_token',
24
token_type: 'Bearer',
25
},
@@ -29,7 +29,7 @@ describe('AuthScheme', () => {
29
expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
30
});
31
32
- it('should create OAuth2 connection data with all optional fields', () => {
32
+ it('should set ACTIVE status with all optional fields when access_token is provided', () => {
33
const params = {
34
access_token: 'test_token',
35
token_type: 'Bearer',
@@ -49,7 +49,7 @@ describe('AuthScheme', () => {
49
expect(result).toEqual({
50
authScheme: AuthSchemeTypes.OAUTH2,
51
val: {
52
- status: ConnectionStatuses.INITIALIZING,
52
+ status: ConnectionStatuses.ACTIVE,
53
...params,
54
},
55
});
@@ -57,10 +57,46 @@ describe('AuthScheme', () => {
57
// Verify Zod schema validation
58
expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
59
});
60
+
61
+ it('should set INITIALIZING status when no access_token is provided', () => {
62
+ const result = AuthScheme.OAuth2({});
63
+
64
+ expect(result).toEqual({
65
+ authScheme: AuthSchemeTypes.OAUTH2,
66
+ val: {
67
+ status: ConnectionStatuses.INITIALIZING,
68
+ },
69
+ });
70
+
71
+ expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
72
+ });
73
+
74
+ it('should set INITIALIZING status when access_token is empty string', () => {
75
+ const result = AuthScheme.OAuth2({ access_token: '' });
76
+
77
+ expect(result).toEqual({
78
+ authScheme: AuthSchemeTypes.OAUTH2,
79
+ val: {
80
+ status: ConnectionStatuses.INITIALIZING,
81
+ access_token: '',
82
+ },
83
+ });
84
+
85
+ expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
86
+ });
87
+
88
+ it('should honor explicit status override from user', () => {
89
+ const result = AuthScheme.OAuth2({
90
+ access_token: 'test_token',
91
+ status: ConnectionStatuses.INITIALIZING,
92
+ });
93
+
94
+ expect(result.val.status).toBe(ConnectionStatuses.INITIALIZING);
95
+ });
96
});
97
98
describe('OAuth1', () => {
63
- it('should create OAuth1 connection data with required fields', () => {
99
+ it('should set INITIALIZING status when only oauth_token is provided (no secret)', () => {
100
const params = {
101
oauth_token: 'test_token',
102
};
@@ -79,7 +115,7 @@ describe('AuthScheme', () => {
115
expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
116
});
117
82
- it('should create OAuth1 connection data with all optional fields', () => {
118
+ it('should set INITIALIZING status with optional fields but no oauth_token_secret', () => {
119
const params = {
120
oauth_token: 'test_token',
121
consumer_key: 'consumer_key',
@@ -100,6 +136,35 @@ describe('AuthScheme', () => {
136
// Verify Zod schema validation
137
expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
138
});
139
+
140
+ it('should set ACTIVE status when both oauth_token and oauth_token_secret are provided', () => {
141
+ const params = {
142
+ oauth_token: 'test_token',
143
+ oauth_token_secret: 'test_secret',
144
+ };
145
+
146
+ const result = AuthScheme.OAuth1(params);
147
+
148
+ expect(result).toEqual({
149
+ authScheme: AuthSchemeTypes.OAUTH1,
150
+ val: {
151
+ status: ConnectionStatuses.ACTIVE,
152
+ oauth_token: 'test_token',
153
+ oauth_token_secret: 'test_secret',
154
+ },
155
+ });
156
+
157
+ expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
158
+ });
159
+
160
+ it('should set INITIALIZING status when oauth_token is empty', () => {
161
+ const result = AuthScheme.OAuth1({
162
+ oauth_token: '',
163
+ oauth_token_secret: 'test_secret',
164
+ });
165
+
166
+ expect(result.val.status).toBe(ConnectionStatuses.INITIALIZING);
167
+ });
168
});
169
170
describe('APIKey', () => {
ts/packages/core/test/connectedAccounts/connectedAccounts.test.ts
+34
-5
@@ -355,13 +355,12 @@ describe('ConnectedAccounts', () => {
355
expect(typeof connectionRequest.waitForConnection).toBe('function');
356
});
357
358
- it('should create a new connected account with config and return a ConnectionRequest', async () => {
358
+ it('should create a connected account with OAuth2 token import and return ACTIVE ConnectionRequest', async () => {
359
const userId = 'user_123';
360
const authConfigId = 'auth_config_123';
361
const options = {
362
callbackUrl: 'https://example.com/callback',
363
config: AuthScheme.OAuth2({
364
- status: ConnectionStatuses.ACTIVE,
364
access_token: 'test_token',
365
token_type: 'Bearer',
366
}),
@@ -374,12 +373,12 @@ describe('ConnectedAccounts', () => {
373
total_pages: 1,
374
});
375
376
+ // When tokens are imported, the API should return ACTIVE with no redirectUrl
377
const mockResponse = {
378
id: 'conn_123',
379
connectionData: {
380
val: {
381
- status: ConnectionStatuses.INITIALIZING,
382
- redirectUrl: 'https://auth.example.com/connect',
381
+ status: ConnectionStatuses.ACTIVE,
382
},
383
},
384
};
@@ -400,9 +399,39 @@ describe('ConnectedAccounts', () => {
399
});
400
401
expect(connectionRequest).toHaveProperty('id', 'conn_123');
403
- expect(connectionRequest).toHaveProperty('waitForConnection');
402
+ expect(connectionRequest).toHaveProperty('status', ConnectionStatuses.ACTIVE);
403
+ expect(connectionRequest).toHaveProperty('redirectUrl', null);
404
expect(typeof connectionRequest.waitForConnection).toBe('function');
405
});
406
+
407
+ it('should return INITIATED status with redirectUrl when no tokens are provided', async () => {
408
+ const userId = 'user_123';
409
+ const authConfigId = 'auth_config_123';
410
+
411
+ extendedMockClient.connectedAccounts.list.mockResolvedValueOnce({
412
+ items: [],
413
+ next_cursor: null,
414
+ total_pages: 1,
415
+ });
416
+
417
+ const mockResponse = {
418
+ id: 'conn_456',
419
+ connectionData: {
420
+ val: {
421
+ status: ConnectionStatuses.INITIATED,
422
+ redirectUrl: 'https://auth.example.com/connect',
423
+ },
424
+ },
425
+ };
426
+
427
+ extendedMockClient.connectedAccounts.create.mockResolvedValueOnce(mockResponse);
428
+
429
+ const connectionRequest = await connectedAccounts.initiate(userId, authConfigId);
430
+
431
+ expect(connectionRequest).toHaveProperty('id', 'conn_456');
432
+ expect(connectionRequest).toHaveProperty('status', ConnectionStatuses.INITIATED);
433
+ expect(connectionRequest).toHaveProperty('redirectUrl', 'https://auth.example.com/connect');
434
+ });
435
});
436
437
describe('get', () => {