Restful 服务和应用程序用户的 Web2py 身份验证

Web2py Authentication for Restful service and also application users

在 web2py 中我们可以有 restful 服务,如下所述,

auth.settings.allow_basic_login = True

@auth.requires_login()
@request.restful()
def api():
   def GET(s):
       return 'access granted, you said %s' % s
   return locals()

该服务将被外部系统调用。现在如何定义两个级别的服务使用。一个可以访问服务的用户(外部系统)。访问后,外部系统将使用 webapp 将相关数据显示给最终用户。此外,将使用外部系统的最终用户需要登录,我将其存储在 auth 相关表中。 换句话说:最终用户使用基于 java 的外部 webapp 进行注册和登录。该应用会将我们的 web2py 称为 restful。如果我们使用 '@auth.requires_login()' 装饰器,它是否验证 API 调用系统或最终用户。 还提到 api 调用系统可以调用为

curl --user name:password http://127.0.0.1:8000/myapp/default/api/hello

这意味着外部系统每次调用 web2py APIs 时都会传递用户名和密码。即使是这样,最终用户登录令牌也将如何 checked/send.

如果有人能回答这个问题,我将不胜感激。

基本上,您需要两种类型的 web2py Restfull 服务身份验证。这可以使用更通用的 auth.requires 装饰器

来实现
@auth.requires(custom_auth, requires_login=False)

其中自定义 custom_auth 是模型中定义的函数,

def custom_auth():
     """Just a prototype that needs to be extended"""
     web2py_login = auth.user is not None
     other_login  = # do some other checks from a third party system
     return  web2py_login or other_login

请注意,基本身份验证是 not secure, and should be avoided when possible in production. Use a token based authentification