Skip to content

Commit 7a4a8e5

Browse files
committed
Add authorization to Redis instances with passwords
1 parent 5b1377b commit 7a4a8e5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Sources/NIORedis/Commands/BasicCommands.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ extension NIORedisConnection {
1111
.map { _ in return () }
1212
}
1313

14+
/// Request for authentication in a password-protected Redis server.
15+
///
16+
/// https://redis.io/commands/auth
17+
public func authorize(with password: String) -> EventLoopFuture<Void> {
18+
return command("AUTH", [RedisData(bulk: password)]).map { _ in return () }
19+
}
20+
1421
/// Removes the specified keys. A key is ignored if it does not exist.
1522
///
1623
/// https://redis.io/commands/del

Sources/NIORedis/NIORedis.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public final class NIORedis {
5050

5151
return bootstrap.connect(host: hostname, port: port)
5252
.map { return NIORedisConnection(channel: $0, handler: channelHandler) }
53+
.then { connection in
54+
guard let pw = password else {
55+
return self.elg.next().makeSucceededFuture(result: connection)
56+
}
57+
58+
return connection.authorize(with: pw).map { _ in return connection }
59+
}
5360
}
5461

5562
/// Handles the proper shutdown of managed resources.

0 commit comments

Comments
 (0)