Implement API endpoint to generate token
This commit is contained in:
parent
9fb2149488
commit
3ee6235787
7 changed files with 80 additions and 1 deletions
|
@ -55,6 +55,9 @@ library:
|
||||||
- containers
|
- containers
|
||||||
- base64-bytestring
|
- base64-bytestring
|
||||||
- wai
|
- wai
|
||||||
|
- blake3
|
||||||
|
- memory
|
||||||
|
- ghc-prim
|
||||||
|
|
||||||
executables:
|
executables:
|
||||||
zgo-backend-exe:
|
zgo-backend-exe:
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
module WooCommerce where
|
module WooCommerce where
|
||||||
|
|
||||||
|
import qualified BLAKE3 as BLK
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import qualified Data.Bson as B
|
import qualified Data.Bson as B
|
||||||
|
import qualified Data.ByteArray as BA
|
||||||
import qualified Data.ByteString as BS
|
import qualified Data.ByteString as BS
|
||||||
import qualified Data.ByteString.Base64 as B64
|
import qualified Data.ByteString.Base64 as B64
|
||||||
import qualified Data.ByteString.Char8 as C
|
import qualified Data.ByteString.Char8 as C
|
||||||
|
@ -14,6 +16,7 @@ import Data.Text.Encoding.Error (lenientDecode)
|
||||||
import Database.MongoDB
|
import Database.MongoDB
|
||||||
import Network.HTTP.Simple
|
import Network.HTTP.Simple
|
||||||
import Network.HTTP.Types.Status
|
import Network.HTTP.Types.Status
|
||||||
|
import Owner
|
||||||
|
|
||||||
-- | Type to represent the WooCommerce token
|
-- | Type to represent the WooCommerce token
|
||||||
data WooToken =
|
data WooToken =
|
||||||
|
@ -75,3 +78,23 @@ payWooOrder u i o t p z = do
|
||||||
if getResponseStatus res == ok200
|
if getResponseStatus res == ok200
|
||||||
then return ()
|
then return ()
|
||||||
else error "Failed to report payment to WooCommerce"
|
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"
|
||||||
|
|
|
@ -634,6 +634,15 @@ routes pipe config = do
|
||||||
, "token" .= w_token t
|
, "token" .= w_token t
|
||||||
, "siteurl" .= w_url 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
|
-- Authenticate the WooCommerce plugin
|
||||||
get "/auth" $ do
|
get "/auth" $ do
|
||||||
oid <- param "ownerid"
|
oid <- param "ownerid"
|
||||||
|
|
|
@ -44,6 +44,7 @@ packages:
|
||||||
extra-deps:
|
extra-deps:
|
||||||
- git: https://github.com/reach-sh/haskell-hexstring.git
|
- git: https://github.com/reach-sh/haskell-hexstring.git
|
||||||
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
|
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
|
||||||
|
- blake3-0.2@sha256:d1146b9a51ccfbb0532780778b6d016a614e3d44c05d8c1923dde9a8be869045,2448
|
||||||
# Override default flag values for local packages and extra-deps
|
# Override default flag values for local packages and extra-deps
|
||||||
# flags: {}
|
# flags: {}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,13 @@ packages:
|
||||||
original:
|
original:
|
||||||
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
|
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
|
||||||
git: https://github.com/reach-sh/haskell-hexstring.git
|
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:
|
snapshots:
|
||||||
- completed:
|
- completed:
|
||||||
sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4
|
sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4
|
||||||
|
|
35
test/Spec.hs
35
test/Spec.hs
|
@ -252,7 +252,13 @@ main = do
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` ok200
|
getResponseStatus res `shouldBe` ok200
|
||||||
describe "WooCommerce endpoints" $ do
|
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
|
it "authenticate with incorrect owner" $ do
|
||||||
req <-
|
req <-
|
||||||
testPublicGet
|
testPublicGet
|
||||||
|
@ -695,6 +701,33 @@ startAPI config = do
|
||||||
False
|
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"))
|
_ <- access pipe master "test" (Database.MongoDB.delete (select [] "owners"))
|
||||||
let o = val myOwner
|
let o = val myOwner
|
||||||
case o of
|
case o of
|
||||||
|
|
|
@ -46,14 +46,17 @@ library
|
||||||
, array
|
, array
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, base64-bytestring
|
, base64-bytestring
|
||||||
|
, blake3
|
||||||
, bson
|
, bson
|
||||||
, bytestring
|
, bytestring
|
||||||
, configurator
|
, configurator
|
||||||
, containers
|
, containers
|
||||||
|
, ghc-prim
|
||||||
, hexstring
|
, hexstring
|
||||||
, http-conduit
|
, http-conduit
|
||||||
, http-types
|
, http-types
|
||||||
, jwt
|
, jwt
|
||||||
|
, memory
|
||||||
, mongoDB
|
, mongoDB
|
||||||
, quickcheck-instances
|
, quickcheck-instances
|
||||||
, random
|
, random
|
||||||
|
|
Loading…
Reference in a new issue