我正在使用AppEngine Cloud Endpoints和Javascript客戶端和Google+登錄,我正在使用endpoints.get_current_user()
。有沒有辦法檢查用戶是否是AppEngine管理員?與用戶API中的users.is_current_user_admin()
類似。有沒有辦法在AppEngine Cloud Endpoints中檢查用戶是否是管理員
由於
我正在使用AppEngine Cloud Endpoints和Javascript客戶端和Google+登錄,我正在使用endpoints.get_current_user()
。有沒有辦法檢查用戶是否是AppEngine管理員?與用戶API中的users.is_current_user_admin()
類似。有沒有辦法在AppEngine Cloud Endpoints中檢查用戶是否是管理員
由於
爲ID令牌並執行身份驗證時承載的令牌之間的差的一個長的描述見Google Endpoints API + Chrome Extension returns None for endpoints.get_current_user().user_id()。
如果您不使用Android應用程序,則可以自由使用持票人令牌,而不必擔心身份證令牌的某些限制。
接下來get_current_user()
,oauth
庫提供oauth.is_current_user_admin()
方法。正如get_current_user()
,this method calls_maybe_call_get_oauth_user
然後檢查一個簡單的環境變量。
正如在其他答覆中提到:
的
oauth.get_current_user()
電話是隻買貴的IF它使 的RPC。_maybe_call_get_oauth_user
方法存儲最後一次調用的值 ,所以第二次調用oauth.get_current_user()
時 將不會導致網絡/速度開銷超過幾納秒至 查找來自Pythondict
的值。
所以,如果你只使用承載令牌,你可以做以下
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(...)