-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for the new experimental API
- Loading branch information
Showing
4 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Main (main) where | ||
|
||
import Data.Proxy | ||
import Network.HTTP.Client (defaultManagerSettings, newManager) | ||
import Servant.API | ||
import Servant.Auth.Hmac.Crypto | ||
import Servant.Auth.Hmac.Secure | ||
import Servant.Client | ||
import qualified Servant.Client.Core as Client | ||
|
||
data MyUser | ||
= UserA | ||
| UserB | ||
deriving stock (Show) | ||
|
||
type MyApi = | ||
HmacAuthed MyUser :> "messages" :> ReqBody '[PlainText] String :> Post '[PlainText] String | ||
:<|> HmacAuthed MyUser :> "messages" :> Get '[PlainText] String | ||
|
||
myApi :: Proxy MyApi | ||
myApi = Proxy | ||
|
||
postMessage :: String -> HmacClientM MyUser String | ||
getMessages :: HmacClientM MyUser String | ||
(postMessage :<|> getMessages) = hmacClient myApi | ||
|
||
clientSideAuth :: HmacClientSideAuth MyUser | ||
clientSideAuth = | ||
HmacClientSideAuth | ||
{ hcsaSign = signSHA256 | ||
, hcsaUserRequest = \case | ||
UserA -> pure . Client.addHeader "X-User" ("user-a" :: String) | ||
UserB -> pure . Client.addHeader "X-User" ("user-b" :: String) | ||
, hcsaSecretKey = \case | ||
UserA -> SecretKey "User-A-Secret" | ||
UserB -> SecretKey "Secret-from-User-B" | ||
} | ||
|
||
main :: IO () | ||
main = do | ||
manager <- newManager defaultManagerSettings | ||
let clientEnv = mkClientEnv manager (BaseUrl Http "localhost" 8080 "") | ||
postResponse <- runHmacClient (postMessage "Hello!") clientEnv clientSideAuth UserA | ||
print postResponse | ||
getResponse <- runHmacClient getMessages clientEnv clientSideAuth UserB | ||
print getResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Main (main) where | ||
|
||
import Control.Monad.IO.Class | ||
import qualified Network.Wai as Wai | ||
import Network.Wai.Handler.Warp (run) | ||
import Servant | ||
import Servant.Auth.Hmac.Crypto | ||
import Servant.Auth.Hmac.Secure | ||
|
||
data MyUser | ||
= UserA | ||
| UserB | ||
deriving stock (Show) | ||
|
||
type MyApi = | ||
HmacAuthed MyUser :> "messages" :> ReqBody '[PlainText] String :> Post '[PlainText] String | ||
:<|> HmacAuthed MyUser :> "messages" :> Get '[PlainText] String | ||
|
||
myApi :: Proxy MyApi | ||
myApi = Proxy | ||
|
||
postMessage :: MyUser -> String -> Handler String | ||
postMessage usr msg = do | ||
liftIO . putStrLn $ "Message from " <> show usr <> ": " <> msg | ||
pure "Message printed!" | ||
|
||
getMessages :: MyUser -> Handler String | ||
getMessages usr = pure $ "Nothing else for " <> show usr | ||
|
||
myServer :: Server MyApi | ||
myServer = postMessage :<|> getMessages | ||
|
||
serverSideAuth :: HmacServerSideAuth MyUser | ||
serverSideAuth = | ||
HmacServerSideAuth | ||
{ hssaSign = signSHA256 | ||
, hssaIdentifyUser = \req -> case lookup "X-User" (Wai.requestHeaders req) of | ||
Just "user-a" -> pure $ Just UserA | ||
Just "user-b" -> pure $ Just UserB | ||
_ -> pure Nothing | ||
, hssaSecretKey = \case | ||
UserA -> SecretKey "User-A-Secret" | ||
UserB -> SecretKey "Secret-from-User-B" | ||
} | ||
|
||
myApp :: Application | ||
myApp = | ||
serveWithContext | ||
myApi | ||
(serverSideAuth :. EmptyContext) | ||
myServer | ||
|
||
main :: IO () | ||
main = run 8080 myApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters