ai_co_discuss 0.2.95 → 0.2.97

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/backend/admin.py CHANGED
@@ -126,10 +126,15 @@ def register_admin(connection, payload, setup_key, now):
126
126
  username = valid_username(payload.get("username"))
127
127
  password = valid_password(payload.get("password"))
128
128
  supplied = payload.get("setup_key")
129
- if not isinstance(supplied, str) or not same_secret(supplied.strip(), setup_key):
129
+ setup = connection.execute("SELECT token_hash FROM admin_setup WHERE id=1").fetchone()
130
+ # Either the install token or the one-time token written by `codiscuss admin token` registers.
131
+ if not isinstance(supplied, str) or not (
132
+ same_secret(supplied.strip(), setup_key)
133
+ or setup and hmac.compare_digest(hashlib.sha256(supplied.strip().encode()).hexdigest(), setup["token_hash"])):
130
134
  raise AdminError("安装令牌不正确", 403)
131
135
  connection.execute("INSERT INTO site_admin (id,username,password_hash,created_at) VALUES (1,?,?,?)",
132
136
  (username, hash_password(password), now))
137
+ connection.execute("DELETE FROM admin_setup")
133
138
  return {"token": open_session(connection, now), "profile": {"username": username, "avatar": ""}}
134
139
 
135
140