Skip to content

Commit 959af72

Browse files
committed
store fresh auth context, add onSocketError cb
1 parent 52f54db commit 959af72

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

browser/boilingdata.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/boilingdata/boilingdata.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface IBDCallbacks {
1717
onLambdaEvent?: (data: unknown) => void;
1818
onSocketOpen?: () => void;
1919
onSocketClose?: () => void;
20+
onSocketError?: (err: unknown) => void;
2021
unknown?: (data: unknown) => void;
2122
}
2223

@@ -133,6 +134,7 @@ export class BoilingData {
133134
private socketInstance: ISocketInstance;
134135
private logger: Console;
135136
private closedPromise?: Promise<void>;
137+
private authcontext!: any;
136138

137139
constructor(public props: IBoilingData) {
138140
this.logger = createLogger({ name: "boilingdata", logLevel: this.props.logLevel ?? "info" });
@@ -178,7 +180,7 @@ export class BoilingData {
178180
}
179181

180182
public getCachedAuthContext(): { idToken: any } | undefined {
181-
return this.creds?.idToken;
183+
return this.authcontext;
182184
}
183185

184186
public async connect(): Promise<void> {
@@ -192,11 +194,12 @@ export class BoilingData {
192194
this.region,
193195
this.props.endpointUrl,
194196
this.props.mfa,
195-
this.props.authcontext,
197+
this.authcontext ?? this.props.authcontext, // this.authcontext is more fresh
196198
this.logger,
197199
)
198200
.then(creds => {
199201
this.creds = creds;
202+
this.authcontext = creds.idToken;
200203
sock.socket = new WebSocket(this.creds.signedWebsocketUrl);
201204
sock.socket!.onclose = () => {
202205
if (cbs?.onSocketClose) cbs.onSocketClose();
@@ -206,8 +209,10 @@ export class BoilingData {
206209
if (cbs?.onSocketOpen) cbs.onSocketOpen();
207210
resolve();
208211
};
209-
sock.socket!.onerror = (err: any) => {
212+
sock.socket!.onerror = (err: unknown) => {
210213
this.logger.error(err);
214+
this.authcontext = undefined; // reset auth context
215+
if (cbs?.onSocketError) cbs.onSocketError(err);
211216
reject(err);
212217
};
213218
sock.socket!.onmessage = (msg: MessageEvent) => {

src/common/identity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ export async function getBoilingDataCredentials(
9595
} else if (authContext && authContext.idToken?.jwtToken) {
9696
logger?.debug("Using existing ID token");
9797
idToken = new CognitoIdToken({ IdToken: authContext.idToken?.jwtToken });
98+
} else {
99+
throw new Error("No credentials for creating signed WS URL");
98100
}
99-
if (!idToken) throw new Error("No credentials for creating signed WS URL");
101+
if (!idToken) throw new Error("Failed using existing auth context");
100102
const creds = await refreshCredsWithToken(idToken.getJwtToken());
103+
if (!creds) throw new Error("Failed refreshing credentials");
101104
const { accessKeyId, secretAccessKey, sessionToken } = creds;
102105
if (!accessKeyId || !secretAccessKey) throw new Error("Missing credentials (after refresh)!");
103106
const credentials = { accessKeyId, secretAccessKey, sessionToken };

0 commit comments

Comments
 (0)