From e13c1f2f6f9d59746a537ae8439bd1e99d49494e Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Fri, 19 Aug 2016 17:00:06 -0700 Subject: [PATCH] Add Django Samples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contains two sets of samples - one for the “Google auth” system and one for the “Django user auth” system. --- samples/django/README.md | 17 +++ samples/django/django_user/manage.py | 10 ++ .../django/django_user/myoauth/__init__.py | 0 .../django/django_user/myoauth/settings.py | 117 ++++++++++++++++++ samples/django/django_user/myoauth/urls.py | 31 +++++ samples/django/django_user/myoauth/wsgi.py | 21 ++++ samples/django/django_user/polls/__init__.py | 0 samples/django/django_user/polls/models.py | 34 +++++ .../django_user/polls/templates/index.html | 1 + .../polls/templates/registration/login.html | 47 +++++++ samples/django/django_user/polls/views.py | 44 +++++++ samples/django/django_user/requirements.txt | 3 + samples/django/google_user/manage.py | 10 ++ .../django/google_user/myoauth/__init__.py | 0 .../django/google_user/myoauth/settings.py | 109 ++++++++++++++++ samples/django/google_user/myoauth/urls.py | 27 ++++ samples/django/google_user/myoauth/wsgi.py | 21 ++++ samples/django/google_user/polls/__init__.py | 0 samples/django/google_user/polls/index.html | 1 + samples/django/google_user/polls/models.py | 26 ++++ .../google_user/polls/templates/index.html | 1 + .../polls/templates/registration/login.html | 47 +++++++ samples/django/google_user/polls/views.py | 45 +++++++ samples/django/google_user/requirements.txt | 3 + 24 files changed, 615 insertions(+) create mode 100644 samples/django/README.md create mode 100755 samples/django/django_user/manage.py create mode 100644 samples/django/django_user/myoauth/__init__.py create mode 100644 samples/django/django_user/myoauth/settings.py create mode 100644 samples/django/django_user/myoauth/urls.py create mode 100644 samples/django/django_user/myoauth/wsgi.py create mode 100644 samples/django/django_user/polls/__init__.py create mode 100644 samples/django/django_user/polls/models.py create mode 100644 samples/django/django_user/polls/templates/index.html create mode 100644 samples/django/django_user/polls/templates/registration/login.html create mode 100644 samples/django/django_user/polls/views.py create mode 100644 samples/django/django_user/requirements.txt create mode 100755 samples/django/google_user/manage.py create mode 100644 samples/django/google_user/myoauth/__init__.py create mode 100644 samples/django/google_user/myoauth/settings.py create mode 100644 samples/django/google_user/myoauth/urls.py create mode 100644 samples/django/google_user/myoauth/wsgi.py create mode 100644 samples/django/google_user/polls/__init__.py create mode 100644 samples/django/google_user/polls/index.html create mode 100644 samples/django/google_user/polls/models.py create mode 100644 samples/django/google_user/polls/templates/index.html create mode 100644 samples/django/google_user/polls/templates/registration/login.html create mode 100644 samples/django/google_user/polls/views.py create mode 100644 samples/django/google_user/requirements.txt diff --git a/samples/django/README.md b/samples/django/README.md new file mode 100644 index 000000000..b79b5b8c4 --- /dev/null +++ b/samples/django/README.md @@ -0,0 +1,17 @@ +# Django Samples + +These two sample Django apps provide a skeleton for the two main use cases of the +Django contrib helpers + +## google_user + +This is the simpler use case of the library and assumes you are using Google OAuth as your primary +authorization and authentication mechanism for your app. Users log in with their Google ID and +their OAauth2 credentials are stored inside the session. Please see the core docs for + usage examples. + +## django_user + +This is the use case where the application is already using the Django authorization system and +has a Django model with a `django.contrib.auth.models.User` field, and would like to attach +a Google OAuth2 credentials object to that model. Please see the core docs for usage examples. diff --git a/samples/django/django_user/manage.py b/samples/django/django_user/manage.py new file mode 100755 index 000000000..7a07babe9 --- /dev/null +++ b/samples/django/django_user/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myoauth.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/samples/django/django_user/myoauth/__init__.py b/samples/django/django_user/myoauth/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/samples/django/django_user/myoauth/settings.py b/samples/django/django_user/myoauth/settings.py new file mode 100644 index 000000000..066001fe1 --- /dev/null +++ b/samples/django/django_user/myoauth/settings.py @@ -0,0 +1,117 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'eiw+mvmua#98n@p2xq+c#liz@r2&#-s07nkgz)+$zcl^o4$-$o' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +# Application definition + +INSTALLED_APPS = ( + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'polls', + 'oauth2client.contrib.django_util' + +) + +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.security.SecurityMiddleware', +) + +ROOT_URLCONF = 'myoauth.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'myoauth.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/1.8/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + +# Internationalization +# https://docs.djangoproject.com/en/1.8/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.8/howto/static-files/ + +STATIC_URL = '/static/' + +GOOGLE_OAUTH2_CLIENT_ID = 'YOUR_CLIENT_ID' + +GOOGLE_OAUTH2_CLIENT_SECRET = 'YOUR_CLIENT_SECRET' + +GOOGLE_OAUTH2_SCOPES = ('email', 'https://www.googleapis.com/auth/calendar', + 'profile') + +GOOGLE_OAUTH2_STORAGE_MODEL = { + 'model': 'polls.models.CredentialsModel', + 'user_property': 'user_id', + 'credentials_property': 'credential' +} + +LOGIN_URL = '/login' diff --git a/samples/django/django_user/myoauth/urls.py b/samples/django/django_user/myoauth/urls.py new file mode 100644 index 000000000..d54f64168 --- /dev/null +++ b/samples/django/django_user/myoauth/urls.py @@ -0,0 +1,31 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.conf.urls import include, url +from django.contrib import admin +import django.contrib.auth.views +from polls.views import get_profile_optional, get_profile_required, index + +from oauth2client.contrib.django_util.site import urls as oauth2_urls + + +urlpatterns = [ + url(r'^$', index), + url(r'^profile_required$', get_profile_required), + url(r'^profile_enabled$', get_profile_optional), + url(r'^admin/', include(admin.site.urls)), + url(r'^login', django.contrib.auth.views.login, name="login") +] + +urlpatterns += [url(r'^oauth2/', include(oauth2_urls))] diff --git a/samples/django/django_user/myoauth/wsgi.py b/samples/django/django_user/myoauth/wsgi.py new file mode 100644 index 000000000..39ee64865 --- /dev/null +++ b/samples/django/django_user/myoauth/wsgi.py @@ -0,0 +1,21 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myoauth.settings") + +application = get_wsgi_application() diff --git a/samples/django/django_user/polls/__init__.py b/samples/django/django_user/polls/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/samples/django/django_user/polls/models.py b/samples/django/django_user/polls/models.py new file mode 100644 index 000000000..584b1c47f --- /dev/null +++ b/samples/django/django_user/polls/models.py @@ -0,0 +1,34 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.contrib.auth.models import User +from django.db import models + +from oauth2client.contrib.django_util.models import CredentialsField + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + +class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + +class CredentialsModel(models.Model): + user_id = models.OneToOneField(User) + credential = CredentialsField() diff --git a/samples/django/django_user/polls/templates/index.html b/samples/django/django_user/polls/templates/index.html new file mode 100644 index 000000000..802992c42 --- /dev/null +++ b/samples/django/django_user/polls/templates/index.html @@ -0,0 +1 @@ +Hello world diff --git a/samples/django/django_user/polls/templates/registration/login.html b/samples/django/django_user/polls/templates/registration/login.html new file mode 100644 index 000000000..3fa2983c8 --- /dev/null +++ b/samples/django/django_user/polls/templates/registration/login.html @@ -0,0 +1,47 @@ +{# +# Copyright 2016 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#} + +{% if form.errors %} +

Your username and password didn't match. Please try again.

+{% endif %} + +{% if next %} +{% if user.is_authenticated %} +

Your account doesn't have access to this page. To proceed, + please login with an account that has access.

+{% else %} +

Please login to see this page.

+{% endif %} +{% endif %} + +
+ {% csrf_token %} + + + + + + + + + +
{{ form.username.label_tag }}{{ form.username }}
{{ form.password.label_tag }}{{ form.password }}
+ + + +
+ + diff --git a/samples/django/django_user/polls/views.py b/samples/django/django_user/polls/views.py new file mode 100644 index 000000000..8943642be --- /dev/null +++ b/samples/django/django_user/polls/views.py @@ -0,0 +1,44 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.http import HttpResponse +from django.template import loader + +from oauth2client.contrib.django_util.decorators import ( + oauth_enabled, oauth_required) + + +def index(request): + template = loader.get_template('index.html') + return HttpResponse(template.render({}, request)) + + +@oauth_required +def get_profile_required(request): + resp, content = request.oauth.http.request( + 'https://www.googleapis.com/plus/v1/people/me') + return HttpResponse(content) + + +@oauth_enabled +def get_profile_optional(request): + if request.oauth.has_credentials(): + # this could be passed into a view + # request.oauth.http is also initialized + return HttpResponse('User email: {}'.format( + request.oauth.credentials.id_token['email'])) + else: + return HttpResponse('Here is an OAuth Authorize link:' + 'Authorize' + .format(request.oauth.get_authorize_redirect())) diff --git a/samples/django/django_user/requirements.txt b/samples/django/django_user/requirements.txt new file mode 100644 index 000000000..b64fd95c5 --- /dev/null +++ b/samples/django/django_user/requirements.txt @@ -0,0 +1,3 @@ +Django==0.10.0 +oauth2client== +jsonpickle==0.9.3 diff --git a/samples/django/google_user/manage.py b/samples/django/google_user/manage.py new file mode 100755 index 000000000..7a07babe9 --- /dev/null +++ b/samples/django/google_user/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myoauth.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/samples/django/google_user/myoauth/__init__.py b/samples/django/google_user/myoauth/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/samples/django/google_user/myoauth/settings.py b/samples/django/google_user/myoauth/settings.py new file mode 100644 index 000000000..c030b55c1 --- /dev/null +++ b/samples/django/google_user/myoauth/settings.py @@ -0,0 +1,109 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'eiw+mvmua#98n@p2xq+c#liz@r2&#-s07nkgz)+$zcl^o4$-$o' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +# Application definition + +INSTALLED_APPS = ( + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'polls', + 'oauth2client.contrib.django_util' + +) + +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.security.SecurityMiddleware', +) + +ROOT_URLCONF = 'myoauth.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'myoauth.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/1.8/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + +# Internationalization +# https://docs.djangoproject.com/en/1.8/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.8/howto/static-files/ + +STATIC_URL = '/static/' + +GOOGLE_OAUTH2_CLIENT_ID = 'YOUR_CLIENT_ID' + +GOOGLE_OAUTH2_CLIENT_SECRET = 'YOUR_CLIENT_SECRET' + +GOOGLE_OAUTH2_SCOPES = ('email', 'https://www.googleapis.com/auth/calendar', + 'profile') diff --git a/samples/django/google_user/myoauth/urls.py b/samples/django/google_user/myoauth/urls.py new file mode 100644 index 000000000..66f96e78d --- /dev/null +++ b/samples/django/google_user/myoauth/urls.py @@ -0,0 +1,27 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.conf.urls import include, url +from polls.views import get_profile_optional, get_profile_required, index + +from oauth2client.contrib.django_util.site import urls as oauth2_urls + + +urlpatterns = [ + url(r'^$', index), + url(r'^profile_required$', get_profile_required), + url(r'^profile_enabled$', get_profile_optional), +] + +urlpatterns += [url(r'^oauth2/', include(oauth2_urls))] diff --git a/samples/django/google_user/myoauth/wsgi.py b/samples/django/google_user/myoauth/wsgi.py new file mode 100644 index 000000000..39ee64865 --- /dev/null +++ b/samples/django/google_user/myoauth/wsgi.py @@ -0,0 +1,21 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myoauth.settings") + +application = get_wsgi_application() diff --git a/samples/django/google_user/polls/__init__.py b/samples/django/google_user/polls/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/samples/django/google_user/polls/index.html b/samples/django/google_user/polls/index.html new file mode 100644 index 000000000..802992c42 --- /dev/null +++ b/samples/django/google_user/polls/index.html @@ -0,0 +1 @@ +Hello world diff --git a/samples/django/google_user/polls/models.py b/samples/django/google_user/polls/models.py new file mode 100644 index 000000000..4a8a757bc --- /dev/null +++ b/samples/django/google_user/polls/models.py @@ -0,0 +1,26 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.db import models + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + +class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) diff --git a/samples/django/google_user/polls/templates/index.html b/samples/django/google_user/polls/templates/index.html new file mode 100644 index 000000000..802992c42 --- /dev/null +++ b/samples/django/google_user/polls/templates/index.html @@ -0,0 +1 @@ +Hello world diff --git a/samples/django/google_user/polls/templates/registration/login.html b/samples/django/google_user/polls/templates/registration/login.html new file mode 100644 index 000000000..3fa2983c8 --- /dev/null +++ b/samples/django/google_user/polls/templates/registration/login.html @@ -0,0 +1,47 @@ +{# +# Copyright 2016 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#} + +{% if form.errors %} +

Your username and password didn't match. Please try again.

+{% endif %} + +{% if next %} +{% if user.is_authenticated %} +

Your account doesn't have access to this page. To proceed, + please login with an account that has access.

+{% else %} +

Please login to see this page.

+{% endif %} +{% endif %} + +
+ {% csrf_token %} + + + + + + + + + +
{{ form.username.label_tag }}{{ form.username }}
{{ form.password.label_tag }}{{ form.password }}
+ + + +
+ + diff --git a/samples/django/google_user/polls/views.py b/samples/django/google_user/polls/views.py new file mode 100644 index 000000000..70a50a924 --- /dev/null +++ b/samples/django/google_user/polls/views.py @@ -0,0 +1,45 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from django.http import HttpResponse +from django.template import loader + +from oauth2client.contrib.django_util.decorators import ( + oauth_enabled, oauth_required) + + +def index(request): + template = loader.get_template('index.html') + return HttpResponse(template.render({}, request)) + + +@oauth_required +def get_profile_required(request): + resp, content = request.oauth.http.request( + 'https://www.googleapis.com/plus/v1/people/me') + return HttpResponse(content) + + +@oauth_enabled +def get_profile_optional(request): + if request.oauth.has_credentials(): + # this could be passed into a view + # request.oauth.http is also initialized + return HttpResponse('User email: {}'.format( + request.oauth.credentials.id_token['email'])) + else: + return HttpResponse('Here is an OAuth Authorize link:' + 'Authorize' + .format(request.oauth.get_authorize_redirect())) diff --git a/samples/django/google_user/requirements.txt b/samples/django/google_user/requirements.txt new file mode 100644 index 000000000..a1b218668 --- /dev/null +++ b/samples/django/google_user/requirements.txt @@ -0,0 +1,3 @@ +Django==0.10.0 +oauth2client==3.0.0 +jsonpickle==0.9.3