Skip to content

Commit

Permalink
Protoype of kerberos support (non parameterized with TODOs)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackentropy committed Jun 25, 2024
1 parent b5281f5 commit 93f30ed
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/pg-protocol/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ export class Parser {
return new AuthenticationMD5Password(length, salt)
}
break
case 7: // GSS Init (Kerberos)
message.name = 'GSSInit';
break;
case 8: // GSSAPI Continue (Kerberos)
message.name = 'GSSContinue';
message.inToken = this.reader.bytes(length - 8).toString('base64');
return message
case 10: // AuthenticationSASL
message.name = 'authenticationSASL'
message.mechanisms = []
Expand Down
5 changes: 5 additions & 0 deletions packages/pg-protocol/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ const copyData = (chunk: Buffer): Buffer => {
return writer.add(chunk).flush(code.copyFromChunk)
}

const sendBinaryPassword = (chunk: Buffer): Buffer => {
return writer.add(chunk).flush(code.startup)
}

const copyFail = (message: string): Buffer => {
return cstringMessage(code.copyFail, message)
}
Expand Down Expand Up @@ -266,6 +270,7 @@ const serialize = {
sync: () => syncBuffer,
end: () => endBuffer,
copyData,
sendBinaryPassword,
copyDone: () => copyDoneBuffer,
copyFail,
cancel,
Expand Down
37 changes: 37 additions & 0 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class Client extends EventEmitter {
}

_attachListeners(con) {
// kerberos
con.on('GSSInit', this._handleGSSInit.bind(this))
con.on('GSSContinue', this._handleGSSContinue.bind(this))

// password request handling
con.on('authenticationCleartextPassword', this._handleAuthCleartextPassword.bind(this))
// password request handling
Expand All @@ -198,6 +202,39 @@ class Client extends EventEmitter {
con.on('notification', this._handleNotification.bind(this))
}

async _handleGSSInit(msg) {
try {
// TODO: Below needs to be parameterized
this.client = await kerberos.initializeClient('[email protected]', {
mechOID: kerberos.GSS_MECH_OID_SPNEGO,
})

// TODO: below this might need to be a recursive loop to step multiple times.
const token = await this.client.step('')

const buf = Buffer.from(token, 'base64')
this.connection.sendBinaryPassword(buf)
} catch (e) {
this.emit('error', e)
}
}

async _handleGSSContinue(msg) {
try {
const inToken = msg.inToken
const token = await this.client.step(inToken)

// TODO: probably a better way to handle this.
if (token == null) {
return
}
const buf = Buffer.from(token, 'base64')
this.connection.sendBinaryPassword(buf)
} catch (e) {
this.emit('error', e)
}
}

// TODO(bmc): deprecate pgpass "built in" integration since this.password can be a function
// it can be supplied by the user if required - this is a breaking change!
_checkPgPass(cb) {
Expand Down
4 changes: 4 additions & 0 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class Connection extends EventEmitter {
this._send(serialize.password(password))
}

sendBinaryPassword(password) {
this._send(serialize.sendBinaryPassword(password))
}

sendSASLInitialResponseMessage(mechanism, initialResponse) {
this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
}
Expand Down
1 change: 1 addition & 0 deletions packages/pg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"author": "Brian Carlson <[email protected]>",
"main": "./lib",
"dependencies": {
"kerberos": "^2.1.0",
"pg-connection-string": "^2.6.4",
"pg-pool": "^3.6.2",
"pg-protocol": "^1.6.1",
Expand Down
Loading

0 comments on commit 93f30ed

Please sign in to comment.