Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

Add auth hash tracking and refresh mechanism

Dane Schneiderf73cb1084bdc
diff --git a/app/cli/auth/api.go b/app/cli/auth/api.go
index e8ec7ab1..4dc9e416 100644
--- a/app/cli/auth/api.go
+++ b/app/cli/auth/api.go
@@ -20,10 +20,12 @@ func SetAuthHeader(req *http.Request) error {
if Current == nil {
return fmt.Errorf("error setting auth header: auth not loaded")
}
+ hash := Current.ToHash()
authHeader := shared.AuthHeader{
Token: Current.Token,
OrgId: Current.OrgId,
+ Hash: hash,
}
bytes, err := json.Marshal(authHeader)
diff --git a/app/cli/auth/auth.go b/app/cli/auth/auth.go
index 181d4860..dc3dbda5 100644
--- a/app/cli/auth/auth.go
+++ b/app/cli/auth/auth.go
@@ -110,3 +110,27 @@ func RefreshInvalidToken() error {
return nil
}
+
+func RefreshAuth() error {
+ if Current == nil {
+ return fmt.Errorf("error refreshing auth: auth not loaded")
+ }
+
+ org, apiErr := apiClient.GetOrgSession()
+
+ if apiErr != nil {
+ return fmt.Errorf("error getting org session: %v", apiErr.Msg)
+ }
+
+ Current.OrgName = org.Name
+ Current.OrgIsTrial = org.IsTrial
+ Current.IntegratedModelsMode = org.IntegratedModelsMode
+
+ err := writeCurrentAuth()
+
+ if err != nil {
+ return fmt.Errorf("error writing auth: %v", err)
+ }
+
+ return nil
+}
diff --git a/app/shared/auth.go b/app/shared/auth.go
index 48a468cc..bb751d6b 100644
--- a/app/shared/auth.go
+++ b/app/shared/auth.go
@@ -1,16 +1,24 @@
package shared
-import "fmt"
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "strconv"
+ "strings"
+)
type AuthHeader struct {
Token string `json:"token"`
OrgId string `json:"orgId"`
+ Hash string `json:"hash"`
}
type ApiErrorType string
const (
ApiErrorTypeInvalidToken ApiErrorType = "invalid_token"
+ ApiErrorTypeAuthOutdated ApiErrorType = "auth_outdated"
ApiErrorTypeTrialPlansExceeded ApiErrorType = "trial_plans_exceeded"
ApiErrorTypeTrialMessagesExceeded ApiErrorType = "trial_messages_exceeded"
ApiErrorTypeTrialActionNotAllowed ApiErrorType = "trial_action_not_allowed"
@@ -76,3 +84,15 @@ type ClientAuth struct {
OrgIsTrial bool `json:"orgIsTrial"`
IntegratedModelsMode bool `json:"integratedModelsMode"`
}
+
+// Helps the client refresh auth if any of the org fields change
+func (c *ClientAuth) ToHash() string {
+ s := strings.Join([]string{
+ c.OrgName,
+ strconv.FormatBool(c.OrgIsTrial),
+ strconv.FormatBool(c.IntegratedModelsMode),
+ }, "||")
+
+ hash := sha256.Sum256([]byte(s))
+ return hex.EncodeToString(hash[:])
+}