Files
OpenShell/sdk/go/openshell/v1/oidc/example_test.go
Roland HussandDrew Newberry 8dc55e21ee feat(sdk/go): complete Go SDK with domain clients, auth, and hardening (#2702)
* feat(sdk/go): complete Go SDK with domain clients, auth, and hardening

Add the Go SDK implementation covering all gateway RPC services with
domain-typed clients, comprehensive OIDC authentication flows, fake
test doubles, and proto converters.

Domain clients: Sandbox, Provider, Exec, File, TCP, SSH, Policy,
Profile, Health, Service, Config, Workspace, Inference, Refresh.
Each client validates inputs, resolves sandboxes by name, and converts
between domain types and proto at the boundary.

Auth: OIDC authorization code (PKCE), device code (RFC 8628), and
client credentials (RFC 6749 Section 4.4) flows with gateway config
auto-resolution. Token refresh with singleflight deduplication and
exponential backoff. Edge tunnel proxy for gRPC-over-WebSocket.

Gateway: On-disk gateway discovery with user/system directory
precedence, lazy token loading, and auth mode mapping.

Testing: In-memory fake client with deep-copy isolation, watch
broadcasting with filtering, and workspace-scoped object stores.
Bufconn-based gRPC tests for all domain clients.

Ref: #2044

Signed-off-by: Roland Huß <rhuss@redhat.com>

* feat(sdk/go): add gateway client options for logger, timeout, and retry

Add WithLogger, WithTimeout, and WithRetryPolicy options to the gateway
package's NewClient function. These forward the corresponding Config
fields (Logger, Timeout, RetryPolicy) through to the underlying SDK
client, giving callers full control over observability, connection
timeouts, and retry behavior when constructing clients from on-disk
gateway configurations.

Also adds RetryPolicy type to types package and extends Config with the
three new fields.

* fix(mise): sync sccache lock entry

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

---------

Signed-off-by: Roland Huß <rhuss@redhat.com>
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
Co-authored-by: Drew Newberry <anewberry@nvidia.com>
2026-08-13 19:15:21 +00:00

161 lines
3.5 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// These examples demonstrate OIDC package usage but are guarded from
// execution during `go test` because they require real network access
// and user interaction. The guard `if false` keeps the code type-checked
// by the compiler without executing during tests.
package oidc_test
import (
"context"
"fmt"
"log"
"time"
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/oidc"
)
func ExampleLogin_gateway() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
token, err := oidc.Login(ctx, "my-gateway")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Authenticated. Token expires at %s\n", token.Expiry.Format(time.RFC3339))
}
fmt.Println("ok")
// Output: ok
}
func ExampleLogin_standalone() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
token, err := oidc.Login(ctx, "",
oidc.WithIssuer("https://auth.example.com"),
oidc.WithClientID("my-app"),
oidc.WithInMemory(),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Access token: %s...\n", token.AccessToken[:10])
}
fmt.Println("ok")
// Output: ok
}
func ExampleLogin_keyboard() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
token, err := oidc.Login(ctx, "my-gateway",
oidc.WithKeyboardFlow(),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Authenticated via keyboard flow. Token type: %s\n", token.TokenType)
}
fmt.Println("ok")
// Output: ok
}
func ExampleDeviceLogin() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
token, err := oidc.DeviceLogin(ctx,
oidc.WithIssuer("https://auth.example.com"),
oidc.WithClientID("my-device-app"),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Device authorized. Token expires at %s\n", token.Expiry.Format(time.RFC3339))
}
fmt.Println("ok")
// Output: ok
}
func ExampleDeviceLogin_customDisplay() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
token, err := oidc.DeviceLogin(ctx,
oidc.WithIssuer("https://auth.example.com"),
oidc.WithClientID("my-tui-app"),
oidc.WithDisplayFunc(func(verificationURL, userCode string) {
fmt.Printf("Please visit: %s\n", verificationURL)
fmt.Printf("Enter code: %s\n", userCode)
}),
)
if err != nil {
log.Fatal(err)
}
_ = token
}
fmt.Println("ok")
// Output: ok
}
func ExampleClientCredentials() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
token, err := oidc.ClientCredentials(ctx,
oidc.WithIssuer("https://auth.example.com"),
oidc.WithClientID("my-service"),
oidc.WithClientSecret("service-secret"),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Service authenticated. Token type: %s\n", token.TokenType)
}
fmt.Println("ok")
// Output: ok
}
func ExampleClientCredentials_gateway() {
if false {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
token, err := oidc.ClientCredentials(ctx,
oidc.WithGateway("my-gateway"),
oidc.WithClientSecret("service-secret"),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Service authenticated via gateway. Token type: %s\n", token.TokenType)
}
fmt.Println("ok")
// Output: ok
}