Skip to main content

A simple authentication package for Flask applications

Project description

Flask LAC

Flask LAC is an authentication package for Flask applications, providing seamless integration with an external authentication service.

Note: This package requires a running instance of the authentication service.

Features

  • User authentication via external service
  • Session and cookie-based authentication
  • Role-based access control (role_required decorator)
  • Permission-based access control (permission_needed decorator)
  • Automatic token verification and renewal
  • Easy integration with Flask apps
  • Redis support for token storage (fallback to in-memory for development)
  • Secure login and logout routes
  • User info and permissions retrieval

Installation

Install the package using pip:

pip install flask-lac

Quick Start

Initialization

from flask import Flask
from flask_lac import AuthPackage

app = Flask(__name__)
auth = AuthPackage(app, auth_service_url="https://auth.luova.club", app_id="your_app_id")

Important: To get your app_id, register as a user on the authentication service and create a new application.

Provided Routes

  • /login: Redirects to the external authentication service.
  • /auth_callback: Handles the authentication callback.
  • /logout: Logs out the user and clears the session.

Protecting Routes

Require Login

from flask_lac import login_required

@app.route('/dashboard')
@login_required
def dashboard():
    return "This is a protected dashboard."

Require Role

from flask_lac.user import role_required

@app.route('/admin')
@role_required(min_role=10)
def admin_panel():
    return "Admin panel."

Require Permission

from flask_lac.user import permission_needed

@app.route('/reports')
@permission_needed('view_reports')
def reports():
    return "Reports page."

Accessing the Current User

from flask_lac.user import User

user = User()
if user.is_authenticated():
    print(f"Logged in as: {user.username} ({user.email})")
    print(f"Role: {user.role}")
    print(f"Permissions: {user.permissions}")

API Reference

AuthPackage

Initializes authentication for your Flask app.

AuthPackage(app, auth_service_url, app_id)
  • app: Flask app instance
  • auth_service_url: URL of the authentication service
  • app_id: Your application ID from the auth service

User

Represents the current user. Main properties:

  • username, email, role, permissions, display_name, profile_pic
  • is_authenticated(): Returns True if the user is logged in

Decorators

  • login_required: Require authentication for a route
  • role_required(min_role, redirect_to=None): Require a minimum role
  • permission_needed(permission): Require a specific permission

Development

Running Tests

Tests are not included in this version. Add your tests in a tests/ directory.

Publishing a New Version

bash release.sh --bump <major|minor|patch>

This will bump the version, build, and upload to PyPI.

Troubleshooting & FAQ

  • Redis is not running: The package will fall back to in-memory token storage, which is not recommended for production.
  • How do I get my app_id? Register on the authentication service and create a new application.
  • How do I add permissions or roles? Use the admin interface of the authentication service.

License

This project is licensed under the MIT License.

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_lac-1.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

flask_lac-1.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file flask_lac-1.1.0.tar.gz.

File metadata

  • Download URL: flask_lac-1.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for flask_lac-1.1.0.tar.gz
Algorithm Hash digest
SHA256 00fa0d23f6974a41a0777d54bd441191e51514958833cf7857eaa2ffb5ca1069
MD5 b38a9979942bf8df26ce36ebb4571c14
BLAKE2b-256 9539706fca6a73e5bad350bb119273d57d7be81b2b52ee44a341007b24bdb738

See more details on using hashes here.

File details

Details for the file flask_lac-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: flask_lac-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for flask_lac-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 345f9f07e48777320b718ae91bc16e0357e7a8b5dda69f757382be3fec9abfac
MD5 7493a32db9f649b3e3194f27ade4116d
BLAKE2b-256 2516d3f992e3162aa4bda86d40c2368ed777d6d1db190a75444efed1826e5124

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