Skip to content

Fix caching_sha2_password authentication for MySQL 8.0.5+ #173

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/auth_plugin/crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function encryptWithPublicKey(
const importedKey = await crypto.subtle.importKey(
"spki",
base64Decode(key),
{ name: "RSA-OAEP", hash: "SHA-256" },
{ name: "RSA-OAEP", hash: "SHA-1" },
false,
["encrypt"],
);
Expand Down
4 changes: 2 additions & 2 deletions src/auth_plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as caching_sha2_password from "./caching_sha2_password.ts";
import { start as caching_sha2_password } from "./caching_sha2_password.ts";
export default {
caching_sha2_password,
caching_sha2_password: { start: caching_sha2_password },
};
2 changes: 1 addition & 1 deletion src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class Connection {
receive = await this.nextPacket();
}
if (result.quickRead) {
await this.nextPacket();
receive = await this.nextPacket();
}
if (result.next) {
result = await result.next(receive);
Expand Down
32 changes: 32 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,38 @@ testWithClient(async function testSelectEmptyString(client) {
);
});

testWithClient(async function testCreateUserWithCachingSha2Password(client) {
const { version } = (await client.query(`SELECT VERSION() as version`))[0];
if (version.startsWith("8.")) {
await client.execute(
`CREATE USER 'sha2_test_user'@'%' IDENTIFIED WITH caching_sha2_password BY 'sha2_password'`,
);
} else {
await client.execute(
`CREATE USER 'sha2_test_user'@'%' IDENTIFIED BY 'sha2_password'`,
);
}
await client.execute(`GRANT ALL ON test.* TO 'sha2_test_user'@'%'`);
});

testWithClient(async function testCachingSha2PasswordAuthenticateRoot(client) {
await client.execute("FLUSH PRIVILEGES");
});

testWithClient(async function testCachingSha2PasswordWithClearCache(client) {
assertEquals(
await client.query(`SELECT CURRENT_USER() AS user`),
[{ user: "sha2_test_user@%" }],
);

const result = await client.query(`SELECT 'caching_sha2_password_test' AS test`);
assertEquals(result, [{ test: "caching_sha2_password_test" }]);
}, { username: "sha2_test_user", password: "sha2_password" });

testWithClient(async function testDropUserWithCachingSha2Password(client) {
await client.execute(`DROP USER 'sha2_test_user'@'%'`);
});

registerTests();

Deno.test("configLogger()", async () => {
Expand Down