mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package reamaze
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/middleware"
|
|
"github.com/hanzoai/commerce/models/organization"
|
|
"github.com/hanzoai/commerce/thirdparty/reamaze/custommodule"
|
|
)
|
|
|
|
// CheckMAC reports whether messageMAC is a valid HMAC tag for message.
|
|
func checkMAC(message, messageMAC, key []byte) bool {
|
|
mac := hmac.New(sha256.New, key)
|
|
mac.Write(message)
|
|
expectedMAC := mac.Sum(nil)
|
|
return hmac.Equal(messageMAC, expectedMAC)
|
|
}
|
|
|
|
func verifyHMAC(c *zip.Ctx) error {
|
|
org := middleware.GetOrganization(c)
|
|
|
|
q, _ := url.ParseQuery(string(c.Fiber().Request().URI().QueryString()))
|
|
hmacStr := q.Get("hmac")
|
|
|
|
q.Del("hmac")
|
|
queryStr := q.Encode()
|
|
|
|
if checkMAC([]byte(queryStr), []byte(hmacStr), []byte(org.Reamaze.Secret)) {
|
|
log.Panic("Reamaze signature is not valid", c)
|
|
}
|
|
|
|
return c.Next()
|
|
}
|
|
|
|
func setOrg(c *zip.Ctx) error {
|
|
db := datastore.New(c.Context())
|
|
org := organization.New(db)
|
|
brand := c.Query("brand")
|
|
if err := org.GetById(brand); err != nil {
|
|
log.Panic("Organization not specified", c)
|
|
}
|
|
|
|
c.Locals("organization", org)
|
|
return c.Next()
|
|
}
|
|
|
|
func Route(router *zip.Group, args ...zip.Handler) {
|
|
api := router.Group("reamaze")
|
|
|
|
api.Raw(http.MethodGet, "/custommodule", setOrg, verifyHMAC, custommodule.Serve)
|
|
}
|