nc-user-terminator 0.1.11__tar.gz → 0.1.12__tar.gz

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.

Potentially problematic release.


This version of nc-user-terminator might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nc-user-terminator
3
- Version: 0.1.11
3
+ Version: 0.1.12
4
4
  Summary: OAuth client wrapper for multi-tenant projects
5
5
  Author: bw_song
6
6
  Author-email: m132777096902@gmail.com
@@ -25,3 +25,7 @@ Dynamic: requires-python
25
25
  - 获取用户信息,本地缓存配置
26
26
  + v0.1.11
27
27
  - 用户头像字段调整
28
+ + v0.1.12
29
+ - 新增密码学登录
30
+ - 新增修改密码
31
+ - 支持重置社媒账号密码
@@ -12,4 +12,8 @@
12
12
  + v0.1.10
13
13
  - 获取用户信息,本地缓存配置
14
14
  + v0.1.11
15
- - 用户头像字段调整
15
+ - 用户头像字段调整
16
+ + v0.1.12
17
+ - 新增密码学登录
18
+ - 新增修改密码
19
+ - 支持重置社媒账号密码
@@ -1,10 +1,10 @@
1
1
  import asyncio
2
2
  from typing import Optional, Dict, Any
3
3
 
4
- from .cache import MemoryCache, BaseCache, AsyncRedisCache
5
- from .models import UserResponse, OAuth2AuthorizeResponse, CallbackResponse
6
- from .exceptions import OAuthError
7
- from .utils import request
4
+ from cache import MemoryCache, BaseCache, AsyncRedisCache
5
+ from models import UserResponse, OAuth2AuthorizeResponse, CallbackResponse
6
+ from exceptions import OAuthError
7
+ from utils import request
8
8
 
9
9
 
10
10
  PRODUCT_HEADER = "X-Product-Code"
@@ -47,8 +47,11 @@ class OAuthClient:
47
47
  def _headers(self, extra: Optional[Dict[str, str]] = None) -> Dict[str, str]:
48
48
  headers = {
49
49
  PRODUCT_HEADER: self._product_code,
50
- "Host": "auth.nc.com"
51
50
  }
51
+ if self._base_url == 'http://172.27.92.74':
52
+ headers.update({
53
+ "Host": "auth.nc.com"
54
+ })
52
55
  if extra:
53
56
  headers.update(extra)
54
57
  return headers
@@ -279,6 +282,59 @@ class OAuthClient:
279
282
  self._uncache_user(token, data)
280
283
  return resp
281
284
 
285
+ # 密码学登录
286
+ def open_sesame(self, user_name:str, password: str) -> CallbackResponse:
287
+ params = {"single_session": str(self._single_session).lower()}
288
+
289
+ form = {
290
+ "username": user_name,
291
+ "password": password,
292
+ }
293
+
294
+ res_dict = request("POST", f"{self._base_url}/api/auth/login", headers=self._headers(), params=params, form_body=form)
295
+
296
+ return CallbackResponse(res_dict)
297
+
298
+
299
+ async def open_sesame_async(self, user_name:str, password: str) -> CallbackResponse:
300
+ params = {"single_session": str(self._single_session).lower()}
301
+
302
+ form = {
303
+ "username": user_name,
304
+ "password": password,
305
+ }
306
+
307
+ res_dict = await self._arequest(
308
+ "POST",
309
+ f"{self._base_url}/api/auth/login",
310
+ params=params,
311
+ form_body=form,
312
+ headers={
313
+ "Content-Type": "application/x-www-form-urlencoded",
314
+ **self._headers(),
315
+ }
316
+ )
317
+ return CallbackResponse(res_dict)
318
+
319
+ def forgot_password(self, token: str) -> str:
320
+ headers = self._headers({"Authorization": f"Bearer {token}"})
321
+ resp = request("POST", f"{self._base_url}/api/auth/forgot-password", headers=headers)
322
+ return resp.get("token")
323
+
324
+ def reset_password(self, password_token: str, password: str) -> None:
325
+ """
326
+ 修改密码
327
+ :param password_token: forgot_password获取的验证token
328
+ :param password: 新密码
329
+ :return:
330
+ """
331
+ json_data = {
332
+ "token": password_token,
333
+ "password": password
334
+ }
335
+ request("POST", f"{self._base_url}/api/auth/forgot-password", headers=self._headers(), json_body=json_data)
336
+
337
+
282
338
  async def main():
283
339
  def get_redis_url() -> str:
284
340
  return f'redis://default:1q2w3e@localhost:6379/1'
@@ -289,11 +345,17 @@ async def main():
289
345
  _redis = redis.from_url(get_redis_url(), decode_responses=True)
290
346
 
291
347
  redis_cache = AsyncRedisCache(_redis)
348
+ import socket
349
+ print(socket.gethostbyname("dev.ncuser.com"))
292
350
 
293
- client = OAuthClient("http://auth.nc.com/", "bankgpt", "http://localhost:8000/auth/google/custom_callback", single_session=True, cache=redis_cache)
294
- authorize = client.authorize("google")
295
- print(authorize)
351
+ client = OAuthClient("kode", "http://dev.ncuser.com", "http://localhost:8000/auth/google/custom_callback", single_session=True, cache=redis_cache)
352
+ authorize = await client.say_my_name_async("QtbMAqXNwrsy-7dr-D_lIVvUu88gjhPM0fBXeJTaOiA")
353
+
354
+ # res = client.forgot_password("QtbMAqXNwrsy-7dr-D_lIVvUu88gjhPM0fBXeJTaOiA")
296
355
 
356
+ token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicGFzc3dvcmRfZmdwdCI6IiRhcmdvbjJpZCR2PTE5JG09NjU1MzYsdD0zLHA9NCRYUlQ1K1p3Uk5sbjhKQzQrZW94TFlnJERadnFKb29xUDNMUGVJbEV6OVR5TjZIdm1LYW9yYnpvdGxGRlB5SlBKZE0iLCJhdWQiOiJmYXN0YXBpLXVzZXJzOnJlc2V0IiwiZXhwIjoxNzYxNTU4MTUxfQ.rSr8tRql4ZsE2k-WGHzhq9aT387E9jXGJ_bNomC4LKA"
357
+ print(client.reset_password(token, "965965965"))
358
+ print(authorize)
297
359
 
298
360
 
299
361
 
@@ -10,7 +10,8 @@ def request(
10
10
  url: str,
11
11
  params: Optional[Dict[str, Any]] = None,
12
12
  headers: Optional[Dict[str, str]] = None,
13
- json_body: Optional[dict] = None
13
+ json_body: Optional[dict] = None,
14
+ form_body: Optional[dict] = None,
14
15
  ) -> dict:
15
16
  """
16
17
  发送 HTTP 请求并返回统一结果,不抛异常
@@ -28,12 +29,20 @@ def request(
28
29
  url = f"{url}?{query}"
29
30
 
30
31
  data = None
32
+
33
+ # JSON 请求
31
34
  if json_body is not None:
32
35
  data = json.dumps(json_body).encode("utf-8")
33
36
  if headers is None:
34
37
  headers = {}
35
38
  headers["Content-Type"] = "application/json"
36
39
 
40
+ # Form 表单请求
41
+ if form_body is not None:
42
+ data = urllib.parse.urlencode(form_body).encode("utf-8")
43
+ headers = headers or {}
44
+ headers["Content-Type"] = "application/x-www-form-urlencoded"
45
+
37
46
  req = urllib.request.Request(url, method=method.upper(), data=data)
38
47
  if headers:
39
48
  for k, v in headers.items():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nc-user-terminator
3
- Version: 0.1.11
3
+ Version: 0.1.12
4
4
  Summary: OAuth client wrapper for multi-tenant projects
5
5
  Author: bw_song
6
6
  Author-email: m132777096902@gmail.com
@@ -25,3 +25,7 @@ Dynamic: requires-python
25
25
  - 获取用户信息,本地缓存配置
26
26
  + v0.1.11
27
27
  - 用户头像字段调整
28
+ + v0.1.12
29
+ - 新增密码学登录
30
+ - 新增修改密码
31
+ - 支持重置社媒账号密码
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "nc-user-terminator"
7
- version = "0.1.11"
7
+ version = "0.1.12"
8
8
  description = "OAuth client wrapper for multi-tenant projects"
9
9
  authors = [
10
10
  { name = "bw_song" }