我们希望在后端实现Django OAuth,以便集成Alexa和其他第三方API。我们一直在关注他们网站上的教程(http://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial.html),但遇到了一个迄今为止逃避我们的安全问题:
是否存在任何用户可以访问https://<oursite.com>/o/applications
的安全问题?如果是这样,需要采取哪些措施来阻止用户访问这些视图?
关于SO的唯一相关问题并不是特别有用:
Secure creation of new applications in Django OAuth Toolkit
Disable or restrict /o/applications (django rest framework, oauth2)
答案 0 :(得分:3)
我做了类似的事情,我认为这是一个安全问题,任何人都可以看到/ o /应用程序 - 据我所知,该页面是一个开发实用程序,而不是一个生产页面。实际上,在中,他们有一个代码示例,可以更有限地访问视图。
from django.conf.urls import url
import oauth2_provider.views as oauth2_views
from django.conf import settings
from .views import ApiEndpoint
# OAuth2 provider endpoints
oauth2_endpoint_views = [
url(r'^authorize/$', oauth2_views.AuthorizationView.as_view(), name="authorize"),
url(r'^token/$', oauth2_views.TokenView.as_view(), name="token"),
url(r'^revoke-token/$', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
]
if settings.DEBUG:
# OAuth2 Application Management endpoints
oauth2_endpoint_views += [
url(r'^applications/$', oauth2_views.ApplicationList.as_view(), name="list"),
url(r'^applications/register/$', oauth2_views.ApplicationRegistration.as_view(), name="register"),
url(r'^applications/(?P<pk>\d+)/$', oauth2_views.ApplicationDetail.as_view(), name="detail"),
url(r'^applications/(?P<pk>\d+)/delete/$', oauth2_views.ApplicationDelete.as_view(), name="delete"),
url(r'^applications/(?P<pk>\d+)/update/$', oauth2_views.ApplicationUpdate.as_view(), name="update"),
]
# OAuth2 Token Management endpoints
oauth2_endpoint_views += [
url(r'^authorized-tokens/$', oauth2_views.AuthorizedTokensListView.as_view(), name="authorized-token-list"),
url(r'^authorized-tokens/(?P<pk>\d+)/delete/$', oauth2_views.AuthorizedTokenDeleteView.as_view(),
name="authorized-token-delete"),
]
urlpatterns = [
# OAuth 2 endpoints:
url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/hello', ApiEndpoint.as_view()), # an example resource endpoint
]
the django-oauth-toolkit documentation,因此需要一个。{3}}。我在我的应用程序中采用了类似的方法,仅包括AuthorizationView,TokenView和RevokeTokenView。
希望有所帮助!
答案 1 :(得分:3)
这是一个安全问题,我建议仅限访问具有活动帐户的超级用户,如以下代码中的urls.py:
from django.contrib.auth.decorators import user_passes_test
import oauth2_provider.views as oauth2_views
def is_super(user):
return user.is_superuser and user.is_active
oauth2_endpoint_views = [
url(r'^authorize/$', oauth2_views.AuthorizationView.as_view(), name="authorize"),
url(r'^token/$', oauth2_views.TokenView.as_view(), name="token"),
url(r'^revoke-token/$', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
# the above are public but we restrict the following:
url(r'^applications/$', user_passes_test(is_super)(oauth2_views.ApplicationList.as_view()), name="list"),
...
]
urlpatterns = [url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider"))]
答案 2 :(得分:1)
要排除'applications /'端点,请仅导入所需的网址,而不要使用整个oauth2_provider.urls
:
from oauth2_provider.urls import app_name, base_urlpatterns, management_urlpatterns
urlpatterns = [
...
# oauth2
path('oauth2/', include((base_urlpatterns, app_name), namespace='oauth2_provider'))
]
仅添加客户端应用授权所需的网址:
oauth2/ ^authorize/$ [name='authorize']
oauth2/ ^token/$ [name='token']
oauth2/ ^revoke_token/$ [name='revoke-token']
oauth2/ ^introspect/$ [name='introspect']
要添加/删除应用程序,您可以使用Django管理站点,也可以允许management_urlpatterns
作为管理员用户,如@David Chander的回答:https://stackoverflow.com/a/49210935/7709003