Browse Source

fixing auth session mgmt

reid 2 years ago
parent
commit
607b3ff5ce
3 changed files with 54 additions and 19 deletions
  1. 6 0
      auth/auth.go
  2. 46 16
      config/config.go
  3. 2 3
      ws/ws.go

+ 6 - 0
auth/auth.go

@@ -120,6 +120,9 @@ func AddSession(tokenID string, hash string, created string, authorized bool) er
 		if err := config.UpdateConf(update); err != nil {
 			return fmt.Errorf("Error adding session: %v", err)
 		}
+		if err := config.RemoveSession(tokenID, false); err != nil {
+			return fmt.Errorf("Error removing session: %v", err)
+		}
 	} else {
 		update := map[string]interface{}{
 			"Sessions": map[string]interface{}{
@@ -132,6 +135,9 @@ func AddSession(tokenID string, hash string, created string, authorized bool) er
 		if err := config.UpdateConf(update); err != nil {
 			return fmt.Errorf("Error adding session: %v", err)
 		}
+		if err := config.RemoveSession(tokenID, true); err != nil {
+			return fmt.Errorf("Error removing session: %v", err)
+		}
 	}
 	return nil
 }

+ 46 - 16
config/config.go

@@ -37,6 +37,7 @@ var (
 	DockerDir    = "/var/lib/docker/volumes/"
 	// version server check
 	checkInterval = 5 * time.Minute
+	confPath      = filepath.Join(BasePath, "settings", "system.json")
 	confMutex     sync.Mutex
 	contMutex     sync.Mutex
 	versMutex     sync.Mutex
@@ -118,41 +119,70 @@ func UpdateConf(values map[string]interface{}) error {
 	// mutex lock to avoid race conditions
 	confMutex.Lock()
 	defer confMutex.Unlock()
-	confPath := filepath.Join(BasePath, "settings", "system.json")
 	file, err := ioutil.ReadFile(confPath)
 	if err != nil {
-		errmsg := fmt.Sprintf("Unable to load config: %v", err)
-		logger.Error(errmsg)
-		return err
+		return fmt.Errorf("Unable to load config: %v", err)
 	}
 	// unmarshal the config to struct
 	var configMap map[string]interface{}
 	if err := json.Unmarshal(file, &configMap); err != nil {
-		errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
-		logger.Error(errmsg)
-		return err
+		return fmt.Errorf("Error decoding JSON: %v", err)
 	}
 	// update our unmarshaled struct
 	for key, value := range values {
 		configMap[key] = value
 	}
+	if err = persistConf(configMap); err != nil {
+		return fmt.Errorf("Unable to persist config update: %v", err)
+	}
+	return nil
+}
+
+// remove a tokenid from the session map if present
+func RemoveSession(sessionID string, fromAuthorized bool) error {
+	confMutex.Lock()
+	defer confMutex.Unlock()
+	confPath := filepath.Join(BasePath, "settings", "system.json")
+	file, err := ioutil.ReadFile(confPath)
+	if err != nil {
+		return fmt.Errorf("Unable to load config: %v", err)
+	}
+	var configMap map[string]interface{}
+	if err := json.Unmarshal(file, &configMap); err != nil {
+		return fmt.Errorf("Error decoding JSON: %v", err)
+	}
+	sessions, ok := configMap["sessions"].(map[string]interface{})
+	if !ok {
+		return fmt.Errorf("Unexpected format for sessions in config")
+	}
+	targetMapName := "unauthorized"
+	if fromAuthorized {
+		targetMapName = "authorized"
+	}
+	targetMap, ok := sessions[targetMapName].(map[string]interface{})
+	if !ok {
+		return fmt.Errorf("Unexpected format for %s in sessions", targetMapName)
+	}
+	delete(targetMap, sessionID)
+	if err = persistConf(configMap); err != nil {
+		return fmt.Errorf("Unable to persist config update: %v", err)
+	}
+	return nil
+}
+
+func persistConf(configMap map[string]interface{}) error {
 	// marshal and persist it
 	updatedJSON, err := json.MarshalIndent(configMap, "", "    ")
 	if err != nil {
-		errmsg := fmt.Sprintf("Error encoding JSON: %v", err)
-		logger.Error(errmsg)
-		return err
+		return fmt.Errorf("Error encoding JSON: %v", err)
 	}
 	// update the globalConfig var
 	if err := json.Unmarshal(updatedJSON, &globalConfig); err != nil {
-		errmsg := fmt.Sprintf("Error updating global config: %v", err)
-		logger.Error(errmsg)
-		return err
+		return fmt.Errorf("Error updating global config: %v", err)
 	}
+	// write to disk
 	if err := ioutil.WriteFile(confPath, updatedJSON, 0644); err != nil {
-		errmsg := fmt.Sprintf("Error writing to file: %v", err)
-		logger.Error(errmsg)
-		return err
+		return fmt.Errorf("Error writing to file: %v", err)
 	}
 	return nil
 }

+ 2 - 3
ws/ws.go

@@ -117,6 +117,7 @@ func WsHandler(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+// validate password and add to auth session
 func loginHandler(msg []byte, payload structs.WsPayload) error {
 	logger.Info("Login")
 	now := time.Now().Format("2006-01-02_15:04:05")
@@ -135,14 +136,12 @@ func loginHandler(msg []byte, payload structs.WsPayload) error {
 		}
 	} else {
 		logger.Info("Login failed")
-		if err := auth.AddSession(payload.Token.ID, payload.Token.Token, now, false); err != nil {
-			return fmt.Errorf("Unable to process login: %v", err)
-		}
 	}
 	return nil
 }
 
 func verifyHandler(msg []byte, payload structs.WsPayload, r *http.Request, conn *websocket.Conn) error {
+	logger.Info("Verify")
 	payload.Payload = structs.WsLoginPayload{}
 	// if we can't unmarshal, assume no token
 	if err := json.Unmarshal(msg, &payload); err != nil {