Skip to content

Commit

Permalink
Remove the loginError property from Connection, move it into `Log…
Browse files Browse the repository at this point in the history
…in7Handler` instead.
  • Loading branch information
arthurschreiber committed Sep 11, 2024
1 parent e1b37fb commit 9c85630
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
32 changes: 13 additions & 19 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,10 +937,6 @@ class Connection extends EventEmitter {
* @private
*/
declare closed: boolean;
/**
* @private
*/
declare loginError: undefined | AggregateError | ConnectionError;
/**
* @private
*/
Expand Down Expand Up @@ -2002,7 +1998,6 @@ class Connection extends EventEmitter {
}

this.closed = true;
this.loginError = undefined;
}
}

Expand Down Expand Up @@ -3305,7 +3300,6 @@ class Connection extends EventEmitter {
this.curTransientRetryCount++;

this.clearConnectTimer();
this.loginError = undefined;

this.socket!.removeListener('error', this._onSocketError);
this.socket!.removeListener('close', this._onSocketClose);
Expand Down Expand Up @@ -3346,12 +3340,12 @@ class Connection extends EventEmitter {
} else {
this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
}
} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);
this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down Expand Up @@ -3399,12 +3393,12 @@ class Connection extends EventEmitter {
});

this.ntlmpacket = undefined;
} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
return this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);

Check warning on line 3401 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3401

Added line #L3401 was not covered by tests
return this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down Expand Up @@ -3487,30 +3481,30 @@ class Connection extends EventEmitter {
try {
tokenResponse = await credentials.getToken(tokenScope);
} catch (err) {
this.loginError = new AggregateError(
const aggrErr = new AggregateError(

Check warning on line 3484 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3484

Added line #L3484 was not covered by tests
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH'), err]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);

Check warning on line 3486 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3486

Added line #L3486 was not covered by tests
this.transitionTo(this.STATE.FINAL);
return;
}

// Type guard the token value so that it is never null.
if (tokenResponse === null) {
this.loginError = new AggregateError(
const aggrErr = new AggregateError(

Check warning on line 3493 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3493

Added line #L3493 was not covered by tests
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH')]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);

Check warning on line 3495 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3495

Added line #L3495 was not covered by tests
this.transitionTo(this.STATE.FINAL);
return;
}

this.sendFedAuthTokenMessage(tokenResponse.token);

} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);

Check warning on line 3507 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3507

Added line #L3507 was not covered by tests
this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/token/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,13 @@ export class Login7TokenHandler extends TokenHandler {

declare loginAckReceived: boolean;

declare loginError: ConnectionError | undefined;

constructor(connection: Connection) {
super();
this.loginAckReceived = false;
this.connection = connection;
this.loginError = undefined;
}

onInfoMessage(token: InfoMessageToken) {
Expand All @@ -270,7 +273,7 @@ export class Login7TokenHandler extends TokenHandler {
error.isTransient = true;
}

this.connection.loginError = error;
this.loginError = error;
}

onSSPI(token: SSPIToken) {
Expand Down Expand Up @@ -305,27 +308,27 @@ export class Login7TokenHandler extends TokenHandler {

if (authentication.type === 'azure-active-directory-password' || authentication.type === 'azure-active-directory-access-token' || authentication.type === 'azure-active-directory-msi-vm' || authentication.type === 'azure-active-directory-msi-app-service' || authentication.type === 'azure-active-directory-service-principal-secret' || authentication.type === 'azure-active-directory-default') {
if (token.fedAuth === undefined) {
this.connection.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement');
this.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement');

Check warning on line 311 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L311

Added line #L311 was not covered by tests
} else if (token.fedAuth.length !== 0) {
this.connection.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`);
this.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`);

Check warning on line 313 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L313

Added line #L313 was not covered by tests
}
} else if (token.fedAuth === undefined && token.utf8Support === undefined) {
this.connection.loginError = new ConnectionError('Received acknowledgement for unknown feature');
this.loginError = new ConnectionError('Received acknowledgement for unknown feature');

Check warning on line 316 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L316

Added line #L316 was not covered by tests
} else if (token.fedAuth) {
this.connection.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment');
this.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment');
}
}

onLoginAck(token: LoginAckToken) {
if (!token.tdsVersion) {
// unsupported TDS version
this.connection.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS');
this.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS');

Check warning on line 325 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L325

Added line #L325 was not covered by tests
return;
}

if (!token.interface) {
// unsupported interface
this.connection.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP');
this.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP');

Check warning on line 331 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L331

Added line #L331 was not covered by tests
return;
}

Expand Down

0 comments on commit 9c85630

Please sign in to comment.