ragflow-cli-myfbee-test 0.27.2.dev0__py3-none-any.whl

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.
user.py ADDED
@@ -0,0 +1,76 @@
1
+ #
2
+ # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ from http_client import HttpClient
18
+
19
+
20
+ class AuthException(Exception):
21
+ def __init__(self, message, code=401):
22
+ super().__init__(message)
23
+ self.code = code
24
+ self.message = message
25
+
26
+
27
+ def encrypt_password(password_plain: str) -> str:
28
+ try:
29
+ import base64
30
+ from Cryptodome.PublicKey import RSA
31
+ from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
32
+
33
+ def crypt(line):
34
+ """
35
+ decrypt(crypt(input_string)) == base64(input_string), which frontend and ragflow_cli use.
36
+ """
37
+ pub = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----"
38
+ rsa_key = RSA.importKey(pub)
39
+ cipher = Cipher_pkcs1_v1_5.new(rsa_key)
40
+ password_base64 = base64.b64encode(line.encode("utf-8")).decode("utf-8")
41
+ encrypted_password = cipher.encrypt(password_base64.encode())
42
+ return base64.b64encode(encrypted_password).decode("utf-8")
43
+ except Exception as exc:
44
+ raise AuthException("Password encryption unavailable; install pycryptodomex (uv sync --python 3.13 --group test).") from exc
45
+ return crypt(password_plain)
46
+
47
+
48
+ def register_user(client: HttpClient, email: str, nickname: str, password: str) -> None:
49
+ password_enc = encrypt_password(password)
50
+ payload = {"email": email, "nickname": nickname, "password": password_enc}
51
+ res = client.request_json("POST", "/users", use_api_base=True, auth_kind=None, json_body=payload)
52
+ if res.get("code") == 0:
53
+ return
54
+ msg = res.get("message", "")
55
+ if "has already registered" in msg:
56
+ return
57
+ raise AuthException(f"Register failed: {msg}")
58
+
59
+
60
+ def login_user(client: HttpClient, server_type: str, email: str, password: str) -> str:
61
+ password_enc = encrypt_password(password)
62
+ payload = {"email": email, "password": password_enc}
63
+ if server_type == "admin":
64
+ response = client.request("POST", "/admin/login", use_api_base=True, auth_kind=None, json_body=payload)
65
+ else:
66
+ response = client.request("POST", "/auth/login", use_api_base=True, auth_kind=None, json_body=payload)
67
+ try:
68
+ res = response.json()
69
+ except Exception as exc:
70
+ raise AuthException(f"Login failed: invalid JSON response ({exc})") from exc
71
+ if res.get("code") != 0:
72
+ raise AuthException(f"Login failed: {res.get('message')}")
73
+ token = response.headers.get("Authorization")
74
+ if not token:
75
+ raise AuthException("Login failed: missing Authorization header")
76
+ return token