Skip to main content

flask api auth modules.

Project description

Flask-APIAuth

提供最基础的API登录认证功能

安装

(还不会上传到pip)下载文件,复制到程序目录好了……

基本示例


from flask import Flask, request  
from werkzeug.security import generate_password_hash, check_password_hash  

from flask_apiauth import ApiAuth  

app = Flask(__name__)  
auth = ApiAuth()  

users = {  
    "john": generate_password_hash("hello"),  
    "susan": generate_password_hash("bye")  
}  


@app.route('/api/login', methods=['POST'])  
def get_auth():  
    username = request.args.get('username')  
    print(username)  
    password = request.args.get('password')  
    print(password)  
    if username and password:  
        if username in users and check_password_hash(users.get(username), password):  
            token = auth.get_token(username, users.get(username))  
            return {  
                    'code': '200',  
                    'data': {  
                        'token': token  
                    },  
                    'message': '登录成功'  
  }  
        else:  
            return {  
                    'code': '403',  
                    'data': {},  
                    'message': '账号或密码错误'  
  }  
    else:  
        return {  
                'code': '403',  
                'data': {},  
                'message': '参数不完整'  
  }  


@auth.error_content  
def error_content():  
    return {  
        'code': '403',  
        'data': {},  
        'message': '请先登录'  
  }  


@auth.verify_password  
def verify_password(username, password):  
    if username in users and users.get(username) == password:  
        print('密码正确')  
        # 返回的数据是下面auth.current_user拿到的  
  return {'username': username, 'sex': '男'}  


@app.route('/')  
@auth.login_required  
def index():  
    return {  
        'code': '200',  
        'data': {  
            'name': auth.current_user.get('username'),  
            'sex': auth.current_user.get('sex')  
        },  
        'message': '成功'  
  }  


if __name__ == '__main__':  
    app.run()

可以使用ApiPost或者PostMan测试接口。 首先要登录,参数写在Query里,有username和password两个 【POST】http://127.0.0.1:5000/api/login?username=john&password=hello 然后使用返回的token,参数写在Header里,名称为token,值为上一个请求返回的值 【GET】http://127.0.0.1:5000 成功得到用户信息。 退出登录即客户端删除token。

其他


Flask-APIAuth

Provides the most basic API login authentication function

Install

Download the file and copy it to the program directory

Examples


from flask import Flask, request  
from werkzeug.security import generate_password_hash, check_password_hash  

from flask_apiauth import ApiAuth  

app = Flask(__name__)  
auth = ApiAuth()  

users = {  
    "john": generate_password_hash("hello"),  
    "susan": generate_password_hash("bye")  
}  


@app.route('/api/login', methods=['POST'])  
def get_auth():  
    username = request.args.get('username')  
    print(username)  
    password = request.args.get('password')  
    print(password)  
    if username and password:  
        if username in users and check_password_hash(users.get(username), password):  
            token = auth.get_token(username, users.get(username))  
            return {  
                    'code': '200',  
                    'data': {  
                        'token': token  
                    },  
                    'message': 'login success'  
  }  
        else:  
            return {  
                    'code': '403',  
                    'data': {},  
                    'message': 'login error'  
  }  
    else:  
        return {  
                'code': '403',  
                'data': {},  
                'message': 'parameters error'  
  }  


@auth.error_content  
def error_content():  
    return {  
        'code': '403',  
        'data': {},  
        'message': 'please login'  
  }  


@auth.verify_password  
def verify_password(username, password):  
    if username in users and users.get(username) == password:  
        print('password ok')  
        # 返回的数据是下面auth.current_user拿到的  
  return {'username': username, 'sex': 'man'}  


@app.route('/')  
@auth.login_required  
def index():  
    return {  
        'code': '200',  
        'data': {  
            'name': auth.current_user.get('username'),  
            'sex': auth.current_user.get('sex')  
        },  
        'message': 'success'  
  }  


if __name__ == '__main__':  
    app.run()

You can use apipost or postman to test the interface. First of all, you need to log in. The parameters are written in query. There are two parameters: username and password 【POST】http://127.0.0.1:5000/api/login?username=john&password=hello Then the returned token is used, and the parameters are written in the header. The name is token and the value is the value returned by the previous request 【GET】http://127.0.0.1:5000 Get the user information successfully. Log out means that the client will delete the token.

Other

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-APIAuth-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_APIAuth-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask-APIAuth-0.1.1.tar.gz.

File metadata

  • Download URL: Flask-APIAuth-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.1

File hashes

Hashes for Flask-APIAuth-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0a22731444121a4f18c421b96edb0404351f879d444a3f681ea3734efbcce711
MD5 89a2447a8e674924d8f3c1b642ca4490
BLAKE2b-256 3f8a60c586c926a1fdfba3d7f3de99e073c3da4d04081a022dab8ab7828046f6

See more details on using hashes here.

File details

Details for the file Flask_APIAuth-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: Flask_APIAuth-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.1

File hashes

Hashes for Flask_APIAuth-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c3180ef9e104a81b0b9d6b1e47a9f0635a71cdc40ce9dcac57b5a3008a422c4
MD5 0c0fa4123c36d96558b5eaa8b2e6c3a6
BLAKE2b-256 31abfb24aa24b66fecabf0bd03d141aabc44c88d95f4a2473d527dfe09975163

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page