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
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/commerce/auth"
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/models/user"
|
|
)
|
|
|
|
func parseAuthHeader(fieldValue string) (string, string) {
|
|
method, encoded := splitAuthorization(fieldValue)
|
|
if method != "Basic" {
|
|
return method, encoded
|
|
}
|
|
|
|
bytes, _ := base64.StdEncoding.DecodeString(encoded)
|
|
credentials := strings.Split(string(bytes), ":")
|
|
return credentials[0], credentials[1]
|
|
}
|
|
|
|
func BasicAuth() zip.Handler {
|
|
realm := "Basic realm=" + strconv.Quote("Authorization Required")
|
|
|
|
return func(c *zip.Ctx) error {
|
|
email, password := parseAuthHeader(c.Header("Authorization"))
|
|
|
|
db := datastore.New(c.Context())
|
|
usr := user.New(db)
|
|
if err := usr.GetByEmail(email); err != nil {
|
|
c.SetHeader("WWW-Authenticate", realm)
|
|
log.Warn("Unable to get user with email '%v': %v", email, err, c)
|
|
return c.NoContent(401)
|
|
}
|
|
|
|
// Validate password
|
|
if !usr.ComparePassword(password) {
|
|
c.SetHeader("WWW-Authenticate", realm)
|
|
log.Warn("Invalid password for user: %v", usr, c)
|
|
return c.NoContent(401)
|
|
}
|
|
|
|
// Login user on session
|
|
auth.Login(c, usr)
|
|
return c.Next()
|
|
}
|
|
}
|