Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 206 lines 5.62 KB
Raw
1 import { AuthSchemeTypes } from '../types/authConfigs.types';
2 import {
3 BaseConnectionFields,
4 ConnectionData,
5 ConnectionStatuses,
6 } from '../types/connectedAccountAuthStates.types';
7
8 export class AuthScheme {
9 /**
10 * Creates a ConnectionData object for OAuth2 authentication.
11 * When `access_token` is provided, status defaults to ACTIVE (token import).
12 * When omitted, status defaults to INITIALIZING (redirect-based OAuth flow).
13 * Pass an explicit `status` in params to override the auto-detected default.
14 * @param params The OAuth2 parameters
15 * @returns ConnectionData object
16 */
17 static OAuth2(
18 params: BaseConnectionFields & {
19 access_token?: string;
20 token_type?: string;
21 id_token?: string;
22 refresh_token?: string;
23 expires_in?: number;
24 scope?: string;
25 webhook_signature?: string;
26 authed_user?: {
27 access_token?: string;
28 scope?: string;
29 };
30 code_verifier?: string;
31 redirectUrl?: string;
32 callback_url?: string;
33 finalRedirectUri?: string;
34 error?: string;
35 error_description?: string;
36 expired_at?: string;
37 }
38 ): ConnectionData {
39 const hasToken = !!params.access_token;
40 return {
41 authScheme: AuthSchemeTypes.OAUTH2,
42 val: {
43 status: hasToken ? ConnectionStatuses.ACTIVE : ConnectionStatuses.INITIALIZING,
44 ...params,
45 },
46 } as ConnectionData;
47 }
48
49 /**
50 * Creates a ConnectionData object for OAuth1 authentication.
51 * When both `oauth_token` and `oauth_token_secret` are provided, status defaults to ACTIVE (token import).
52 * When either is omitted, status defaults to INITIALIZING (redirect-based OAuth flow).
53 * Pass an explicit `status` in params to override the auto-detected default.
54 * @param params The OAuth1 parameters
55 * @returns ConnectionData object
56 */
57 static OAuth1(
58 params: BaseConnectionFields & {
59 oauth_token?: string;
60 oauth_token_secret?: string;
61 consumer_key?: string;
62 redirectUrl?: string;
63 callback_url?: string;
64 error?: string;
65 error_description?: string;
66 expired_at?: string;
67 }
68 ): ConnectionData {
69 const hasTokens = !!params.oauth_token && !!params.oauth_token_secret;
70 return {
71 authScheme: AuthSchemeTypes.OAUTH1,
72 val: {
73 status: hasTokens ? ConnectionStatuses.ACTIVE : ConnectionStatuses.INITIALIZING,
74 ...params,
75 },
76 } as ConnectionData;
77 }
78
79 /**
80 * Creates a ConnectionData object for API Key authentication
81 * @param params The API key parameters
82 * @returns ConnectionData object
83 */
84 static APIKey(
85 params: BaseConnectionFields & { api_key?: string; generic_api_key?: string }
86 ): ConnectionData {
87 return {
88 authScheme: AuthSchemeTypes.API_KEY,
89 val: {
90 status: ConnectionStatuses.ACTIVE,
91 ...params,
92 },
93 };
94 }
95
96 /**
97 * Creates a ConnectionData object for Basic authentication
98 * @param params The basic auth parameters
99 * @returns ConnectionData object
100 */
101 static Basic(
102 params: BaseConnectionFields & { username: string; password: string }
103 ): ConnectionData {
104 return {
105 authScheme: AuthSchemeTypes.BASIC,
106 val: {
107 status: ConnectionStatuses.ACTIVE,
108 ...params,
109 },
110 };
111 }
112
113 /**
114 * Creates a ConnectionData object for Bearer Token authentication
115 * @param params The bearer token parameters
116 * @returns ConnectionData object
117 */
118 static BearerToken(params: BaseConnectionFields & { token: string }): ConnectionData {
119 return {
120 authScheme: AuthSchemeTypes.BEARER_TOKEN,
121 val: {
122 status: ConnectionStatuses.ACTIVE,
123 ...params,
124 },
125 };
126 }
127
128 /**
129 * Creates a ConnectionData object for Google Service Account authentication
130 * @param params The Google service account parameters
131 * @returns ConnectionData object
132 */
133 static GoogleServiceAccount(
134 params: BaseConnectionFields & { credentials_json: string }
135 ): ConnectionData {
136 return {
137 authScheme: AuthSchemeTypes.GOOGLE_SERVICE_ACCOUNT,
138 val: {
139 status: ConnectionStatuses.ACTIVE,
140 ...params,
141 },
142 };
143 }
144
145 /**
146 * Creates a ConnectionData object for No Auth authentication
147 * @returns ConnectionData object
148 */
149 static NoAuth(params?: BaseConnectionFields): ConnectionData {
150 return {
151 authScheme: AuthSchemeTypes.NO_AUTH,
152 val: {
153 status: ConnectionStatuses.ACTIVE,
154 ...(params ?? {}),
155 },
156 };
157 }
158
159 /**
160 * Creates a ConnectionData object for Basic with JWT authentication
161 * @param params The basic with JWT parameters
162 * @returns ConnectionData object
163 */
164 static BasicWithJWT(
165 params: BaseConnectionFields & { username: string; password: string }
166 ): ConnectionData {
167 return {
168 authScheme: AuthSchemeTypes.BASIC_WITH_JWT,
169 val: {
170 status: ConnectionStatuses.ACTIVE,
171 ...params,
172 },
173 };
174 }
175
176 /**
177 * Creates a ConnectionData object for Cal.com authentication
178 * @returns ConnectionData object
179 */
180 static CalcomAuth(params?: BaseConnectionFields): ConnectionData {
181 return {
182 authScheme: AuthSchemeTypes.CALCOM_AUTH,
183 val: {
184 status: ConnectionStatuses.ACTIVE,
185 ...(params ?? {}),
186 },
187 };
188 }
189
190 /**
191 * Creates a ConnectionData object for Bill.com authentication
192 * @param params The Bill.com auth parameters
193 * @returns ConnectionData object
194 */
195 static BillcomAuth(
196 params: BaseConnectionFields & { sessionId: string; devKey: string }
197 ): ConnectionData {
198 return {
199 authScheme: AuthSchemeTypes.BILLCOM_AUTH,
200 val: {
201 status: ConnectionStatuses.ACTIVE,
202 ...params,
203 },
204 };
205 }
206 }