Skip to main content

Authentication library for Flask Web Framework

Project description

Flask-Authlib 🔐

PyPI version


Flask-Authlib - authentication library for Flask Web Framework.

Advantages:

  • Templates: login , register
  • Default user Model
  • View Functions

Install

By using pip:

$ pip install flask-authlib

Simple Usage

  • Import Flask from flask
  • Import SQLAlchemy from flask_sqlalchemy
  • Import Auth from flask_authlib

Code Sample:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_authlib import Auth

Define your app and db. Create auth var from Auth class and call init() method:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_authlib import Auth

app = Flask(__name__)
db = SQLAlchemy(app)

auth = Auth(app=app, db=db)
auth.init()

# simple route
@app.route('/')
def home_page():
    return {'message': 'Hi, bro 😁'}

Run your backend:

$ export FLASK_APP=<app> && export FLASK_ENV=development
$ flask run --reload

Screenshots:

LOGIN Login page at /login

REGISTER Register page at /register

Advanced Usage

You can change urls

Defaults

  • Home page - /
  • Login page - /login
  • Register page - /register
  • Logout url - /logout

Write your urls before calling init() method:

...

auth = Auth(app=app, db=db, login_url='/mylogin',
            register_url='/myreg', logout_url='/myexit',
            home_page='/')
auth.init()
...

You can set your own template config!

You can change:

  • Button color on forms
  • Form title at login and register page
  • All labels like Username.. email ...
  • Text in button.

Default template config

config = {
        "LOGIN_BTN": "btn-success",
        "REGISTER_BTN": "btn-warning",

        "LOGIN_BTN_TEXT": "Login",
        "REGISTER_BTN_TEXT": "Register",

        "LOGIN_PAGE_TITLE": "Login",
        "REGISTER_PAGE_TITLE": "Register",

        "LOGIN_LABEL_USERNAME": "Username",
        "LOGIN_LABEL_PASSWORD": "Password",
        "REGISTER_LABEL_USERNAME": "Username",
        "REGISTER_LABEL_PASSWORD": "Password",
        "REGISTER_LABEL_EMAIL": "Email address"
}

p.s btn colors based on bootstrap classes

Setting your config:

...
my_config = {
        "LOGIN_BTN": "btn-danger",
        "REGISTER_BTN": "btn-primary",

        "LOGIN_PAGE_TITLE": "Admin",
        "REGISTER_PAGE_TITLE": "Admin"
}
auth = Auth(app=app, db=db, template_config=my_config)
auth.init()
...

If some settings are not entered, they remain as default

Screenshots:

LOGIN Login page


REGISTER Register page

Running Example 🚀

First way

Required docker

Project directory have:

  • dockerfile
  • docker_compose.yml

For running this you have to type this command:

$ docker-compose up

Screenshot:

DOCKER

Second way

Clone this repo:

$ git clone https://github.com/AbduazizZiyodov/flask-authlib.git

Navigate to /example:

$ cd example/

Install all required packages:

$ pip install -r requirements.txt

Run development server:

  • $ python app.py or
  • $ gunicorn app:app or
  • $ export FLASK_APP=app && flask run --reload

Enjoy 😅

JWT

v1.3.1

Setup JWT authentication for your API with auth0!

Example:

from flask import Flask
from flask_authlib import JWT

app = Flask(__name__)
jwt = JWT(app=app, AUTH0_DOMAIN='',API_AUDIENCE='')

required = jwt.get_requires_auth_decorator()

@app.route('/', methods=['GET'])
@required('read:data')
def home(token):
    return {"data": "secret"}
  • Import JWT from this library
  • Define your app
  • Create jwt var from JWT class and set some vars.

    Params: AUTH0_DOMAIN: your domain for auth0: DOMAIN "abduaziz.us.auth0.com" API_AUDINCE: your api idenf. for auth0: AUDINCE "test_api" create example of decorator by calling get_requires_auth_decorator() method

@requires(permission:str)

If permission in auth token , this view will be send success response , else 401😅

Before doing these , you have to create user permissions from your API:

AUDINCE

read:data permission

For testing ,you should register new user and get token from response:

LOGIN_URI

LOGIN_URI:

https://{{AUTH0_DOMAIN}}/authorize?
audience={{API_AUDINCE}}&response_type=token&
client_id={{CLIENT_ID}}&redirect_uri={{REDIRECT_URI}}
  • CLIENT_ID - you can get it from your page of API.
  • REDIRECT_URI - you can set it from API settings

After adding new user , navigate user management page and assign permissions to your user:

PERM

logout_uri: {AUTH0_DOMAIN}/logout

Next , login again and you will get permission based jwt token:

TOKEN 🎉

Test it !

Send request without auth token:

401

Send request with auth token:

OK ✅ Success!

Author: Abduaziz Ziyodov

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-Authlib-1.3.2.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

Flask_Authlib-1.3.2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Authlib-1.3.2.tar.gz.

File metadata

  • Download URL: Flask-Authlib-1.3.2.tar.gz
  • Upload date:
  • Size: 10.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.5

File hashes

Hashes for Flask-Authlib-1.3.2.tar.gz
Algorithm Hash digest
SHA256 5ab0fe18b42226de5c6331f41aa182ed81683a0daa3daec47b9eca6210f544f0
MD5 18f033a7794773d69b16f41621793557
BLAKE2b-256 843b2d7e113580db5e7e45a2e2149123b9ccf8ea89c4b26385a6b5f1b7f18ae4

See more details on using hashes here.

File details

Details for the file Flask_Authlib-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: Flask_Authlib-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.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.5

File hashes

Hashes for Flask_Authlib-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e87f3a7699bb31a3525648d910ad4be84a0c4f757ebb90b89118e462894843
MD5 c4ffe419585855f1c4309346fa46e3be
BLAKE2b-256 7384cdec831c813177edddffd3988eac16e4b4f069b8386143857cf896d91156

See more details on using hashes here.

Supported by

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