From c3e363eccc2d2da6dff8a438919c91394af6283d Mon Sep 17 00:00:00 2001 From: mrhid6 Date: Fri, 24 Jul 2026 15:07:06 +0100 Subject: [PATCH] refactor(server): finish the Org to Instance rename Private identifiers plan 0b's naming map missed, plus the OrgOIDC model type. No wire format, database field or route changes. --- server/internal/api/{org.go => instance.go} | 2 +- server/internal/auth/instancehost.go | 34 +++++++++---------- server/internal/auth/oidc.go | 10 +++--- server/internal/auth/session.go | 4 +-- .../models/{org_oidc.go => instance_oidc.go} | 2 +- server/internal/services/instance_oidc.go | 6 ++-- 6 files changed, 29 insertions(+), 29 deletions(-) rename server/internal/api/{org.go => instance.go} (97%) rename server/internal/models/{org_oidc.go => instance_oidc.go} (95%) diff --git a/server/internal/api/org.go b/server/internal/api/instance.go similarity index 97% rename from server/internal/api/org.go rename to server/internal/api/instance.go index 4eb9643..b32235a 100644 --- a/server/internal/api/org.go +++ b/server/internal/api/instance.go @@ -145,7 +145,7 @@ func putInstanceOIDC(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - if err := services.SaveOrgOIDC(auth.InstanceID(c), body.Issuer, body.ClientID, body.ClientSecret, body.Enabled); err != nil { + if err := services.SaveInstanceOIDC(auth.InstanceID(c), body.Issuer, body.ClientID, body.ClientSecret, body.Enabled); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } diff --git a/server/internal/auth/instancehost.go b/server/internal/auth/instancehost.go index 3853e19..97b1447 100644 --- a/server/internal/auth/instancehost.go +++ b/server/internal/auth/instancehost.go @@ -11,17 +11,17 @@ import ( "github.com/mrhid6/vantage/server/internal/services" ) -type cachedOrg struct { - org *models.Instance - at time.Time +type cachedInstance struct { + instance *models.Instance + at time.Time } var ( - orgCacheMu sync.Mutex - orgCache = map[string]cachedOrg{} + instanceCacheMu sync.Mutex + instanceCache = map[string]cachedInstance{} ) -const orgCacheTTL = 60 * time.Second +const instanceCacheTTL = 60 * time.Second func appRootLabel() string { if v := os.Getenv("APP_ROOT_LABEL"); v != "" { @@ -55,20 +55,20 @@ func InstanceFromHost(c *gin.Context) (*models.Instance, bool) { if slug == "" { return nil, false } - orgCacheMu.Lock() - if e, ok := orgCache[slug]; ok && time.Since(e.at) < orgCacheTTL { - orgCacheMu.Unlock() - return e.org, e.org != nil + instanceCacheMu.Lock() + if e, ok := instanceCache[slug]; ok && time.Since(e.at) < instanceCacheTTL { + instanceCacheMu.Unlock() + return e.instance, e.instance != nil } - orgCacheMu.Unlock() + instanceCacheMu.Unlock() - org, err := services.GetInstanceBySlug(slug) - if err != nil || org == nil { + inst, err := services.GetInstanceBySlug(slug) + if err != nil || inst == nil { return nil, false } - orgCacheMu.Lock() - orgCache[slug] = cachedOrg{org: org, at: time.Now()} - orgCacheMu.Unlock() - return org, true + instanceCacheMu.Lock() + instanceCache[slug] = cachedInstance{instance: inst, at: time.Now()} + instanceCacheMu.Unlock() + return inst, true } diff --git a/server/internal/auth/oidc.go b/server/internal/auth/oidc.go index add8191..de8c3ea 100644 --- a/server/internal/auth/oidc.go +++ b/server/internal/auth/oidc.go @@ -32,7 +32,7 @@ func redirectURL(c *gin.Context) string { return fmt.Sprintf("%s://%s/auth/oidc/callback", scheme, c.Request.Host) } -func providerForOrg(ctx context.Context, c *gin.Context, instanceID string) (*oidc.Provider, *oauth2.Config, error) { +func providerForInstance(ctx context.Context, c *gin.Context, instanceID string) (*oidc.Provider, *oauth2.Config, error) { cfg, err := services.GetInstanceOIDC(instanceID) if err != nil || !cfg.Enabled { return nil, nil, fmt.Errorf("inst SSO not configured") @@ -67,7 +67,7 @@ func HandleOIDCStart(c *gin.Context) { return } ctx := c.Request.Context() - _, oauthCfg, err := providerForOrg(ctx, c, inst.InstanceID) + _, oauthCfg, err := providerForInstance(ctx, c, inst.InstanceID) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return @@ -77,7 +77,7 @@ func HandleOIDCStart(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"error": "state gen failed"}) return } - if err := SaveStateOrg(ctx, state, inst.InstanceID); err != nil { + if err := SaveStateInstance(ctx, state, inst.InstanceID); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "state save failed"}) return } @@ -86,12 +86,12 @@ func HandleOIDCStart(c *gin.Context) { func HandleOIDCCallback(c *gin.Context) { ctx := c.Request.Context() - instanceID, ok := ConsumeStateOrg(ctx, c.Query("state")) + instanceID, ok := ConsumeStateInstance(ctx, c.Query("state")) if !ok { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid state"}) return } - provider, oauthCfg, err := providerForOrg(ctx, c, instanceID) + provider, oauthCfg, err := providerForInstance(ctx, c, instanceID) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return diff --git a/server/internal/auth/session.go b/server/internal/auth/session.go index e0ecf0a..2859bcb 100644 --- a/server/internal/auth/session.go +++ b/server/internal/auth/session.go @@ -71,11 +71,11 @@ func DeleteSession(ctx context.Context, id string) error { return rdb.Del(ctx, sessionPrefix+id).Err() } -func SaveStateOrg(ctx context.Context, state, instanceID string) error { +func SaveStateInstance(ctx context.Context, state, instanceID string) error { return rdb.Set(ctx, statePrefix+state, instanceID, 10*time.Minute).Err() } -func ConsumeStateOrg(ctx context.Context, state string) (string, bool) { +func ConsumeStateInstance(ctx context.Context, state string) (string, bool) { instanceID, err := rdb.GetDel(ctx, statePrefix+state).Result() if err != nil || instanceID == "" { return "", false diff --git a/server/internal/models/org_oidc.go b/server/internal/models/instance_oidc.go similarity index 95% rename from server/internal/models/org_oidc.go rename to server/internal/models/instance_oidc.go index b6bead8..fe759d7 100644 --- a/server/internal/models/org_oidc.go +++ b/server/internal/models/instance_oidc.go @@ -6,7 +6,7 @@ import ( "go.mongodb.org/mongo-driver/v2/bson" ) -type OrgOIDC struct { +type InstanceOIDC struct { ID bson.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"` InstanceID string `bson:"instance_id" json:"instance_id"` Issuer string `bson:"issuer" json:"issuer"` diff --git a/server/internal/services/instance_oidc.go b/server/internal/services/instance_oidc.go index 7c3a358..fe0839f 100644 --- a/server/internal/services/instance_oidc.go +++ b/server/internal/services/instance_oidc.go @@ -10,10 +10,10 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) -func GetInstanceOIDC(instanceID string) (*models.OrgOIDC, error) { +func GetInstanceOIDC(instanceID string) (*models.InstanceOIDC, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - var o models.OrgOIDC + var o models.InstanceOIDC err := db.Col("instance_oidc").FindOne(ctx, bson.M{"instance_id": instanceID}).Decode(&o) if err != nil { return nil, err @@ -29,7 +29,7 @@ func GetInstanceOIDCSecret(instanceID string) (string, error) { return decryptString(o.ClientSecretEnc) } -func SaveOrgOIDC(instanceID, issuer, clientID, clientSecret string, enabled bool) error { +func SaveInstanceOIDC(instanceID, issuer, clientID, clientSecret string, enabled bool) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() set := bson.M{