* Handles authentication requests and responses. * @param {Authenticator} authenticator * @param {Buffer} token * @param {Function} callback
(authenticator, token, callback)
| 410 | * @param {Function} callback |
| 411 | */ |
| 412 | authenticate(authenticator, token, callback) { |
| 413 | const self = this; |
| 414 | let request = new requests.AuthResponseRequest(token); |
| 415 | if (this.protocolVersion === 1) { |
| 416 | //No Sasl support, use CREDENTIALS |
| 417 | if (!authenticator.username) { |
| 418 | return self.onAuthenticationError( |
| 419 | callback, new errors.AuthenticationError('Only plain text authenticator providers allowed under protocol v1')); |
| 420 | } |
| 421 | |
| 422 | request = new requests.CredentialsRequest(authenticator.username, authenticator.password); |
| 423 | } |
| 424 | |
| 425 | this.sendStream(request, null, function authResponseCallback(err, result) { |
| 426 | if (err) { |
| 427 | if (err instanceof errors.ResponseError && err.code === types.responseErrorCodes.badCredentials) { |
| 428 | const authError = new errors.AuthenticationError(err.message); |
| 429 | authError.additionalInfo = err; |
| 430 | err = authError; |
| 431 | } |
| 432 | return self.onAuthenticationError(callback, err); |
| 433 | } |
| 434 | |
| 435 | if (result.ready) { |
| 436 | authenticator.onAuthenticationSuccess(); |
| 437 | return callback(); |
| 438 | } |
| 439 | |
| 440 | if (result.authChallenge) { |
| 441 | return authenticator.evaluateChallenge(result.token, function evaluateCallback(err, t) { |
| 442 | if (err) { |
| 443 | return self.onAuthenticationError(callback, err); |
| 444 | } |
| 445 | //here we go again |
| 446 | self.authenticate(authenticator, t, callback); |
| 447 | }); |
| 448 | } |
| 449 | |
| 450 | callback(new errors.DriverInternalError('Unexpected response from Cassandra: ' + util.inspect(result))); |
| 451 | }); |
| 452 | } |
| 453 | |
| 454 | onAuthenticationError(callback, err) { |
| 455 | this._metrics.onAuthenticationError(err); |
no test coverage detected