Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social Oauth需要的潜在后端代码 #63

Merged
6 commits merged into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER': 'users.serializers.UserDetailsSerializer',
}
SOCIAL_LOGIN_GOOGLE_CALLBACK_URL = os.environ.get(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是google还是github?另外我记得要使用第三方登录是要将对应的provider加入app的?

Copy link
Member Author

@tinyx tinyx Apr 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯是github,复制粘贴的错误,已经修正。provider已经加了的,你忘了我们现在已经可以用github登陆了么~这个只是配置REST的流程

'SOCIAL_LOGIN_GOOGLE_CALLBACK_URL',
'http://localhost:8000/social-auth/github/loginsuccess'
)

REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'users.serializers.UserRegistrationSerializer',
Expand Down
27 changes: 26 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import DefaultRouter
from rest_framework_jwt.views import refresh_jwt_token
from rest_auth.registration.views import (
SocialAccountListView, SocialAccountDisconnectView
)

from notifications_extension.views import NotificationViewSet
from posts.views import PostViewSet
from replies.views import ReplyViewSet
from tags.views import TagViewSet
from users.views import EmailAddressViewSet, LoginViewCustom, UserViewSets
from users.views import (
EmailAddressViewSet,
LoginViewCustom,
RegisterViewCustom,
ConfirmEmailView,
UserViewSets,
GitHubLogin,
GitHubConnect
)

router = DefaultRouter()
router.register(r'posts', PostViewSet)
Expand All @@ -39,7 +50,21 @@
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url(r'^rest-auth/login/$', LoginViewCustom.as_view(), name='rest_login'),
url(r'^rest-auth/registration/$', RegisterViewCustom.as_view(), name='rest_register'),
url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$',
ConfirmEmailView.as_view(),
name='account_confirm_email'),
url(r'^rest-auth/github/login/$', GitHubLogin.as_view(), name='github_login'),
url(r'^rest-auth/github/connect/$', GitHubConnect.as_view(), name='github_connect'),
url(r'^rest-auth/socialaccounts/$',
SocialAccountListView.as_view(),
name='social_account_list'),
url(r'^rest-auth/socialaccounts/(?P<pk>\d+)/disconnect/$',
SocialAccountDisconnectView.as_view(),
name='social_account_disconnect'),
url(r'^rest-auth/jwt-refresh/', refresh_jwt_token),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^api-auth/', include('rest_framework.urls')), # 仅仅用于测试
url(r'^', include(router.urls)),
url(r'^docs/', include_docs_urls(title='Django中文社区 API'))
Expand Down
28 changes: 27 additions & 1 deletion users/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import math
import random

from django.conf import settings
from django.db.models import Sum
from rest_auth.registration.views import LoginView
from allauth.account.views import ConfirmEmailView as AllAuthConfirmEmailView
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from rest_auth.registration.views import LoginView, SocialLoginView, SocialConnectView, RegisterView
from rest_framework import mixins, permissions, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
Expand All @@ -18,13 +22,35 @@
from .serializers import EmailAddressSerializer, UserDetailsSerializer


class RegisterViewCustom(RegisterView):
"""
注册视图取消authentication_class一次避免CSRF校验
"""
authentication_classes = ()


class LoginViewCustom(LoginView):
"""
登陆视图取消authentication_class以此避免CSRF校验
"""
authentication_classes = ()


class ConfirmEmailView(AllAuthConfirmEmailView):
template_name = 'account/email_confirm.html'


class GitHubLogin(SocialLoginView):
authentication_classes = ()
adapter_class = GitHubOAuth2Adapter
client_class = OAuth2Client
callback_url = getattr(settings, 'SOCIAL_LOGIN_GOOGLE_CALLBACK_URL')


class GitHubConnect(SocialConnectView):
adapter_class = GitHubOAuth2Adapter


class UserViewSets(viewsets.GenericViewSet):
queryset = User.objects.all()
permission_classes = [AllowAny, ]
Expand Down