From 3ee62357872e143d22f071ecfd72f7076cfe72fa Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 3 Jan 2023 13:00:24 -0600 Subject: [PATCH] Implement API endpoint to generate token --- package.yaml | 3 +++ src/WooCommerce.hs | 23 +++++++++++++++++++++++ src/ZGoBackend.hs | 9 +++++++++ stack.yaml | 1 + stack.yaml.lock | 7 +++++++ test/Spec.hs | 35 ++++++++++++++++++++++++++++++++++- zgo-backend.cabal | 3 +++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/package.yaml b/package.yaml index c996c29..a4fa9f5 100644 --- a/package.yaml +++ b/package.yaml @@ -55,6 +55,9 @@ library: - containers - base64-bytestring - wai + - blake3 + - memory + - ghc-prim executables: zgo-backend-exe: diff --git a/src/WooCommerce.hs b/src/WooCommerce.hs index e062468..a7b16b4 100644 --- a/src/WooCommerce.hs +++ b/src/WooCommerce.hs @@ -2,8 +2,10 @@ module WooCommerce where +import qualified BLAKE3 as BLK import Data.Aeson import qualified Data.Bson as B +import qualified Data.ByteArray as BA import qualified Data.ByteString as BS import qualified Data.ByteString.Base64 as B64 import qualified Data.ByteString.Char8 as C @@ -14,6 +16,7 @@ import Data.Text.Encoding.Error (lenientDecode) import Database.MongoDB import Network.HTTP.Simple import Network.HTTP.Types.Status +import Owner -- | Type to represent the WooCommerce token data WooToken = @@ -75,3 +78,23 @@ payWooOrder u i o t p z = do if getResponseStatus res == ok200 then return () else error "Failed to report payment to WooCommerce" + +generateWooToken :: Owner -> Action IO () +generateWooToken o = + case o_id o of + Just ownerid -> do + let tokenHash = + BLK.hash + [ BA.pack . BS.unpack . C.pack . T.unpack $ oname o <> oaddress o :: BA.Bytes + ] + let wooToken = + val $ + WooToken + Nothing + ownerid + (T.pack . show $ (tokenHash :: BLK.Digest BLK.DEFAULT_DIGEST_LEN)) + Nothing + case wooToken of + Doc wT -> insert_ "wootokens" wT + _ -> error "Couldn't create the WooCommerce token" + Nothing -> error "Bad owner id" diff --git a/src/ZGoBackend.hs b/src/ZGoBackend.hs index 4d2dc7b..78728e4 100644 --- a/src/ZGoBackend.hs +++ b/src/ZGoBackend.hs @@ -634,6 +634,15 @@ routes pipe config = do , "token" .= w_token t , "siteurl" .= w_url t ]) + post "/api/wootoken" $ do + oid <- param "ownerid" + res <- liftAndCatchIO $ run (findOwnerById oid) + let o1 = cast' . Doc =<< res + case o1 of + Nothing -> status noContent204 + Just o -> do + liftAndCatchIO $ run (generateWooToken o) + status accepted202 -- Authenticate the WooCommerce plugin get "/auth" $ do oid <- param "ownerid" diff --git a/stack.yaml b/stack.yaml index f1db236..27fad16 100644 --- a/stack.yaml +++ b/stack.yaml @@ -44,6 +44,7 @@ packages: extra-deps: - git: https://github.com/reach-sh/haskell-hexstring.git commit: 085c16fb21b9f856a435a3faab980e7e0b319341 + - blake3-0.2@sha256:d1146b9a51ccfbb0532780778b6d016a614e3d44c05d8c1923dde9a8be869045,2448 # Override default flag values for local packages and extra-deps # flags: {} diff --git a/stack.yaml.lock b/stack.yaml.lock index 972fc8c..ad88f96 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -15,6 +15,13 @@ packages: original: commit: 085c16fb21b9f856a435a3faab980e7e0b319341 git: https://github.com/reach-sh/haskell-hexstring.git +- completed: + hackage: blake3-0.2@sha256:d1146b9a51ccfbb0532780778b6d016a614e3d44c05d8c1923dde9a8be869045,2448 + pantry-tree: + sha256: 0264ef3e7919e7b0d668c4153f6ce0d88e6965626b52d9dfd2cafd70309501d3 + size: 1433 + original: + hackage: blake3-0.2@sha256:d1146b9a51ccfbb0532780778b6d016a614e3d44c05d8c1923dde9a8be869045,2448 snapshots: - completed: sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4 diff --git a/test/Spec.hs b/test/Spec.hs index 6e6a943..6d1137d 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -252,7 +252,13 @@ main = do res <- httpLBS req getResponseStatus res `shouldBe` ok200 describe "WooCommerce endpoints" $ do - it "generate token" pending + it "generate token" $ do + req <- + testPost + "/api/wootoken" + [("ownerid", Just "627ad3492b05a76be5000001")] + res <- httpLBS req + getResponseStatus res `shouldBe` accepted202 it "authenticate with incorrect owner" $ do req <- testPublicGet @@ -695,6 +701,33 @@ startAPI config = do False "" "" + let myOwner1 = + Owner + (Just (read "627ad3492b05a76be5000001")) + "zs1w6nkameazc5gujm69350syl5w8tgvyaphums3pw8eytzy5ym08x7dvskmykkatmwrucmgv3er8e" + "Test shop" + "usd" + False + 0 + False + 0 + "Bubba" + "Gibou" + "bubba@zgo.cash" + "1 Main St" + "Mpls" + "Minnesota" + "55401" + "" + "bubbarocks.io" + "United States" + True + False + False + (UTCTime (fromGregorian 2022 4 16) (secondsToDiffTime 0)) + False + "" + "" _ <- access pipe master "test" (Database.MongoDB.delete (select [] "owners")) let o = val myOwner case o of diff --git a/zgo-backend.cabal b/zgo-backend.cabal index 5698b56..34fee47 100644 --- a/zgo-backend.cabal +++ b/zgo-backend.cabal @@ -46,14 +46,17 @@ library , array , base >=4.7 && <5 , base64-bytestring + , blake3 , bson , bytestring , configurator , containers + , ghc-prim , hexstring , http-conduit , http-types , jwt + , memory , mongoDB , quickcheck-instances , random