on master help sign in
Make auth options overwrite each other
4a8f53cmrkkrp
Network/HTTP/Req.hs+20−10
@@ -79,7 +79,6 @@
79{-# LANGUAGE DeriveDataTypeable #-}
79{-# LANGUAGE DeriveDataTypeable #-}
80{-# LANGUAGE DeriveGeneric #-}
80{-# LANGUAGE DeriveGeneric #-}
81{-# LANGUAGE FlexibleInstances #-}
81{-# LANGUAGE FlexibleInstances #-}
82−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
83{-# LANGUAGE KindSignatures #-}
82{-# LANGUAGE KindSignatures #-}
84{-# LANGUAGE OverloadedStrings #-}
83{-# LANGUAGE OverloadedStrings #-}
85{-# LANGUAGE RecordWildCards #-}
84{-# LANGUAGE RecordWildCards #-}
@@ -583,11 +582,11 @@ withQueryParams f = Option (Endo (first f)) Nothing
583withRequest :: (L.Request -> L.Request) -> Option scheme
582withRequest :: (L.Request -> L.Request) -> Option scheme
584withRequest f = Option (Endo (second f)) Nothing
583withRequest f = Option (Endo (second f)) Nothing
585
584
586−-- | A helper to create an 'Option' that adds a finalier (request
585+-- | A helper to create an 'Option' that adds a finalizer (request
587-- endomorphism that is run after all other modifications).
586-- endomorphism that is run after all other modifications).
588
587
589−withFinalizer :: (L.Request -> L.Request) -> Option scheme
588+asFinalizer :: (L.Request -> L.Request) -> Option scheme
590−withFinalizer f = Option mempty (Just (Endo f))
589+asFinalizer f = Option mempty (Just (Endo f))
591
590
592instance RequestComponent (Option scheme) where
591instance RequestComponent (Option scheme) where
593 getRequestMod (Option f finalizer) = Endo $ \x ->
592 getRequestMod (Option f finalizer) = Endo $ \x ->
@@ -648,7 +647,13 @@ header
648 :: ByteString -- ^ Header name
647 :: ByteString -- ^ Header name
649 -> ByteString -- ^ Header value
648 -> ByteString -- ^ Header value
650 -> Option scheme
649 -> 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 =
652 x { L.requestHeaders = (CI.mk name, value) : L.requestHeaders x }
657 x { L.requestHeaders = (CI.mk name, value) : L.requestHeaders x }
653
658
654----------------------------------------------------------------------------
659----------------------------------------------------------------------------
@@ -664,6 +669,9 @@ header name value = withRequest $ \x ->
664-- TODO Something. You should always prefer authentication 'Option's to
669-- TODO Something. You should always prefer authentication 'Option's to
665-- manual construction of headers, etc., because it's a better style and
670-- manual construction of headers, etc., because it's a better style and
666-- provides additional type safety that prevents leaking of credentials.
671-- 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.
667
675
668-- | OAuth 1 authentication 'Option'.
676-- | OAuth 1 authentication 'Option'.
669
677
@@ -673,7 +681,7 @@ oAuth1
673 -> ByteString -- ^ OAuth token
681 -> ByteString -- ^ OAuth token
674 -> ByteString -- ^ OAuth token secret
682 -> ByteString -- ^ OAuth token secret
675 -> Option scheme
683 -> Option scheme
676−oAuth1 ctoken csecret otoken osecret = withFinalizer
684+oAuth1 ctoken csecret otoken osecret = asFinalizer
677 (OAuth1.signRequest ctoken csecret otoken osecret)
685 (OAuth1.signRequest ctoken csecret otoken osecret)
678
686
679-- | The 'Option' adds basic authentication. The 'Text' values will be UTF-8
687-- | The 'Option' adds basic authentication. The 'Text' values will be UTF-8
@@ -685,7 +693,7 @@ basicAuth
685 :: ByteString -- ^ Username
693 :: ByteString -- ^ Username
686 -> ByteString -- ^ Password
694 -> ByteString -- ^ Password
687 -> Option 'Https -- ^ Auth 'Option'
695 -> Option 'Https -- ^ Auth 'Option'
688−basicAuth username password = withRequest
696+basicAuth username password = asFinalizer
689 (L.applyBasicAuth username password)
697 (L.applyBasicAuth username password)
690
698
691-- | The 'Option' adds an OAuth2 bearer token. This is treated by many
699-- | The 'Option' adds an OAuth2 bearer token. This is treated by many
@@ -698,7 +706,8 @@ basicAuth username password = withRequest
698oAuth2Bearer
706oAuth2Bearer
699 :: ByteString -- ^ Token
707 :: ByteString -- ^ Token
700 -> Option 'Https -- ^ Auth 'Option'
708 -> Option 'Https -- ^ Auth 'Option'
709+oAuth2Bearer token = asFinalizer
701−oAuth2Bearer token = header "Authorization" ("Bearer " <> token)
710+ (attachHeader "Authorization" ("Bearer " <> token))
702
711
703-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
712-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
704-- to be used only by GitHub). This will be treated by whatever services
713-- to be used only by GitHub). This will be treated by whatever services
@@ -711,7 +720,8 @@ oAuth2Bearer token = header "Authorization" ("Bearer " <> token)
711oAuth2Token
720oAuth2Token
712 :: ByteString -- ^ Token
721 :: ByteString -- ^ Token
713 -> Option 'Https -- ^ Auth 'Option'
722 -> Option 'Https -- ^ Auth 'Option'
723+oAuth2Token token = asFinalizer
714−oAuth2Token token = header "Authorization" ("token" <> token)
724+ (attachHeader "Authorization" ("token" <> token))
715
725
716-- | The 'Option' adds AWS v4 request signature.
726-- | The 'Option' adds AWS v4 request signature.
717
727
@@ -719,7 +729,7 @@ awsAuth
719 :: ByteString -- ^ AWS access key
729 :: ByteString -- ^ AWS access key
720 -> ByteString -- ^ AWS secret access key
730 -> ByteString -- ^ AWS secret access key
721 -> Option 'Https -- ^ Auth 'Option'
731 -> Option 'Https -- ^ Auth 'Option'
722−awsAuth accessKey secretKey = withFinalizer
732+awsAuth accessKey secretKey = asFinalizer
723 (AWS.signRequest accessKey secretKey)
733 (AWS.signRequest accessKey secretKey)
724
734
725----------------------------------------------------------------------------
735----------------------------------------------------------------------------