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
- 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
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitlab.com/ghaymah/gy/internal/uit"
|
|
)
|
|
|
|
// version is overridden at build time via -ldflags "-X gitlab.com/ghaymah/gy/internal/cli.version=x.y.z".
|
|
var version = "dev"
|
|
|
|
// firstNonEmpty returns the first non-empty string.
|
|
func firstNonEmpty(vals ...string) string {
|
|
for _, v := range vals {
|
|
if v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// uitCheck returns a green ✓ (styled when the terminal supports it).
|
|
func uitCheck() string { return uit.Check() }
|
|
|
|
// uitCross returns a red ✗.
|
|
func uitCross() string { return uit.Cross() }
|
|
|
|
var ansiRe = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
|
|
|
|
// stripANSI removes ANSI color/control sequences (Cumin log lines carry them).
|
|
func stripANSI(s string) string { return ansiRe.ReplaceAllString(s, "") }
|
|
|
|
// jwtExpired reports whether a compact JWT has passed (or is within 60s of)
|
|
// its exp claim. Non-JWT tokens (no exp claim) return false.
|
|
func jwtExpired(token string) bool {
|
|
parts := strings.Split(token, ".")
|
|
if len(parts) != 3 {
|
|
return false
|
|
}
|
|
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
|
|
if err != nil {
|
|
return false
|
|
}
|
|
var claims struct {
|
|
Exp int64 `json:"exp"`
|
|
}
|
|
if json.Unmarshal(payload, &claims) != nil || claims.Exp == 0 {
|
|
return false
|
|
}
|
|
return time.Now().Unix() >= claims.Exp-60
|
|
}
|