module Github.Repos.Webhooks.Validate (
isValidPayload
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
import Crypto.Hash
import Data.Byteable (constEqBytes, toBytes)
import qualified Data.ByteString.Base16 as Hex
import qualified Data.ByteString.Char8 as BS
import Data.Monoid
isValidPayload
:: String
-> Maybe String
-> BS.ByteString
-> Bool
isValidPayload secret shaOpt payload = maybe False (constEqBytes sign) shaOptBS
where
shaOptBS = BS.pack <$> shaOpt
hexDigest = Hex.encode . toBytes . hmacGetDigest
hm = hmac (BS.pack secret) payload :: HMAC SHA1
sign = "sha1=" <> hexDigest hm