有没有办法检查用户是否是AppEngine Cloud Endpoint中的管理员? [英] Is there a way to check if the user is an admin in AppEngine Cloud Endpoints

查看:96
本文介绍了有没有办法检查用户是否是AppEngine Cloud Endpoint中的管理员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AppEngine Cloud Endpoints与Javascript客户端和Google+登录,我正在使用 endpoints.get_current_user()。有没有办法检查用户是否是AppEngine管理员?类似于用户API中的 users.is_current_user_admin()


$ b $ p

谢谢

Google Endpoints API + Chrome扩展程序为endpoints.get_current_user()返回无。user_id (),以便在执行身份验证时详细了解ID令牌和持票人令牌之间的差异。



如果您未使用Android应用程序,则可以自由使用持票人令牌,不必担心身份证令牌的一些限制。



右下 get_current_user() oauth 库提供了 oauth.is_current_user_admin()方法。完全如 get_current_user()此方法调用 _maybe_call_get_oauth_user ,然后检查一个简单的环境变量。

正如在其他答案中提到的那样:
$ b


oauth.get_current_user()调用仅花费很多 IF 它使
成为RPC。 _maybe_call_get_oauth_user 方法存储最后一次调用的
的值,因此调用 oauth.get_current_user()一秒时间
不会导致网络/速度开销,除非几微秒到
从Python dict 查找值。


因此,如果您只使用持票人标记,则可以执行以下操作:

  from google.appengine.api从google.appengine.ext导入oauth 
导入终端
$ b ...

endpoints_user = endpoints.get_current_user()
如果endpoints_user是None:
raise ends.UnauthorizedException(...)
$ b $ is_admin = oauth.is_current_user_admin(known_scope)
if_admin:
#抛出一个403 FORBIDDEN
raise endpoints.ForbiddenException(...)


I am using AppEngine Cloud Endpoints with the Javascript client and Google+ Sign In, I am using endpoints.get_current_user(). Is there a way to check if the user is an AppEngine admin? Similar to users.is_current_user_admin() in users API.

Thanks

解决方案

See Google Endpoints API + Chrome Extension returns None for endpoints.get_current_user().user_id() for a long description of the difference between ID tokens and Bearer tokens when performing auth.

If you aren't using an Android application, you can freely use Bearer tokens and not have to worry about some of the limitations of ID tokens.

Right next get_current_user(), the oauth library provides the oauth.is_current_user_admin() method. Exactly as get_current_user(), this method calls _maybe_call_get_oauth_user and then checks a simple environment variable.

As mentioned in the other answer:

The oauth.get_current_user() call is only expensive IF it makes the RPC. The _maybe_call_get_oauth_user method stores the value from the last call, so calling oauth.get_current_user() a second time will incur no network/speed overhead other than the few nanoseconds to lookup a value from a Python dict.

So if you are only using Bearer tokens, you could do the following

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(...)

这篇关于有没有办法检查用户是否是AppEngine Cloud Endpoint中的管理员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆