Skip to content

OpenTDF Web Browser Client opentdf

This project is focused on providing web client support for the OpenTDF platform. This includes encrypting and decrypting TDF content, and some management tasks for ABAC.

OpenTDF Web Documentation

Usage

With Interceptors (Recommended)

Use interceptors to provide authentication. The SDK does not manage tokens — you bring your own auth.

import{authTokenInterceptor,OpenTDF}from'@opentdf/sdk';// Implementation varies by identity provider (e.g. Auth0, Keycloak, oidc-client-ts)asyncfunctiongetAccessToken(): Promise<string>{return'my-access-token';}constclient=newOpenTDF({interceptors: [authTokenInterceptor(getAccessToken)],platformUrl: 'https://platform.example.com',});// EncryptconstcipherText=awaitclient.createTDF({source: {type: 'stream',location: plainTextStream},autoconfigure: false,defaultKASEndpoint: 'https://platform.example.com/kas',});// Decryptconstreader=client.open({source: {type: 'stream',location: cipherText}});constclearText=awaitreader.decrypt();

The authTokenInterceptor takes a function that returns an access token. Your auth library handles token refresh, caching, etc.

For DPoP-bound tokens, use authTokenDPoPInterceptor:

import{authTokenDPoPInterceptor,OpenTDF}from'@opentdf/sdk';constdpopInterceptor=authTokenDPoPInterceptor({tokenProvider: getAccessToken,});constclient=newOpenTDF({interceptors: [dpopInterceptor],dpopKeys: dpopInterceptor.dpopKeys,platformUrl: 'https://platform.example.com',});

You can also write your own interceptor for full control over request headers:

import{typeInterceptor,OpenTDF}from'@opentdf/sdk';constmyInterceptor: Interceptor=(next)=>async(req)=>{req.header.set('Authorization',`Bearer ${awaitgetToken()}`);req.header.set('X-Custom-Header','value');returnnext(req);};constclient=newOpenTDF({interceptors: [myInterceptor],platformUrl: 'https://platform.example.com',});

With AuthProvider (Deprecated)

The AuthProvider pattern is still supported for backwards compatibility but is deprecated since 0.14.0.

import{AuthProviders,OpenTDF}from'@opentdf/sdk';constauthProvider=awaitAuthProviders.refreshAuthProvider({clientId: 'applicationNameFromIdP',exchange: 'refresh',refreshToken: 'refreshTokenValueFromIdP',oidcOrigin: 'http://localhost:65432/auth/realms/opentdf',});constclient=newOpenTDF({
authProvider,platformUrl: 'https://platform.example.com',defaultCreateOptions: {defaultKASEndpoint: 'http://localhost:65432/kas',},});

You can bridge an existing AuthProvider to the interceptor pattern using authProviderInterceptor:

import{AuthProviders,authProviderInterceptor,OpenTDF}from'@opentdf/sdk';constauthProvider=awaitAuthProviders.clientSecretAuthProvider({clientId: 'myClient',clientSecret: 'mySecret',oidcOrigin: 'http://localhost:65432/auth/realms/opentdf',exchange: 'client',});constclient=newOpenTDF({interceptors: [authProviderInterceptor(authProvider)],platformUrl: 'https://platform.example.com',});

Platform Client

The Platform Client provides an interface to interact with the OpenTDF platform's RPC services.

import{authTokenInterceptor}from'@opentdf/sdk';import{PlatformClient}from'@opentdf/sdk/platform';constplatform=newPlatformClient({interceptors: [authTokenInterceptor(getAccessToken)],platformUrl: 'https://platform.example.com',});// Fetch well-known configurationconstwellKnownResponse=awaitplatform.v1.wellknown.getWellKnownConfiguration({});console.log('Well-known configuration:',wellKnownResponse.configuration);// List policy attributesconstattributesResponse=awaitplatform.v1.attributes.listAttributes({});console.log('Policy Attributes:',attributesResponse.attributes);

Building and Testing

Makefile Commands

The project provides a Makefile to simplify common development tasks. Below are the available commands:

CommandDescription
makeBuilds and tests everything (default target).
make startBuilds all packages and starts the web application in development mode.
make ciInstalls dependencies and links the SDK package for all subprojects.
make iInstalls dependencies and links the SDK package for all subprojects (without clean install).
make cleanRemoves build artifacts, packed files, and node_modules directories.
make cliBuilds and packs the CLI tool.
make auditRuns npm audit for all packages except dev dependencies.
make formatRuns code formatting for all packages.
make lintRuns linter for all packages.
make testRuns tests for all packages.
make license-checkChecks license compliance for all packages.
make docGenerates documentation for the SDK.
make generate-platformRuns the platform code generation script.
make distCopies the SDK package to the root directory.

You can run any of these commands using make <command>.

Contribute

Prerequisites

Developing with this code requires a recent version of npm and node. We develop using nvm, which allows us to pin to the same version of npm easily.

Build

To check out, build, and validate your installation, and test the sample web application, you may:

nvm use
make test
make start