Skip to content

Smart backend services#46

Merged
spicyfalafel merged 5 commits intomasterfrom
smart-backend-services
Feb 2, 2026
Merged

Smart backend services#46
spicyfalafel merged 5 commits intomasterfrom
smart-backend-services

Conversation

@spicyfalafel
Copy link
Copy Markdown
Contributor

@spicyfalafel spicyfalafel commented Jan 28, 2026

Summary

Add SMART Backend Services authentication provider for server-to-server OAuth 2.0 authentication per HL7 spec.

Uses oauth4webapi library for OAuth 2.0 implementation.

Features

  • SmartBackendServicesAuthProvider - OAuth 2.0 client_credentials grant with JWT bearer assertion
  • CryptoKey - accepts Web Crypto API key (not PEM string)
  • Discovery via .well-known/smart-configuration endpoint
  • Token caching with proactive refresh before expiry (tokenExpirationBuffer)
  • Thundering herd prevention — concurrent token requests deduplicated via shared promise
  • Retry on 401 with fresh token (handles ReadableStream body via .tee())

Also includes

  • validateBaseUrl and mergeHeaders utilities extracted to shared utils.ts
  • Integration tests with real Aidbox (no mocks)

Usage

import { AidboxClient, SmartBackendServicesAuthProvider } from "@health-samurai/aidbox-client";

// Import or generate your private key using Web Crypto API
const privateKey = await crypto.subtle.generateKey(
  { name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-384" },
  true,
  ["sign", "verify"]
).then(kp => kp.privateKey);

const auth = new SmartBackendServicesAuthProvider({
  baseUrl: "https://fhir.example.com",
  clientId: "my-backend-service",
  privateKey: privateKey,      // CryptoKey from Web Crypto API
  keyId: "key-001",            // Must match kid in JWKS registered on server
  scope: "system/*.read",
  // tokenExpirationBuffer: 30,  // Optional: seconds before expiry to refresh (default: 30)
  // allowInsecureRequests: true, // Optional: allow HTTP for testing (default: false)
});

const client = new AidboxClient("https://fhir.example.com", auth);

Test plan

  • Integration tests with real Aidbox (no mocks)
  • Token acquisition and caching
  • FHIR operations via AidboxClient
  • 401 retry logic
  • Session management (establishSession, revokeSession)
  • Lint and typecheck pass

Documentation


Note

Medium Risk
Introduces new OAuth/token-handling logic (caching, retry, discovery) and changes request header/baseUrl validation behavior in auth providers; functional risk is moderate despite added integration coverage.

Overview
Adds a new SmartBackendServicesAuthProvider to @health-samurai/aidbox-client for SMART Backend Services (OAuth 2.0 client_credentials with private-key JWT), including OAuth discovery, token caching/refresh, concurrent token-request deduplication, and a single 401 retry.

Refactors existing auth providers to share new validateBaseUrl and mergeHeaders helpers, and updates docs/exports/dependencies (oauth4webapi) accordingly.

Expands integration testing: new end-to-end tests for SMART backend services (including on-the-fly keypair/JWKS setup in Aidbox), more deterministic FHIR HTTP tests with DB cleanup, and forces Vitest to run files sequentially; also updates .gitignore for local/dev artifacts.

Written by Cursor Bugbot for commit 017a933. This will update automatically on new commits. Configure here.

Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from 038f662 to fa9ccdc Compare January 28, 2026 12:21
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from bac954e to 3abb534 Compare January 28, 2026 14:15
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch 2 times, most recently from 5483056 to e1b35c2 Compare January 28, 2026 14:48
Comment thread packages/aidbox-client/src/utils.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch 2 times, most recently from 6cfdfb2 to 92890b2 Compare January 28, 2026 15:19
@spicyfalafel spicyfalafel requested a review from Aitem January 28, 2026 15:39
Copy link
Copy Markdown
Member

@Aitem Aitem left a comment

Choose a reason for hiding this comment

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

Use real smart app instead mock

Comment thread packages/aidbox-client/test/smart-backend-services.test.ts Outdated
@Aitem
Copy link
Copy Markdown
Member

Aitem commented Jan 28, 2026

Basically, by code organization, it's ok for me
@rublag-hs need you asist with cryprography part

Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from 3f1691b to 63920b9 Compare January 29, 2026 09:15
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
@spicyfalafel spicyfalafel marked this pull request as draft January 29, 2026 10:07
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from 96b5c79 to 11685f6 Compare January 29, 2026 11:38
spicyfalafel and others added 3 commits January 29, 2026 14:43
Implement SMART Backend Services authentication per HL7 spec:
- JWT-based client credentials flow with RS384 and ES384 signing
- Discovery via .well-known/smart-configuration endpoint
- Token caching with automatic refresh
- generateKeyPair() helper for RSA and EC key generation

Also includes:
- Export isAuthenticated from AuthProvider interface
- CI workflow for e2e tests with Aidbox
- vitest config for sequential test execution

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove mocks and test against real Aidbox instance:
- Integration tests for token acquisition and FHIR operations
- Tests for generateKeyPair() with RS384 and ES384
- Proper test isolation with table truncation in beforeAll

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from 11685f6 to 2ae913a Compare January 29, 2026 11:44
@spicyfalafel spicyfalafel marked this pull request as ready for review January 29, 2026 11:46
@spicyfalafel
Copy link
Copy Markdown
Contributor Author

@rublag-hs please take a look

@Aitem Aitem requested a review from rublag-hs January 29, 2026 12:35
Copy link
Copy Markdown
Collaborator

@rublag-hs rublag-hs left a comment

Choose a reason for hiding this comment

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

Are there any widespread libraries to interact with OAuth2/OIDC? Maybe we can use them instead of reinventing the wheel?

Comment thread packages/aidbox-client/src/types.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/utils.ts Outdated
Comment thread packages/aidbox-client/src/utils.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
@spicyfalafel spicyfalafel force-pushed the smart-backend-services branch from 0c8a2ef to 47f1bfa Compare January 29, 2026 18:22
Copy link
Copy Markdown
Member

@Aitem Aitem left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts Outdated
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/src/smart-backend-services.ts
Comment thread packages/aidbox-client/docker-compose.yaml Outdated
@spicyfalafel spicyfalafel merged commit ee665ea into master Feb 2, 2026
6 checks passed
@spicyfalafel spicyfalafel deleted the smart-backend-services branch February 2, 2026 11:59
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.

3 participants