Giter VIP home page Giter VIP logo

ldap3-fastapi-auth-simple's Introduction

**Please consider this project to see domain controller auth using sessions and sqlite db with a professional login page https://github.com/RetributionByRevenue/fastapi-sessions-domaincontroler

  1. LDAP_AUTH Function:

    • The LDAP_AUTH function takes three parameters: domain, username, and password.
    • Inside the function, a connection to the LDAP server is established using the provided domain, username, and password.
    • The Connection object attempts to bind (authenticate) using the provided credentials.
    • If the bind is successful (conn.result['result'] == 0), the function sets didConnect to True and prints an authentication success message.
    • If an exception occurs during the authentication attempt, it prints an authentication failure message.
    • Finally, the connection is closed (conn.unbind()).
  2. FastAPI Application Setup:

    • A FastAPI instance (app) is created.
    • The HTTPBasic class from fastapi.security is used to define Basic Authentication, and an instance named security is created.
    • The check_ldap_auth function is defined as a FastAPI dependency. It takes HTTPBasicCredentials as a parameter, which represents the username and password extracted from the request headers.
    • Inside check_ldap_auth, the LDAP_AUTH function is called with the provided domain, username (from credentials), and password (from credentials).
    • If the LDAP authentication fails, a HTTPException with a 401 status code and "Invalid credentials" detail is raised.
    • If authentication succeeds, the username is returned.
  3. Protected Route:

    • An example protected route /protected is defined.
    • The check_ldap_auth dependency is used to ensure that only authenticated users can access the route.
    • If authentication is successful, a message is returned, indicating that the user has access to the protected route.
  4. Running the Application:

    • When you run the FastAPI application and access the /protected route in a browser or a tool like Swagger UI, a pop-up window appears for you to input the username and password.
    • The entered credentials are then passed to the check_ldap_auth function, which, in turn, calls the LDAP_AUTH function for LDAP authentication.
  5. Note on Domain Credentials:

    • The domain credentials (in this case, the LDAP server domain, username, and password) are hardcoded within the LDAP_AUTH function. In a real-world scenario, you might want to externalize and secure these credentials, potentially using environment variables, configuration files, or a secure credential management system.

Overall, this setup demonstrates how to integrate LDAP authentication with FastAPI, leveraging Basic Authentication for user credential input and LDAP for authentication against an LDAP server.

ldap3-fastapi-auth-simple's People

Contributors

retributionbyrevenue avatar

Stargazers

Audric.S avatar  avatar  avatar Hamza Khan Niazi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.