密码保护静态页面AppEngine HowTo



所以我正在使用AppEngine(Python),我想做的是提供OpenID Login设置默认提供程序,这样用户就可以使用该提供程序登录而不会出现问题。问题是,我想在用户登录后立即提示他们一个密码,以便显示静态内容(HTML页面);如果用户没有输入正确的密码,那么我想将他们重定向到另一个页面。保护必须是服务器端,请:)有什么想法吗??

附言:我正在寻找一个类似于".htaccess/htpasswd"但适用于应用程序引擎的解决方案。

AFAIK,GAE不支持这样的设置(OpenID登录后的静态密码)。

我认为实现这一点的唯一方法是通过您的处理程序提供静态内容:

  1. 客户端请求静态内容
  2. 您的处理程序已注册以处理此URL
  3. 处理程序检查用户是否已通过身份验证。如果没有,则请求密码
  4. 经过身份验证后,处理程序读取静态文件并将其发送回用户

试试这个,你可以用谷歌应用引擎模拟.htaccess风格的密码:

def basicAuth(func):
  def callf(webappRequest, *args, **kwargs):
    # Parse the header to extract a user/password combo.
    # We're expecting something like "Basic XZxgZRTpbjpvcGVuIHYlc4FkZQ=="
    auth_header = webappRequest.request.headers.get('Authorization')
    if auth_header == None:
      webappRequest.response.set_status(401, message="Authorization Required")
      webappRequest.response.headers['WWW-Authenticate'] = 'Basic realm="Kalydo School"'
    else:
      # Isolate the encoded user/passwd and decode it
      auth_parts = auth_header.split(' ')
      user_pass_parts = base64.b64decode(auth_parts[1]).split(':')
      user_arg = user_pass_parts[0]
      pass_arg = user_pass_parts[1]
      if user_arg != "admin" or pass_arg != "foobar":
        webappRequest.response.set_status(401, message="Authorization Required")
        webappRequest.response.headers['WWW-Authenticate'] = 'Basic realm="Secure Area"'
        # Rendering a 401 Error page is a good way to go...
        self.response.out.write(template.render('templates/error/401.html', {}))
      else:
        return func(webappRequest, *args, **kwargs)
  return callf
class AuthTest(webapp.RequestHandler):
  @basicAuth
  def get(self):
     ....

如何:谷歌应用引擎上的动态WWW身份验证(.htaccess风格)

最新更新