Skip to content

Commit

Permalink
Add username support in auth command
Browse files Browse the repository at this point in the history
  • Loading branch information
qnikst committed May 31, 2023
1 parent 5f4c071 commit 9930c10
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Database/Redis/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
module Database.Redis.Commands (

-- ** Connection
auth, -- |Authenticate to the server (<http://redis.io/commands/auth>). Since Redis 1.0.0

-- *** auth
-- $auth
auth,
authOpts,
AuthOpts(..),
defaultAuthOpts,
-- *** Other commands
echo, -- |Echo the given string (<http://redis.io/commands/echo>). Since Redis 1.0.0
ping, -- |Ping the server (<http://redis.io/commands/ping>). Since Redis 1.0.0
quit, -- |Close the connection (<http://redis.io/commands/quit>). Since Redis 1.0.0
Expand Down Expand Up @@ -1167,3 +1174,7 @@ sismember key member = sendRequest (["SISMEMBER"] ++ [encode key] ++ [encode mem

-- $xgroupSetId
-- Sets last delivered ID for a consumer group. The redis command @XGROUP SETID@ is split up into 'xgroupSetId' and 'xgroupSetIdOpts' methods.


-- $auth
-- Authenticate to the server (<http://redis.io/commands/auth>). Since Redis 1.0.0
39 changes: 37 additions & 2 deletions src/Database/Redis/ManualCommands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,11 +1727,46 @@ xtrim stream opts = sendRequest $ ["XTRIM", stream] ++ internalTrimArgToList opt
inf :: RealFloat a => a
inf = 1 / 0

-- | Additional parameters for the auth command.
data AuthOpts = AuthOpts
{ authOptsUsername
:: Maybe ByteString
{- ^ Username.
Since Redis 6.0: fails on earlier
-}
}

-- | Default options for AuthOpts
--
-- >>> defaultAuthOpts
-- AuthOpts { authOptsUsername = Nothing }
defaultAuthOpts :: AuthOpts
defaultAuthOpts = AuthOpts
{ authOptsUsername = Nothing
}

-- | /O(N)/ where N is the number of passwords defined for the user.
--
-- Authenticates client to the server.
auth
:: RedisCtx m f
=> ByteString -- ^ password
=> ByteString -- ^ Password.
-> m (f Status)
auth password = authOpts password defaultAuthOpts

-- | /O(N)/ where N is the number of passwords defined for the user.
--
-- Authenticates client to the server.
--
-- This method allows passing additional options.
authOpts
:: RedisCtx m f
=> ByteString -- ^ Password.
-> AuthOpts -- ^ Additional options.
-> m (f Status)
auth password = sendRequest ["AUTH", password]
authOpts password AuthOpts{..} = sendRequest $
["AUTH"] <> maybe [] (:[]) authOptsUsername <> [password]

-- the select command. used in 'connect'.
select
Expand Down

0 comments on commit 9930c10

Please sign in to comment.