Skip to content

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

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#291
samsternberg wants to merge 1 commit intoskyflowapi:mainfrom
samsternberg:feat/ctx-json-object-support

Conversation

@samsternberg
Copy link
Copy Markdown

Summary

  • Extends bearer token and signed data token generation to accept Map<String, Object> 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 key validation: map keys must match [a-zA-Z0-9_]+ for CEL compatibility
  • JJWT's .claim(String, Object) natively serializes Map as a JSON object in the JWT payload — no custom serialization needed
  • Updated Credentials.setContext(), BearerTokenBuilder.setCtx(), SignedDataTokensBuilder.setCtx() with overloaded methods
  • New error messages: InvalidContextType, InvalidContextMapKey
  • 51 tests passing (including new Map-based context tests)
  • Updated README and samples with both string and object ctx patterns

Test plan

  • mvn test — 51 tests pass (BearerTokenTests, SignedDataTokensTests, CredentialsTests)
  • Integration test with Skyflow backend using Map ctx in bearer token
  • Verify JWT payload contains ctx as JSON object (not stringified)

Resolves: SK-2679, DOCU-1438

🤖 Generated with Claude Code

…d data token generation

Extend the Java SDK's bearer token and signed data token generation to accept
a JSON object (Map<String, Object>) 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:
- Credentials: widen context field from String to Object, add overloaded
  setContext(Map<String, Object>)
- BearerToken/SignedDataTokens: widen ctx to Object, add overloaded
  setCtx(Map<String, Object>), conditionally include ctx in JWT claims
- Utils: dispatch to correct setCtx overload based on context type
- Validations: validate both String and Map context types, validate map keys
  match [a-zA-Z0-9_]+ for CEL compatibility
- ErrorMessage/ErrorLogs: add InvalidContextType and InvalidContextMapKey
- Tests: add Map-based context tests for Credentials, BearerToken, and
  SignedDataTokens (51 tests, all passing)
- Samples: add JSON object context examples
- README: document both string and object ctx patterns with CEL policy
  variable mapping

Technical note: JJWT's .claim(String, Object) handles both types — String
serializes as a JSON string, Map serializes as a JSON object in the JWT
payload. No custom serialization needed.

Resolves: SK-2679, DOCU-1438

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant