Big Data & Tools with NoSQL
  • Big Data & Tools
  • ReadMe
  • Big Data Overview
    • Overview
    • Job Opportunities
    • What is Data?
    • How does it help?
    • Types of Data
    • The Big 4 V's
      • Variety
      • Volume
      • Velocity
      • Veracity
      • Other V's
    • Trending Technologies
    • Big Data Concerns
    • Big Data Challenges
    • Data Integration
    • Scaling
      • CAP Theorem
      • Optimistic concurrency
      • Eventual consistency
      • Concurrent vs. Parallel Programming
    • Big Data Tools
    • No SQL Databases
    • What does Big Data learning means?
  • Linux & Tools
    • Overview
    • Linux Commands - 01
    • Linux Commands - 02
    • AWK
    • CSVKIT
    • CSVSQL
    • CSVGREP
  • Data Format
    • Storage Formats
    • CSV/TSV/Parquet
    • Parquet Example
    • JSON
    • HTTP & REST API
      • Terms to Know
        • Statefulness
        • Statelessness
        • Monolithic Architecture
        • Microservices
        • Idempotency
    • REST API
    • Python
      • Setup
      • Decorator
      • Unit Testing
      • Flask Demo
      • Flask Demo - 01
      • Flask Demo - 02
      • Flask Demo - 03
      • Flask Demo - 04
      • Flask Demo - 06
    • API Testing
    • Flask Demo Testing
    • API Performance
    • API in Big Data World
  • NoSQL
    • Types of NoSQL Databases
    • Redis
      • Overview
      • Terms to know
      • Redis - (RDBMS) MySql
      • Redis Cache Demo
      • Use Cases
      • Data Structures
        • Strings
        • List
        • Set
        • Hash
        • Geospatial Index
        • Pub/Sub
        • Redis - Python
      • Redis JSON
      • Redis Search
      • Persistence
      • Databases
      • Timeseries
    • Neo4J
      • Introduction
      • Neo4J Terms
      • Software
      • Neo4J Components
      • Hello World
      • Examples
        • MySQL: Neo4J
        • Sample Transactions
        • Sample
        • Create Nodes
        • Update Nodes
        • Relation
        • Putting it all together
        • Commonly used Functions
        • Data Profiling
        • Queries
        • Python Scripts
      • More reading
    • MongoDB
      • Sample JSON
      • Introduction
      • Software
      • MongoDB Best Practices
      • MongoDB Commands
      • Insert Document
      • Querying MongoDB
      • Update & Remove
      • Import
      • Logical Operators
      • Data Types
      • Operators
      • Aggregation Pipeline
      • Further Reading
      • Fun Task
        • Sample
    • InfluxDB
      • Data Format
      • Scripts
  • Python
    • Python Classes
    • Serialization-Deserialization
  • Tools
    • JQ
    • DUCK DB
    • CICD Intro
    • CICD Tools
      • CI YAML
      • CD Yaml
    • Containers
      • VMs or Containers
      • What container does
      • Podman
      • Podman Examples
  • Cloud Everywhere
    • Overview
    • Types of Cloud Services
    • Challenges of Cloud Computing
    • High Availability
    • Azure Cloud
      • Services
      • Storages
      • Demo
    • Terraform
  • Data Engineering
    • Batch vs Streaming
    • Kafka
      • Introduction
      • Kafka Use Cases
      • Kafka Software
      • Python Scripts
      • Different types of Streaming
    • Quality & Governance
    • Medallion Architecture
    • Data Engineering Model
    • Data Mesh
  • Industry Trends
    • Roadmap - Data Engineer
    • Good Reads
      • IP & SUBNET
Powered by GitBook
On this page
  • HTTP Basic Authentication
  • Other @auth decorators
  • Usage
  1. Data Format
  2. Python

Flask Demo - 03

HTTP Basic Authentication

HTTP Basic Authentication

  1. Simplicity: Basic Authentication is simple to implement, as it doesn't require additional libraries or infrastructure. It's part of the HTTP standard.

  2. Suitability for Simple Use Cases: It's suitable for simple, internal applications or services where ease of implementation is more critical than advanced security features.

  3. Limited Security: The credentials are only base64 encoded, not encrypted, making it less secure unless used with HTTPS. It's also more vulnerable to CSRF (Cross-Site Request Forgery) attacks.

  4. Stateful: Basic Authentication is typically stateful, requiring the server to maintain session state, which can be a drawback in distributed systems.

python3 api_demo/flask_03_basic_auth_app.py

http://127.0.0.1:5003/items

Other @auth decorators

@auth.verify_password:

  • This decorator defines a function that verifies user credentials during authentication.

  • Example:

    @auth.verify_password
    def verify_password(username, password):
        # Check username and password, return username if authentication succeeds

auth.username():

  • After successful authentication, you can use auth.username() to retrieve the authenticated username within a route function.

  • Example:

    @app.route('/profile')
    @auth.login_required
    def get_profile():
        username = auth.username()
        # Use the username to fetch user-specific data

@auth.login_required:

  • This decorator protects routes that require authentication. It ensures that only authenticated users can access the decorated route.

  • Example:

    @app.route('/secure_data')
    @auth.login_required
    def secure_data():
        # Only authenticated users can access this route

@auth.error_handler:

  • You can define a custom error handler for authentication failures using this decorator. It allows you to handle authentication errors in a customized way.

  • Example:

    @auth.error_handler
    def unauthorized():
        return jsonify({"message": "Unauthorized access"}), 401

@auth.token_authentication:

  • If you want to implement token-based authentication, you can use this decorator to specify a function that verifies tokens.

  • Example:

    @auth.token_authentication
    def verify_token(token):
        # Check if the token is valid and return the associated user

@auth.get_password and @auth.get_user_roles:

  • These decorators allow you to customize how passwords and user roles are retrieved from your data source. They are useful for complex authentication systems.

  • Example:

    @auth.get_password
    def get_password(username):
        # Retrieve and return the password for the given username

Usage


users = {
    "user1": "password1",
    "user2": "password2"
}

user_roles = {
    "user1": ["admin"],
    "user2": ["user"]
}

@auth.get_password
def get_password(username):
    return users.get(username)

@auth.get_user_roles
def get_user_roles(user):
    return user_roles.get(user)

tokens = {
    "token1": "user1",
    "token2": "user2"
}

@auth.verify_token
def verify_token(token):
    if token in tokens:
        return tokens[token]

PreviousFlask Demo - 02NextFlask Demo - 04

Last updated 1 year ago