Skip to content

Commit 83dcdbe

Browse files
authored
Update the OAuth2 login password policy (#741)
* Update the OAuth2 login password policy * Update the crud pwd * Update the reset pwd service
1 parent d64f7c2 commit 83dcdbe

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

backend/app/admin/crud/crud_user.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ async def add_by_oauth2(self, db: AsyncSession, obj: AddOAuth2UserParam) -> None
8989
:param obj: 注册用户参数
9090
:return:
9191
"""
92-
salt = bcrypt.gensalt()
93-
obj.password = get_hash_password(obj.password, salt)
9492
dict_obj = obj.model_dump()
95-
dict_obj.update({'is_staff': True, 'salt': salt})
93+
dict_obj.update({'is_staff': True, 'salt': None})
9694
new_user = self.model(**dict_obj)
9795

9896
stmt = select(Role)
@@ -156,10 +154,12 @@ async def reset_password(self, db: AsyncSession, pk: int, new_pwd: str) -> int:
156154
157155
:param db: 数据库会话
158156
:param pk: 用户 ID
159-
:param new_pwd: 新密码(已加密)
157+
:param new_pwd: 新密码
160158
:return:
161159
"""
162-
return await self.update_model(db, pk, {'password': new_pwd})
160+
salt = bcrypt.gensalt()
161+
new_pwd = get_hash_password(new_pwd, salt)
162+
return await self.update_model(db, pk, {'password': new_pwd, 'salt': salt})
163163

164164
async def get_list(self, dept: int | None, username: str | None, phone: str | None, status: int | None) -> Select:
165165
"""

backend/app/admin/model/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class User(Base):
2727
uuid: Mapped[str] = mapped_column(String(50), init=False, default_factory=uuid4_str, unique=True)
2828
username: Mapped[str] = mapped_column(String(20), unique=True, index=True, comment='用户名')
2929
nickname: Mapped[str] = mapped_column(String(20), comment='昵称')
30-
password: Mapped[str] = mapped_column(String(255), comment='密码')
31-
salt: Mapped[bytes] = mapped_column(VARBINARY(255).with_variant(BYTEA(255), 'postgresql'), comment='加密盐')
30+
password: Mapped[str | None] = mapped_column(String(255), comment='密码')
31+
salt: Mapped[bytes | None] = mapped_column(VARBINARY(255).with_variant(BYTEA(255), 'postgresql'), comment='加密盐')
3232
email: Mapped[str | None] = mapped_column(String(50), default=None, unique=True, index=True, comment='邮箱')
3333
phone: Mapped[str | None] = mapped_column(String(11), default=None, comment='手机号')
3434
avatar: Mapped[str | None] = mapped_column(String(255), default=None, comment='头像')

backend/app/admin/service/user_service.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from backend.common.enums import UserPermissionType
2020
from backend.common.exception import errors
21-
from backend.common.security.jwt import get_hash_password, get_token, jwt_decode, password_verify, superuser_verify
21+
from backend.common.security.jwt import get_token, jwt_decode, password_verify, superuser_verify
2222
from backend.core.conf import settings
2323
from backend.database.db import async_db_session
2424
from backend.database.redis import redis_client
@@ -249,8 +249,7 @@ async def reset_pwd(*, pk: int, obj: ResetPasswordParam) -> int:
249249
raise errors.RequestError(msg='原密码错误')
250250
if obj.new_password != obj.confirm_password:
251251
raise errors.RequestError(msg='密码输入不一致')
252-
new_pwd = get_hash_password(obj.new_password, user.salt)
253-
count = await user_dao.reset_password(db, user.id, new_pwd)
252+
count = await user_dao.reset_password(db, user.id, obj.new_password)
254253
key_prefix = [
255254
f'{settings.TOKEN_REDIS_PREFIX}:{user.id}',
256255
f'{settings.TOKEN_REFRESH_REDIS_PREFIX}:{user.id}',

backend/plugin/oauth2/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[plugin]
22
summary = 'OAuth 2.0'
3-
version = '0.0.3'
3+
version = '0.0.4'
44
description = '通过 OAuth 2.0 的方式登录系统'
55
author = 'wu-clan'
66

backend/plugin/oauth2/service/oauth2_service.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,36 @@ async def create_with_login(
5757
sid = user.get('id')
5858
nickname = user.get('name')
5959

60-
sys_user = None
6160
user_social = await user_social_dao.get_by_sid(db, str(sid), str(social.value))
62-
if not user_social:
61+
if user_social:
62+
sys_user = await user_dao.get(db, user_social.user_id)
63+
# 更新用户头像
64+
if not sys_user.avatar and avatar is not None:
65+
await user_dao.update_avatar(db, sys_user.id, avatar)
66+
else:
67+
sys_user = None
68+
# 检测系统用户是否已存在
6369
if email:
64-
sys_user = await user_dao.check_email(db, email)
65-
66-
# 创建系统用户
67-
if not sys_user:
68-
while await user_dao.get_by_username(db, username):
69-
username = f'{username}_{text_captcha(5)}'
70-
new_sys_user = AddOAuth2UserParam(
71-
username=username,
72-
password='123456', # 默认密码,可修改系统用户表进行默认密码检测并配合前端进行修改密码提示
73-
nickname=nickname,
74-
email=email,
75-
avatar=avatar,
76-
)
77-
await user_dao.add_by_oauth2(db, new_sys_user)
78-
await db.flush()
79-
sys_user = await user_dao.get_by_username(db, username)
70+
sys_user = await user_dao.check_email(db, email) # 通过邮箱验证绑定保证邮箱真实性
8071

81-
# 绑定社交用户
82-
new_user_social = CreateUserSocialParam(sid=str(sid), source=social.value, user_id=sys_user.id)
83-
await user_social_dao.create(db, new_user_social)
72+
# 创建系统用户
73+
if not sys_user:
74+
while await user_dao.get_by_username(db, username):
75+
username = f'{username}_{text_captcha(5)}'
76+
new_sys_user = AddOAuth2UserParam(
77+
username=username,
78+
password=None,
79+
nickname=nickname,
80+
email=email,
81+
avatar=avatar,
82+
)
83+
await user_dao.add_by_oauth2(db, new_sys_user)
84+
await db.flush()
85+
sys_user = await user_dao.get_by_username(db, username)
8486

85-
if not sys_user:
86-
sys_user = await user_dao.get(db, user_social.user_id)
87-
if avatar:
88-
await user_dao.update_avatar(db, sys_user.id, avatar)
87+
# 绑定社交账号
88+
new_user_social = CreateUserSocialParam(sid=str(sid), source=social.value, user_id=sys_user.id)
89+
await user_social_dao.create(db, new_user_social)
8990

9091
# 创建 token
9192
access_token = await jwt.create_access_token(

0 commit comments

Comments
 (0)