Skip to content

Commit fb0358c

Browse files
chore(docs): Improve readme samples (#359)
- Uses newer 'options object' constructor parameter - Be more clear about what the auth providers are and how to select one, or write your own
1 parent 10ff5c7 commit fb0358c

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

README.md

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,81 @@ This project is focused on providing web client support for the OpenTDF platform
44
This includes encrypting and decrypting TDF content,
55
and some management tasks for ABAC.
66

7-
## Usage
7+
## Usage (NanoTDF)
88

99
```typescript
10-
// currently we support only ESM import
11-
import { AuthProviders, NanoTDFClient } from '@opentdf/client';
12-
13-
const oidcCredentials: RefreshTokenCredentials = {
14-
clientId: keycloakClientId,
15-
exchange: 'refresh',
16-
refreshToken: refreshToken,
17-
oidcOrigin: keycloakUrlWithRealm,
18-
}
19-
const authProvider = await AuthProviders.refreshAuthProvider(oidcCredentials);
20-
const client = new NanoTDFClient(authProvider, access);
21-
const cipherText = await client.encrypt(plainText);
22-
const clearText = await client.decrypt(cipherText);
10+
import { AuthProviders, NanoTDFClient } from '@opentdf/client';
11+
12+
// Configuration Options
13+
const kasEndpoint = "http://localhost:65432/kas";
14+
15+
// Authentication options (vary by middleware)
16+
const oidcOrigin = "http://localhost:65432/auth/realms/tdf";
17+
const clientId = "applicationNameFromIdP";
18+
const refreshToken = "refreshTokenValueFromIdP";
19+
20+
// AuthProviders are middlewares that add `Authorization` or other bearer tokens to requests.
21+
// These include The `refresh` provider can be handed a refresh and optional access token.
22+
const authProvider = await AuthProviders.refreshAuthProvider({
23+
clientId,
24+
exchange: 'refresh',
25+
refreshToken,
26+
oidcOrigin,
27+
});
28+
29+
const client = new NanoTDFClient({
30+
authProvider,
31+
kasEndpoint,
32+
});
33+
client.dataAttributes = ["http://opentdf.io/attr/class/value/secret"]
34+
const cipherText = await client.encrypt(plainText);
35+
const clearText = await client.decrypt(cipherText);
2336
```
2437

38+
### Authorization Middleware Options
39+
40+
#### Client Credentials
41+
42+
For long running server-side apps, a client id + secret is allowed with OAuth2.
43+
This should not be used in a browser, but within a Deno or Node process.
44+
45+
```typescript
46+
import { AuthProviders } from '@opentdf/client';
47+
48+
// Authentication options (vary by middleware)
49+
const oidcOrigin = "http://localhost:65432/auth/realms/tdf";
50+
const clientId = "username";
51+
const clientSecret = "IdP_GENERATED_SECRET";
52+
53+
const authProvider = await AuthProviders.clientSecretAuthProvider({
54+
clientId,
55+
clientSecret,
56+
oidcOrigin,
57+
exchange: 'client',
58+
});
59+
```
60+
61+
#### Given Credentials
62+
63+
The `refreshAuthProvider` and `externalAuthProvder` allow the application developer to use existing tokens.
64+
65+
```typescript
66+
import { AuthProviders, NanoTDFClient } from '@opentdf/client';
67+
68+
const oidcCredentials: RefreshTokenCredentials = {
69+
clientId: keycloakClientId,
70+
exchange: 'refresh',
71+
refreshToken: refreshToken,
72+
oidcOrigin: keycloakUrlWithRealm,
73+
}
74+
```
75+
76+
#### Building your own provider
77+
78+
A more complete example of using an OIDC compatible provider
79+
with support for authorization code flow with PKCE and DPoP
80+
is available in the [sample `web-app` folder](./web-app/src/session.ts)
81+
2582
## Build and Test
2683

2784
```shell
@@ -37,7 +94,7 @@ We develop using [nvm](https://github.com/nvm-sh/nvm#readme),
3794
which allows us to pin to the same version of `npm` easily.
3895

3996
- Install [nvm](https://github.com/nvm-sh/nvm#readme)
40-
- see https://github.com/nvm-sh/nvm#installing-and-updating
97+
- see <https://github.com/nvm-sh/nvm#installing-and-updating>
4198
- `nvm use` will install `npm` and `node`
4299

43100
[![Build](https://github.com/opentdf/client-web/actions/workflows/build.yaml/badge.svg)](https://github.com/opentdf/client-web/actions/workflows/build.yaml)

0 commit comments

Comments
 (0)