Django RestFramework OAuth2 - Tirrilee/TechTalk GitHub Wiki
INSTALLED_APPS = (
...
# OAuth
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
)
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
# OAuth
# 'oauth2_provider.ext.rest_framework.OAuth2Authentication', # django-oauth-toolkit < 1.0.0
'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # django-oauth-toolkit >= 1.0.0
'rest_framework_social_oauth2.authentication.SocialAuthentication',
)
}
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
# OAuth
'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # django-oauth-toolkit >= 1.0.0
'rest_framework_social_oauth2.authentication.SocialAuthentication',
)
}
AUTHENTICATION_BACKENDS = (
# Facebook OAuth2
'social_core.backends.facebook.FacebookAppOAuth2',
'social_core.backends.facebook.FacebookOAuth2',
# django-rest-framework-social-oauth2
'rest_framework_social_oauth2.backends.DjangoOAuth2',
# Django
'django.contrib.auth.backends.ModelBackend',
)
# Facebook configuration
SOCIAL_AUTH_FACEBOOK_KEY = '<your app id goes here>'
SOCIAL_AUTH_FACEBOOK_SECRET = '<your app secret goes here>'
# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from facebook.
# Email is not sent by default, to get it, you must request the email permission:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'id, name, email'
}AUTHENTICATION_BACKENDS = (
# KAKAO OAuth2
'social_core.backends.kakao.KakaoOAuth2',
)
# Kakao configuration
SOCIAL_AUTH_KAKAO_KEY = '<your kakao key goes here>'
SOCIAL_AUTH_KAKAO_SCOPE = ['email']SOCIAL_AUTH_PIPELINE = (
# user์ ๋ํด์ ์ป์ ์ ์๋ ์ ๋ณด์, ์ดํ์ user instance๋ฅผ ์์ฑํ ์ ์๋ ๊ฐ๋จํ format
'social_core.pipeline.social_auth.social_details',
# ์ฐ๋ฆฌ๊ฐ ๊ถํ์ ์ค ์ด๋ค ์๋น์ค๋ก๋ถํฐ ์ป์ social uid
'social_core.pipeline.social_auth.social_uid',
# ํ์ฌ auth process์ validation์ ํ์ธ
# email์ด๋ domain์ด whitelists์ ์ ์ฉ์ด ๋๋์ง ๋ฑ
'social_core.pipeline.social_auth.auth_allowed',
# social-account๊ฐ ์ด๋ฏธ site์ ์ํด์๋์ง ์ฌ๋ถ๋ฅผ ์ฒดํฌ
'social_core.pipeline.social_auth.social_user',
# username์ ๋ง๋ค๊ณ ๋ง์ง๋ง์ random string์ ๋ถ์ (์ถฉ๋์ด ์์ ๋)
'social_core.pipeline.user.get_username',
# email address๋ฅผ ํ์ธํ๊ธฐ ์ํด validation email์ ๋ณด๋
# 'social_core.pipeline.mail.mail_validation',
# ํ์ฌ ํ ๋น๋ social detail์ ๋น์ทํ ์ด๋ฉ์ผ์ ๊ฐ์ง ๋ค๋ฅธ ์ฌ์ฉ์๋ฅผ ์ถ๊ฐ
# 'social_core.pipeline.social_auth.associate_by_email',
# ์ฌ์ฉ์๋ฅผ ์ฐพ์ง ๋ชปํ๋ค๋ฉด User ์์ฑ
'social_core.pipeline.user.create_user',
# ์ค๊ฐ์ Profile์ ๋ฑ๋กํด์ผํ๊ธฐ ๋๋ฌธ์
'profiles.pipeline.create_profile', # ์๋ก ๋ง๋ค์ด์ ๋ฑ๋กํ pipeline
# social account์ ํ ๋น๋ record ์์ฑ
'social_core.pipeline.social_auth.associate_user',
# social record ์์ ํ ๋น๋ ๊ฐ๊ณผ ํจ๊ป extra_data field๋ฅผ ๋ง๋ถ์
'social_core.pipeline.social_auth.load_extra_data',
# auth service์์ ๋ฐ๋ ๋ฐ์ดํฐ ์ ๋ณด๋ฅผ ์
๋ฐ์ดํธ
'social_core.pipeline.user.user_details',
)from urllib.request import urlopen
from django.core.files.base import ContentFile
from django.contrib.auth.models import User
from profiles.models import Profile
def create_profile(backend, user, response, *args, **kwargs):
profile = Profile(user=user) if kwargs['is_new'] else Profile.objects.get(user__username=user.username)
if backend.name == 'facebook':
profile.email = response.get('email', '')
elif backend.name == 'kakao':
properties = response.get('properties', '')
if properties != '':
profile.nick_name = properties.nickname
else:
return 0
profile.save()urlpatterns = [
path('auth/', include('rest_framework_social_oauth2.urls')),
] django admin์ Application model ์ ์๋ก์ด Application ํ ๊ฐ๋ฅผ ๋ฑ๋กํ๋ค.
- Client ID : ์นด์นด์คํก์์ ์ป์ด์จ Key, Facebook์์ ์ป์ด์จ APP ID
- User ์ ํ
- Client Type : Public
- Authorization Grant Type : Client credentials
localhost:8000/auth/convert-token ์์ POST ๋ก
{
"grant_type" : "convert_token",
"client_id" :"kakaotalk client id",
"backend":"kakao",
"token":"access token"
}
๋ฅผ ๋ณด๋ด๋ฉด response๊ฐ ์จ๋ค.
$ mkdir .ebextensions
$ vi django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: sinabro/wsgi.py # wsgi.py ํ์ผ์ด ์๋ ๊ฒฝ๋ก
$ eb init
$ eb create
$ eb deploy
WSGIPassAuthorization๋ ๊ธฐ๋ณธ์ ์ผ๋ก OFF ๋์ด์๋๋ฐ ์ด ๊ฐ์ ON ์ผ๋ก ๋ณ๊ฒฝํด์ผํ๋ค.
๋ง์ฝ OFF๋ก ํด๋์ผ๋ฉด 401 UnAuthorized / Invalid token header. No credentials provided ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
WSGIPassAuthorizaion ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
.ebextensions ์๋์ wsgi_custom.config (์ด๋ฆ์ ์๊ด์์) ์ ๋ง๋ค๊ณ
files:
"/etc/httpd/conf.d/wsgihacks.conf":
mode: "000644"
owner: root
group: root
content: |
WSGIPassAuthorization On
ํด๋น ๊ฐ์ ์ถ๊ฐํ๋ค.
{
"grant_type" : "convert_token",
"client_id" :"",
"backend":"google-oauth2",
"token":"ya29.GlyCBha5vkQ_r2vok-dMeC1gyNFOVGpkgx_OA_-On1pw0โฆn-YEQ3G8amzhonFw8kWiew4SOQ5HkLZWFF8mg2EjfeIimUiEg"
}
{
"grant_type" : "convert_token",
"client_id" :"",
"backend":"facebook",
"token":"EAAcp1dqQxVYBAHC4BV2FskQigFMlP4qv3nO2sFAOZAkS8X1nZBTd3XTcLQzoDDuAq9RKh9ZBKa79FZAekrz5TSHl49iI2eNF3L1qUihqmomC7jTNRci9UyoEas6CZCCApbIo7XIcVCGM4g3mhkqiNXyILZAZBX3OWNJzYKP4kUxKW1CL8oMhYS8yTDgSdtS0YoZD"
}
{
"grant_type" : "convert_token",
"client_id" :"",
"backend":"kakao",
"token":"Kev3-by9CquBv17E7cIo9eC2MEyb-OJIno6cWQoqAuYAAAFoZyx2AA"
}
{
"grant_type":"refresh_token",
"client_id":"",
"refresh_token":"fMDnbxKxvtiuSZFuvDElyGR15Ybe3N"
}