Skip to content

Commit

Permalink
feat: password authentication (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored May 31, 2020
1 parent a32caa8 commit 599720e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ export type RedisConnectOptions = {
port?: number | string;
tls?: boolean;
db?: number;
password?: string;
};

function prasePortLike(port: string | number | undefined): number {
Expand All @@ -1488,6 +1489,7 @@ export async function connect({
port,
tls,
db,
password,
}: RedisConnectOptions): Promise<Redis> {
const dialOpts: Deno.ConnectOptions = {
hostname,
Expand All @@ -1504,6 +1506,14 @@ export async function connect({
const bufw = new BufWriter(conn);
const exec = muxExecutor(bufr, bufw);
const client = await create(conn, conn, conn, exec);
if (password != null) {
try {
await client.auth(password);
} catch (err) {
client.close();
throw err;
}
}
if (db) {
await client.select(db);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/general_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
assertThrowsAsync,
} from "../vendor/https/deno.land/std/testing/asserts.ts";
import { makeTest } from "./test_util.ts";
import { ErrorReplyError } from "../errors.ts";
import { Redis } from "../redis.ts";

const { test, client: redis, opts } = await makeTest("general");
test("conccurent", async function testConcurrent() {
Expand Down Expand Up @@ -35,6 +37,25 @@ test("db0", async function testDb0Option() {
client2.close();
});

test("connect with wrong password", async function testConnectWithWrongPassword() {
await assertThrowsAsync(async () => {
await connect({
...opts,
password: "wrong_password",
});
}, ErrorReplyError);
});

test("connect with empty password", async function testConnectWithEmptyPassword() {
// In Redis, authentication with an empty password will always fail.
await assertThrowsAsync(async () => {
await connect({
...opts,
password: "",
});
}, ErrorReplyError);
});

test("exists", async function testDb1Option() {
const key = "exists";
const client1 = await connect({ ...opts, db: 0 });
Expand Down

0 comments on commit 599720e

Please sign in to comment.