Standard library only
Go has had crypto/ed25519 since Go 1.13, and encoding/base64 has a RawURLEncoding for base64url without padding, which is the encoding Clavem uses for the key, the payload and the signature. Nothing to add to go.mod. The code belongs in the Go backend of your Wails app, next to the validation client that calls it, so the webview never holds the token.
package license
import (
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"regexp"
"strings"
"time"
)
type Claims struct {
V int `json:"v"`
LID int64 `json:"lid"`
PID int64 `json:"pid"`
KH string `json:"kh"`
Status string `json:"status"`
Valid bool `json:"valid"`
IID string `json:"iid"`
FP string `json:"fp"`
Email string `json:"email"`
IAT int64 `json:"iat"`
EXP *int64 `json:"exp"` // nil for perpetual licenses
RefreshBy int64 `json:"refresh_by"`
}
var b64 = base64.RawURLEncoding // rejects "=" padding, accepts unpadded input
// VerifyToken returns the claims when the signature is valid, otherwise nil.
func VerifyToken(token, publicKey string) *Claims {
parts := strings.Split(token, ".")
if len(parts) != 3 || parts[0] != "sgt1" {
return nil
}
key, err1 := b64.DecodeString(publicKey)
sig, err2 := b64.DecodeString(parts[2])
body, err3 := b64.DecodeString(parts[1])
// ed25519.Verify panics when the key is not 32 bytes, so check both lengths first.
if err1 != nil || err2 != nil || err3 != nil ||
len(key) != ed25519.PublicKeySize || len(sig) != ed25519.SignatureSize {
return nil
}
if !ed25519.Verify(key, []byte(parts[0]+"."+parts[1]), sig) {
return nil
}
var c Claims
if json.Unmarshal(body, &c) != nil {
return nil
}
return &c
}
// Licensed applies the checks from the docs. A nil receiver is false.
func (c *Claims) Licensed(productID int64, now time.Time) bool {
if c == nil {
return false
}
t := now.Unix()
return c.V == 1 && c.PID == productID && c.Valid &&
(c.EXP == nil || *c.EXP > t) && c.RefreshBy > t
}
var nativeKey = regexp.MustCompile(`^[0-9A-Za-z]{5}(?:-[0-9A-Za-z]{5}){4}$`)
// MatchesKey is the optional check that the token was issued for the key the customer typed.
func (c *Claims) MatchesKey(licenseKey string) bool {
k := strings.TrimSpace(licenseKey)
if nativeKey.MatchString(k) {
k = strings.NewReplacer("I", "1", "L", "1", "O", "0", "U", "V").Replace(strings.ToUpper(k))
} else {
k = strings.ToLower(k)
}
sum := sha256.Sum256([]byte(k))
return hex.EncodeToString(sum[:])[:16] == c.KH
}
A method on a nil *Claims is legal in Go, so VerifyToken(t, key).Licensed(id, time.Now()) is safe when verification failed.
What I ran
I ran this on the Go Playground with a throwaway key pair and tokens in Clavem’s format. The real demo product’s public key is not available to me, so the demo token could not be verified.
| Token | Signature | Licensed | Reason |
|---|---|---|---|
| good | valid | yes | All checks pass |
| tampered | invalid | no | pid edited after signing |
| stale | valid | no | refresh_by has passed |
| expiredLicense | valid | no | valid is false |
| otherProduct | valid | no | pid is not yours |
| wrongVersion | valid | no | v is 2 |
| pastExp | valid | no | exp is in the past |
| wrongKey | invalid | no | Signed by another product’s key |
The garbage inputs not-a-token and sgt1.a.b with a 5-byte key returned nil. The kh check matched the demo key S1GNE-TDEM0-7K4QX-9WZ2P-H5RB8 against a token carrying its real kh, 7e0e4148136b6dae, also when typed in lowercase with spaces, and rejected another key.
Go traps
ed25519.Verifypanics on a bad key. In the playground, a 5-byte key produceded25519: bad public key length: 5. A panic skips your normal error handling, so checklen(key)andlen(sig)as above, and never pass an unvalidated key string through.RawURLEncodingis strict.YQ==failed withillegal base64 data at input byte 2andYQdecoded. If you ever paste a token through a tool that adds padding, strip the=first.- Optional
exp. A plainint64would read a missingexpas 0 and mark every perpetual license expired. Use*int64, as above. - Whole-number JSON. Unix seconds fit
int64exactly. Do not decode intofloat64and compare. - Clock set back. Save the highest
iatyou have seen next to the token. Ifnow + 5 minutes < maxIAT, treat the clock as untrustworthy and ask for an online validation. The five minutes absorb a slow clock.
Where the key and token live
Compile the public key into the binary as a constant, or set it at build time with -ldflags "-X". It is not a secret. The token goes in the keychain with the license key. A user who patches a Go binary can still skip the check; the signature stops forged tokens, not removed checks.
When refresh_by passes, Licensed returns false and the validation client asks the server again. See the validation guide.
Doing this with Clavem
Each product has its own Ed25519 key pair. The public half is on the product page and in GET /v1/admin/products. The token fields and the 14-day window are described in the offline verification docs.
Questions
Should the webview verify the token with Web Crypto?
No. Do it in Go. The webview version is outside your control, especially on Linux where it is the system's WebKitGTK, and JavaScript in a webview can be inspected and changed. Go's standard library behaves the same on every target.
Does verification need the network?
No. It uses only the public key compiled into your app and the token you stored. The network matters only when refresh_by has passed and the app has to validate online again.