Skip to content

Commit a92b95b

Browse files
authored
feat(sdk/js): add OIDC auth support for TypeScript SDK (#1219)
* feat(sdk/js): add oauth authentication type Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com> * fix(sdk/ts): use milliseconds in cached token timestamps Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com> --------- Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
1 parent a3556ce commit a92b95b

File tree

7 files changed

+1314
-22
lines changed

7 files changed

+1314
-22
lines changed

sdk/dir-js/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,43 @@ const jwtTransport = await Client.createGRPCTransport(jwtConfig);
105105
const jwtClient = new Client(jwtConfig, jwtTransport);
106106
```
107107

108+
### OAuth 2.0 for Directory bearer auth
109+
110+
The SDK supports OIDC/OAuth for Directory bearer authentication on gRPC:
111+
112+
- Interactive login via Authorization Code + PKCE with a loopback callback (`authenticateOAuthPkce()`)
113+
- Pre-issued access token via `DIRECTORY_CLIENT_AUTH_TOKEN`
114+
115+
Interactive PKCE sessions are cached alongside other Directory tooling at `$XDG_CONFIG_HOME/dirctl/auth-token.json` or `~/.config/dirctl/auth-token.json`. Pre-issued tokens from configuration are used directly and are not written to the cache by the constructor.
116+
117+
Use this mode when your deployment expects a **Bearer access token** on gRPC (for example via a gateway that validates OIDC tokens). Register your IdP application with a **redirect URI** that matches `DIRECTORY_CLIENT_OIDC_REDIRECT_URI` exactly (for example `http://localhost:8484/callback`). The SDK starts a short-lived HTTP server on loopback to receive the authorization redirect.
118+
119+
Some IdPs use **public clients** with PKCE; your IdP may still expect a `client_secret` field in configuration. In that case, use a **random placeholder** from environment variables, not a real secret in source code.
120+
121+
**Important:** The default in-repo Envoy authorization stack validates **GitHub** tokens. OIDC access tokens from your IdP only work if your environment’s gateway or auth service is configured to accept them.
122+
123+
```bash
124+
export DIRECTORY_CLIENT_AUTH_MODE="oidc"
125+
export DIRECTORY_CLIENT_SERVER_ADDRESS="directory.example.com:443"
126+
export DIRECTORY_CLIENT_OIDC_ISSUER="https://your-idp-provider.example.com"
127+
export DIRECTORY_CLIENT_OIDC_CLIENT_ID="your-app-client-id"
128+
# Optional placeholder for public clients:
129+
export DIRECTORY_CLIENT_OIDC_CLIENT_SECRET="random-non-secret-string"
130+
export DIRECTORY_CLIENT_OIDC_REDIRECT_URI="http://localhost:8484/callback"
131+
# Optional: comma-separated scopes
132+
# export DIRECTORY_CLIENT_OIDC_SCOPES="openid,profile,email"
133+
```
134+
135+
```js
136+
import { Client } from 'agntcy-dir';
137+
138+
// After exporting the variables above (or building a Config with authMode: 'oidc'):
139+
const client = new Client();
140+
await client.authenticateOAuthPkce();
141+
```
142+
143+
For custom transports, call `Client.createGRPCTransport(oidcConfig, { oidcTokenHolder })` with an `OAuthTokenHolder` (exported from this package). The usual path is `new Client(oidcConfig)`, which wires the holder and transport automatically.
144+
108145
## Getting Started
109146

110147
### Prerequisites

0 commit comments

Comments
 (0)