diff --git a/CHANGES.md b/CHANGES.md index 603fa1f..6de349c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ Changelog ========= -1.0.1 (Oct 10st 2019) +1.0.2 (Oct 11st 2019) +------------------ +- Add `RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO` option in setting + - to use token_info for getting user object, it can be used. + +1.0.1 (Oct 11st 2019) ------------------ - Change lib dir to ridi_django_oauth2_lib for preventing dir conflict - Update README.md diff --git a/README.md b/README.md index e30faaf..ce8a4eb 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,21 @@ RIDI_OAUTH2_CLIENT_SECRET = 'this-is-client-secret' RIDI_OAUTH2_AUTHORIZATION_URL = 'https://{auth_server_host}/oauth2/authorize/' RIDI_OAUTH2_TOKEN_URL: 'https://{auth_server_host}/oauth2/token/' - REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'ridi_django_oauth2.rest_framework.authentication.OAuth2Authentication', ) } + +# OPTIONAL + +# RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO 는 user model이 `u_idx` col과 호환되지 않을시 사용합니다. + +def _get_user_from_token_info(token_info): + user, _ = get_user_model().objects.get_or_create(idx=token_info.u_idx) + return user + +RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO = _get_user_from_token_info ``` diff --git a/ridi_django_oauth2/config.py b/ridi_django_oauth2/config.py index 67c1c63..33fc952 100644 --- a/ridi_django_oauth2/config.py +++ b/ridi_django_oauth2/config.py @@ -1,3 +1,5 @@ +from typing import Callable, Optional + from django.conf import settings @@ -6,6 +8,7 @@ class _SettingKeyName: COOKIE_DOMAIN = 'RIDI_OAUTH2_COOKIE_DOMAIN' ACCESS_TOKEN_COOKIE_KEY = 'RIDI_OAUTH2_ACCESS_TOKEN_COOKIE_KEY' REFRESH_TOKEN_COOKIE_KEY = 'RIDI_OAUTH2_REFRESH_TOKEN_COOKIE_KEY' + GET_USER_FROM_TOKEN_INFO = 'RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO' class _Default: @@ -21,6 +24,8 @@ class _Default: _RIDI_OAUTH2_KEY_URL = getattr(settings, _SettingKeyName.KEY_URL) +_RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO = getattr(settings, _SettingKeyName.GET_USER_FROM_TOKEN_INFO, None) + class RidiOAuth2Config: @staticmethod @@ -38,3 +43,7 @@ def get_access_token_cookie_key() -> str: @staticmethod def get_refresh_token_cookie_key() -> str: return _RIDI_REFRESH_TOKEN_COOKIE_KEY + + @staticmethod + def get_user_from_token_info_callable() -> Optional[Callable]: + return _RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO if callable(_RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO) else None diff --git a/ridi_django_oauth2/middlewares.py b/ridi_django_oauth2/middlewares.py index d3f9592..bfec056 100644 --- a/ridi_django_oauth2/middlewares.py +++ b/ridi_django_oauth2/middlewares.py @@ -2,8 +2,11 @@ from django.contrib.auth.models import AnonymousUser from django.utils.deprecation import MiddlewareMixin +from ridi_django_oauth2.config import RidiOAuth2Config from ridi_django_oauth2.response import HttpUnauthorizedResponse from ridi_django_oauth2.utils.token import get_token_from_cookie, get_token_info +from ridi_oauth2.client.dtos import TokenData +from ridi_oauth2.introspector.dtos import AccessTokenInfo from ridi_oauth2.introspector.exceptions import PublicKeyException @@ -20,9 +23,20 @@ def process_request(self, request): return HttpUnauthorizedResponse() if token_info is not None: - user, _ = get_user_model().objects.get_or_create(u_idx=token_info.u_idx) - user.token = token - user.token_info = token_info - request.user = user + self._set_user_in_request(request, token_info, token) return None + + @staticmethod + def _set_user_in_request(request, token_info: AccessTokenInfo, token: TokenData): + get_user_from_token_info = RidiOAuth2Config.get_user_from_token_info_callable() + + if get_user_from_token_info: + user = get_user_from_token_info(token_info) + + else: + user, _ = get_user_model().objects.get_or_create(u_idx=token_info.u_idx) + + user.token = token + user.token_info = token_info + request.user = user diff --git a/ridi_django_oauth2_lib/__init__.py b/ridi_django_oauth2_lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/runtests.py b/runtests.py index 0728ffa..affb956 100644 --- a/runtests.py +++ b/runtests.py @@ -3,9 +3,16 @@ import django from django.conf import settings +from django.contrib.auth import get_user_model sys.path.append(os.path.abspath('./src')) + +def _get_user_from_token_info(token_info): + user, _ = get_user_model().objects.get_or_create(u_idx=token_info.u_idx) + return user + + SETTINGS_DICT = { 'DEBUG': True, 'USE_TZ': True, @@ -32,6 +39,7 @@ 'RIDI_OAUTH2_AUTHORIZATION_URL': 'http://localhost/oauth2/authorize/', 'RIDI_OAUTH2_TOKEN_URL': 'http://localhost/oauth2/token/', 'RIDI_OAUTH2_KEY_URL': 'https://account.dev.ridi.io/oauth2/keys/public', + 'RIDI_OAUTH2_GET_USER_FROM_TOKEN_INFO': _get_user_from_token_info } diff --git a/setup.py b/setup.py index b9c768a..3521b62 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import find_packages, setup -version = '1.0.1' +version = '1.0.2' # When the project is installed by pip, this is the specification that is used to install its dependencies. install_requires = [