Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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(
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH'), err]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);
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(
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH')]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);
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);
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');
} 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`);
}
} 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');
} 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');
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');
return;
}

Expand Down