smcli: self-healing sessions — refresh-token grant + auto-relogin
ci / vet (pull_request) Successful in 1m9s
ci / vet (pull_request) Successful in 1m9s
Vault access tokens expire (~10min); consumers hit 401s after container recreates. Now: refresh_token persisted at login and rotated on every refresh (Vaultwarden semantics); 401 on an authed call refreshes once and retries; if refresh is unavailable, full relogin (password+TOTP injected by the sm shims from the TSGCOO vault-account env) is attempted before failing. cmdLogin passes the TOTP seed again (regression). QA: corrupted access_token -> TSGCOO sm read self-healed end-to-end. Ticket: https://projects.knownelement.com/issues/832
This commit is contained in:
+35
-5
@@ -17,10 +17,12 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Server string // e.g. https://pwvault.turnsys.com
|
||||
HTTP *http.Client
|
||||
Email string
|
||||
Password string
|
||||
Server string // e.g. https://pwvault.turnsys.com
|
||||
HTTP *http.Client
|
||||
Email string
|
||||
Password string
|
||||
TOTPSecret string
|
||||
reloginDone bool
|
||||
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
@@ -180,7 +182,7 @@ func (c *Client) Login() error {
|
||||
if t.AccessToken == "" {
|
||||
// 2FA retry path (provider 0 = authenticator TOTP)
|
||||
if strings.Contains(string(out), "Two factor required") {
|
||||
secret := os.Getenv("SM_TOTP_SECRET")
|
||||
secret := c.TOTPSecret
|
||||
if secret != "" {
|
||||
code, terr := totpNow(secret, time.Now())
|
||||
if terr != nil {
|
||||
@@ -327,3 +329,31 @@ func (c *Client) DeleteCipher(id string) error {
|
||||
_, _ = c.api("PUT", "/api/ciphers/"+id+"/purge", map[string]any{}, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
// selfRelogin performs the full login+unlock using SM_* env credentials
|
||||
// (injected by the sm shims from the TSGCOO vault-account env). Saves state.
|
||||
func (c *Client) selfRelogin() error {
|
||||
if c.Password == "" {
|
||||
c.Password = os.Getenv("SM_PASSWORD")
|
||||
}
|
||||
if c.TOTPSecret == "" {
|
||||
c.TOTPSecret = os.Getenv("SM_TOTP_SECRET")
|
||||
}
|
||||
if c.Password == "" {
|
||||
return errors.New("relogin unavailable: SM_PASSWORD not set")
|
||||
}
|
||||
if err := c.Login(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.Unlock(); err != nil {
|
||||
return err
|
||||
}
|
||||
if s, err := loadState(); err == nil {
|
||||
s.AccessToken = c.AccessToken
|
||||
s.UserSymKey = toHex(c.UserSymKey)
|
||||
s.StretchedKey = toHex(c.StretchedKey)
|
||||
s.MasterKey = toHex(c.MasterKey)
|
||||
_ = saveState(s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user