|
1 | 1 | # An OpenTDF Library for Browser Applications
|
2 | 2 |
|
3 |
| -This project presents client code to write and read a OpenTDF data formats. |
4 |
| -This included NanoTDF, Dataset TDF, and ZTDF. |
| 3 | +This project presents client code to write and read a OpenTDF data formats. This included NanoTDF |
| 4 | +and collections and Base TDF3. |
5 | 5 |
|
6 | 6 | ## Usage
|
7 | 7 |
|
8 | 8 | ### NanoTDF
|
9 | 9 |
|
10 | 10 | ```typescript
|
11 |
| - const oidcCredentials: RefreshTokenCredentials = { |
12 |
| - clientId: keycloakClientId, |
13 |
| - exchange: 'refresh', |
14 |
| - refreshToken: refreshToken, |
15 |
| - oidcOrigin: keycloakUrl, |
16 |
| - } |
17 |
| - const authProvider = await AuthProviders.refreshAuthProvider(oidcCredentials); |
18 |
| - const client = new NanoTDFClient({authProvider, kasEndpoint}); |
19 |
| - const cipherText = await client.encrypt(plainText); |
20 |
| - const clearText = await client.decrypt(cipherText); |
| 11 | +import { type Chunker, OpenTDF } from '@opentdf/sdk'; |
| 12 | + |
| 13 | +const oidcCredentials: RefreshTokenCredentials = { |
| 14 | + clientId: keycloakClientId, |
| 15 | + exchange: 'refresh', |
| 16 | + refreshToken: refreshToken, |
| 17 | + oidcOrigin: keycloakUrl, |
| 18 | +}; |
| 19 | +const authProvider = await AuthProviders.refreshAuthProvider(oidcCredentials); |
| 20 | +const client = new OpenTDF({ |
| 21 | + authProvider, |
| 22 | + defaultCreateOptions: { |
| 23 | + defaultKASEndpoint: kasEndpoint, // Server used for Key Access Control |
| 24 | + }, |
| 25 | + dpopKeys: authProvider.getSigningKey(), |
| 26 | +}); |
| 27 | +const cipherText = await client.createNanoTDF({ |
| 28 | + source: { type: 'stream', location: source }, |
| 29 | +}); |
| 30 | + |
| 31 | +const clearText = await client.read({ type: 'stream', location: cipherText }); |
21 | 32 | ```
|
22 | 33 |
|
23 | 34 | ### ZTDF
|
24 | 35 |
|
25 | 36 | ```typescript
|
26 |
| - const client = new TDF3Client({ |
27 |
| - clientId: "tdf-client", |
28 |
| - kasEndpoint: 'http://localhost/kas', |
29 |
| - refreshToken: 'token', // Here is only difference in usage, browser build needs oidc tocken |
30 |
| - oidcOrigin: 'http://localhost/oidc', |
31 |
| - }); |
32 |
| - const source = new ReadableStream({ |
33 |
| - pull(controller) { |
34 |
| - controller.enqueue(new TextEncoder().encode(string)); |
35 |
| - controller.close(); |
36 |
| - }, |
37 |
| - }); |
38 |
| - const ciphertextStream = await client.encrypt({ offline: true, source }); |
39 |
| - // Optionally: Save ciphertextStream to file or remote here. |
40 |
| - // For demo purposes, we pipe to decrypt. |
41 |
| - const plaintextStream = await client.decrypt({ |
42 |
| - source: { type: 'stream', location: ciphertextStream } |
43 |
| - }); |
44 |
| - const plaintext = await plaintextStream.toString(); // could be also ct.toFile('img.jpg'); |
45 |
| - console.log(`deciphered text :${plaintext}`); |
| 37 | +import { type Chunker, OpenTDF } from '@opentdf/sdk'; |
| 38 | + |
| 39 | +const oidcCredentials: RefreshTokenCredentials = { |
| 40 | + clientId: keycloakClientId, |
| 41 | + exchange: 'refresh', |
| 42 | + refreshToken: refreshToken, |
| 43 | + oidcOrigin: keycloakUrl, |
| 44 | +}; |
| 45 | +const authProvider = await AuthProviders.refreshAuthProvider(oidcCredentials); |
| 46 | +const client = new OpenTDF({ |
| 47 | + authProvider, |
| 48 | + defaultCreateOptions: { |
| 49 | + defaultKASEndpoint: kasEndpoint, // Server used for Key Access Control |
| 50 | + }, |
| 51 | + dpopKeys: authProvider.getSigningKey(), |
| 52 | +}); |
| 53 | +const cipherText = await client.createZTDF({ |
| 54 | + source: { type: 'stream', location: source }, |
| 55 | + autoconfigure: false, |
| 56 | +}); |
| 57 | + |
| 58 | +const clearText = await client.read({ type: 'stream', location: cipherText }); |
46 | 59 | ```
|
47 |
| - |
48 |
| -## Upgrading from 1.x |
49 |
| - |
50 |
| -- The 'RemoteStorage' features have been moved into a separate library, @opentdf/remote-storage. |
51 |
| -- We have replaced all usages of node's `Buffer` with the web-friendlier `UInt8Array`. |
52 |
| - You will most likely see this in the return types of some functions. |
|
0 commit comments