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
43 lines
792 B
Go
43 lines
792 B
Go
package jwt
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
)
|
|
|
|
func Encode(claims Claimable, secret []byte, algorithm string) (string, error) {
|
|
var (
|
|
headerJson, jsonValue []byte
|
|
str, sig string
|
|
err error
|
|
)
|
|
|
|
method := jwt.GetSigningMethod(algorithm)
|
|
|
|
// Lookup signature method
|
|
if method == nil {
|
|
return "", UnspecifiedSigningMethod
|
|
}
|
|
|
|
header := Header{
|
|
Type: "JWT",
|
|
Algorithm: algorithm,
|
|
}
|
|
|
|
if headerJson, err = json.Marshal(header); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if jsonValue, err = json.Marshal(claims); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
str = jwt.EncodeSegment(headerJson) + "." + jwt.EncodeSegment(jsonValue)
|
|
|
|
if sig, err = method.Sign(str, secret); err != nil {
|
|
return "", err
|
|
}
|
|
return str + "." + sig, nil
|
|
}
|