@@ -128,19 +128,122 @@ func ExampleClient() {
128
128
// Output: key value
129
129
// key2 does not exist
130
130
}
131
+
132
+ ### Authentication
133
+
134
+ The Redis client supports multiple ways to provide authentication credentials, with a clear priority order. Here are the available options:
135
+
136
+ #### 1 . Streaming Credentials Provider (Highest Priority )
137
+
138
+ 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.
139
+
140
+ ` ` ` go
141
+ type StreamingCredentialsProvider interface {
142
+ Subscribe(listener CredentialsListener) (Credentials, UnsubscribeFunc, error)
143
+ }
144
+
145
+ type CredentialsListener interface {
146
+ OnNext(credentials Credentials) // Called when credentials are updated
147
+ OnError(err error) // Called when an error occurs
148
+ }
149
+
150
+ type Credentials interface {
151
+ BasicAuth() (username string, password string)
152
+ RawCredentials() string
153
+ }
131
154
` ` `
132
155
133
- The above can be modified to specify the version of the RESP protocol by adding the ` protocol `
134
- option to the ` Options ` struct:
156
+ Example usage:
157
+ ` ` ` go
158
+ rdb := redis.NewClient(&redis.Options{
159
+ Addr: "localhost:6379",
160
+ StreamingCredentialsProvider: &MyCredentialsProvider{},
161
+ })
162
+ ` ` `
135
163
164
+ **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.
165
+
166
+ Example with Entra ID :
136
167
` ` ` go
137
- rdb := redis.NewClient (&redis.Options {
138
- Addr : " localhost:6379" ,
139
- Password : " " , // no password set
140
- DB : 0 , // use default DB
141
- Protocol : 3 , // specify 2 for RESP 2 or 3 for RESP 3
142
- })
168
+ import (
169
+ "github.com/redis/go-redis/v9"
170
+ "github.com/redis-developer/go-redis-entraid"
171
+ )
172
+
173
+ // Create an Entra ID credentials provider
174
+ provider := entraid.NewDefaultAzureIdentityProvider()
175
+
176
+ // Configure Redis client with Entra ID authentication
177
+ rdb := redis.NewClient(&redis.Options{
178
+ Addr: "your-redis-server.redis.cache.windows.net:6380",
179
+ StreamingCredentialsProvider: provider,
180
+ TLSConfig: &tls.Config{
181
+ MinVersion: tls.VersionTLS12,
182
+ },
183
+ })
184
+ ` ` `
143
185
186
+ #### 2 . Context -based Credentials Provider
187
+
188
+ The context-based provider allows credentials to be determined at the time of each operation, using the context.
189
+
190
+ ` ` ` go
191
+ rdb := redis.NewClient(&redis.Options{
192
+ Addr: "localhost:6379",
193
+ CredentialsProviderContext: func(ctx context.Context) (string, string, error) {
194
+ // Return username, password, and any error
195
+ return "user", "pass", nil
196
+ },
197
+ })
198
+ ` ` `
199
+
200
+ #### 3 . Regular Credentials Provider
201
+
202
+ A simple function-based provider that returns static credentials.
203
+
204
+ ` ` ` go
205
+ rdb := redis.NewClient(&redis.Options{
206
+ Addr: "localhost:6379",
207
+ CredentialsProvider: func() (string, string) {
208
+ // Return username and password
209
+ return "user", "pass"
210
+ },
211
+ })
212
+ ` ` `
213
+
214
+ #### 4 . Username /Password Fields (Lowest Priority )
215
+
216
+ The most basic way to provide credentials is through the ` Username` and ` Password` fields in the options.
217
+
218
+ ` ` ` go
219
+ rdb := redis.NewClient(&redis.Options{
220
+ Addr: "localhost:6379",
221
+ Username: "user",
222
+ Password: "pass",
223
+ })
224
+ ` ` `
225
+
226
+ #### Priority Order
227
+
228
+ The client will use credentials in the following priority order:
229
+ 1 . Streaming Credentials Provider (if set)
230
+ 2 . Context -based Credentials Provider (if set)
231
+ 3 . Regular Credentials Provider (if set)
232
+ 4 . Username /Password fields (if set)
233
+
234
+ If none of these are set, the client will attempt to connect without authentication.
235
+
236
+ ### Protocol Version
237
+
238
+ The client supports both RESP2 and RESP3 protocols. You can specify the protocol version in the options:
239
+
240
+ ` ` ` go
241
+ rdb := redis.NewClient(&redis.Options{
242
+ Addr: "localhost:6379",
243
+ Password: "", // no password set
244
+ DB: 0, // use default DB
245
+ Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
246
+ })
144
247
` ` `
145
248
146
249
### Connecting via a redis url
0 commit comments