AtlatestRepositorysigil-oauth
sigil-oauth / treeREADME.md
1
# sigil-oauth3
OAuth 2.0 client library for Sigil — shared auth for YouTube, Twitch, Wise, and other API clients.5
## Modules7
### `(sigil oauth)` — Core OAuth2 client9
Records, authorization URL generation with PKCE, token exchange, token refresh, token validation, and provider presets.11
### `(sigil oauth store)` — Token persistence13
Save and load tokens as JSON files under `~/.config/sigil/<service>/tokens.json`.15
### `(sigil oauth server)` — Local callback server17
Temporary localhost HTTP server that captures the OAuth redirect, exchanges the authorization code for tokens, and shuts down.19
## Quick Start21
```scheme22
(import (sigil oauth)23
(sigil oauth store)24
(sigil oauth server))26
;; 1. Create a provider config27
(define config28
(oauth-google-config29
"your-client-id"30
"your-client-secret"31
'("https://www.googleapis.com/auth/youtube")))33
;; 2. Run the full authorization flow (opens browser, waits for callback)34
(define tokens35
(oauth-run-authorization-flow config (current-second)))37
;; 3. Save tokens for later38
(oauth-save-tokens "youtube" tokens)40
;; 4. Load tokens in a future session41
(define tokens (oauth-load-tokens "youtube"))43
;; 5. Ensure token is valid before making API calls (auto-refreshes if expired)44
(define tokens (oauth-ensure-valid-token config tokens (current-second)))45
```47
## Manual Flow49
For more control over the authorization flow:51
```scheme52
;; Generate state and PKCE53
(define state (oauth-generate-state))54
(define pkce (oauth-generate-pkce))56
;; Build the authorization URL57
(define auth-url58
(oauth-authorization-url config state #{ pkce: pkce }))60
;; Display URL for user to visit...61
;; Then start the callback server to capture the redirect:62
(define tokens63
(oauth-start-callback-server config state (current-second)64
#{ pkce-verifier: (oauth-pkce-verifier pkce) }))65
```67
## Provider Presets69
### Google / YouTube71
```scheme72
(oauth-google-config client-id client-secret scopes)73
(oauth-google-config client-id client-secret scopes #{ redirect-uri: "..." })74
```76
Sets `access_type=offline` and `prompt=consent` automatically to ensure refresh tokens are issued.78
### Twitch80
```scheme81
(oauth-twitch-config client-id client-secret scopes)82
(oauth-twitch-config client-id client-secret scopes #{ redirect-uri: "..." })83
```85
### Custom Provider87
```scheme88
(oauth-config89
client-id: "..."90
client-secret: "..."91
auth-url: "https://provider.com/oauth2/authorize"92
token-url: "https://provider.com/oauth2/token"93
scopes: '("scope1" "scope2")94
redirect-uri: "http://localhost:8085/callback"95
extra-params: #{ custom_param: "value" })96
```98
## Token Management100
```scheme101
;; Check if a token has expired (with 60s grace period)102
(oauth-token-expired? tokens (current-second))104
;; Custom grace period (300 seconds)105
(oauth-token-expired? tokens (current-second) 300)107
;; Refresh manually108
(define new-tokens109
(oauth-refresh-token config (oauth-tokens-refresh-token tokens) (current-second)))111
;; Auto-refresh: returns existing tokens if valid, refreshes if expired112
(define valid-tokens113
(oauth-ensure-valid-token config tokens (current-second)))115
;; Validate against a provider endpoint116
(oauth-validate-token tokens "https://id.twitch.tv/oauth2/validate")117
```119
## Token Storage121
Tokens are stored as JSON in `~/.config/sigil/<service-name>/tokens.json`:123
```scheme124
(oauth-save-tokens "youtube" tokens) ;; default path125
(oauth-save-tokens "youtube" tokens "/custom/path.json") ;; custom path127
(oauth-load-tokens "youtube") ;; returns oauth-tokens or #f128
(oauth-clear-tokens "youtube") ;; returns #t if deleted, #f if missing129
```131
## Dependencies133
- sigil-stdlib134
- sigil-http135
- sigil-json136
- sigil-log138
## Building140
```bash141
sigil deps install142
sigil build143
sigil test144
```