Add mock OAuth2 provider to testproj

master
Cristi Vîjdea 2018-12-21 01:02:01 +02:00 committed by Cristi Vijdea
parent bbed2acf06
commit b385228f7d
7 changed files with 116 additions and 5 deletions

View File

@ -8,3 +8,4 @@ djangorestframework-camel-case>=0.2.0
djangorestframework-recursive>=0.1.2 djangorestframework-recursive>=0.1.2
dj-database-url>=0.4.2 dj-database-url>=0.4.2
user_agents>=1.1.0 user_agents>=1.1.0
django-oauth-toolkit>=1.2.0

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,8 @@ import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from django.urls import reverse_lazy from django.urls import reverse_lazy
from testproj.util import full_url_lazy, static_lazy
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
ALLOWED_HOSTS = [ ALLOWED_HOSTS = [
@ -22,6 +24,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework', 'rest_framework',
'oauth2_provider',
'corsheaders', 'corsheaders',
'drf_yasg', 'drf_yasg',
@ -64,6 +67,8 @@ TEMPLATES = [
WSGI_APPLICATION = 'testproj.wsgi.application' WSGI_APPLICATION = 'testproj.wsgi.application'
LOGIN_URL = reverse_lazy('admin:login')
# Password validation # Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
@ -90,8 +95,15 @@ REST_FRAMEWORK = {
) )
} }
# drf-yasg OAUTH2_CLIENT_ID = '12ee6bgxtpSEgP8TioWcHSXOiDBOUrVav4mRbVEs'
OAUTH2_CLIENT_SECRET = '5FvYALo7W4uNnWE2ySw7Yzpkxh9PSf5GuY37RvOys00ydEyph64dbl1ECOKI9ceQAKoz0JpiVQtq0DUnsxNhU3ubrJgZ9YbtiXymbLGJq8L7n4fiER7gXbXaNSbze3BN'
OAUTH2_APP_NAME = 'drf-yasg OAuth2 provider'
OAUTH2_REDIRECT_URL = full_url_lazy(static_lazy('drf-yasg/swagger-ui-dist/oauth2-redirect.html'))
OAUTH2_AUTHORIZE_URL = full_url_lazy(reverse_lazy('oauth2_provider:authorize'))
OAUTH2_TOKEN_URL = full_url_lazy(reverse_lazy('oauth2_provider:token'))
# drf-yasg
SWAGGER_SETTINGS = { SWAGGER_SETTINGS = {
'LOGIN_URL': reverse_lazy('admin:login'), 'LOGIN_URL': reverse_lazy('admin:login'),
'LOGOUT_URL': '/admin/logout', 'LOGOUT_URL': '/admin/logout',
@ -114,7 +126,22 @@ SWAGGER_SETTINGS = {
'type': 'apiKey', 'type': 'apiKey',
'name': 'auth', 'name': 'auth',
'in': 'query' 'in': 'query'
},
'OAuth2 password': {
'type': 'oauth2',
'flow': 'password',
'tokenUrl': OAUTH2_TOKEN_URL,
'scopes': {
'read': 'Read everything.',
'write': 'Write everything,',
}
} }
},
'OAUTH2_REDIRECT_URL': OAUTH2_REDIRECT_URL,
'OAUTH2_CONFIG': {
'clientId': OAUTH2_CLIENT_ID,
'clientSecret': OAUTH2_CLIENT_SECRET,
'appName': OAUTH2_APP_NAME,
} }
} }
@ -126,13 +153,9 @@ REDOC_SETTINGS = {
# https://docs.djangoproject.com/en/1.11/topics/i18n/ # https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC' TIME_ZONE = 'UTC'
USE_I18N = True USE_I18N = True
USE_L10N = True USE_L10N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)

View File

@ -60,6 +60,8 @@ urlpatterns = [
url(r'^$', root_redirect), url(r'^$', root_redirect),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
url(r'^snippets/', include('snippets.urls')), url(r'^snippets/', include('snippets.urls')),
url(r'^articles/', include('articles.urls')), url(r'^articles/', include('articles.urls')),
url(r'^users/', include('users.urls')), url(r'^users/', include('users.urls')),

View File

@ -0,0 +1,17 @@
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.urls import NoReverseMatch
from django.utils.encoding import force_text
from django.utils.functional import lazy
def full_url(absolute_path):
try:
return "http://test.local:8002" + force_text(absolute_path)
except NoReverseMatch:
# if absolute_path is a resolve_lazy, it might point to an invalid name
# just ignore it if it does
return "http://test.local:8002/no-reverse-match/"
full_url_lazy = lazy(full_url, str, type(None))
static_lazy = lazy(static, str)

View File

@ -0,0 +1,42 @@
# Generated by Django 2.1.3 on 2018-12-19 07:57
from django.conf import settings
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db import migrations, IntegrityError
from testproj.util import full_url_lazy
def add_oauth_apps(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
User = apps.get_model(settings.AUTH_USER_MODEL)
Application = apps.get_model('oauth2_provider', 'application')
user = User.objects.get(username='admin')
oauth2_apps = [
{
"user": user,
"client_type": "public",
"authorization_grant_type": "password",
"client_id": settings.OAUTH2_CLIENT_ID,
"client_secret": settings.OAUTH2_CLIENT_SECRET,
"redirect_uris": settings.OAUTH2_REDIRECT_URL,
"name": settings.OAUTH2_APP_NAME
}
]
for app in oauth2_apps:
Application.objects.get_or_create(client_id=app['client_id'], defaults=app)
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('oauth2_provider', '0006_auto_20171214_2232'),
('users', '0001_create_admin_user'),
]
operations = [
migrations.RunPython(add_oauth_apps)
]

View File

@ -27,6 +27,13 @@ securityDefinitions:
in: header in: header
name: Authorization name: Authorization
type: apiKey type: apiKey
OAuth2 password:
flow: password
scopes:
read: Read everything.
write: Write everything,
tokenUrl: http://test.local:8002/o/token/
type: oauth2
Query: Query:
in: query in: query
name: auth name: auth
@ -34,6 +41,7 @@ securityDefinitions:
security: security:
- Basic: [] - Basic: []
- Bearer: [] - Bearer: []
- OAuth2 password: []
- Query: [] - Query: []
paths: paths:
/articles/: /articles/: