Files
tasks/common/nexus/callback_token.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

75 lines
2.1 KiB
Go

package nexus
import (
"encoding/base64"
"encoding/json"
tokenspb "github.com/hanzoai/tasks/api/token/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
const (
// Currently supported token version.
TokenVersion = 1
// Header key for the callback token in StartOperation requests.
CallbackTokenHeader = "Temporal-Callback-Token"
)
// CallbackToken contains an encoded NexusOperationCompletion message.
type CallbackToken struct {
// Token version - currently only [TokenVersion] is supported.
Version int `json:"v"`
// Encoded [tokenspb.NexusOperationCompletion].
Data string `json:"d"`
// More fields and encryption support will come later.
}
type CallbackTokenGenerator struct {
// In the future this will contain more fields such as encryption keys.
}
func NewCallbackTokenGenerator() *CallbackTokenGenerator {
return &CallbackTokenGenerator{}
}
func (g *CallbackTokenGenerator) Tokenize(completion *tokenspb.NexusOperationCompletion) (string, error) {
b, err := proto.Marshal(completion)
if err != nil {
return "", err
}
token := CallbackToken{
Version: TokenVersion,
Data: base64.URLEncoding.EncodeToString(b),
}
b, err = json.Marshal(token)
if err != nil {
return "", err
}
return string(b), nil
}
// DecodeCompletion decodes a callback token unwrapping the contained NexusOperationCompletion proto struct.
func (g *CallbackTokenGenerator) DecodeCompletion(token *CallbackToken) (*tokenspb.NexusOperationCompletion, error) {
plaintext, err := base64.URLEncoding.DecodeString(token.Data)
if err != nil {
return nil, err
}
completion := &tokenspb.NexusOperationCompletion{}
return completion, proto.Unmarshal(plaintext, completion)
}
// DecodeCallbackToken unmarshals the given token applying minimal data verification.
func DecodeCallbackToken(encoded string) (token *CallbackToken, err error) {
err = json.Unmarshal([]byte(encoded), &token)
if err != nil {
return nil, err
}
if token.Version != TokenVersion {
return nil, status.Errorf(codes.InvalidArgument, "unsupported token version: %d", token.Version)
}
return
}