[英]Is there a way to check if the user is an admin in AppEngine Cloud Endpoints
我正在使用AppEngine Cloud Endpoints和Javascript客戶端以及Google+登錄,我正在使用endpoints.get_current_user()
。 有沒有辦法檢查用戶是否是AppEngine管理員? 與用戶API中的users.is_current_user_admin()
類似。
謝謝
有關執行身份驗證時ID令牌和承載令牌之間差異的詳細說明,請參閱Google端點API + Chrome擴展程序,對於endpoints.get_current_user()。user_id()返回None 。
如果您不使用Android應用程序,則可以自由使用Bearer令牌,而不必擔心ID令牌的某些限制。
接下來get_current_user()
, oauth
庫提供oauth.is_current_user_admin()
方法。 正如get_current_user()
, 此方法調用 _maybe_call_get_oauth_user
,然后檢查一個簡單的環境變量。
正如其他答案所述:
該
oauth.get_current_user()
調用僅僅是昂貴的, 如果它使RPC。_maybe_call_get_oauth_user
方法存儲上次調用的值,因此第二次調用oauth.get_current_user()
將不會產生網絡/速度開銷,除了從Pythondict
查找值的幾納秒之外。
因此,如果您只使用Bearer令牌,則可以執行以下操作
from google.appengine.api import oauth
from google.appengine.ext import endpoints
...
endpoints_user = endpoints.get_current_user()
if endpoints_user is None:
raise endpoints.UnauthorizedException(...)
is_admin = oauth.is_current_user_admin(known_scope)
if not is_admin:
# Throw a 403 FORBIDDEN
raise endpoints.ForbiddenException(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.