Skip to content

feat: add JSON object support for ctx in token generation#176

Open
samsternberg wants to merge 1 commit intoskyflowapi:mainfrom
samsternberg:feat/ctx-json-object-support
Open

feat: add JSON object support for ctx in token generation#176
samsternberg wants to merge 1 commit intoskyflowapi:mainfrom
samsternberg:feat/ctx-json-object-support

Conversation

@samsternberg
Copy link
Copy Markdown
Collaborator

Summary

  • Extends bearer token and signed data token generation to accept map[string]interface{} for the Ctx field, in addition to the existing string type
  • Enables structured context for conditional data access policies where ctx object keys map to Skyflow CEL policy variables (request.context.role, request.context.department, etc.)
  • Adds ValidateAndResolveCtx() helper with key validation (^[a-zA-Z0-9_]+$ for CEL compatibility)
  • golang-jwt/jwt/v4's MapClaims natively serializes map[string]interface{} as a JSON object in JWT payload
  • Widened Ctx fields on BearerTokenOptions, SignedDataTokensOptions, and Credentials.Context to interface{}
  • New error messages: INVALID_CTX_TYPE, INVALID_CTX_MAP_KEY, EMPTY_CONTEXT
  • No test regressions (59 passed / 12 failed — same as main branch)
  • Updated README and samples with both string and map ctx patterns

Test plan

  • go test ./internal/helpers/... — no regressions vs main (59 pass / 12 pre-existing fail)
  • go test ./serviceaccount/... — no regressions vs main (14 pass / 3 pre-existing fail)
  • go build ./... — compiles clean
  • Integration test with Skyflow backend using map ctx in bearer token

Resolves: SK-2680, DOCU-1439

🤖 Generated with Claude Code

…d data token generation

Extend the Go SDK's bearer token and signed data token generation to accept
a map[string]interface{} for the Ctx field, in addition to the existing
string type. This enables structured context for conditional data access
policies where ctx object keys map to Skyflow CEL policy variables
(e.g., request.context.role, request.context.department).

Changes:
- common.go: widen Ctx fields to interface{} on BearerTokenOptions,
  SignedDataTokensOptions, and Credentials.Context
- helpers.go: add ValidateAndResolveCtx() function with key validation
  (^[a-zA-Z0-9_]+$), update both token generation paths to use it
- vault_controller.go: update nil check for Context (was != "")
- message.go: add EMPTY_CONTEXT, INVALID_CTX_TYPE, INVALID_CTX_MAP_KEY
- Samples: add JSON object context example
- README: document both string and map ctx patterns with CEL policy
  variable mapping

Technical note: golang-jwt/jwt/v4's MapClaims automatically serializes
map[string]interface{} as a JSON object in the JWT payload. No custom
serialization needed.

No test regressions (59 passed / 12 failed — same as main branch).

Resolves: SK-2680, DOCU-1439

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@saileshwar-skyflow
Copy link
Copy Markdown
Collaborator

Unit tests are missing for ctx as object

}
// ValidateAndResolveCtx validates the ctx value and returns the resolved value for JWT claims.
// Returns (nil, nil) if ctx should be omitted, (value, nil) if valid, or (nil, error) if invalid.
var ctxKeyPattern = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use constants instead hard-coded strings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants