2015-09-05 157 views
4

我正在開發iOS應用程序並將Django用於後端。有兩個應用程序,我用在Django將令牌傳遞給客戶端

  • Django OAuth Toolkit支持OAuth認證
  • Python Social Auth支持社會認證

社會認證過程應該是:

  1. GET本地主機/ login/{application}
  2. 應用程序站點上的身份驗證
  3. 重定向到localhost /完整/ {應用}
  4. 獲取{應用}的訪問令牌
  5. 創建我的服務器的訪問令牌的新用戶,並將其與{應用}相關聯的訪問令牌
  6. 重定向到本地主機/帳戶/配置文件

然後,我可以使用我的服務器的訪問令牌與{application}進行通信。

但客戶端會看到瀏覽器以localhost/login/{application}開頭並以localhost/accounts/profile結尾,但仍不知道我的服務器的訪問令牌是什麼,所以我的問題是如何通過訪問令牌給客戶端?

一種解決方案是將訪問令牌重定向爲localhost/accounts/profile?token = MyServerToken,但是如何在重定向到配置文件url時添加參數?

+0

什麼是客戶打算與服務器的訪問令牌呢? –

+1

我將使用服務器的訪問令牌請求數據,如果數據位於{application}上,我的服務器將使用{application}的訪問令牌請求數據並返回給客戶端。 –

回答

1

您不應該在查詢字符串中傳遞訪問令牌,如/?token=my_token。它不是一個安全的方式,絕對不推薦。

您可以使用其他一些方法是:

方法1:在響應頭

您可以設置訪問令牌的響應報頭,並使用HTTPS協議發送設置server_access_token

令牌將被髮送一次並被客戶端使用。由於響應標頭未在後續請求中傳遞,令牌只會傳遞給客戶端一次。然後,客戶端將通過在請求標頭中設置令牌來使用它來發出進一步的請求。

class MySocialApplicationRedirectView(View): 

    def get(self, request, *args, **kwargs): 
     # Here, write your code to fetch the {application}'s access token, 
     # creating a new user with your server's access token, and then 
     # associating it with {application}'s access token 

     # assign the response to a variable and set the access token as a header in the response 
     response = HttpResponseRedirect('/accounts/profile/')  
     response['X-Auth-Token'] = 'my_server_access_token'  

     # can also use the below name as 'X-' prefixed headers are deprecated  
     # response['Auth-Token'] = 'my_server_access_token' 

     return response 

客戶端然後可以從標題中檢索令牌並使用此令牌進行進一步的請求。在進一步的請求中,他必須在請求標題中發送訪問令牌。

方法2:設置server_access_token作爲cookie

另一種選擇是設置server_access_token餅乾作爲@Ben提到你的迴應。

response.set_cookie()將在響應中設置server_access_token cookie,然後客戶端可以讀取cookie並在請求頭中的其他請求中發送該cookie。

class MySocialApplicationRedirectView(View): 

     def get(self, request, *args, **kwargs): 
      # Here, write your code to fetch the {application}'s access token, 
      # creating a new user with your server's access token, and then 
      # associating it with {application}'s access token 

      # assign the response to a variable and set the access token as a cookie in the response object 
      response = HttpResponseRedirect('/accounts/profile/')  
      response.set_cookie(key, value='my_server_access_token', ..other parameters) 
      return response 

注:出於安全,所有的要求(包括獲取和使用代幣)必須使用HTTPS端點。

+0

感謝您的回答,您的意思是我需要直接更改Python社區認證的代碼嗎? –

+0

這複製了其他更多符合標準的功能。會話cookie是爲了這個確切目的而創建的,並且它們可能已經由客戶端代碼支持而不需要任何特殊處理,因爲會話本身由後端管理。 –

+0

@ybbaigo當你正在做4,5,6分的問題時,你必須這樣做。在重定向到'localhost/accounts/profile'時,您可以設置標題.'MySocialApplicationRedirectView'是社交應用程序將重定向到您的服務器正在偵聽的URL的視圖。 –

0

它沒有回答您的具體問題,但我使用TastyPie解決了類似的問題。它非常簡單,不需要處理多個應用程序,但是因爲它爲任何給定的應用程序提供了一個API,所以不應該是一個問題。

1

對於有問題的用戶,您可能已經擁有了在Django會話中需要的內容。也就是說,假如你使用的是會話中間件(這種類型的身份驗證幾乎是不可能的),那麼你的身份提供者特定的令牌通常會被填充到SocialUser模型的extra_data字典中。

例如,假設你有Django的用戶模型的引用(可以稱之爲user):

access_token = user.social_auth.get(provider='google-oauth2').extra_data['access_token'] 

不幸的是,具體取決於您正在使用的後端變化。請記住,這些工具旨在讓用戶針對您的應用進行身份驗證,而不是讓您針對各種身份提供商公開的特定於產品的API執行任意操作。

至於將這些令牌傳遞給客戶端,我需要更多地瞭解您的用例。有可能是身份提供者在身份驗證流程中在客戶端上設置了一些會話cookie。例如,如果您使用Facebook登錄,他們會設置一些由Facebook客戶端JavaScript API自動檢索的Cookie。因此,服務器和客戶端之間不需要顯式共享令牌。

否則,如果你必須自己做,把它們存儲在一個安全的會話cookie如下:

response.set_cookie(social_auth_tokens, 
    value=your_data_here, 
    max_age=None, #cookie will expire at end of user session 
    expires=None, 
    path='/', 
    domain=None, #only readable by this domain 
    secure=True, #only transmitted over https 
    httponly=False) #readable by scripts running on the page 
+0

絕對清除了一些疑惑。 :) –

相關問題