简单的用户名&散景服务器的密码保护

时间:2017-04-03 11:32:07

标签: python azure authentication password-protection bokeh

我有一个简单的散景服务器应用程序,我想在基于Linux的Azure节点上公开它。服务器已启动并正在运行。

我的问题是:如何通过用户名和密码保护内容?我不需要对用户进行身份验证。

到目前为止我的想法(未尝试,可能无效)

  1. 使用文本字段创建额外的散景服务器页面。
  2. 在按钮的回调中,如果密码适合,则添加测试。如果是,则重定向到原始服务器页面。否则,请通知用户有关错误凭据的信息。

1 个答案:

答案 0 :(得分:5)

您可以尝试禁用散景服务器生成会话ID,并仅在用户身份验证后通过外部应用程序生成它们:
(基于散景文档的this part

  1. 使用bokeh secret命令生成密钥:
  2. $ bokeh secret
    oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
    
    1. BOKEH_SECRET_KEY环境变量设置为生成值;
    2. $ export BOKEH_SECRET_KEY=oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
      1. 设置另一个环境变量:
      2. $ export BOKEH_SIGN_SESSIONS=True
        1. 使用--session-ids external-signed参数运行散景服务器:
        2. $ bokeh serve myApp --session-ids external-signed

          在此模式下,用户应提供有效(签名)会话ID以访问散景服务器。

          1. 运行简单的外部流程,要求用户输入登录名和密码,并为他们生成ID。 以下是基于Flask文档中的snippet的示例:
          2. 
            
                from functools import wraps
                from flask import request, Response, redirect, Flask
                from bokeh.util import session_id
            
                app = Flask(__name__)
            
                def check_auth(username, password):
                    return username == 'valid_user' and password == 'valid_password'
            
                def authenticate():
                    """Sends a 401 response that enables basic auth"""
                    return Response(
                    'Could not verify your access level for that URL.\n'
                    'You have to login with proper credentials', 401,
                    {'WWW-Authenticate': 'Basic realm="Login Required"'})
            
                def requires_auth(f):
                    @wraps(f)
                    def decorated(*args, **kwargs):
                        auth = request.authorization
                        if not auth or not check_auth(auth.username, auth.password):
                            return authenticate()
                        return f(*args, **kwargs)
                    return decorated
            
                @app.route('/')
                @requires_auth
                def redirect_to_bokeh():
                    s_id = session_id.generate_session_id()
                    return redirect("http://<bokeh-server-addr>:<port>/?bokeh-session-id={}".format(s_id), code=302)
            
                if __name__ == "__main__":
                    app.run()    
            
            1. 现在访问散景服务器用户应该转到Flask应用程序并指定登录名和密码。