2014-10-11 45 views
4

我正在使用瓶子創建一個簡單的網絡應用程序。我將在我的Linux服務器上託管它。使用Linux系統證書的瓶子登錄

網絡應用程序會執行多個用戶特定的事情。像用戶主頁中的列表目錄一樣,爲用戶和類似的東西添加ssh-keys。

我想知道是否有方法讓燒瓶打開登錄頁面,並根據系統用戶名和密碼驗證用戶名和密碼。 (即用戶系統憑證)。如果是,那麼如何。如果沒有,我還能做什麼?

回答

5

使用'simpepam'python軟件包,您可以在Linux上對PAM系統進行身份驗證。這裏是我修改爲使用simplepam的flask basic example

from flask import Flask, session, redirect, url_for, escape, request 
from simplepam import authenticate 


app = Flask(__name__) 

@app.route('/') 
def index(): 
    if 'username' in session: 
     return 'Logged in as %s' % escape(session['username']) 
    return 'You are not logged in' 

@app.route('/login', methods=['GET', 'POST']) 
def login(): 
    if request.method == 'POST': 
     username = request.form['username'] 
     password = request.form['password'] 
     if authenticate(str(username), str(password)): 
      session['username'] = request.form['username'] 
      return redirect(url_for('index')) 
     else: 
      return 'Invalid username/password' 
    return ''' 
     <form action="" method="post"> 
      <p><input type=text name=username> 
      <p><input type=password name=password> 
      <p><input type=submit value=Login> 
     </form> 
    ''' 

@app.route('/logout') 
def logout(): 
    # remove the username from the session if it's there 
    session.pop('username', None) 
    return redirect(url_for('index')) 

# set the secret key. keep this really secret: 
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' 

if __name__ == '__main__': 
    app.run(debug='True') 
+0

謝謝你的回覆!我今天會嘗試並回復結果。 – 2014-10-13 05:29:30