1package probeservices 2 3import ( 4 "context" 5 "time" 6) 7 8// LoginCredentials contains the login credentials 9type LoginCredentials struct { 10 ClientID string `json:"username"` 11 Password string `json:"password"` 12} 13 14// LoginAuth contains authentication info 15type LoginAuth struct { 16 Expire time.Time `json:"expire"` 17 Token string `json:"token"` 18} 19 20// MaybeLogin performs login if necessary 21func (c Client) MaybeLogin(ctx context.Context) error { 22 state := c.StateFile.Get() 23 if state.Auth() != nil { 24 return nil // we're already good 25 } 26 creds := state.Credentials() 27 if creds == nil { 28 return ErrNotRegistered 29 } 30 c.LoginCalls.Add(1) 31 var auth LoginAuth 32 if err := c.Client.PostJSON(ctx, "/api/v1/login", *creds, &auth); err != nil { 33 return err 34 } 35 state.Expire = auth.Expire 36 state.Token = auth.Token 37 return c.StateFile.Set(state) 38} 39