Skip to content

Commit 3b69153

Browse files
committed
init
0 parents  commit 3b69153

25 files changed

+397
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If you need to exclude files such as those generated by an IDE, use
2+
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as
3+
# described in https://git-scm.com/docs/gitignore
4+
5+
*.egg-info
6+
*.pot
7+
*.py[co]
8+
.tox/
9+
__pycache__
10+
MANIFEST
11+
dist/
12+
docs/_build/
13+
docs/locale/
14+
node_modules/
15+
tests/coverage_html/
16+
tests/.coverage
17+
build/
18+
tests/report/

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Django Oauth Demo
2+
3+
## Package
4+
使用 python-social-auth (django-social-auth is deprecated)
5+
6+
installation:
7+
pip install -r requirements.txt
8+
9+
優點:將 Oauth User model 整合,並且要實作 account method, 修改 template 相較於 django-allauth 都很簡單。
10+
11+
12+
## Run Demo
13+
14+
1. sync database model
15+
$ ./manage.py migrate
16+
17+
2. run server
18+
$ ./manage.py runserver
19+
20+
3. access endpoint
21+
using browser: http://localhost:8000/accounts/login
22+
23+
24+
## Endpoints
25+
- /accounts/login
26+
目前只支援 Oauth Login<br>
27+
在 Facebook Login 的部分,Domain name 不可以使用 127.0.0.1:8000<br>
28+
要改用 localhost:8000/accounts/login
29+
Oauth 登入成功後,會導入 "localhost:8000" home page
30+
31+
- /accounts/logout
32+
- /accounts/info
33+
console 顯示 username & user email, FB Oauth 不會取得 Email!
34+
35+
36+
## Apps
37+
38+
- accounts
39+
用於管理基本帳戶操作,目前只有實作 Google, Facebook 登入

oauth_demo/account/__init__.py

Whitespace-only changes.

oauth_demo/account/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

oauth_demo/account/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountConfig(AppConfig):
5+
name = 'account'

oauth_demo/account/migrations/__init__.py

Whitespace-only changes.

oauth_demo/account/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
# Create your models here.
4+
class UserProfile(models.Model):
5+
pass

oauth_demo/account/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

oauth_demo/account/urls.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""oauth_demo URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/1.10/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.conf.urls import url, include
14+
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
15+
"""
16+
from django.conf.urls import url, include
17+
from .views import sign_in, sign_out, get_user_info
18+
19+
urlpatterns = [
20+
url(r'^info/$', get_user_info),
21+
url(r'^login/$', sign_in),
22+
url(r'^logout', sign_out),
23+
url(r'^', include('social_django.urls', namespace='social'))
24+
]

oauth_demo/account/views.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from django.shortcuts import render_to_response, redirect
2+
from django.http import HttpResponse
3+
from django.contrib.auth import logout
4+
from django.contrib.auth.decorators import login_required
5+
6+
7+
def sign_in(request):
8+
if request.method == "GET":
9+
return render_to_response('login.html')
10+
else:
11+
# processing signin action
12+
13+
# authenticate & exception handling
14+
15+
# status 未定
16+
return HttpResponse(status=200)
17+
18+
19+
20+
def sign_out(request):
21+
if request.user.is_authenticated:
22+
print ("user has been login")
23+
logout(request)
24+
25+
return redirect("/")
26+
27+
28+
def get_user_info(request):
29+
if request.user.is_authenticated:
30+
print (request.user.username, request.user.email)
31+
else:
32+
print ("Anoymous user")
33+
34+
return HttpResponse(status=200)

oauth_demo/introduction/__init__.py

Whitespace-only changes.

oauth_demo/introduction/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

oauth_demo/introduction/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class IntroductionConfig(AppConfig):
5+
name = 'introduction'

oauth_demo/introduction/migrations/__init__.py

Whitespace-only changes.

oauth_demo/introduction/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

oauth_demo/introduction/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

oauth_demo/introduction/views.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.shortcuts import render
2+
from django.http import HttpResponse
3+
4+
# Create your views here.
5+
def home(request):
6+
return HttpResponse(status=200)

oauth_demo/manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oauth_demo.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError:
10+
# The above import may fail for some other reason. Ensure that the
11+
# issue is really that Django is missing to avoid masking other
12+
# exceptions on Python 2.
13+
try:
14+
import django
15+
except ImportError:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
)
21+
raise
22+
execute_from_command_line(sys.argv)

oauth_demo/oauth_demo/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Google Oauth2 App 權限設定
2+
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
3+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
4+
5+
# Facebook Oauth2 App 權限設定
6+
SOCIAL_AUTH_FACEBOOK_KEY = ''
7+
SOCIAL_AUTH_FACEBOOK_SECRET = ''

oauth_demo/oauth_demo/settings.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
"""
2+
Django settings for oauth_demo project.
3+
4+
Generated by 'django-admin startproject' using Django 1.10.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.10/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.10/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# import Oauth 憑證
16+
from .oauth_credentials import *
17+
18+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
19+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20+
21+
22+
# Quick-start development settings - unsuitable for production
23+
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
24+
25+
# SECURITY WARNING: keep the secret key used in production secret!
26+
SECRET_KEY = '$6794efdb^g%=gfi6!sa)-7imxaljaxa$qu7%k8$9%@^dbu@*a'
27+
28+
# SECURITY WARNING: don't run with debug turned on in production!
29+
DEBUG = True
30+
31+
ALLOWED_HOSTS = []
32+
33+
34+
# Application definition
35+
36+
INSTALLED_APPS = [
37+
'django.contrib.admin',
38+
'django.contrib.auth',
39+
'django.contrib.contenttypes',
40+
'django.contrib.sessions',
41+
'django.contrib.messages',
42+
'django.contrib.staticfiles',
43+
44+
# using python-social-auth package(django version)
45+
'social_django',
46+
'account',
47+
]
48+
49+
MIDDLEWARE = [
50+
'django.middleware.security.SecurityMiddleware',
51+
'django.contrib.sessions.middleware.SessionMiddleware',
52+
'django.middleware.common.CommonMiddleware',
53+
'django.middleware.csrf.CsrfViewMiddleware',
54+
'django.contrib.auth.middleware.AuthenticationMiddleware',
55+
'django.contrib.messages.middleware.MessageMiddleware',
56+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
57+
58+
# Oauth authentication middleware
59+
'social_django.middleware.SocialAuthExceptionMiddleware',
60+
]
61+
62+
ROOT_URLCONF = 'oauth_demo.urls'
63+
64+
TEMPLATES = [
65+
{
66+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
67+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
68+
'APP_DIRS': True,
69+
'OPTIONS': {
70+
'context_processors': [
71+
'django.template.context_processors.debug',
72+
'django.template.context_processors.request',
73+
'django.contrib.auth.context_processors.auth',
74+
'django.contrib.messages.context_processors.messages',
75+
76+
# Oauth 的頁面機制
77+
'social_django.context_processors.backends',
78+
'social_django.context_processors.login_redirect',
79+
],
80+
},
81+
},
82+
]
83+
SITE_ID = 1
84+
85+
WSGI_APPLICATION = 'oauth_demo.wsgi.application'
86+
87+
88+
# Database
89+
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
90+
91+
DATABASES = {
92+
'default': {
93+
'ENGINE': 'django.db.backends.sqlite3',
94+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
95+
}
96+
}
97+
98+
99+
# Password validation
100+
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
101+
102+
AUTH_PASSWORD_VALIDATORS = [
103+
{
104+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
105+
},
106+
{
107+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
108+
},
109+
{
110+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
111+
},
112+
{
113+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
114+
},
115+
]
116+
117+
# 驗證密碼的 Backend 機制
118+
AUTHENTICATION_BACKENDS = (
119+
'social_core.backends.google.GoogleOAuth2',
120+
'social_core.backends.facebook.FacebookOAuth2',
121+
'django.contrib.auth.backends.ModelBackend',
122+
)
123+
SOCIAL_AUTH_URL_NAMESPACE = 'social'
124+
125+
SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'email']
126+
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = "/"
127+
SOCIAL_AUTH_LOGIN_REDIRECT_URL = "/"
128+
SOCIAL_AUTH_LOGIN_URL = '/'
129+
130+
# Internationalization
131+
# https://docs.djangoproject.com/en/1.10/topics/i18n/
132+
133+
LANGUAGE_CODE = 'en-us'
134+
135+
TIME_ZONE = 'UTC'
136+
137+
USE_I18N = True
138+
139+
USE_L10N = True
140+
141+
USE_TZ = True
142+
143+
144+
# Static files (CSS, JavaScript, Images)
145+
# https://docs.djangoproject.com/en/1.10/howto/static-files/
146+
147+
STATIC_URL = '/static/'

oauth_demo/oauth_demo/urls.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""oauth_demo URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/1.10/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.conf.urls import url, include
14+
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
15+
"""
16+
from django.conf.urls import url, include
17+
from django.contrib import admin
18+
from introduction.views import home
19+
20+
urlpatterns = [
21+
url(r'^$', home),
22+
url(r'^admin/', admin.site.urls),
23+
url(r'^accounts/', include('account.urls'))
24+
]

oauth_demo/oauth_demo/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for oauth_demo project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oauth_demo.settings")
15+
16+
application = get_wsgi_application()

oauth_demo/templates/login.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>
5+
Login
6+
</title>
7+
</head>
8+
<body>
9+
<h1>Login</h1>
10+
<a href="{% url "social:begin" "google-oauth2" %}">Google+</a>
11+
<a href="{% url "social:begin" "facebook" %}">facebook</a>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)