Skip to content

Commit

Permalink
Merge pull request #35 from ridi/feature/user_handler
Browse files Browse the repository at this point in the history
Feature/user handler
  • Loading branch information
jaquan-paik authored Oct 11, 2019
2 parents 0d1a9d9 + 4c5f43d commit b9cf8ac
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
9 changes: 9 additions & 0 deletions ridi_django_oauth2/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Callable, Optional

from django.conf import settings


Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
22 changes: 18 additions & 4 deletions ridi_django_oauth2/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Empty file.
8 changes: 8 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down

0 comments on commit b9cf8ac

Please sign in to comment.