feat: add atproto OAuth login and token commands
New internal/atproto package implements the atproto OAuth profile as a public client: handle/DID resolution with bidirectional verification, PDS and authorization-server metadata discovery, PAR + PKCE, ES256 DPoP proofs with server-nonce retry, and token exchange/refresh with sub verification. Sessions (tokens + DPoP key) are saved 0600. 'latha-auth atproto' collects the code via the existing deploy relay; 'latha-auth atproto-token' prints a refreshed access token.
This commit is contained in:
1 parent
653da47753
commit
fb79db4e21
9 files changed
+1531
-6
No files matched your search
@@ -0,0 +1,154 @@
|
||||
package atproto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const DefaultSessionPath = "~/.config/latha-auth/atproto-session.json"
|
||||
|
||||
// Session is the persisted OAuth session. It contains the DPoP private
|
||||
// key, so the file must stay 0600: anyone holding it holds the session.
|
||||
type Session struct {
|
||||
DID string `json:"did"`
|
||||
Handle string `json:"handle,omitempty"`
|
||||
PDS string `json:"pds"`
|
||||
Issuer string `json:"issuer"`
|
||||
ClientID string `json:"client_id"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
TokenType string `json:"token_type"`
|
||||
Scope string `json:"scope"`
|
||||
ObtainedAt string `json:"obtained_at"`
|
||||
ExpiresAt string `json:"expires_at,omitempty"`
|
||||
DPoPKey JWK `json:"dpop_key"`
|
||||
}
|
||||
|
||||
// TokenInfo is the non-secret-bearing view of a session for display to
|
||||
// DPoP-aware consumers. It omits the refresh token and DPoP key.
|
||||
type TokenInfo struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
DID string `json:"did"`
|
||||
Handle string `json:"handle,omitempty"`
|
||||
PDS string `json:"pds"`
|
||||
Issuer string `json:"issuer"`
|
||||
Scope string `json:"scope"`
|
||||
ExpiresAt string `json:"expires_at,omitempty"`
|
||||
}
|
||||
|
||||
func NewSession(id *ResolvedIdentity, clientID string, tok *TokenResponse, key *DPoPKey) *Session {
|
||||
s := &Session{
|
||||
DID: id.DID,
|
||||
Handle: id.Handle,
|
||||
PDS: id.PDS,
|
||||
Issuer: id.Issuer,
|
||||
ClientID: clientID,
|
||||
DPoPKey: key.PrivateJWK(),
|
||||
}
|
||||
s.ApplyToken(tok)
|
||||
return s
|
||||
}
|
||||
|
||||
// ApplyToken rotates the session's tokens after an exchange or refresh.
|
||||
func (s *Session) ApplyToken(tok *TokenResponse) {
|
||||
now := time.Now().UTC()
|
||||
s.AccessToken = tok.AccessToken
|
||||
if tok.RefreshToken != "" {
|
||||
s.RefreshToken = tok.RefreshToken
|
||||
}
|
||||
if tok.TokenType != "" {
|
||||
s.TokenType = tok.TokenType
|
||||
}
|
||||
if tok.Scope != "" {
|
||||
s.Scope = tok.Scope
|
||||
}
|
||||
s.ObtainedAt = now.Format(time.RFC3339)
|
||||
if tok.ExpiresIn > 0 {
|
||||
s.ExpiresAt = now.Add(time.Duration(tok.ExpiresIn) * time.Second).Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
|
||||
// DPoP reconstructs the session's DPoP signing key.
|
||||
func (s *Session) DPoP() (*DPoPKey, error) {
|
||||
return ParseDPoPKey(s.DPoPKey)
|
||||
}
|
||||
|
||||
// ExpiresWithin reports whether the access token is expired or will
|
||||
// expire within d. An unparseable/absent expiry counts as expired.
|
||||
func (s *Session) ExpiresWithin(d time.Duration) bool {
|
||||
exp, err := time.Parse(time.RFC3339, s.ExpiresAt)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
return time.Until(exp) <= d
|
||||
}
|
||||
|
||||
func (s *Session) Info() TokenInfo {
|
||||
return TokenInfo{
|
||||
AccessToken: s.AccessToken,
|
||||
TokenType: s.TokenType,
|
||||
DID: s.DID,
|
||||
Handle: s.Handle,
|
||||
PDS: s.PDS,
|
||||
Issuer: s.Issuer,
|
||||
Scope: s.Scope,
|
||||
ExpiresAt: s.ExpiresAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ExpandPath(p string) (string, error) {
|
||||
if strings.HasPrefix(p, "~/") {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, p[2:]), nil
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func SaveSessionFile(path string, s *Session) error {
|
||||
resolved, err := ExpandPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(resolved), 0o700); err != nil {
|
||||
return fmt.Errorf("create config dir: %w", err)
|
||||
}
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data = append(data, '\n')
|
||||
if err := os.WriteFile(resolved, data, 0o600); err != nil {
|
||||
return fmt.Errorf("write session file: %w", err)
|
||||
}
|
||||
if err := os.Chmod(resolved, 0o600); err != nil {
|
||||
return fmt.Errorf("chmod session file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadSessionFile(path string) (*Session, error) {
|
||||
resolved, err := ExpandPath(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := os.ReadFile(resolved)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read session file: %w (run 'latha-auth atproto' first)", err)
|
||||
}
|
||||
var s Session
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return nil, fmt.Errorf("decode session file %s: %w", resolved, err)
|
||||
}
|
||||
if s.DID == "" || s.AccessToken == "" {
|
||||
return nil, fmt.Errorf("session file %s is missing did/access_token", resolved)
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
Reference in new issue
Block a user