Skip to content

Commit

Permalink
refactor auth
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Apr 11, 2020
1 parent 1d21069 commit 9fcd2de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
3 changes: 3 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { decode, encode } from "https://deno.land/[email protected]/encoding/utf8.ts";
export { format as byteFormat } from "https://deno.land/x/[email protected]/mod.ts";
export { replaceParams } from "https://deno.land/x/[email protected]/util.ts";
export { Hash } from "https://deno.land/x/[email protected]/mod.ts";
export { sha256 } from "https://denopkg.com/chiefbiiko/[email protected]/mod.ts";

export {
deferred,
Deferred,
Expand Down
45 changes: 45 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { encode, Hash, sha256 } from "../deps.ts";

function xor(a: Uint8Array, b: Uint8Array): Uint8Array {
return a.map((byte, index) => {
return byte ^ b[index];
});
}

function mysqlNativePassword(password: string, seed: Uint8Array): Uint8Array {
const hash = new Hash("sha1");
const pwd1 = hash.digest(encode(password)).data;
const pwd2 = hash.digest(pwd1).data;

let seedAndPwd2 = new Uint8Array(seed.length + pwd2.length);
seedAndPwd2.set(seed);
seedAndPwd2.set(pwd2, seed.length);
seedAndPwd2 = hash.digest(seedAndPwd2).data;

return xor(seedAndPwd2, pwd1);
}

function cachingSha2Password(password: string, seed: Uint8Array): Uint8Array {
const stage1 = sha256(password, "utf8") as Uint8Array;
const stage2 = sha256(stage1) as Uint8Array;
const stage3 = sha256(Uint8Array.from([...stage2, ...seed])) as Uint8Array;
return xor(stage1, stage3);
}

export default function auth(
authPluginName: string,
password: string,
seed: Uint8Array
) {
switch (authPluginName) {
case "mysql_native_password":
return mysqlNativePassword(password, seed);

case "caching_sha2_password":
// TODO
// return cachingSha2Password(password, seed);

default:
throw new Error("Not supported");
}
}
20 changes: 0 additions & 20 deletions src/auth/mysql_native_password.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/packets/builders/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { auth } from "../../auth/mysql_native_password.ts";
import auth from "../../auth.ts";
import { BufferWriter } from "../../buffer.ts";
import ServerCapabilities from "../../constant/capabilities.ts";
import { Charset } from "../../constant/charset.ts";
Expand Down Expand Up @@ -39,7 +39,11 @@ export function buildAuth(
.skip(23)
.writeNullTerminatedString(params.username);
if (params.password) {
const authData = auth(params.password, packet.seed);
const authData = auth(
packet.authPluginName,
params.password,
packet.seed
);
if (
clientParam &
ServerCapabilities.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA ||
Expand Down
16 changes: 0 additions & 16 deletions tt.ts

This file was deleted.

0 comments on commit 9fcd2de

Please sign in to comment.