2016-11-17 75 views
2

我們希望在後端實現Django OAuth,以整合Alexa和其他第三方API。我們一直在關注他們網站上的教程(http://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial.html),但遇到了迄今爲​​止已經逃脫的安全問題:保護Django OAuth工具包視圖

是否存在安全問題,任何用戶都可以訪問https://<oursite.com>/o/applications?如果是這樣,那麼需要採取哪些措施來阻止用戶訪問這些視圖?在SO

唯一的相關問題並沒有特別有幫助:

Secure creation of new applications in Django OAuth Toolkit

Disable or restrict /o/applications (django rest framework, oauth2)

回答

2

我在做類似的事情,我相信這是一個安全問題,任何人都可以看到/ 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和RevokeTokenView。

希望有幫助!

0
from django.contrib.auth.decorators import user_passes_test 

def is_super(user): 
    return user.is_superuser and user.is_active 

... 
    url(r'^applications/$', user_passes_test(is_super)(oauth2_views.ApplicationList.as_view()), name="list"), 
...