我正在将 AppEngine Cloud Endpoints 与 Javascript 客户端和 Google+ 登录一起使用,我正在使用endpoints.get_current_user()
. 有没有办法检查用户是否是 AppEngine 管理员?类似于users.is_current_user_admin()
用户 API。
谢谢
我正在将 AppEngine Cloud Endpoints 与 Javascript 客户端和 Google+ 登录一起使用,我正在使用endpoints.get_current_user()
. 有没有办法检查用户是否是 AppEngine 管理员?类似于users.is_current_user_admin()
用户 API。
谢谢
请参阅Google Endpoints API + Chrome 扩展为 endpoints.get_current_user().user_id() 返回 None,详细了解执行身份验证时 ID 令牌和 Bearer 令牌之间的区别。
如果您不使用 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()
第二次调用不会产生网络/速度开销,除了从 Python 查找值的几纳秒dict
。
因此,如果您只使用 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(...)