用于移动和浏览器API调用的中间件处理程序



我想开发一个基于Django的web服务,我的web应用程序(平台:Angular JS)和移动应用程序(平台:iOS,android,Windows Phone)将与它通信。我的django web服务应用程序只处理API调用。出于安全原因,我选择了Oauth工具包用于移动应用程序,用于web会话认证?

Settings.py for Authentication,

'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.SessionAuthentication',
    'oauth2_provider.ext.rest_framework.OAuth2Authentication',
),

我的问题:

  1. 如果我收到来自浏览器端的呼叫,我想处理基于CSRF的身份验证。
  2. 如果我接到本机移动客户端的呼叫,则调用Oauth认证。如何区分两者,以及如何处理这种认证技术。

需要你的帮助!!

您可以使用django-mobile库来检测来自移动浏览器的请求,然后调用不同的身份验证类

django-mobile库在其中定义了MobileDetectionMiddlewareSetFlavourMiddleware中间件类。

MobileDetectionMiddleware检测请求是否来自移动设备或web浏览器。

SetFlavourMiddleware class在请求中设置flavour属性。flavour有两种可能的值:

'mobile' # Mobile requests
'full' # Web requests 

在中间件以某种方式选择了正确的风味之后,它被分配给request.flavour属性。

步骤1:在应用程序中配置django-mobile

按照https://github.com/gregmuellegger/django-mobile#installation给出的步骤在应用程序中安装和配置django-mobile

配置完成后,您可以使用request.flavour来检查请求是来自移动浏览器还是web浏览器。

步骤2:创建自定义WebSessionAuthentication

既然你想在移动应用中使用OAuth的OAuth2Authentication,在web应用中使用DRF的SessionAuthentication,我们可以创建一个自定义的WebSessionAuthentication类,它将继承DRF的SessionAuthentication

WebSessionAuthentication类将不执行移动请求的身份验证。在web请求的情况下,它将执行正确的会话认证与CSRF检查。

from rest_framework.authentication import  SessionAuthentication
class WebSessionAuthentication(SessionAuthentication):
    """
    Performs session authentication for web requests
    """
    def authenticate(self, request):
        """
        Returns a `User` if the request session currently has a logged in user 
        and request is a web request
        Otherwise returns `None`.
        """
        underlying_request = request._request # get the underlying HttpRequest object
        if underlying_request.flavour == 'mobile': # check if mobile request
            return None # No authentication performed for mobile requests
        # For web requests perform DRF's original session authentication    
        return super(WebSessionAuthentication, self).authenticate(request) 

步骤3:创建自定义MobileOAuth2Authentication

MobileOAuth2Authentication类仅对mobile请求执行身份验证,即将.flavour作为mobileweb请求不鉴权

from oauth2_provider.ext.rest_framework import OAuth2Authentication
class MobileOAuth2Authentication(OAuth2Authentication):
    """
    Performs outh2 authentication for mobile requests
    """
    def authenticate(self, request):
        """
        Returns two-tuple of (user, token) if mobile authentication succeeds,
        or None otherwise.
        """
        underlying_request = request._request # get the underlying HttpRequest object
        if underlying_request.flavour == 'full': # check if web request
            return None # No authentication performed for web requests
        # For mobile requests perform OAuth2's original authentication    
        return super(MobileOAuth2Authentication, self).authenticate(request) 

步骤4:在settings

中定义认证类

创建自定义WebSessionAuthenticationMobileOuth2Authentication身份验证类后,在项目设置中定义这些身份验证类。

'DEFAULT_AUTHENTICATION_CLASSES': (
    'my_app.authentication.WebSessionAuthentication', # custom session authentication class for web requests
    'my_app.authentication.MobileOAuth2Authentication', # custom oauth2 authentication class for mobile requests
),

最新更新