Skip to content

Commit b8d070f

Browse files
committed
address review comments
1 parent 1ec8d39 commit b8d070f

File tree

10 files changed

+306
-232
lines changed

10 files changed

+306
-232
lines changed

packages/javascript/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"dependencies": {
5656
"@asgardeo/i18n": "workspace:*",
5757
"tslib": "2.8.1",
58-
"base64url": "^3.0.1",
5958
"jose": "^5.2.0"
6059
},
6160
"publishConfig": {

packages/javascript/src/AsgardeoJavaScriptClient.ts

Lines changed: 103 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -16,237 +16,233 @@
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-
}
45+
import StorageManager from './StorageManager';
4346

44-
export interface AuthCodeResponse {
45-
code: string;
46-
state: string;
47-
session_state: string;
48-
}
49-
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;
63-
void: void;
64-
57+
6558
constructor(config?: AuthClientConfig<T>, cacheStore?: Storage, cryptoUtils?: Crypto) {
6659
this.cacheStore = cacheStore ?? new DefaultCacheStore();
67-
this.cryptoUtils = cryptoUtils ?? new DefaultCrypto();
60+
this.cryptoUtils = cryptoUtils ?? new DefaultCrypto();
6861
this.auth = new AsgardeoAuthClient();
69-
this.auth.initialize(config, this.cacheStore, this.cryptoUtils);
70-
this.storageManager = this.auth.getStorageManager();
7162

72-
this.baseURL = config.baseUrl ?? "";
63+
if (config) {
64+
this.auth.initialize(config, this.cacheStore, this.cryptoUtils);
65+
this.storageManager = this.auth.getStorageManager();
66+
}
67+
68+
this.baseURL = config?.baseUrl ?? '';
7369
}
7470

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

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

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

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

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

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

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

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

107104
isLoading(): boolean {
108-
throw new Error("Method not implemented.");
105+
throw new Error('Method not implemented.');
109106
}
110107

111108
isSignedIn(): Promise<boolean> {
112-
throw new Error("Method not implemented.");
109+
throw new Error('Method not implemented.');
113110
}
114111

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

119116
getConfiguration(): T {
120-
throw new Error("Method not implemented.");
117+
throw new Error('Method not implemented.');
121118
}
122119

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

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

131-
getAccessToken(sessionId?: string): Promise<string> {
132-
throw new Error("Method not implemented.");
128+
getAccessToken(_sessionId?: string): Promise<string> {
129+
throw new Error('Method not implemented.');
133130
}
134131

135-
clearSession(sessionId?: string): void {
136-
throw new Error("Method not implemented.");
132+
clearSession(_sessionId?: string): void {
133+
throw new Error('Method not implemented.');
137134
}
138135

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

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

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

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

155156
signUp(options?: SignUpOptions): Promise<void>;
156157

157158
signUp(payload: EmbeddedFlowExecuteRequestPayload): Promise<EmbeddedFlowExecuteResponse>;
158159

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

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

169172
const authorizeURL: URL = new URL(await this.auth.getSignInUrl(customParam));
170173

171174
const authorizeResponse: EmbeddedSignInFlowInitiateResponse = await initializeEmbeddedSignInFlow({
172-
url: `${authorizeURL.origin}${authorizeURL.pathname}`,
173175
payload: Object.fromEntries(authorizeURL.searchParams.entries()),
176+
url: `${authorizeURL.origin}${authorizeURL.pathname}`,
174177
});
175178

176-
const usernamePasswordAuthenticator = authorizeResponse.nextStep.authenticators.find(
177-
(auth) => auth.authenticator === "Username & Password",
178-
);
179+
const authenticatorName: string = agentConfig.authenticatorName ?? AgentConfig.DEFAULT_AUTHENTICATOR_NAME;
179180

180-
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."));
181+
const targetAuthenticator: EmbeddedSignInFlowAuthenticator | undefined =
182+
authorizeResponse.nextStep.authenticators.find(
183+
(auth: EmbeddedSignInFlowAuthenticator) => auth.authenticator === authenticatorName,
184+
);
185+
186+
if (!targetAuthenticator) {
187+
throw new Error(`Authenticator '${authenticatorName}' not found among authentication steps.`);
183188
}
184189

185190
const authnRequest: EmbeddedFlowExecuteRequestConfig = {
186191
baseUrl: this.baseURL,
187192
payload: {
188193
flowId: authorizeResponse.flowId,
189194
selectedAuthenticator: {
190-
authenticatorId: usernamePasswordAuthenticator.authenticatorId,
195+
authenticatorId: targetAuthenticator.authenticatorId,
191196
params: {
192-
username: agentConfig.agentID,
193197
password: agentConfig.agentSecret,
198+
username: agentConfig.agentID,
194199
},
195200
},
196201
},
197202
};
198203

199204
const authnResponse: EmbeddedSignInFlowHandleResponse = await executeEmbeddedSignInFlow(authnRequest);
200205

201-
if (authnResponse.flowStatus != EmbeddedSignInFlowStatus.SuccessCompleted) {
202-
console.error("Agent Authentication Failed.");
203-
return Promise.reject(new Error("Agent Authentication Failed."));
206+
if (authnResponse.flowStatus !== EmbeddedSignInFlowStatus.SuccessCompleted) {
207+
throw new Error('Agent authentication failed.');
204208
}
205209

206-
const tokenResponse = await this.auth.requestAccessToken(
210+
return this.auth.requestAccessToken(
207211
authnResponse.authData['code'],
208212
authnResponse.authData['session_state'],
209213
authnResponse.authData['state'],
210214
);
211-
212-
return tokenResponse;
213215
}
214216

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

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

225224
if (authURL) {
226-
return Promise.resolve(authURL.toString());
225+
return authURL.toString();
227226
}
228-
return Promise.reject(new Error("Could not build Authorize URL"));
227+
228+
throw new Error('Could not build Authorize URL');
229229
}
230230

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

236-
// Pass Agent Token when requesting access token
237-
const tokenRequestConfig = {
234+
const tokenRequestConfig: {params: {actor_token: string}} = {
238235
params: {
239236
actor_token: agentToken.accessToken,
240237
},
241238
};
242239

243-
// Return OBO Token
244-
return await this.auth.requestAccessToken(
240+
return this.auth.requestAccessToken(
245241
authCodeResponse.code,
246242
authCodeResponse.session_state,
247243
authCodeResponse.state,
248244
undefined,
249-
tokenRequestConfig
245+
tokenRequestConfig,
250246
);
251247
}
252248
}

0 commit comments

Comments
 (0)