Files
hatchet/pkg/client/loader/token.go
abelanger5 77e5d2b77c feat(go-sdk): spawnWorkflow method and get up to speed with other sdks (#297)
* feat(go-sdk): spawnWorkflow method and get up to speed with other sdks

* fix: manual trigger example

* fix: linting errors

* fix: double serialization from go sdk

* fix: spawn workflow logic and procedural example

* test(e2e): add procedural test

* fix: panic in e2e test

* fix: e2e test preparation

* fix: api server url in test.yml

* fix: load test server url

* chore: make num children configurable

* address pr review
2024-03-29 14:07:39 -07:00

64 lines
1.3 KiB
Go

package loader
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
)
type tokenConf struct {
serverURL string
grpcBroadcastAddress string
tenantId string
}
func getConfFromJWT(token string) (*tokenConf, error) {
claims, err := extractClaimsFromJWT(token)
if err != nil {
return nil, err
}
serverURL, ok := claims["server_url"].(string)
if !ok {
return nil, fmt.Errorf("server_url claim not found")
}
grpcBroadcastAddress, ok := claims["grpc_broadcast_address"].(string)
if !ok {
return nil, fmt.Errorf("grpc_broadcast_address claim not found")
}
tenantId, ok := claims["sub"].(string)
if !ok {
return nil, fmt.Errorf("sub claim not found")
}
return &tokenConf{
serverURL: serverURL,
grpcBroadcastAddress: grpcBroadcastAddress,
tenantId: tenantId,
}, nil
}
func extractClaimsFromJWT(token string) (map[string]interface{}, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid token format")
}
claimsData, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
var claims map[string]interface{}
err = json.Unmarshal(claimsData, &claims)
if err != nil {
return nil, err
}
return claims, nil
}