packages feed

ec2-signature (empty) → 1.0

raw patch · 4 files changed

+162/−0 lines, 4 filesdep +SHAdep +basedep +base64-bytestringsetup-changed

Dependencies added: SHA, base, base64-bytestring, bytestring, containers, http-types

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Yusuke Nomura++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Yusuke Nomura nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Network/HTTP/Rest/Signature/EC2.hs view
@@ -0,0 +1,101 @@+module Network.HTTP.Rest.Signature.EC2+    ( -- * Types+      QueryString+    , Method+    , Endpoint+    , Path+    , SecretKey+    , SignatureMethod(..)+      -- * Building signature+    , toString+    , queryString+    , queryStringFromMap+    , stringToSign+    , stringToSign'+    , signature+    , signature'+    ) where++import Data.ByteString (ByteString)+import qualified Data.ByteString as BS+import Data.ByteString.Char8 ()+import qualified Data.ByteString.Lazy as BL+import qualified Data.ByteString.Base64 as Base64+import qualified Data.Digest.Pure.SHA as SHA+import Data.List+import Data.Map (Map)+import qualified Data.Map as Map+import Data.Monoid+import qualified Network.HTTP.Types as HTTP++-- | A query string for HTTP.+--+-- > "param1=value1&param2=value2"+data QueryString = QueryString { toString :: ByteString }+  deriving (Eq)++type Method = ByteString+type Endpoint = ByteString+type Path = ByteString+type SecretKey = ByteString+data SignatureMethod = HmacSHA256++-- | Convert a parameter list to 'QueryString'.+--+-- ex:+--+-- > queryString [("param1", "value1"), ("param2", "value2)] --> "param1=value1&param2=value2"+queryString :: [(ByteString, ByteString)] -> QueryString+queryString = queryString' . sort++-- | Convert a parameter map to 'QueryString'.+queryStringFromMap :: Map ByteString ByteString -> QueryString+queryStringFromMap = queryString' . Map.toList++queryString' :: [(ByteString, ByteString)] -> QueryString+queryString' = QueryString . BS.intercalate "&" . map concatWithEqual+  where+    concatWithEqual (key, val) = mconcat+        [ key+        , "="+        , HTTP.urlEncode True val+        ]++-- | Make a string for making signature.+--+-- ex:+--+-- > GET+-- > ec2.amazonaws.com+-- > /+-- > Action=DescribeInstances&AWSAccessKeyId=xxx&...+stringToSign :: Method -> Endpoint -> Path -> [(ByteString, ByteString)] -> ByteString+stringToSign method end path = stringToSign' method end path . queryString++stringToSign' :: Method -> Endpoint -> Path -> QueryString -> ByteString+stringToSign' method end path params = BS.intercalate "\n"+    [ method+    , end+    , path+    , toString params+    ]++-- | Make signature from a parameter list.+signature :: Method -> Endpoint -> Path -> SecretKey -> SignatureMethod -> [(ByteString, ByteString)] -> ByteString+signature method end path secret sigm+    = signature' method end path secret sigm . queryString++-- | Make signature from a 'QueryString'.+signature' :: Method -> Endpoint -> Path -> SecretKey -> SignatureMethod -> QueryString -> ByteString+signature' method end path secret sigm params+    = HTTP.urlEncode True+    $ Base64.encode+    $ signedString sigm+  where+    strToSign = stringToSign' method end path params+    signedString HmacSHA256+        = mconcat+        $ BL.toChunks+        $ SHA.bytestringDigest+        $ SHA.hmacSha256 (toL secret) (toL strToSign)+    toL = BL.fromChunks . (:[])
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ ec2-signature.cabal view
@@ -0,0 +1,29 @@+name:                ec2-signature+version:             1.0+synopsis:            The Amazon EC2 style signature calculator.+description:         The Amazon EC2 style signature calculator.+                     <http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html>+license:             BSD3+license-file:        LICENSE+author:              Yusuke Nomura<yunomu@gmail.com>+maintainer:          Yusuke Nomura<yunomu@gmail.com>+-- copyright:           +homepage:            https://github.com/worksap-ate/ec2-signature+category:            Network+build-type:          Simple+cabal-version:       >=1.8++library+  exposed-modules:   Network.HTTP.Rest.Signature.EC2+  extensions:        OverloadedStrings+  ghc-options:       -Wall -fno-warn-unused-do-bind+  build-depends:       base == 4.*+                     , bytestring+                     , base64-bytestring+                     , containers+                     , SHA+                     , http-types++source-repository head+    type:            git+    location:        git://github.com/yunomu/ec2-signature.git