reid пре 2 година
родитељ
комит
8fdd858600
3 измењених фајлова са 21 додато и 35 уклоњено
  1. 21 2
      auth/auth.go
  2. 0 1
      broadcast/broadcast.go
  3. 0 32
      config/config.go

+ 21 - 2
auth/auth.go

@@ -98,6 +98,25 @@ func AddToAuthMap(conn *websocket.Conn, token map[string]string, authed bool) er
 	return nil
 }
 
+// the same but the other way
+func RemoveFromAuthMap(tokenId string, fromAuthorized bool) error {
+	if fromAuthorized {
+		AuthenticatedClients.Lock()
+		if _, ok := AuthenticatedClients.Conns[tokenId]; ok {
+			delete(AuthenticatedClients.Conns, tokenId)
+		}
+		AuthenticatedClients.Unlock()
+	} else {
+		UnauthClients.Lock()
+		if _, ok := UnauthClients.Conns[tokenId]; ok {
+			delete(UnauthClients.Conns, tokenId)
+		}
+		UnauthClients.Unlock()
+	}
+	return nil
+}
+
+
 // check the validity of the token
 func CheckToken(token map[string]string, conn *websocket.Conn, r *http.Request, setup bool) bool {
 	// great you have token. we see if valid.
@@ -197,7 +216,7 @@ 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 {
+		if err := RemoveFromAuthMap(tokenID, false); err != nil {
 			return fmt.Errorf("Error removing session: %v", err)
 		}
 		} else {
@@ -211,7 +230,7 @@ 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 {
+		if err := RemoveFromAuthMap(tokenID, true); err != nil {
 			return fmt.Errorf("Error removing session: %v", err)
 		}
 	}

+ 0 - 1
broadcast/broadcast.go

@@ -347,7 +347,6 @@ func BroadcastToClients() error {
 	for client := range auth.AuthenticatedClients.Conns {
 		if err := auth.AuthenticatedClients.Conns[client].WriteMessage(websocket.TextMessage, authJson); err != nil {
 			config.Logger.Error(fmt.Sprintf("Error writing response: %v", err))
-			config.Logger.Info(fmt.Sprintf("Broadcasting to authenticated token %s",client))
 			return err
 		}
 	}

+ 0 - 32
config/config.go

@@ -138,38 +138,6 @@ func UpdateConf(values map[string]interface{}) error {
 	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, "", "    ")