|
| 1 | +--- |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +title: "Authentication" |
| 5 | +description: "OIDC authentication, token refresh, and gateway auth configuration for the Go SDK." |
| 6 | +keywords: "Go SDK, Authentication, OIDC, Token Refresh, TLS, Security" |
| 7 | +--- |
| 8 | + |
| 9 | +The Go SDK supports multiple authentication modes depending on how the |
| 10 | +gateway is configured. The gateway client reads auth settings from the |
| 11 | +CLI configuration automatically. |
| 12 | + |
| 13 | +## Authentication Modes |
| 14 | + |
| 15 | +| Mode | When to Use | Configuration | |
| 16 | +|------|-------------|---------------| |
| 17 | +| **OIDC** | Production gateways with identity provider | `openshell gateway auth` configures tokens | |
| 18 | +| **API Key** | Simple deployments, development | Set via gateway config or `WithAuth` option | |
| 19 | +| **None** | Local development, insecure gateways | Default when no auth is configured | |
| 20 | + |
| 21 | +## Automatic Auth (Recommended) |
| 22 | + |
| 23 | +When using `gateway.NewClient()`, authentication is resolved automatically |
| 24 | +from the CLI configuration: |
| 25 | + |
| 26 | +```go |
| 27 | +// Auth is read from ~/.config/openshell/<gateway>/config.yaml |
| 28 | +client, err := gateway.NewClient("my-gateway") |
| 29 | +``` |
| 30 | + |
| 31 | +The CLI stores OIDC tokens after `openshell gateway auth`. The SDK reads |
| 32 | +these tokens and refreshes them transparently. |
| 33 | + |
| 34 | +## OIDC Token Refresh |
| 35 | + |
| 36 | +For OIDC-authenticated gateways, the SDK handles token refresh |
| 37 | +automatically. You can customize the refresh behavior: |
| 38 | + |
| 39 | +```go |
| 40 | +import "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/oidc" |
| 41 | + |
| 42 | +// The refresh wrapper handles token renewal before expiry. |
| 43 | +// Configure leeway to refresh tokens before they expire. |
| 44 | +refresher := v1.NewTokenRefresher(baseAuth, |
| 45 | + v1.WithLeeway(30 * time.Second), |
| 46 | + v1.WithLogger(myLogger), |
| 47 | +) |
| 48 | + |
| 49 | +client, err := gateway.NewClient("my-gateway", |
| 50 | + gateway.WithAuth(refresher), |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +## Custom Auth Provider |
| 55 | + |
| 56 | +For programmatic authentication, implement the `AuthProvider` interface |
| 57 | +or use the built-in providers: |
| 58 | + |
| 59 | +```go |
| 60 | +// Static API key |
| 61 | +client, err := gateway.NewClient("my-gateway", |
| 62 | + gateway.WithAuth(types.StaticToken("my-api-key")), |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +The `AuthProvider` interface provides gRPC per-RPC credentials. Each |
| 67 | +call attaches the token to the request metadata automatically. |
| 68 | + |
| 69 | +## TLS Configuration |
| 70 | + |
| 71 | +Control TLS settings when connecting to gateways: |
| 72 | + |
| 73 | +```go |
| 74 | +client, err := gateway.NewClient("my-gateway", |
| 75 | + gateway.WithTLS(&types.TLSConfig{ |
| 76 | + InsecureSkipVerify: false, |
| 77 | + CACertFile: "/path/to/ca.crt", |
| 78 | + }), |
| 79 | +) |
| 80 | +``` |
| 81 | + |
| 82 | +For local development with self-signed certificates, you can skip |
| 83 | +verification, but this should never be used in production. |
| 84 | + |
| 85 | +## Testing with Fakes |
| 86 | + |
| 87 | +The fake client does not require authentication. Use it in tests to |
| 88 | +avoid gateway dependencies: |
| 89 | + |
| 90 | +```go |
| 91 | +import "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/fake" |
| 92 | + |
| 93 | +func TestMyFeature(t *testing.T) { |
| 94 | + client := fake.NewClient() |
| 95 | + // Use client.Sandboxes(), client.Exec(), etc. |
| 96 | + // No gateway connection or auth needed. |
| 97 | +} |
| 98 | +``` |
0 commit comments