@@ -159,94 +159,94 @@ class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T> {
159159 }
160160
161161 // Get Agent Token. (AI agent acting on its own)
162- public async getAgentToken ( agentConfig : AgentConfig ) : Promise < TokenResponse > {
163- const customParam = {
164- response_mode : "direct" ,
165- } ;
166-
167- const authorizeURL : URL = new URL ( await this . auth . getSignInUrl ( customParam ) ) ;
168-
169- const authorizeResponse : EmbeddedSignInFlowInitiateResponse = await initializeEmbeddedSignInFlow ( {
170- url : `${ authorizeURL . origin } ${ authorizeURL . pathname } ` ,
171- payload : Object . fromEntries ( authorizeURL . searchParams . entries ( ) ) ,
172- } ) ;
173-
174- const usernamePasswordAuthenticator = authorizeResponse . nextStep . authenticators . find (
175- ( auth ) => auth . authenticator === "Username & Password" ,
176- ) ;
177-
178- if ( ! usernamePasswordAuthenticator ) {
179- console . error ( "Basic authenticator not found among authentication steps." ) ;
180- return Promise . reject ( new Error ( "Basic authenticator not found among authentication steps." ) ) ;
181- }
182-
183- const authnRequest : EmbeddedFlowExecuteRequestConfig = {
184- baseUrl : this . baseURL ,
185- payload : {
186- flowId : authorizeResponse . flowId ,
187- selectedAuthenticator : {
188- authenticatorId : usernamePasswordAuthenticator . authenticatorId ,
189- params : {
190- username : agentConfig . agentID ,
191- password : agentConfig . agentSecret ,
192- } ,
193- } ,
194- } ,
195- } ;
162+ public async getAgentToken ( agentConfig : AgentConfig ) : Promise < TokenResponse > {
163+ const customParam = {
164+ response_mode : "direct" ,
165+ } ;
196166
197- const authnResponse : EmbeddedSignInFlowHandleResponse = await executeEmbeddedSignInFlow ( authnRequest ) ;
167+ const authorizeURL : URL = new URL ( await this . auth . getSignInUrl ( customParam ) ) ;
198168
199- if ( authnResponse . flowStatus != EmbeddedSignInFlowStatus . SuccessCompleted ) {
200- console . error ( "Agent Authentication Failed." ) ;
201- return Promise . reject ( new Error ( "Agent Authentication Failed." ) ) ;
202- }
169+ const authorizeResponse : EmbeddedSignInFlowInitiateResponse = await initializeEmbeddedSignInFlow ( {
170+ url : ` ${ authorizeURL . origin } ${ authorizeURL . pathname } ` ,
171+ payload : Object . fromEntries ( authorizeURL . searchParams . entries ( ) ) ,
172+ } ) ;
203173
204- const tokenResponse = await this . auth . requestAccessToken (
205- authnResponse . authData [ 'code' ] ,
206- authnResponse . authData [ 'session_state' ] ,
207- authnResponse . authData [ 'state' ] ,
208- ) ;
174+ const usernamePasswordAuthenticator = authorizeResponse . nextStep . authenticators . find (
175+ ( auth ) => auth . authenticator === "Username & Password" ,
176+ ) ;
209177
210- return tokenResponse ;
178+ if ( ! usernamePasswordAuthenticator ) {
179+ console . error ( "Basic authenticator not found among authentication steps." ) ;
180+ return Promise . reject ( new Error ( "Basic authenticator not found among authentication steps." ) ) ;
211181 }
212182
213- // Build Authorize request for the OBO Flow
214- public async getOBOSignInURL ( agentConfig : AgentConfig ) : Promise < string > {
215- // The authorize request must include requested_actor parameter from the agent configs
216- const customParam = {
217- requested_actor : agentConfig . agentID ,
218- } ;
183+ const authnRequest : EmbeddedFlowExecuteRequestConfig = {
184+ baseUrl : this . baseURL ,
185+ payload : {
186+ flowId : authorizeResponse . flowId ,
187+ selectedAuthenticator : {
188+ authenticatorId : usernamePasswordAuthenticator . authenticatorId ,
189+ params : {
190+ username : agentConfig . agentID ,
191+ password : agentConfig . agentSecret ,
192+ } ,
193+ } ,
194+ } ,
195+ } ;
219196
220- // Build authorize URL using AsgardeoAuthClient
221- const authURL : string | undefined = await this . auth . getSignInUrl ( customParam ) ;
197+ const authnResponse : EmbeddedSignInFlowHandleResponse = await executeEmbeddedSignInFlow ( authnRequest ) ;
222198
223- if ( authURL ) {
224- return Promise . resolve ( authURL . toString ( ) ) ;
225- }
226- return Promise . reject ( new Error ( "Could not build Authorize URL" ) ) ;
199+ if ( authnResponse . flowStatus != EmbeddedSignInFlowStatus . SuccessCompleted ) {
200+ console . error ( "Agent Authentication Failed." ) ;
201+ return Promise . reject ( new Error ( "Agent Authentication Failed." ) ) ;
227202 }
228203
229- // Get OBO Token. (AI agent acting on behalf of a user)
230- public async getOBOToken ( agentConfig : AgentConfig , authCodeResponse : AuthCodeResponse ) : Promise < TokenResponse > {
231- // Get Agent Token
232- const agentToken = await this . getAgentToken ( agentConfig ) ;
204+ const tokenResponse = await this . auth . requestAccessToken (
205+ authnResponse . authData [ 'code' ] ,
206+ authnResponse . authData [ 'session_state' ] ,
207+ authnResponse . authData [ 'state' ] ,
208+ ) ;
233209
234- // Pass Agent Token when requesting access token
235- const tokenRequestConfig = {
236- params : {
237- actor_token : agentToken . accessToken ,
238- } ,
239- } ;
240-
241- // Return OBO Token
242- return await this . auth . requestAccessToken (
243- authCodeResponse . code ,
244- authCodeResponse . session_state ,
245- authCodeResponse . state ,
246- undefined ,
247- tokenRequestConfig
248- ) ;
210+ return tokenResponse ;
211+ }
212+
213+ // Build Authorize request for the OBO Flow
214+ public async getOBOSignInURL ( agentConfig : AgentConfig ) : Promise < string > {
215+ // The authorize request must include requested_actor parameter from the agent configs
216+ const customParam = {
217+ requested_actor : agentConfig . agentID ,
218+ } ;
219+
220+ // Build authorize URL using AsgardeoAuthClient
221+ const authURL : string | undefined = await this . auth . getSignInUrl ( customParam ) ;
222+
223+ if ( authURL ) {
224+ return Promise . resolve ( authURL . toString ( ) ) ;
249225 }
226+ return Promise . reject ( new Error ( "Could not build Authorize URL" ) ) ;
227+ }
228+
229+ // Get OBO Token. (AI agent acting on behalf of a user)
230+ public async getOBOToken ( agentConfig : AgentConfig , authCodeResponse : AuthCodeResponse ) : Promise < TokenResponse > {
231+ // Get Agent Token
232+ const agentToken = await this . getAgentToken ( agentConfig ) ;
233+
234+ // Pass Agent Token when requesting access token
235+ const tokenRequestConfig = {
236+ params : {
237+ actor_token : agentToken . accessToken ,
238+ } ,
239+ } ;
240+
241+ // Return OBO Token
242+ return await this . auth . requestAccessToken (
243+ authCodeResponse . code ,
244+ authCodeResponse . session_state ,
245+ authCodeResponse . state ,
246+ undefined ,
247+ tokenRequestConfig
248+ ) ;
249+ }
250250}
251251
252252export default AsgardeoJavaScriptClient ;
0 commit comments