Skip to content

feat: Introducing StreamingCredentialsProvider for token based authentication #3320

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 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5410adb
wip
ndyakov Mar 18, 2025
9ef438b
Merge remote-tracking branch 'origin/master' into ndyakov/token-based…
ndyakov Mar 24, 2025
df9bfce
update documentation
ndyakov Mar 24, 2025
140a278
add streamingcredentialsprovider in options
ndyakov Mar 24, 2025
d3a25f9
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Mar 24, 2025
7f5d87b
fix: put back option in pool creation
ndyakov Mar 24, 2025
fa59cce
add package level comment
ndyakov Mar 24, 2025
c248425
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Mar 31, 2025
847f1f9
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 3, 2025
40a89c5
Initial re authentication implementation
ndyakov Apr 15, 2025
d0a8c76
Change function type name
ndyakov Apr 16, 2025
e0c224d
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 22, 2025
44628c5
add tests
ndyakov Apr 22, 2025
4ab4980
fix race in tests
ndyakov Apr 22, 2025
420c4fb
fix example tests
ndyakov Apr 22, 2025
5fac913
wip, hooks refactor
ndyakov Apr 22, 2025
2a97f2e
fix build
ndyakov Apr 22, 2025
f103a7d
update README.md
ndyakov Apr 22, 2025
6e17fb4
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 22, 2025
3acfb1c
update wordlist
ndyakov Apr 22, 2025
7eea9e7
update README.md
ndyakov Apr 22, 2025
5f91e66
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 23, 2025
d0bfdab
refactor(auth): early returns in cred listener
ndyakov Apr 24, 2025
f6f892d
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 24, 2025
544bdb2
fix(doctest): simulate some delay
ndyakov Apr 24, 2025
cff6b9b
Merge branch 'master' into ndyakov/token-based-auth
ndyakov Apr 29, 2025
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
10 changes: 9 additions & 1 deletion .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ RedisGears
RedisTimeseries
RediSearch
RawResult
RawVal
RawVal
entra
EntraID
Entra
OAuth
Azure
StreamingCredentialsProvider
oauth
entraid
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ testdata/*
redis8tests.sh
coverage.txt
**/coverage.txt
.vscode
.vscode
tmp/*
121 changes: 113 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ key value NoSQL database that uses RocksDB as storage engine and is compatible w

- Redis commands except QUIT and SYNC.
- Automatic connection pooling.
- [StreamingCredentialsProvider (e.g. entra id, oauth)](#1-streaming-credentials-provider-highest-priority)
- [Pub/Sub](https://redis.uptrace.dev/guide/go-redis-pubsub.html).
- [Pipelines and transactions](https://redis.uptrace.dev/guide/go-redis-pipelines.html).
- [Scripting](https://redis.uptrace.dev/guide/lua-scripting.html).
Expand Down Expand Up @@ -136,17 +137,121 @@ func ExampleClient() {
}
```

The above can be modified to specify the version of the RESP protocol by adding the `protocol`
option to the `Options` struct:
### Authentication

The Redis client supports multiple ways to provide authentication credentials, with a clear priority order. Here are the available options:

#### 1. Streaming Credentials Provider (Highest Priority)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mention this is still an experimental feature


The streaming credentials provider allows for dynamic credential updates during the connection lifetime. This is particularly useful for managed identity services and token-based authentication.

```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
})
type StreamingCredentialsProvider interface {
Subscribe(listener CredentialsListener) (Credentials, UnsubscribeFunc, error)
}

type CredentialsListener interface {
OnNext(credentials Credentials) // Called when credentials are updated
OnError(err error) // Called when an error occurs
}

type Credentials interface {
BasicAuth() (username string, password string)
RawCredentials() string
}
```

Example usage:
```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
StreamingCredentialsProvider: &MyCredentialsProvider{},
})
```

**Note:** The streaming credentials provider can be used with [go-redis-entraid](https://github.com/redis-developer/go-redis-entraid) to enable Entra ID (formerly Azure AD) authentication. This allows for seamless integration with Azure's managed identity services and token-based authentication.

Example with Entra ID:
```go
import (
"github.com/redis/go-redis/v9"
"github.com/redis-developer/go-redis-entraid"
)

// Create an Entra ID credentials provider
provider := entraid.NewDefaultAzureIdentityProvider()

// Configure Redis client with Entra ID authentication
rdb := redis.NewClient(&redis.Options{
Addr: "your-redis-server.redis.cache.windows.net:6380",
StreamingCredentialsProvider: provider,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
})
```

#### 2. Context-based Credentials Provider

The context-based provider allows credentials to be determined at the time of each operation, using the context.

```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
CredentialsProviderContext: func(ctx context.Context) (string, string, error) {
// Return username, password, and any error
return "user", "pass", nil
},
})
```

#### 3. Regular Credentials Provider

A simple function-based provider that returns static credentials.

```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
CredentialsProvider: func() (string, string) {
// Return username and password
return "user", "pass"
},
})
```

#### 4. Username/Password Fields (Lowest Priority)

The most basic way to provide credentials is through the `Username` and `Password` fields in the options.

```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "user",
Password: "pass",
})
```

#### Priority Order

The client will use credentials in the following priority order:
1. Streaming Credentials Provider (if set)
2. Context-based Credentials Provider (if set)
3. Regular Credentials Provider (if set)
4. Username/Password fields (if set)

If none of these are set, the client will attempt to connect without authentication.

### Protocol Version

The client supports both RESP2 and RESP3 protocols. You can specify the protocol version in the options:

```go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
})
```

### Connecting via a redis url
Expand Down
61 changes: 61 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Package auth package provides authentication-related interfaces and types.
// It also includes a basic implementation of credentials using username and password.
package auth

// StreamingCredentialsProvider is an interface that defines the methods for a streaming credentials provider.
// It is used to provide credentials for authentication.
// The CredentialsListener is used to receive updates when the credentials change.
type StreamingCredentialsProvider interface {
// Subscribe subscribes to the credentials provider for updates.
// It returns the current credentials, a cancel function to unsubscribe from the provider,
// and an error if any.
// TODO(ndyakov): Should we add context to the Subscribe method?
Subscribe(listener CredentialsListener) (Credentials, UnsubscribeFunc, error)
}

// UnsubscribeFunc is a function that is used to cancel the subscription to the credentials provider.
// It is used to unsubscribe from the provider when the credentials are no longer needed.
type UnsubscribeFunc func() error

// CredentialsListener is an interface that defines the methods for a credentials listener.
// It is used to receive updates when the credentials change.
// The OnNext method is called when the credentials change.
// The OnError method is called when an error occurs while requesting the credentials.
type CredentialsListener interface {
OnNext(credentials Credentials)
OnError(err error)
}

// Credentials is an interface that defines the methods for credentials.
// It is used to provide the credentials for authentication.
type Credentials interface {
// BasicAuth returns the username and password for basic authentication.
BasicAuth() (username string, password string)
// RawCredentials returns the raw credentials as a string.
// This can be used to extract the username and password from the raw credentials or
// additional information if present in the token.
RawCredentials() string
}

type basicAuth struct {
username string
password string
}

// RawCredentials returns the raw credentials as a string.
func (b *basicAuth) RawCredentials() string {
return b.username + ":" + b.password
}

// BasicAuth returns the username and password for basic authentication.
func (b *basicAuth) BasicAuth() (username string, password string) {
return b.username, b.password
}

// NewBasicCredentials creates a new Credentials object from the given username and password.
func NewBasicCredentials(username, password string) Credentials {
return &basicAuth{
username: username,
password: password,
}
}
Loading
Loading