-
Notifications
You must be signed in to change notification settings - Fork 11
feat(auth): SASL SCRAM-SHA-256 support #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
d9bc422
init
jgoux e5e5418
simplify
jgoux 81d13bd
fix salt
jgoux 8a6a596
fix sendAuthentictionSASL
jgoux 540639a
getting closer
jgoux bcfc264
read clientFinalMessage correctly
jgoux 06b3796
it works!
jgoux ebaf371
refactor
jgoux d289bf0
use biome
jgoux 1e3d59e
lock
jgoux 159939b
call validateCredentials for the certificate auth mode
jgoux ddbac77
delete extra types
jgoux 0a7cb3c
use supabase styles
jgoux 60df052
align sasl workflow with pg server
jgoux 3d0425e
use biome and tsconfig everywhere
jgoux 16a2c26
fix types
jgoux 61a6486
refactor incoming
jgoux 1bc57ca
big refactor
jgoux fdf0e34
remove commented code
jgoux 46bc054
rename types
jgoux bdc951a
remove util
jgoux 30ef55d
rename type
jgoux 5a01051
isolate buffer logic
jgoux 4dfdcab
isolate TLS
jgoux 1a641df
address comments
jgoux 322451e
export BackendError
jgoux 25764a5
isolate all the auth flow
jgoux f488d5d
add all the auth examples
jgoux 2e66022
put regular pg port back
jgoux 64da415
remove logs
jgoux b8c8b37
harmonize how the password is provided
jgoux ff64552
memoize for scram
jgoux 3f428aa
fix cert flow
jgoux 2ec114b
pass ConnectionState in all callbacks
jgoux 9a301a3
Update examples/pglite-auth/package.json
jgoux 0fe04a0
Update examples/pglite-auth/cert.ts
jgoux 39a77ee
apply comment
jgoux 716c1d9
apply password naming change
jgoux 8a2554d
Update examples/pglite-auth/scram-sha-256.ts
jgoux be9649d
Update examples/pglite-auth/scram-sha-256.ts
jgoux e1d19ff
Update examples/pglite-auth/trust.ts
jgoux 7a0bb68
remove the possibility to pass a salt
jgoux 82a1310
pause/resume socket
jgoux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ dist/ | |
dbs/ | ||
*.pem | ||
*.srl | ||
tsconfig.tsbuildinfo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space", | ||
"indentWidth": 2 | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "single" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import net from 'node:net'; | ||
import { PGlite } from '@electric-sql/pglite'; | ||
import { | ||
type BackendError, | ||
PostgresConnection, | ||
createPreHashedPassword, | ||
} from 'pg-gateway'; | ||
|
||
const db = new PGlite(); | ||
|
||
const server = net.createServer((socket) => { | ||
const connection = new PostgresConnection(socket, { | ||
serverVersion: '16.3 (PGlite 0.2.0)', | ||
auth: { | ||
method: 'md5', | ||
getPreHashedPassword({ username }) { | ||
return createPreHashedPassword(username, 'postgres'); | ||
}, | ||
}, | ||
|
||
async onStartup() { | ||
// Wait for PGlite to be ready before further processing | ||
await db.waitReady; | ||
return false; | ||
}, | ||
async onMessage(data, { isAuthenticated }) { | ||
// Only forward messages to PGlite after authentication | ||
if (!isAuthenticated) { | ||
return false; | ||
} | ||
|
||
// Forward raw message to PGlite | ||
try { | ||
const [result] = await db.execProtocol(data); | ||
if (result) { | ||
const [_, responseData] = result; | ||
connection.sendData(responseData); | ||
} | ||
} catch (err) { | ||
connection.sendError(err as BackendError); | ||
connection.sendReadyForQuery(); | ||
} | ||
return true; | ||
}, | ||
}); | ||
|
||
socket.on('end', () => { | ||
console.log('Client disconnected'); | ||
}); | ||
}); | ||
|
||
server.listen(5432, () => { | ||
console.log('Server listening on port 5432'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "pglite-auth-example", | ||
"type": "module", | ||
"scripts": { | ||
"format": "biome format --write .", | ||
"lint": "biome lint --error-on-warnings .", | ||
"type-check": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"pg-gateway": "*" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.8.3", | ||
"@electric-sql/pglite": "0.2.0-alpha.7", | ||
"@total-typescript/tsconfig": "^1.0.4", | ||
"@types/node": "^20.14.11", | ||
"tsx": "^4.16.2", | ||
"typescript": "^5.5.3" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import net from 'node:net'; | ||
import { PGlite } from '@electric-sql/pglite'; | ||
import { type BackendError, PostgresConnection } from 'pg-gateway'; | ||
|
||
const db = new PGlite(); | ||
|
||
const server = net.createServer((socket) => { | ||
const connection = new PostgresConnection(socket, { | ||
serverVersion: '16.3 (PGlite 0.2.0)', | ||
auth: { | ||
method: 'password', | ||
// this is the password stored in the server | ||
getClearTextPassword(credentials) { | ||
return 'postgres'; | ||
}, | ||
// uncomment to override the default password validation logic | ||
// async validateCredentials(credentials) { | ||
// const { clearTextPassword, password } = credentials; | ||
// // we allow case insensitive password validation | ||
// return password.toUpperCase() === clearTextPassword.toUpperCase(); | ||
// }, | ||
}, | ||
|
||
async onStartup() { | ||
// Wait for PGlite to be ready before further processing | ||
await db.waitReady; | ||
return false; | ||
}, | ||
async onMessage(data, { isAuthenticated }) { | ||
// Only forward messages to PGlite after authentication | ||
if (!isAuthenticated) { | ||
return false; | ||
} | ||
|
||
// Forward raw message to PGlite | ||
// Forward raw message to PGlite | ||
try { | ||
const [result] = await db.execProtocol(data); | ||
if (result) { | ||
const [_, responseData] = result; | ||
connection.sendData(responseData); | ||
} | ||
} catch (err) { | ||
connection.sendError(err as BackendError); | ||
connection.sendReadyForQuery(); | ||
} | ||
return true; | ||
}, | ||
}); | ||
|
||
socket.on('end', () => { | ||
console.log('Client disconnected'); | ||
}); | ||
}); | ||
|
||
server.listen(5432, () => { | ||
console.log('Server listening on port 5432'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import net from 'node:net'; | ||
import { PGlite } from '@electric-sql/pglite'; | ||
import { | ||
type BackendError, | ||
PostgresConnection, | ||
createScramSha256Data, | ||
} from 'pg-gateway'; | ||
|
||
const db = new PGlite(); | ||
|
||
const server = net.createServer((socket) => { | ||
const connection = new PostgresConnection(socket, { | ||
serverVersion: '16.3 (PGlite 0.2.0)', | ||
auth: { | ||
method: 'scram-sha-256', | ||
async getScramSha256Data(credentials) { | ||
// Utility function to generate scram-sha-256 data (like salt) for the given user | ||
// You would likely store this info in a database and retrieve it here | ||
return createScramSha256Data('postgres'); | ||
jgoux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
async onStartup() { | ||
// Wait for PGlite to be ready before further processing | ||
await db.waitReady; | ||
return false; | ||
}, | ||
async onMessage(data, { isAuthenticated }) { | ||
// Only forward messages to PGlite after authentication | ||
if (!isAuthenticated) { | ||
return false; | ||
} | ||
|
||
// Forward raw message to PGlite | ||
try { | ||
const [result] = await db.execProtocol(data); | ||
if (result) { | ||
const [_, responseData] = result; | ||
connection.sendData(responseData); | ||
} | ||
} catch (err) { | ||
connection.sendError(err as BackendError); | ||
connection.sendReadyForQuery(); | ||
} | ||
return true; | ||
}, | ||
}); | ||
|
||
socket.on('close', () => { | ||
console.log('Client disconnected'); | ||
}); | ||
}); | ||
|
||
server.listen(5432, () => { | ||
console.log('Server listening on port 5432'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import net from 'node:net'; | ||
import { PGlite } from '@electric-sql/pglite'; | ||
import { type BackendError, PostgresConnection } from 'pg-gateway'; | ||
|
||
const db = new PGlite(); | ||
|
||
const server = net.createServer((socket) => { | ||
const connection = new PostgresConnection(socket, { | ||
serverVersion: '16.3 (PGlite 0.2.0)', | ||
auth: { | ||
method: 'trust', | ||
}, | ||
|
||
async onStartup() { | ||
// Wait for PGlite to be ready before further processing | ||
await db.waitReady; | ||
return false; | ||
}, | ||
async onMessage(data, { isAuthenticated }) { | ||
// Only forward messages to PGlite after authentication | ||
if (!isAuthenticated) { | ||
return false; | ||
} | ||
|
||
// Forward raw message to PGlite | ||
jgoux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const [result] = await db.execProtocol(data); | ||
if (result) { | ||
const [_, responseData] = result; | ||
connection.sendData(responseData); | ||
} | ||
} catch (err) { | ||
connection.sendError(err as BackendError); | ||
connection.sendReadyForQuery(); | ||
} | ||
return true; | ||
}, | ||
}); | ||
|
||
socket.on('end', () => { | ||
console.log('Client disconnected'); | ||
}); | ||
}); | ||
|
||
server.listen(5432, () => { | ||
console.log('Server listening on port 5432'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "@total-typescript/tsconfig/tsc/no-dom/app", | ||
"include": ["*.ts"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.