保护Django-OAuth工具箱视图

2024-04-20 08:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我们希望在后端实现djangoauth,以便集成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)


Tags: django用户ioapihttp网站readthedocsoauth
3条回答

这是一个安全问题,我建议将访问权限限制为只有具有活动帐户的超级用户,如下面的代码网址.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"))]

要排除'applications/'端点,只导入所需的URL,而不是使用整个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'))
]

将只添加客户端应用程序授权所需的URL:

^{pr2}$

要添加/删除应用程序,您可以使用Django管理站点,或者允许management_urlpatterns管理用户,如@David Chander answer:https://stackoverflow.com/a/49210935/7709003

我也在做类似的事情,我相信任何人都可以看到/o/应用程序,这是一个安全问题——据我所知,这个页面应该是一个开发工具,而不是一个生产页面。事实上,在the django-oauth-toolkit documentation中,它们有一个代码示例,对视图的访问更受限制。在

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
]

revoke token view is part of the RFC,因此需要一个。我在我的应用程序中采用了类似的方法,只包括AuthorizationView、TokenView和RevokeTokeView。在

希望有帮助!在

相关问题 更多 >