Files
ghaymah_dev2 a993c45893
ci / test (push) Canceled after 0s
ci / cross-build (amd64, darwin) (push) Canceled after 0s
ci / cross-build (amd64, linux) (push) Canceled after 0s
ci / cross-build (amd64, windows) (push) Canceled after 0s
ci / cross-build (arm64, darwin) (push) Canceled after 0s
ci / cross-build (arm64, linux) (push) Canceled after 0s
ci / cross-build (arm64, windows) (push) Canceled after 0s
Add email/password login via auth.ghaymah.systems + auto-refresh
- apiclient: AuthSignIn (/signin/email-password) + AuthRefresh (/token)
- login: interactive email+password prompt, --email/--password for CI, --token paste
- resolveClient: auto-refresh expired session tokens using the stored refresh token
- credentials store: refresh_token + auth_url
2026-10-25 02:55:10 -04:00

100 lines
2.7 KiB
Go

package apiclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// DefaultAuthURL is the Hasura Auth service the CLI signs in against.
const DefaultAuthURL = "https://auth.ghaymah.systems"
// AuthSignIn signs in with email + password and returns the session's access
// token (a JWT the gateway accepts directly), the refresh token, and the
// user's email.
func AuthSignIn(authURL, email, password string) (accessToken, refreshToken, userEmail string, err error) {
authURL = authBase(authURL)
body, _ := json.Marshal(map[string]string{"email": email, "password": password})
resp, err := http.Post(authURL+"/signin/email-password", "application/json", bytes.NewReader(body))
if err != nil {
return "", "", "", err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", "", "", authError(resp.StatusCode, data)
}
var out struct {
Session struct {
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
User struct {
Email string `json:"email"`
} `json:"user"`
} `json:"session"`
}
if err := json.Unmarshal(data, &out); err != nil {
return "", "", "", fmt.Errorf("decode signin response: %w", err)
}
return out.Session.AccessToken, out.Session.RefreshToken, out.Session.User.Email, nil
}
// AuthRefresh exchanges a refresh token for a fresh access token.
func AuthRefresh(authURL, refreshToken string) (accessToken string, err error) {
authURL = authBase(authURL)
body, _ := json.Marshal(map[string]string{"refreshToken": refreshToken})
resp, err := http.Post(authURL+"/token", "application/json", bytes.NewReader(body))
if err != nil {
return "", err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", authError(resp.StatusCode, data)
}
var out struct {
AccessToken string `json:"accessToken"`
}
if err := json.Unmarshal(data, &out); err != nil {
return "", fmt.Errorf("decode refresh response: %w", err)
}
return out.AccessToken, nil
}
func authBase(authURL string) string {
if strings.TrimSpace(authURL) == "" {
authURL = DefaultAuthURL
}
return strings.TrimSuffix(authURL, "/")
}
func authError(status int, data []byte) error {
var e struct {
Error string `json:"error"`
Message string `json:"message"`
}
_ = json.Unmarshal(data, &e)
msg := e.Message
if msg == "" {
msg = e.Error
}
if msg == "" {
msg = http.StatusText(status)
}
// The auth service uses these generic strings for bad credentials.
switch msg {
case "The request payload is incorrect", "invalid-request":
msg = "invalid email or password"
}
return fmt.Errorf("%s", msg)
}