Repository navigation
ActiveDirectoryPassword and sqlpassword authentication #661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2d43f36
b2d0a5c
3f1840d
3d3b3d7
4cf47ed
e7f385b
46d2d58
4f6f7a1
936713a
7eac627
7ca7afc
f977a46
c12d559
c6b1914
fce3230
eb804f4
d05e2ff
d668c1e
7d9eab4
f679c92
514666b
fb8b03e
bbaf393
ddc9299
559349a
e058282
b928f0f
9a8e3f2
e2d695c
2c7388a
2187468
287d27a
b4b9310
a74cc22
a26b779
b2d2ac3
303650a
3ed5425
a2766f5
c2700a7
225dedb
cab06d1
4913dc4
30c3590
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| const deprecate = require('depd')('tedious'); | ||
|
|
||
| const BulkLoad = require('./bulk-load'); | ||
| const Debug = require('./debug'); | ||
| const EventEmitter = require('events').EventEmitter; | ||
|
|
@@ -20,6 +19,7 @@ const crypto = require('crypto'); | |
| const ConnectionError = require('./errors').ConnectionError; | ||
| const RequestError = require('./errors').RequestError; | ||
| const Connector = require('./connector').Connector; | ||
| const AuthenticationContext = require('adal-node').AuthenticationContext; | ||
|
|
||
| // A rather basic state machine for managing a connection. | ||
| // Implements something approximating s3.2.1. | ||
|
|
@@ -79,6 +79,19 @@ class Connection extends EventEmitter { | |
| throw new TypeError('Invalid server: ' + config.server); | ||
| } | ||
|
|
||
| this.fedAuthInfo = { | ||
| ValidFedAuthEnum: { | ||
| SqlPassword: 'SQLPASSWORD', | ||
| ActiveDirectoryPassword: 'ACTIVEDIRECTORYPASSWORD', | ||
| // TODO: ActiveDirectoryIntegrated | ||
| }, | ||
| method: undefined, | ||
| fedAuthLibrary: undefined, | ||
| requiredPreLoginResponse: false, | ||
| fedAuthInfoRequested: false, | ||
| responsePending: false, | ||
| token: undefined | ||
| }; | ||
| if (config.domain != undefined) { | ||
| deprecateNonStringConfigValue('domain', config.domain); | ||
| } | ||
|
|
@@ -145,6 +158,7 @@ class Connection extends EventEmitter { | |
| tdsVersion: DEFAULT_TDS_VERSION, | ||
| textsize: DEFAULT_TEXTSIZE, | ||
| trustServerCertificate: true, | ||
| authentication: undefined, | ||
| useColumnNames: false, | ||
| useUTC: true | ||
| } | ||
|
|
@@ -479,6 +493,25 @@ class Connection extends EventEmitter { | |
| deprecateNonBooleanConfigValue('options.useUTC', config.options.useUTC); | ||
| this.config.options.useUTC = config.options.useUTC; | ||
| } | ||
|
|
||
| // Whenever authentication property is used to specify an authentication method, | ||
| // the client will request encryption (the default value of the Encrypt property will be true) | ||
| // and it will validate the server certificate (regardless of the encryption setting), unless TrustServerCertificate = true | ||
| if (config.options.authentication != undefined) { | ||
| // check for valid options | ||
| if (!(config.options.authentication.toUpperCase() === this.fedAuthInfo.ValidFedAuthEnum.SqlPassword || | ||
| config.options.authentication.toUpperCase() === this.fedAuthInfo.ValidFedAuthEnum.ActiveDirectoryPassword)) { | ||
| throw new Error('An invalid authentication method is specified'); | ||
| } | ||
| if (this.config.options.tdsVersion < '7_4') { | ||
| throw new Error(`Azure Active Directory authentication is not supported in the TDS version ${this.config.options.tdsVersion}`); | ||
| } | ||
| this.config.options.encrypt = true; | ||
| this.fedAuthInfo.method = config.options.authentication; | ||
| if (!config.options.trustServerCertificate) { | ||
| this.config.options.trustServerCertificate = false; | ||
| } | ||
| } | ||
| deprecateNullConfigValue('options.useUTC', config.options.useUTC); | ||
| } | ||
|
|
||
|
|
@@ -496,6 +529,7 @@ class Connection extends EventEmitter { | |
| this.transactionDescriptors = [new Buffer([0, 0, 0, 0, 0, 0, 0, 0])]; | ||
| this.transitionTo(this.STATE.CONNECTING); | ||
|
|
||
|
|
||
| if (this.config.options.tdsVersion < '7_2') { | ||
| // 'beginTransaction', 'commitTransaction' and 'rollbackTransaction' | ||
| // events are utilized to maintain inTransaction property state which in | ||
|
|
@@ -605,6 +639,23 @@ class Connection extends EventEmitter { | |
| this.emit('charsetChange', token.newValue); | ||
| }); | ||
|
|
||
| this.tokenStreamParser.on('fedAuthInfo', (token) => { | ||
| const clientId = '7f98cb04-cd1e-40df-9140-3bf7e2cea4db'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the meaning behind this value? 😅
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this id Azure Active Direcotry would know what's getting connected to it, it's registered within Azure. :) |
||
| if (token.fedAuthInfoData.stsurl && token.fedAuthInfoData.spn) { | ||
| this.fedAuthInfo.responsePending = true; | ||
| var context = new AuthenticationContext(token.fedAuthInfoData.stsurl); | ||
| context.acquireTokenWithUsernamePassword(token.fedAuthInfoData.spn, this.config.userName, this.config.password, clientId, (err, tokenResponse) => { | ||
| if (err) { | ||
| this.fedAuthInfo.responsePending = false; | ||
| this.loginError = ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH'); | ||
| } else { | ||
| this.fedAuthInfo.responsePending = false; | ||
| this.fedAuthInfo.token = tokenResponse; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| this.tokenStreamParser.on('loginack', (token) => { | ||
| if (!token.tdsVersion) { | ||
| // unsupported TDS version | ||
|
|
@@ -971,7 +1022,8 @@ class Connection extends EventEmitter { | |
|
|
||
| sendPreLogin() { | ||
| const payload = new PreloginPayload({ | ||
| encrypt: this.config.options.encrypt | ||
| encrypt: this.config.options.encrypt, | ||
| fedAuthRequested: (this.fedAuthInfo.method != undefined) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can/should |
||
| }); | ||
| this.messageIo.sendMessage(TYPE.PRELOGIN, payload.data); | ||
| this.debug.payload(function() { | ||
|
|
@@ -992,7 +1044,13 @@ class Connection extends EventEmitter { | |
| this.debug.payload(function() { | ||
| return preloginPayload.toString(' '); | ||
| }); | ||
|
|
||
| if (this.fedAuthInfo.method != undefined) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. If we can limit the potential types of |
||
| if (0 !== preloginPayload.fedAuthRequired && 1 !== preloginPayload.fedAuthRequired) { | ||
| this.emit('connect', ConnectionError(`Server sent an unexpected response for federated authentication value during negotiation. Value was ${preloginPayload.fedAuthRequired}`, 'EFEDAUTH')); | ||
| return this.close(); | ||
| } | ||
| this.fedAuthInfo.requiredPreLoginResponse = (preloginPayload.fedAuthRequired == 1); | ||
| } | ||
| if (preloginPayload.encryptionString === 'ON' || preloginPayload.encryptionString === 'REQ') { | ||
| if (!this.config.options.encrypt) { | ||
| this.emit('connect', ConnectionError("Server requires encryption, set 'encrypt' config option to true.", 'EENCRYPT')); | ||
|
|
@@ -1019,7 +1077,8 @@ class Connection extends EventEmitter { | |
| initDbFatal: !this.config.options.fallbackToDefaultDb, | ||
| readOnlyIntent: this.config.options.readOnlyIntent, | ||
| sspiBlob: clientResponse, | ||
| language: this.config.options.language | ||
| language: this.config.options.language, | ||
| fedAuthInfo: this.fedAuthInfo | ||
| }); | ||
|
|
||
| this.routingData = undefined; | ||
|
|
@@ -1056,6 +1115,22 @@ class Connection extends EventEmitter { | |
| process.nextTick(boundTransitionTo, this.STATE.SENT_NTLM_RESPONSE); | ||
| } | ||
|
|
||
| sendFedAuthResponsePacket() { | ||
| const accessTokenLen = Buffer.byteLength(this.fedAuthInfo.token.accessToken, 'ucs2'); | ||
| const data = new Buffer(8 + accessTokenLen); | ||
| let offset = 0; | ||
| data.writeUInt32LE(accessTokenLen + 4, offset); | ||
| offset += 4; | ||
| data.writeUInt32LE(accessTokenLen, offset); | ||
| offset += 4; | ||
| data.write(this.fedAuthInfo.token.accessToken, offset, 'ucs2'); | ||
| this.messageIo.sendMessage(TYPE.FEDAUTH_TOKEN, data); | ||
| // sent the fedAuth token message, the rest is similar to standard login 7 | ||
| process.nextTick(() => { | ||
| this.transitionTo(this.STATE.SENT_LOGIN7_WITH_STANDARD_LOGIN); | ||
| }); | ||
| } | ||
|
|
||
| // Returns false to apply backpressure. | ||
| sendDataToTokenStreamParser(data) { | ||
| return this.tokenStreamParser.addBuffer(data); | ||
|
|
@@ -1214,6 +1289,19 @@ class Connection extends EventEmitter { | |
| } | ||
| } | ||
|
|
||
| processLogin7FedAuthResponse() { | ||
| if (this.fedAuthInfo.fedAuthInfoRequested && !this.loginError) { | ||
| return this.dispatchEvent('receivedFedAuthInfo'); | ||
| } else { | ||
| if (this.loginError) { | ||
| this.emit('connect', this.loginError); | ||
| } else { | ||
| this.emit('connect', ConnectionError('Login failed.', 'ELOGIN')); | ||
| } | ||
| return this.dispatchEvent('loginFailed'); | ||
| } | ||
| } | ||
|
|
||
| execSqlBatch(request) { | ||
| this.makeRequest(request, TYPE.SQL_BATCH, new SqlBatchPayload(request.sqlTextOrProcedure, this.currentTransactionDescriptor(), this.config.options)); | ||
| } | ||
|
|
@@ -1619,10 +1707,13 @@ Connection.prototype.STATE = { | |
| message: function() { | ||
| if (this.messageIo.tlsNegotiationComplete) { | ||
| this.sendLogin7Packet(() => { | ||
| if (this.config.domain) { | ||
| this.transitionTo(this.STATE.SENT_LOGIN7_WITH_NTLM); | ||
| } else { | ||
| this.transitionTo(this.STATE.SENT_LOGIN7_WITH_STANDARD_LOGIN); | ||
| if (this.fedAuthInfo.requiredPreLoginResponse) { | ||
| return this.transitionTo(this.STATE.SENT_LOGIN7_WITH_FEDAUTH); | ||
| } else if (this.config.domain) { | ||
| return this.transitionTo(this.STATE.SENT_LOGIN7_WITH_NTLM); | ||
| } | ||
| else { | ||
| return this.transitionTo(this.STATE.SENT_LOGIN7_WITH_STANDARD_LOGIN); | ||
| } | ||
| }); | ||
| } | ||
|
|
@@ -1704,6 +1795,47 @@ Connection.prototype.STATE = { | |
| } | ||
| } | ||
| }, | ||
| SENT_LOGIN7_WITH_FEDAUTH: { | ||
| name: 'SentLogin7Withfedauth', | ||
| events: { | ||
| socketError: function() { | ||
| return this.transitionTo(this.STATE.FINAL); | ||
| }, | ||
| connectTimeout: function() { | ||
| return this.transitionTo(this.STATE.FINAL); | ||
| }, | ||
| data: function(data) { | ||
| if (this.fedAuthInfo.responsePending) { | ||
| // We got data from the server while we're waiting for adal authentication context | ||
| // call to complete on the client. We cannot process server data | ||
| // until this call completes as the state can change on completion of | ||
| // the call. Queue it for later. | ||
| const boundDispatchEvent = this.dispatchEvent.bind(this); | ||
| return setImmediate(boundDispatchEvent, 'data', data); | ||
| } else { | ||
| return this.sendDataToTokenStreamParser(data); | ||
| } | ||
| }, | ||
| receivedFedAuthInfo: function() { | ||
| return this.sendFedAuthResponsePacket(); | ||
| }, | ||
| loginFailed: function() { | ||
| return this.transitionTo(this.STATE.FINAL); | ||
| }, | ||
| message: function() { | ||
| if (this.fedAuthInfo.responsePending) { | ||
| // We got data from the server while we're waiting for adal authentication context | ||
| // call to complete on the client. We cannot process server data | ||
| // until this call completes as the state can change on completion of | ||
| // the call. Queue it for later. | ||
| const boundDispatchEvent = this.dispatchEvent.bind(this); | ||
| return setImmediate(boundDispatchEvent, 'message'); | ||
| } else { | ||
| return this.processLogin7FedAuthResponse(); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| LOGGED_IN_SENDING_INITIAL_SQL: { | ||
| name: 'LoggedInSendingInitialSql', | ||
| enter: function() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an FYI,
encryptwill be true by default once #606 is merged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't better to drop it after the PR is merged? cause without encryption fed auth would fail, so I think it's better to set it to true for now