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.2.0.tar.gz (10.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_lac-1.2.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_lac-1.2.0.tar.gz
  • Upload date:
  • Size: 10.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 a3c533f49de90469786eec38cae86c9213ee7690a801d868beaba7bc65f55307
MD5 b38e2fa37db34d6bd302067d8ba72352
BLAKE2b-256 6d60da6defb5ae4d82601b23b5cd88be85c1d789bfd7d4d61afeb478af5d31ce

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flask_lac-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 379f756d662e97829f1a345c6933cc7f5ffcb7c5ee63414b293d907ad5849796
MD5 d9e48c248393225a374f9e0f137d8778
BLAKE2b-256 85e82e8d09d8470848da07cb59b77003408ccb833fba1a4d675e832fe70f77d7

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