PrintToVeraeTimeStamp/cmd/verae-print/stampcore.go
Cursor Agent f96ade41bd
Ship the Mac menu bar as an app bundle and use one real stamp path.
The tray and AirDrop helper now live in Verae NetPrint.app so notifications can start, /api/stamp uses the same client as Print-Job and refuses mock receipts, and the default printer name includes the hostname with an mDNS rename when that name is taken.

Co-authored-by: George Lambert <marchon@gmail.com>
2026-10-05 02:09:48 +00:00

75 lines
2.5 KiB
Go

package main
import (
"fmt"
"time"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/auth"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/job"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/veraeapi"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/vlog"
)
// clientForChannel builds an API client from the stored credentials.
// Missing xVerae- key (and no JWT fallback) → errNoKey.
func clientForChannel(ch auth.Channel, lj vlog.Job) (*veraeapi.Client, auth.Channel, error) {
client := &veraeapi.Client{Channel: ch}
switch ch {
case auth.ChannelZapier:
tok, err := auth.LoadXVeraeToken()
if err != nil {
if jwt, jerr := auth.LoadVeraeJWT(); jerr == nil {
lj.Printf("auth: no xVerae- token, using verae channel (auth.properties)")
client.Channel, client.VeraeToken = auth.ChannelVerae, jwt
return client, auth.ChannelVerae, nil
}
return nil, ch, fmt.Errorf("%w (%v)", errNoKey, err)
}
client.XVeraeToken = tok
default:
tok, err := auth.LoadVeraeJWT()
if err != nil {
return nil, ch, fmt.Errorf("verae channel: %v", err)
}
client.VeraeToken = tok
}
return client, ch, nil
}
// stampWithClient is the real stamp used by the desktop driver, Print-Job, and
// POST /api/stamp. It hashes the file and calls the Verae client. It does not
// invent a receipt.
func stampWithClient(pdf, title, outDir, outPath string, lj vlog.Job) (job.Result, error) {
client, ch, err := clientForChannel(auth.PreferredChannel(), lj)
if err != nil {
return job.Result{}, err
}
client.HTTPDo = loggingDo(lj)
return job.Run(job.Options{
SourcePDF: pdf,
OutDir: outDir,
OutPath: outPath,
DocumentName: safeStem(title) + ".pdf",
Channel: ch,
Client: client,
})
}
// stampFile stamps one PDF with the current key (no holding / prompting).
func stampFile(pdf string, t stampTarget, lj vlog.Job) (job.Result, error) {
start := time.Now()
res, err := stampWithClient(pdf, t.Title, t.OutDir, t.OutPath, lj)
if err != nil {
return res, err
}
lj.Printf("stamp: OK in %s receipt=%s sha256=%s\n original=%q\n cert=%q\n json=%q",
time.Since(start).Round(time.Millisecond), res.ReceiptID, res.SHA256, res.OriginalPDF, res.CertificatePDF, res.ReceiptJSON)
return res, nil
}
func authReason(err error) string {
if veraeapi.IsAuthError(err) {
return "Verae did not accept your signing key (it may have expired or been revoked). Please sign in again."
}
return "No Verae signing key is set up for this user yet. Please sign in or create an account."
}