Skip to content

Commit 4807fb9

Browse files
committed
address review comments
1 parent 1ec8d39 commit 4807fb9

File tree

9 files changed

+293
-224
lines changed

9 files changed

+293
-224
lines changed

packages/javascript/src/AsgardeoJavaScriptClient.ts

Lines changed: 103 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -16,170 +16,177 @@
1616
* under the License.
1717
*/
1818

19-
import {EmbeddedSignInFlowInitiateResponse} from "./models/embedded-signin-flow";
19+
import {AsgardeoAuthClient} from './__legacy__/client';
2020
import {AuthClientConfig} from './__legacy__/models/client-config';
21-
import {EmbeddedFlowExecuteRequestConfig} from "./models/embedded-flow";
22-
import {EmbeddedSignInFlowHandleResponse} from "./models/embedded-signin-flow";
23-
import {EmbeddedSignInFlowStatus} from "./models/embedded-signin-flow";
24-
import {Crypto} from "./models/crypto";
25-
import {AsgardeoAuthClient} from "./__legacy__/client";
26-
import StorageManager from "./StorageManager";
27-
import executeEmbeddedSignInFlow from "./api/executeEmbeddedSignInFlow";
21+
import executeEmbeddedSignInFlow from './api/executeEmbeddedSignInFlow';
22+
import initializeEmbeddedSignInFlow from './api/initializeEmbeddedSignInFlow';
23+
import {DefaultCacheStore} from './DefaultCacheStore';
24+
import {DefaultCrypto} from './DefaultCrypto';
25+
import {AgentConfig} from './models/agent';
26+
import {AuthCodeResponse} from './models/auth-code-response';
2827
import {AsgardeoClient} from './models/client';
2928
import {Config, SignInOptions, SignOutOptions, SignUpOptions} from './models/config';
30-
import {EmbeddedFlowExecuteRequestPayload, EmbeddedFlowExecuteResponse} from './models/embedded-flow';
29+
import {Crypto} from './models/crypto';
30+
import {
31+
EmbeddedFlowExecuteRequestConfig,
32+
EmbeddedFlowExecuteRequestPayload,
33+
EmbeddedFlowExecuteResponse,
34+
} from './models/embedded-flow';
35+
import {
36+
EmbeddedSignInFlowAuthenticator,
37+
EmbeddedSignInFlowHandleResponse,
38+
EmbeddedSignInFlowInitiateResponse,
39+
EmbeddedSignInFlowStatus,
40+
} from './models/embedded-signin-flow';
3141
import {AllOrganizationsApiResponse, Organization} from './models/organization';
3242
import {Storage} from './models/store';
3343
import {TokenExchangeRequestConfig, TokenResponse} from './models/token';
3444
import {User, UserProfile} from './models/user';
35-
import initializeEmbeddedSignInFlow from './api/initializeEmbeddedSignInFlow';
36-
import {DefaultCacheStore} from './DefaultCacheStore';
37-
import {DefaultCrypto} from './DefaultCrypto';
38-
39-
interface AgentConfig {
40-
agentID: string;
41-
agentSecret: string;
42-
}
43-
44-
export interface AuthCodeResponse {
45-
code: string;
46-
state: string;
47-
session_state: string;
48-
}
45+
import StorageManager from './StorageManager';
4946

50-
/**
51-
* Base class for implementing Asgardeo clients.
52-
* This class provides the core functionality for managing user authentication and sessions.
53-
*
54-
* @typeParam T - Configuration type that extends Config.
55-
*/
5647
class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T> {
57-
5848
private cacheStore: Storage;
49+
5950
private cryptoUtils: Crypto;
51+
6052
private auth: AsgardeoAuthClient<T>;
53+
6154
private storageManager: StorageManager<T>;
55+
6256
private baseURL: string;
57+
6358
void: void;
64-
59+
6560
constructor(config?: AuthClientConfig<T>, cacheStore?: Storage, cryptoUtils?: Crypto) {
6661
this.cacheStore = cacheStore ?? new DefaultCacheStore();
67-
this.cryptoUtils = cryptoUtils ?? new DefaultCrypto();
62+
this.cryptoUtils = cryptoUtils ?? new DefaultCrypto();
6863
this.auth = new AsgardeoAuthClient();
69-
this.auth.initialize(config, this.cacheStore, this.cryptoUtils);
70-
this.storageManager = this.auth.getStorageManager();
7164

72-
this.baseURL = config.baseUrl ?? "";
65+
if (config) {
66+
this.auth.initialize(config, this.cacheStore, this.cryptoUtils);
67+
this.storageManager = this.auth.getStorageManager();
68+
}
69+
70+
this.baseURL = config?.baseUrl ?? '';
7371
}
7472

75-
switchOrganization(organization: Organization, sessionId?: string): Promise<TokenResponse | Response> {
76-
throw new Error("Method not implemented.");
73+
/* eslint-disable class-methods-use-this, @typescript-eslint/no-unused-vars */
74+
switchOrganization(_organization: Organization, _sessionId?: string): Promise<TokenResponse | Response> {
75+
throw new Error('Method not implemented.');
7776
}
7877

79-
initialize(config: T, storage?: Storage): Promise<boolean> {
80-
throw new Error("Method not implemented.");
78+
initialize(_config: T, _storage?: Storage): Promise<boolean> {
79+
throw new Error('Method not implemented.');
8180
}
8281

83-
reInitialize(config: Partial<T>): Promise<boolean> {
84-
throw new Error("Method not implemented.");
82+
reInitialize(_config: Partial<T>): Promise<boolean> {
83+
throw new Error('Method not implemented.');
8584
}
8685

87-
getUser(options?: any): Promise<User> {
88-
throw new Error("Method not implemented.");
86+
getUser(_options?: any): Promise<User> {
87+
throw new Error('Method not implemented.');
8988
}
9089

91-
getAllOrganizations(options?: any, sessionId?: string): Promise<AllOrganizationsApiResponse> {
92-
throw new Error("Method not implemented.");
90+
getAllOrganizations(_options?: any, _sessionId?: string): Promise<AllOrganizationsApiResponse> {
91+
throw new Error('Method not implemented.');
9392
}
9493

95-
getMyOrganizations(options?: any, sessionId?: string): Promise<Organization[]> {
96-
throw new Error("Method not implemented.");
94+
getMyOrganizations(_options?: any, _sessionId?: string): Promise<Organization[]> {
95+
throw new Error('Method not implemented.');
9796
}
9897

99-
getCurrentOrganization(sessionId?: string): Promise<Organization | null> {
100-
throw new Error("Method not implemented.");
98+
getCurrentOrganization(_sessionId?: string): Promise<Organization | null> {
99+
throw new Error('Method not implemented.');
101100
}
102101

103-
getUserProfile(options?: any): Promise<UserProfile> {
104-
throw new Error("Method not implemented.");
102+
getUserProfile(_options?: any): Promise<UserProfile> {
103+
throw new Error('Method not implemented.');
105104
}
106105

107106
isLoading(): boolean {
108-
throw new Error("Method not implemented.");
107+
throw new Error('Method not implemented.');
109108
}
110109

111110
isSignedIn(): Promise<boolean> {
112-
throw new Error("Method not implemented.");
111+
throw new Error('Method not implemented.');
113112
}
114113

115-
updateUserProfile(payload: any, userId?: string): Promise<User> {
116-
throw new Error("Method not implemented.");
114+
updateUserProfile(_payload: any, _userId?: string): Promise<User> {
115+
throw new Error('Method not implemented.');
117116
}
118117

119118
getConfiguration(): T {
120-
throw new Error("Method not implemented.");
119+
throw new Error('Method not implemented.');
121120
}
122121

123-
exchangeToken(config: TokenExchangeRequestConfig, sessionId?: string): Promise<TokenResponse | Response> {
124-
throw new Error("Method not implemented.");
122+
exchangeToken(_config: TokenExchangeRequestConfig, _sessionId?: string): Promise<TokenResponse | Response> {
123+
throw new Error('Method not implemented.');
125124
}
126125

127-
signInSilently(options?: SignInOptions): Promise<User | boolean> {
128-
throw new Error("Method not implemented.");
126+
signInSilently(_options?: SignInOptions): Promise<User | boolean> {
127+
throw new Error('Method not implemented.');
129128
}
130129

131-
getAccessToken(sessionId?: string): Promise<string> {
132-
throw new Error("Method not implemented.");
130+
getAccessToken(_sessionId?: string): Promise<string> {
131+
throw new Error('Method not implemented.');
133132
}
134133

135-
clearSession(sessionId?: string): void {
136-
throw new Error("Method not implemented.");
134+
clearSession(_sessionId?: string): void {
135+
throw new Error('Method not implemented.');
137136
}
138137

139-
setSession(sessionData: Record<string, unknown>, sessionId?: string): Promise<void> {
140-
throw new Error("Method not implemented.");
138+
setSession(_sessionData: Record<string, unknown>, _sessionId?: string): Promise<void> {
139+
throw new Error('Method not implemented.');
141140
}
142141

143-
decodeJwtToken<R = Record<string, unknown>>(token: string): Promise<R> {
144-
throw new Error("Method not implemented.");
142+
decodeJwtToken<R = Record<string, unknown>>(_token: string): Promise<R> {
143+
throw new Error('Method not implemented.');
145144
}
146145

147-
signIn(options?: SignInOptions): Promise<User> {
148-
throw new Error("Method not implemented.");
146+
signIn(_options?: SignInOptions): Promise<User> {
147+
throw new Error('Method not implemented.');
149148
}
150149

151-
signOut(options?: SignOutOptions, sessionIdOrAfterSignOut?: string | ((afterSignOutUrl: string) => void), afterSignOut?: (afterSignOutUrl: string) => void): Promise<string> {
152-
throw new Error("Method not implemented.");
150+
signOut(
151+
_options?: SignOutOptions,
152+
_sessionIdOrAfterSignOut?: string | ((afterSignOutUrl: string) => void),
153+
_afterSignOut?: (afterSignOutUrl: string) => void,
154+
): Promise<string> {
155+
throw new Error('Method not implemented.');
153156
}
154157

155158
signUp(options?: SignUpOptions): Promise<void>;
156159

157160
signUp(payload: EmbeddedFlowExecuteRequestPayload): Promise<EmbeddedFlowExecuteResponse>;
158161

159-
signUp(optionsOrPayload?: SignUpOptions | EmbeddedFlowExecuteRequestPayload): Promise<void | EmbeddedFlowExecuteResponse> {
160-
throw new Error("Method not implemented.");
162+
signUp(
163+
_optionsOrPayload?: SignUpOptions | EmbeddedFlowExecuteRequestPayload,
164+
): Promise<void | EmbeddedFlowExecuteResponse> {
165+
throw new Error('Method not implemented.');
161166
}
167+
/* eslint-enable class-methods-use-this, @typescript-eslint/no-unused-vars */
162168

163-
// Get Agent Token. (AI agent acting on its own)
164169
public async getAgentToken(agentConfig: AgentConfig): Promise<TokenResponse> {
165-
const customParam = {
166-
response_mode: "direct",
170+
const customParam: Record<string, string> = {
171+
response_mode: 'direct',
167172
};
168173

169174
const authorizeURL: URL = new URL(await this.auth.getSignInUrl(customParam));
170175

171176
const authorizeResponse: EmbeddedSignInFlowInitiateResponse = await initializeEmbeddedSignInFlow({
172-
url: `${authorizeURL.origin}${authorizeURL.pathname}`,
173177
payload: Object.fromEntries(authorizeURL.searchParams.entries()),
178+
url: `${authorizeURL.origin}${authorizeURL.pathname}`,
174179
});
175180

176-
const usernamePasswordAuthenticator = authorizeResponse.nextStep.authenticators.find(
177-
(auth) => auth.authenticator === "Username & Password",
178-
);
181+
const usernamePasswordAuthenticator: EmbeddedSignInFlowAuthenticator | undefined =
182+
authorizeResponse.nextStep.authenticators.find(
183+
(auth: EmbeddedSignInFlowAuthenticator) => auth.authenticator === 'Username & Password',
184+
);
179185

180186
if (!usernamePasswordAuthenticator) {
181-
console.error("Basic authenticator not found among authentication steps.");
182-
return Promise.reject(new Error("Basic authenticator not found among authentication steps."));
187+
// eslint-disable-next-line no-console
188+
console.error('Basic authenticator not found among authentication steps.');
189+
return Promise.reject(new Error('Basic authenticator not found among authentication steps.'));
183190
}
184191

185192
const authnRequest: EmbeddedFlowExecuteRequestConfig = {
@@ -189,21 +196,22 @@ class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T> {
189196
selectedAuthenticator: {
190197
authenticatorId: usernamePasswordAuthenticator.authenticatorId,
191198
params: {
192-
username: agentConfig.agentID,
193199
password: agentConfig.agentSecret,
200+
username: agentConfig.agentID,
194201
},
195202
},
196203
},
197204
};
198205

199206
const authnResponse: EmbeddedSignInFlowHandleResponse = await executeEmbeddedSignInFlow(authnRequest);
200207

201-
if (authnResponse.flowStatus != EmbeddedSignInFlowStatus.SuccessCompleted) {
202-
console.error("Agent Authentication Failed.");
203-
return Promise.reject(new Error("Agent Authentication Failed."));
208+
if (authnResponse.flowStatus !== EmbeddedSignInFlowStatus.SuccessCompleted) {
209+
// eslint-disable-next-line no-console
210+
console.error('Agent Authentication Failed.');
211+
return Promise.reject(new Error('Agent Authentication Failed.'));
204212
}
205213

206-
const tokenResponse = await this.auth.requestAccessToken(
214+
const tokenResponse: TokenResponse = await this.auth.requestAccessToken(
207215
authnResponse.authData['code'],
208216
authnResponse.authData['session_state'],
209217
authnResponse.authData['state'],
@@ -212,41 +220,35 @@ class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T> {
212220
return tokenResponse;
213221
}
214222

215-
// Build Authorize request for the OBO Flow
216223
public async getOBOSignInURL(agentConfig: AgentConfig): Promise<string> {
217-
// The authorize request must include requested_actor parameter from the agent configs
218-
const customParam = {
224+
const customParam: Record<string, string> = {
219225
requested_actor: agentConfig.agentID,
220226
};
221227

222-
// Build authorize URL using AsgardeoAuthClient
223228
const authURL: string | undefined = await this.auth.getSignInUrl(customParam);
224229

225230
if (authURL) {
226231
return Promise.resolve(authURL.toString());
227232
}
228-
return Promise.reject(new Error("Could not build Authorize URL"));
233+
234+
return Promise.reject(new Error('Could not build Authorize URL'));
229235
}
230236

231-
// Get OBO Token. (AI agent acting on behalf of a user)
232237
public async getOBOToken(agentConfig: AgentConfig, authCodeResponse: AuthCodeResponse): Promise<TokenResponse> {
233-
// Get Agent Token
234-
const agentToken = await this.getAgentToken(agentConfig);
238+
const agentToken: TokenResponse = await this.getAgentToken(agentConfig);
235239

236-
// Pass Agent Token when requesting access token
237-
const tokenRequestConfig = {
240+
const tokenRequestConfig: {params: {actor_token: string}} = {
238241
params: {
239242
actor_token: agentToken.accessToken,
240243
},
241244
};
242245

243-
// Return OBO Token
244-
return await this.auth.requestAccessToken(
246+
return this.auth.requestAccessToken(
245247
authCodeResponse.code,
246248
authCodeResponse.session_state,
247249
authCodeResponse.state,
248250
undefined,
249-
tokenRequestConfig
251+
tokenRequestConfig,
250252
);
251253
}
252254
}

0 commit comments

Comments
 (0)