Skip to content

Commit

Permalink
Remove dependency on cryptohash, use cryptonite instead. haskell#120
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrap committed Feb 28, 2018
1 parent 56349ef commit bcd1bca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
38 changes: 24 additions & 14 deletions Network/Wreq/Internal/AWS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Network.Wreq.Internal.AWS

import Control.Applicative ((<$>))
import Control.Lens ((%~), (^.), (&), to)
import Crypto.MAC (hmac, hmacGetDigest)
import Crypto.MAC.HMAC (HMAC (..), hmac, hmacGetDigest)
import Data.ByteString.Base16 as HEX (encode)
import Data.Byteable (toBytes)
import Data.ByteArray (convert)
import Data.Char (toLower)
import Data.List (sort)
import Data.Monoid ((<>))
Expand All @@ -21,9 +21,9 @@ import Data.Time.LocalTime (utc, utcToLocalTime)
import Network.HTTP.Types (parseSimpleQuery, urlEncode)
import Network.Wreq.Internal.Lens
import Network.Wreq.Internal.Types (AWSAuthVersion(..))
import qualified Crypto.Hash as CT (HMAC, SHA256)
import qualified Crypto.Hash.SHA256 as SHA256 (hash, hashlazy)
import qualified Crypto.Hash as CT (Digest, SHA256, hash, hashlazy)
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.CaseInsensitive as CI (original)
import qualified Data.HashSet as HashSet
import qualified Network.HTTP.Client as HTTP
Expand All @@ -44,6 +44,17 @@ signRequest :: AWSAuthVersion -> S.ByteString -> S.ByteString ->
Request -> IO Request
signRequest AWSv4 = signRequestV4

hexSha256Hash :: S.ByteString -> S.ByteString
hexSha256Hash dta =
let digest = CT.hash dta :: CT.Digest CT.SHA256
in S.pack (show digest)

hexSha256HashLazy :: L.ByteString -> S.ByteString
hexSha256HashLazy dta =
let digest = CT.hashlazy dta :: CT.Digest CT.SHA256
in S.pack (show digest)


signRequestV4 :: S.ByteString -> S.ByteString -> Request -> IO Request
signRequestV4 key secret request = do
!ts <- timestamp -- YYYYMMDDT242424Z, UTC based
Expand All @@ -55,7 +66,7 @@ signRequestV4 key secret request = do
date = S.takeWhile (/= 'T') ts -- YYYYMMDD
hashedPayload
| request ^. method `elem` ["POST", "PUT"] = payloadHash req
| otherwise = HEX.encode $ SHA256.hash ""
| otherwise = hexSha256Hash ""
-- add common v4 signing headers, service specific headers, and
-- drop tmp header and Runscope-Bucket-Auth header (if present).
req = request & requestHeaders %~
Expand Down Expand Up @@ -87,7 +98,7 @@ signRequestV4 key secret request = do
"AWS4-HMAC-SHA256"
, ts
, dateScope
, HEX.encode $ SHA256.hash canonicalReq
, hexSha256Hash canonicalReq
]
-- task 3, steps 1 and 2
let signature = ("AWS4" <> secret) &
Expand All @@ -113,16 +124,15 @@ signRequestV4 key secret request = do
timestamp = render <$> getCurrentTime
where render = S.pack . formatTime defaultTimeLocale "%Y%m%dT%H%M%SZ" .
utcToLocalTime utc -- UTC printable: YYYYMMDDTHHMMSSZ
hmac' s k = toBytes (hmacGetDigest h)
where h = hmac k s :: (CT.HMAC CT.SHA256)
hmac' :: S.ByteString -> S.ByteString -> S.ByteString
hmac' s k = convert (hmacGetDigest h)
where h = hmac k s :: (HMAC CT.SHA256)

payloadHash :: Request -> S.ByteString
payloadHash req =
case HTTP.requestBody req of
HTTP.RequestBodyBS bs ->
HEX.encode $ SHA256.hash bs
HTTP.RequestBodyLBS lbs ->
HEX.encode $ SHA256.hashlazy lbs
HTTP.RequestBodyBS bs -> hexSha256Hash bs
HTTP.RequestBodyLBS lbs -> hexSha256HashLazy lbs
_ -> error "addTmpPayloadHashHeader: unexpected request body type"

-- Per AWS documentation at:
Expand Down Expand Up @@ -179,5 +189,5 @@ removeRunscope hostname
| otherwise = hostname
where p1 "-" = "."
p1 other = other
p2 "--" = "-"
p2 other = other
p2 "--" = "-"
p2 other = other
4 changes: 2 additions & 2 deletions wreq.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ library
authenticate-oauth >= 1.5,
base >= 4.5 && < 5,
base16-bytestring,
byteable,
bytestring >= 0.9,
case-insensitive,
containers,
cryptohash,
cryptonite,
exceptions >= 0.5,
ghc-prim,
hashable,
Expand All @@ -118,6 +117,7 @@ library
http-types >= 0.8,
lens >= 4.5,
lens-aeson,
memory,
mime-types,
time-locale-compat,
template-haskell,
Expand Down

0 comments on commit bcd1bca

Please sign in to comment.