on master help sign in
Make auth options overwrite each other
commit4a8f53c30f67264160f53b65bb3cd3e76a7e78ec authormrkkrp <markkarpov@opmbx.org> dateSat 29 Oct 2016, 00:27 (10 years ago) parent21c052d
1 file changed+20−10
Network/HTTP/Req.hs+20−10
Network/HTTP/Req.hs+20−10
@@ -79,7 +79,6 @@
7979{-# LANGUAGE DeriveDataTypeable #-}
8080{-# LANGUAGE DeriveGeneric #-}
8181{-# LANGUAGE FlexibleInstances #-}
82−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
8382{-# LANGUAGE KindSignatures #-}
8483{-# LANGUAGE OverloadedStrings #-}
8584{-# LANGUAGE RecordWildCards #-}
@@ -583,11 +582,11 @@ withQueryParams f = Option (Endo (first f)) Nothing
583582withRequest :: (L.Request -> L.Request) -> Option scheme
584583withRequest f = Option (Endo (second f)) Nothing
585584
586−-- | A helper to create an 'Option' that adds a finalier (request
585+-- | A helper to create an 'Option' that adds a finalizer (request
587586-- endomorphism that is run after all other modifications).
588587
589−withFinalizer :: (L.Request -> L.Request) -> Option scheme
590−withFinalizer f = Option mempty (Just (Endo f))
588+asFinalizer :: (L.Request -> L.Request) -> Option scheme
589+asFinalizer f = Option mempty (Just (Endo f))
591590
592591instance RequestComponent (Option scheme) where
593592 getRequestMod (Option f finalizer) = Endo $ \x ->
@@ -648,7 +647,13 @@ header
648647 :: ByteString -- ^ Header name
649648 -> ByteString -- ^ Header value
650649 -> Option scheme
651−header name value = withRequest $ \x ->
650+header name value = withRequest (attachHeader name value)
651+
652+-- | A non-public helper that attaches a header with given name and content
653+-- to a 'L.Request'.
654+
655+attachHeader :: ByteString -> ByteString -> L.Request -> L.Request
656+attachHeader name value x =
652657 x { L.requestHeaders = (CI.mk name, value) : L.requestHeaders x }
653658
654659----------------------------------------------------------------------------
@@ -664,6 +669,9 @@ header name value = withRequest $ \x ->
664669-- TODO Something. You should always prefer authentication 'Option's to
665670-- manual construction of headers, etc., because it's a better style and
666671-- provides additional type safety that prevents leaking of credentials.
672+--
673+-- Mention that auth 'Option's overwrite each other unlike other 'Option's,
674+-- so several auth options are never active at the same time.
667675
668676-- | OAuth 1 authentication 'Option'.
669677
@@ -673,7 +681,7 @@ oAuth1
673681 -> ByteString -- ^ OAuth token
674682 -> ByteString -- ^ OAuth token secret
675683 -> Option scheme
676−oAuth1 ctoken csecret otoken osecret = withFinalizer
684+oAuth1 ctoken csecret otoken osecret = asFinalizer
677685 (OAuth1.signRequest ctoken csecret otoken osecret)
678686
679687-- | The 'Option' adds basic authentication. The 'Text' values will be UTF-8
@@ -685,7 +693,7 @@ basicAuth
685693 :: ByteString -- ^ Username
686694 -> ByteString -- ^ Password
687695 -> Option 'Https -- ^ Auth 'Option'
688−basicAuth username password = withRequest
696+basicAuth username password = asFinalizer
689697 (L.applyBasicAuth username password)
690698
691699-- | The 'Option' adds an OAuth2 bearer token. This is treated by many
@@ -698,7 +706,8 @@ basicAuth username password = withRequest
698706oAuth2Bearer
699707 :: ByteString -- ^ Token
700708 -> Option 'Https -- ^ Auth 'Option'
701−oAuth2Bearer token = header "Authorization" ("Bearer " <> token)
709+oAuth2Bearer token = asFinalizer
710+ (attachHeader "Authorization" ("Bearer " <> token))
702711
703712-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
704713-- to be used only by GitHub). This will be treated by whatever services
@@ -711,7 +720,8 @@ oAuth2Bearer token = header "Authorization" ("Bearer " <> token)
711720oAuth2Token
712721 :: ByteString -- ^ Token
713722 -> Option 'Https -- ^ Auth 'Option'
714−oAuth2Token token = header "Authorization" ("token" <> token)
723+oAuth2Token token = asFinalizer
724+ (attachHeader "Authorization" ("token" <> token))
715725
716726-- | The 'Option' adds AWS v4 request signature.
717727
@@ -719,7 +729,7 @@ awsAuth
719729 :: ByteString -- ^ AWS access key
720730 -> ByteString -- ^ AWS secret access key
721731 -> Option 'Https -- ^ Auth 'Option'
722−awsAuth accessKey secretKey = withFinalizer
732+awsAuth accessKey secretKey = asFinalizer
723733 (AWS.signRequest accessKey secretKey)
724734
725735----------------------------------------------------------------------------