Documentation
¶
Overview ¶
Package sourceoauth holds the OAuth2 machinery shared by the Google Drive and OneDrive sources: the local-callback authorization-code flow and a token source that persists refreshed tokens.
It lives under internal/ because it is an implementation detail of those two providers, not part of the source contract in pkg/source.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( DefaultGoogleClientID string DefaultGoogleClientSecret string DefaultOneDriveClientID string )
Default OAuth client credentials, injected at build time via ldflags:
-X github.com/cloudstic/cli/internal/sourceoauth.DefaultGoogleClientID=... -X github.com/cloudstic/cli/internal/sourceoauth.DefaultGoogleClientSecret=... -X github.com/cloudstic/cli/internal/sourceoauth.DefaultOneDriveClientID=...
These paths are mirrored in .goreleaser.yml. Keep the two in sync: the Go linker silently ignores -X for a symbol that does not exist, so a stale path yields release binaries with empty client IDs and cloud auth that fails only at runtime.
Users can still override at runtime via environment variables (GOOGLE_APPLICATION_CREDENTIALS, ONEDRIVE_CLIENT_ID).
OneDrive uses the public client flow (PKCE) and does not need a client secret.
Functions ¶
func ExchangeWithLocalServer ¶
func ExchangeWithLocalServer(ctx context.Context, config *oauth2.Config, authCodeOpts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
ExchangeWithLocalServer performs the OAuth2 authorization code flow by spinning up a temporary local HTTP server to receive the callback. It opens the user's default browser to the consent page and automatically captures the authorization code, eliminating the need to copy-paste.
If the browser cannot be opened, the auth URL is printed so the user can navigate to it manually; the local server still captures the redirect.
ctx governs the wait. This is the whole reason it is a parameter: the flow blocks until a human finishes authorizing in a browser, which they may never do, and the cloudstic CLI installs a SIGINT handler that cancels a context rather than killing the process — so a wait that ignored ctx could not be interrupted with Ctrl+C at all, and pressing it again would not help either. Cancellation returns ctx.Err() promptly and shuts the local server down.
func NewPersistentTokenSource ¶
func NewPersistentTokenSource(ts oauth2.TokenSource, lastTok *oauth2.Token, save func(*oauth2.Token) error, log *logger.Logger) oauth2.TokenSource
NewPersistentTokenSource returns a TokenSource that delegates to ts and calls save whenever it yields an access token different from lastTok, so a refreshed token is written back to wherever it came from. A save error is logged and swallowed: the token is still valid for the current session.
lastTok is the token already on disk; pass it so the first call does not re-save an unchanged token.
Types ¶
This section is empty.