AUTH
AUTH [username] password
- Available since:
- Redis Open Source 1.0.0
- Time complexity:
- O(N) where N is the number of passwords defined for the user
- ACL categories:
-
@fast,@connection, - Compatibility:
- Redis Software and Redis Cloud compatibility
The AUTH command authenticates the current connection in two cases:
- If the Redis server is password protected via the
requirepassoption. - A Redis 6.0 instance, or greater, is using the Redis ACL system.
Redis versions prior of Redis 6 were only able to understand the one argument version of the command:
AUTH "temp-pass"
res1 = r.auth(password="temp_pass")
print(res1) # >>> True
res2 = r.auth(password="temp_pass", username="default")
print(res2) # >>> True
import redis
r = redis.Redis(decode_responses=True)
res1 = r.auth(password="temp_pass")
print(res1) # >>> True
res2 = r.auth(password="temp_pass", username="default")
print(res2) # >>> True
res = r.auth(username="test-user", password="strong_password")
print(res) # >>> True
const res1 = await client.auth({ password: 'temp_pass' });
console.log(res1); // OK
const res2 = await client.auth({ username: 'default', password: 'temp_pass' });
console.log(res2); // OK
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const res1 = await client.auth({ password: 'temp_pass' });
console.log(res1); // OK
const res2 = await client.auth({ username: 'default', password: 'temp_pass' });
console.log(res2); // OK
const res3 = await client.auth({ username: 'test-user', password: 'strong_password' });
console.log(res3); // OK
await client.close();
const res1 = await redis.auth('temp_pass');
console.log(res1); // >>> OK
const res2 = await redis.auth('default', 'temp_pass');
console.log(res2); // >>> OK
import { Redis } from 'ioredis';
const redis = new Redis();
const res1 = await redis.auth('temp_pass');
console.log(res1); // >>> OK
const res2 = await redis.auth('default', 'temp_pass');
console.log(res2); // >>> OK
const res3 = await redis.auth('test-user', 'strong_password');
console.log(res3); // >>> OK
redis.disconnect();
// Note: you must use the `Jedis` class rather than `RedisClient`
// to access the `auth` commands.
String authResult1 = jedis.auth("default", "temp_pass");
System.out.println(authResult1); // >>> OK
import redis.clients.jedis.Jedis;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CmdsCnxmgmtExample {
public void run() {
Jedis jedis = new Jedis("redis://localhost:6379");
// Note: you must use the `Jedis` class rather than `RedisClient`
// to access the `auth` commands.
String authResult1 = jedis.auth("default", "temp_pass");
System.out.println(authResult1); // >>> OK
// Note: you must use the `Jedis` class rather than `RedisClient`
// to access the `auth` commands.
String authResult2 = jedis.auth("test-user", "strong_password");
System.out.println(authResult2); // >>> OK
}
}
CompletableFuture<Void> authExample1 = asyncCommands.auth("temp_pass")
.thenCompose(res1 -> {
System.out.println(res1); // >>> OK
return asyncCommands.auth("default", "temp_pass");
}).thenAccept(res2 -> {
System.out.println(res2); // >>> OK
}).toCompletableFuture();
package io.redis.examples.async;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.StatefulRedisConnection;
import java.util.concurrent.CompletableFuture;
public class CmdsCnxmgmtExample {
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisAsyncCommands<String, String> asyncCommands = connection.async();
CompletableFuture<Void> authExample1 = asyncCommands.auth("temp_pass")
.thenCompose(res1 -> {
System.out.println(res1); // >>> OK
return asyncCommands.auth("default", "temp_pass");
}).thenAccept(res2 -> {
System.out.println(res2); // >>> OK
}).toCompletableFuture();
authExample1.join();
CompletableFuture<Void> authExample2 = asyncCommands.auth("test-user", "strong_password")
.thenAccept(res3 -> {
System.out.println(res3); // >>> OK
}).toCompletableFuture();
authExample2.join();
} finally {
redisClient.shutdown();
}
}
}
Mono<Void> authExample1 = reactiveCommands.auth("temp_pass")
.doOnNext(res1 -> {
System.out.println(res1); // >>> OK
})
.then(reactiveCommands.auth("default", "temp_pass"))
.doOnNext(res2 -> {
System.out.println(res2); // >>> OK
})
.then();
package io.redis.examples.reactive;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import io.lettuce.core.api.StatefulRedisConnection;
import reactor.core.publisher.Mono;
public class CmdsCnxmgmtExample {
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisReactiveCommands<String, String> reactiveCommands = connection.reactive();
Mono<Void> authExample1 = reactiveCommands.auth("temp_pass")
.doOnNext(res1 -> {
System.out.println(res1); // >>> OK
})
.then(reactiveCommands.auth("default", "temp_pass"))
.doOnNext(res2 -> {
System.out.println(res2); // >>> OK
})
.then();
authExample1.block();
Mono<Void> authExample2 = reactiveCommands.auth("test-user", "strong_password")
.doOnNext(res3 -> {
System.out.println(res3); // >>> OK
})
.then();
authExample2.block();
} finally {
redisClient.shutdown();
}
}
}
authResult1, err := conn.Auth(ctx, "temp_pass").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult1) // >>> OK
authResult2, err := conn.AuthACL(ctx, "default", "temp_pass").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult2) // >>> OK
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_cnxmgmt_auth() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// AUTH is connection-scoped, so run it on a dedicated connection from
// Conn() rather than on the pooled client.
conn := rdb.Conn()
defer conn.Close()
authResult1, err := conn.Auth(ctx, "temp_pass").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult1) // >>> OK
authResult2, err := conn.AuthACL(ctx, "default", "temp_pass").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult2) // >>> OK
authResult3, err := conn.AuthACL(ctx, "test-user", "strong_password").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult3) // >>> OK
}
var res1 = db.Execute("AUTH", "temp_pass");
Console.WriteLine(res1); // >>> OK
var res2 = db.Execute("AUTH", "default", "temp_pass");
Console.WriteLine(res2); // >>> OK
using System;
using StackExchange.Redis;
public class CmdsCnxmgmt
{
public void Run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
// Note: StackExchange.Redis has no dedicated AUTH method — you normally
// supply credentials on the connection (for example
// ConfigurationOptions.User / ConfigurationOptions.Password). The raw
// AUTH command is shown here for parity with the other clients.
var res1 = db.Execute("AUTH", "temp_pass");
Console.WriteLine(res1); // >>> OK
var res2 = db.Execute("AUTH", "default", "temp_pass");
Console.WriteLine(res2); // >>> OK
var res3 = db.Execute("AUTH", "test-user", "strong_password");
Console.WriteLine(res3); // >>> OK
}
}
$res1 = $r->auth('temp_pass');
echo $res1 . PHP_EOL; // >>> OK
$res2 = $r->auth('default', 'temp_pass');
echo $res2 . PHP_EOL; // >>> OK
<?php
use Predis\Client as PredisClient;
class CmdsCnxmgmtTest
{
public function testCmdsCnxmgmt() {
$r = new PredisClient([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'database' => 0,
]);
$res1 = $r->auth('temp_pass');
echo $res1 . PHP_EOL; // >>> OK
$res2 = $r->auth('default', 'temp_pass');
echo $res2 . PHP_EOL; // >>> OK
$res3 = $r->auth('test-user', 'strong_password');
echo $res3 . PHP_EOL; // >>> OK
}
}
res1 = r.auth('temp_pass')
puts res1 # >>> OK
res2 = r.auth('default', 'temp_pass')
puts res2 # >>> OK
require 'redis'
r = Redis.new
res1 = r.auth('temp_pass')
puts res1 # >>> OK
res2 = r.auth('default', 'temp_pass')
puts res2 # >>> OK
res3 = r.auth('test-user', 'strong_password')
puts res3 # >>> OK
r.close
if let Ok(res1) = redis::cmd("AUTH").arg("temp_pass").query::<String>(&mut r) {
println!("{res1}"); // >>> OK
}
if let Ok(res2) = redis::cmd("AUTH").arg("default").arg("temp_pass").query::<String>(&mut r) {
println!("{res2}"); // >>> OK
}
mod cmds_cnxmgmt_tests {
fn run() {
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => match client.get_connection() {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
if let Ok(res1) = redis::cmd("AUTH").arg("temp_pass").query::<String>(&mut r) {
println!("{res1}"); // >>> OK
}
if let Ok(res2) = redis::cmd("AUTH").arg("default").arg("temp_pass").query::<String>(&mut r) {
println!("{res2}"); // >>> OK
}
if let Ok(res3) = redis::cmd("AUTH").arg("test-user").arg("strong_password").query::<String>(&mut r) {
println!("{res3}"); // >>> OK
}
}
}
if let Ok(res1) = redis::cmd("AUTH").arg("temp_pass").query_async::<String>(&mut r).await {
println!("{res1}"); // >>> OK
}
if let Ok(res2) = redis::cmd("AUTH").arg("default").arg("temp_pass").query_async::<String>(&mut r).await {
println!("{res2}"); // >>> OK
}
mod cmds_cnxmgmt_tests {
async fn run() {
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => match client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
if let Ok(res1) = redis::cmd("AUTH").arg("temp_pass").query_async::<String>(&mut r).await {
println!("{res1}"); // >>> OK
}
if let Ok(res2) = redis::cmd("AUTH").arg("default").arg("temp_pass").query_async::<String>(&mut r).await {
println!("{res2}"); // >>> OK
}
if let Ok(res3) = redis::cmd("AUTH").arg("test-user").arg("strong_password").query_async::<String>(&mut r).await {
println!("{res3}"); // >>> OK
}
}
}
This form just authenticates against the password set with requirepass.
In this configuration Redis will deny any command executed by the just
connected clients, unless the connection gets authenticated via AUTH.
If the password provided via AUTH matches the password in the configuration file, the server replies with the OK status code and starts accepting commands.
Otherwise, an error is returned and the clients needs to try a new password.
When Redis ACLs are used, the command should be given in an extended way:
AUTH "test-user" "strong_password"
res = r.auth(username="test-user", password="strong_password")
print(res) # >>> True
const res3 = await client.auth({ username: 'test-user', password: 'strong_password' });
console.log(res3); // OK
const res3 = await redis.auth('test-user', 'strong_password');
console.log(res3); // >>> OK
// Note: you must use the `Jedis` class rather than `RedisClient`
// to access the `auth` commands.
String authResult2 = jedis.auth("test-user", "strong_password");
System.out.println(authResult2); // >>> OK
CompletableFuture<Void> authExample2 = asyncCommands.auth("test-user", "strong_password")
.thenAccept(res3 -> {
System.out.println(res3); // >>> OK
}).toCompletableFuture();
Mono<Void> authExample2 = reactiveCommands.auth("test-user", "strong_password")
.doOnNext(res3 -> {
System.out.println(res3); // >>> OK
})
.then();
authResult3, err := conn.AuthACL(ctx, "test-user", "strong_password").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(authResult3) // >>> OK
var res3 = db.Execute("AUTH", "test-user", "strong_password");
Console.WriteLine(res3); // >>> OK
$res3 = $r->auth('test-user', 'strong_password');
echo $res3 . PHP_EOL; // >>> OK
res3 = r.auth('test-user', 'strong_password')
puts res3 # >>> OK
if let Ok(res3) = redis::cmd("AUTH").arg("test-user").arg("strong_password").query::<String>(&mut r) {
println!("{res3}"); // >>> OK
}
if let Ok(res3) = redis::cmd("AUTH").arg("test-user").arg("strong_password").query_async::<String>(&mut r).await {
println!("{res3}"); // >>> OK
}
In order to authenticate the current connection with one of the connections
defined in the ACL list (see ACL SETUSER) and the official ACL guide for more information.
When ACLs are used, the single argument form of the command, where only the password is specified, assumes that the implicit username is "default".
Required arguments
password
The password to authenticate with. With requirepass, this is the server password; with ACLs, it is the user's password.
Optional arguments
username
The ACL username to authenticate as. If omitted, the default user is used.
Details
Security notice
Because of the high performance nature of Redis, it is possible to try
a lot of passwords in parallel in very short time, so make sure to generate a
strong and very long password so that this attack is infeasible.
A good way to generate strong passwords is via the ACL GENPASS command.
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ✅ Standard |
✅ Standard |
Return information
OK, or an error if the password, or username/password pair, is invalid.
History
- Starting with Redis version 6.0.0: Added ACL style (username and password).