🔍 Code Extractor

function login

Maturity: 48

Flask route handler that renders the login page for user authentication, with support for Azure SSO integration and automatic redirection for authenticated users or when authentication is disabled.

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
714 - 730
Complexity:
moderate

Purpose

This function serves as the entry point for user authentication in a Flask web application. It handles three scenarios: (1) redirects to the main application if authentication is disabled via config, (2) redirects already-authenticated users to the index page, and (3) renders the login template with an optional Azure SSO authentication URL for unauthenticated users. It integrates with Azure Active Directory for single sign-on capabilities.

Source Code

def login():
    """Login page"""
    if not config.REQUIRE_AUTH:
        # If auth is disabled, redirect to main app
        return redirect(url_for('index'))
    
    if 'user' in session:
        # Already logged in
        return redirect(url_for('index'))
    
    # Get Azure auth URL if available
    azure_auth_url = None
    if azure_sso:
        azure_auth_url = azure_sso.get_auth_url()
        logger.info(f"Generated Azure auth URL: {azure_auth_url}")
    
    return render_template('login.html', azure_auth_url=azure_auth_url)

Return Value

Returns a Flask response object. This can be: (1) a redirect response to the 'index' route if authentication is disabled or user is already logged in, or (2) a rendered HTML template ('login.html') with the 'azure_auth_url' context variable passed to it (which may be None if Azure SSO is not configured).

Dependencies

  • flask
  • logging

Required Imports

from flask import Flask
from flask import render_template
from flask import session
from flask import redirect
from flask import url_for
import logging
import config
from auth.azure_auth import setup_azure_sso

Conditional/Optional Imports

These imports are only needed under specific conditions:

from auth.azure_auth import setup_azure_sso

Condition: Required if Azure SSO authentication is enabled and the 'azure_sso' object needs to be initialized

Optional

Usage Example

from flask import Flask, session
import config
from auth.azure_auth import setup_azure_sso
import logging

app = Flask(__name__)
app.secret_key = 'your-secret-key-here'
logger = logging.getLogger(__name__)

# Setup Azure SSO (optional)
azure_sso = setup_azure_sso(app) if config.AZURE_ENABLED else None

# Configure authentication requirement
config.REQUIRE_AUTH = True

@app.route('/login')
def login():
    """Login page"""
    if not config.REQUIRE_AUTH:
        return redirect(url_for('index'))
    
    if 'user' in session:
        return redirect(url_for('index'))
    
    azure_auth_url = None
    if azure_sso:
        azure_auth_url = azure_sso.get_auth_url()
        logger.info(f"Generated Azure auth URL: {azure_auth_url}")
    
    return render_template('login.html', azure_auth_url=azure_auth_url)

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

Best Practices

  • Ensure Flask session secret key is properly configured and kept secure before using this function
  • The 'azure_sso' object must be properly initialized before this function is called if Azure authentication is intended
  • Always validate that config.REQUIRE_AUTH is properly set according to your security requirements
  • Ensure the 'login.html' template exists and properly handles the azure_auth_url parameter (including None values)
  • The function assumes an 'index' route exists for redirection - ensure this route is defined
  • Consider implementing rate limiting on the login route to prevent brute force attacks
  • The logger object should be configured at the module level before this function is used
  • Session data should be validated and sanitized when checking for 'user' key
  • Consider adding CSRF protection to the login form rendered by this route

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function login_v2 90.1% similar

    Flask route handler that displays the login page and redirects authenticated users to the chat interface.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function login_v1 89.9% similar

    Flask route handler that manages user authentication by redirecting authenticated users to the index page, initiating Azure SSO login for production, or creating a development session for local testing.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function login_v3 88.0% similar

    Flask route handler that displays the login page and redirects authenticated users to the document workspace.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function logout_v1 78.2% similar

    Flask route handler that logs out the current user by clearing their session data and redirecting them to the login page.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function index_v3 75.5% similar

    Flask route handler that serves as the application's main entry point, redirecting users to either the chat page if authenticated or the login page if not.

    From: /tf/active/vicechatdev/vice_ai/app.py
← Back to Browse