[fix] - <Customizing jwt authentication rule>
Closed this issue · 1 comments
LeeJuHwan commented
Fix
이슈 내용
storead-api/booklog/config/settings/base.py
Lines 112 to 114 in 8b257f6
위와 같은 실수로 인해 정상적인 토큰 접근이 안되어 수정 해보니 소셜 유저 모델에 접근 하지 않음
변경이 필요한 라인
storead-api/booklog/config/settings/base.py
Lines 130 to 136 in 8b257f6
관련 설명
simplejwt는 기본 authentication rule을 적용 하고 있으며, 이 내부적인 코드에는 설정 파일에서 지정한 기초 유저 모델 변수를 사용한다.
storead-api/booklog/config/settings/base.py
Line 154 in 8b257f6
이 모델 변수가 갖고 있는 어트리뷰트(model fields)로 simple_jwt를 구성 해야 하는데, 관리자 계정 모델과 소셜 유저 모델이 분리 되어 사용되는 현재 상황에 부적합하다.
- 여기서 부적합 하다는 것은 Social User는 pkid를 사용하지만 기본 유저 모델로 지정된 Admin 모델은 id를 사용하고 있기 때문에 pkid를 활용할 수 없는 상황이다.
시도 해볼만한 개선 방안
base.py
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=30),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"SIGNING_KEY": SECRET_KEY,
"USER_ID_FIELD": "pkid", # TODO: uuid로 변경
"USER_ID_CLAIM": "pkid", # TODO: uuid로 변경
"USER_AUTHENTICATION_RULE": "utils.social_users.authentication.SocialAuthenticationRule",
}
authentication.py
from rest_framework_simplejwt.authentication import AuthenticationRule
class SocialAuthenticationRule(AuthenticationRule):
def authenticate(self, header, payload):
user_id = payload.get("uuid")
if user_id is not None:
try:
# 소셜 유저 인증
user = SocialUser.objects.get(pkid=user_id) # TODO: uuid로 변경
except SocialUser.DoesNotExist:
user = None
else:
user = None
return user
LeeJuHwan commented
BaseUser 상속이 불필요할 것이라고 판단 하여 기초 모델만 생성 하려 했지만 BaseUser가 갖고 있는 인증을 위한 필드들의 필요성으로 인하여 상속 후 구현
- authenticaion_rule은 모델링 된 필드를 변경하거나, 가져올 수 없기 때문에 AUTH_USER_MODEL에 정의 된 모델의 어트리뷰트가 중요하였음