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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user