0

今日、私は Python とフラスコを使用して小さな RESTfull サービスを作成しています。私は認証を解決できないようです。

これが私のコードです:

# Extensions
auth = HTTPBasicAuth()

def login_user(username,password):
    # If the username can be decoded as a JWT & the password is empty, return true
    if jwt.decode(username, SERVER_PAYLOARD_SECRET, algorithms=['HS256']) == '{"some":"payload"}' and password == "":
        return username and password
    else:
        # Otherwise, make the classic username/password verification
        # Get the password corresponding to the username.
        connection = sqlite3.connect(database_name)
        cursor = connection.cursor()
        cursor.execute("SELECT Username AND Password FROM Users WHERE Username = ?", (username))
        returned_data = cursor.fetchall()
        # Do the verification
        if returned_data[0] == username and pwd_context.verify(password,returned_data[1]):
            return username and password
        else:
            abort(401)

@auth.get_password
def verify_password(username,password):
    return login_user(username,password)
    # Look if the token used is in memory

@app.route('/api/user/delete_user', methods=['GET','POST'])
@auth.login_required
def delete_user():
    # Validate the login token, and then delete the user from the
    # database and all the streams or information he has.
    return "Hello World"

何を返すことができるのか、login_user() 関数が正しいかどうかはわかりません。

ご協力いただきありがとうございます、

乾杯

編集:コードを実行すると、次のエラーが発生します:

    Traceback (most recent call last):
  [...]
  File "/home/n07070/FarDrive/Code/OpenPhotoStream/lib/python3.4/site-packages/flask_httpauth.py", line 57, in decorated
    password = self.get_password_callback(auth.username)
TypeError: verify_password() missing 1 required positional argument: 'password'
4

1 に答える 1

1

小さな間違い。これを変える:

@auth.get_password
def verify_password(username,password):
    return login_user(username,password)
    # Look if the token used is in memory

これに:

@auth.verify_password
def verify_password(username,password):
    return login_user(username,password)
    # Look if the token used is in memory

Flask-HTTPAuth は、クライアントの資格情報を検証できるいくつかの異なる方法を提供します。これ@auth.get_passwordは非常に単純なものです。装飾された関数はユーザー名を受け取り、そのアカウントに対応するパスワードを返す必要があります。これ@auth.verify_passwordはより柔軟なオプションであり、クライアントから提供された資格情報を提供し、独自の検証ロジックを実装できます。この最後のオプションが必要なようです。

于 2016-02-16T15:11:20.213 に答える