ai-cfia / membrane-backend

Membrane Backend: A centralized authentication service for Single Sign-On (SSO) enabling seamless token-based email verification across multiple client applications.
MIT License
1 stars 0 forks source link

Work Flow

Louis Login Backend offers a comprehensive workflow that ensures a seamless Single Sign-On (SSO) experience. The system operates through a series of steps starting from the client application check to email verification and eventual redirection after verification.

For a detailed step-by-step breakdown and to understand how the API endpoints function within this process, please refer to the WORKFLOW documentation.

Setting Up a Quart Application

Note: If you're using a devcontainer, the steps involving virtual environments are not necessary. However, for developers not using devcontainer, the virtual environment setup remains relevant.

Follow the instructions below to set up a Quart application in your environment:

Using virtual environment

1. Check Pip Version:

Before you start, ensure you have pip installed. Check its version with:

pip --version

2. Upgrade Pip, Setuptools, and Virtualenv:

It's a good practice to keep your tools updated. Run the following command:

python -m pip install --upgrade pip setuptools virtualenv

3. Create a Virtual Environment:

To create an isolated environment for your project, set up a virtual environment named venv (or another name you prefer):

python -m venv venv

4. Activate the Virtual Environment:

Before installing any packages, activate your virtual environment:

On Windows:

venv\Scripts\activate

5. Install Project Dependencies:

Ensure you have a requirements.txt file in your project directory. Install all dependencies with:

pip install -r requirements.txt

Now, you can proceed with running your Quart application or any other tasks. Always ensure that your virtual environment is activated when working on the project to maintain dependencies separately from your global Python environment.

Environment Variable Configuration:

To run the Quart application correctly, it requires some environment variables to be set. Follow the steps below to set them up:

  1. Navigate to the root directory of the project and create a new file named .env.
  2. Open this .env file using your preferred text editor.

Now, define each of the following variables:

Mandatory Variables

MEMBRANE_CORS_ALLOWED_ORIGINS

MEMBRANE_FRONTEND

MEMBRANE_SECRET_KEY

MEMBRANE_CLIENT_PUBLIC_KEYS_DIRECTORY

MEMBRANE_SERVER_PRIVATE_KEY

MEMBRANE_SERVER_PUBLIC_KEY

MEMBRANE_COMM_CONNECTION_STRING

MEMBRANE_SENDER_EMAIL

Optional Variables

MEMBRANE_JWT_ACCESS_TOKEN_EXPIRE_SECONDS

MEMBRANE_JWT_EXPIRE_SECONDS

MEMBRANE_SESSION_LIFETIME_SECONDS

MEMBRANE_SESSION_COOKIE_SECURE

MEMBRANE_SESSION_TYPE

MEMBRANE_TOKEN_BLACKLIST

MEMBRANE_APP_ID_FIELD

MEMBRANE_DATA_FIELD

MEMBRANE_REDIRECT_URL_FIELD

MEMBRANE_ENCODE_ALGORITHM

MEMBRANE_ALLOWED_EMAIL_DOMAINS_PATTERN

MEMBRANE_EMAIL_SUBJECT

MEMBRANE_EMAIL_SEND_HTML_TEMPLATE

MEMBRANE_EMAIL_SEND_POLLER_WAIT_TIME

MEMBRANE_EMAIL_SEND_TIMEOUT_SECONDS

MEMBRANE_EMAIL_SEND_SUCCESS

MEMBRANE_GENERIC_500_ERROR_FIELD

MEMBRANE_GENERIC_500_ERROR

MEMBRANE_LOGGING_LEVEL

MEMBRANE_LOGGING_FORMAT

MEMBRANE_HEALTH_MESSAGE

MEMBRANE_WORKERS

MEMBRANE_KEEP_ALIVE

Once you have defined all these variables, save and close the .env file. The Quart application will now use these environment variable values when it runs.

Running the App Locally

1. Run the Main Quart Application:

With your virtual environment activated, start the main app.py:

quart run

2. Simulate a Client Application:

Open a separate terminal or command prompt. Make sure the virtual environment is activated and then run the testapp1.py to simulate a client application:

quart --app testapp1.py run --port=4000

3. Interact with Membrane Frontend:

Ensure that the Membrane Frontend React application is running, ideally on localhost. This application will serve as the frontend interface for users to provide their email addresses to Membrane Backend.


You can now interact with both the main Quart application and the client simulator to validate the entire authentication flow.

Setting Up the App

1. Generate Server, Client Keys, and Environment Files

Prerequisites

Steps

  1. Navigate to the project's root directory.
  2. Run the initialization script:

    ./init_project.sh <your-test-app-id>

    This script will:

    • Generate keys in a keys folder for both the server and the specified app id.
    • Copy .env.template to .env.
    • Copy .env.tests.template to .env.tests.

Note

2. Configure Environment Variables

  1. Open the .env file generated in the project's root directory.

  2. Generate a secret key:

    openssl rand -hex 32
  3. Populate the following variables in the .env file. Example for tests and dev:

    # Mandatory
    MEMBRANE_CORS_ALLOWED_ORIGINS=http://localhost:3000
    MEMBRANE_FRONTEND=http://localhost:3000
    MEMBRANE_SECRET_KEY=your_secret_key
    MEMBRANE_CLIENT_PUBLIC_KEYS_DIRECTORY=keys/
    MEMBRANE_SERVER_PRIVATE_KEY=keys/server_private_key.pem
    MEMBRANE_SERVER_PUBLIC_KEY=keys/server_public_key.pem
    MEMBRANE_COMM_CONNECTION_STRING=your_azure_communication_service_connection_string
    MEMBRANE_SENDER_EMAIL=DoNotReply@your_domain.com
    
    # Optional
    # MEMBRANE_JWT_ACCESS_TOKEN_EXPIRE_SECONDS=
    # MEMBRANE_JWT_EXPIRE_SECONDS=
    # MEMBRANE_SESSION_LIFETIME_SECONDS=
    # MEMBRANE_SESSION_COOKIE_SECURE=
    # MEMBRANE_SESSION_TYPE=
    # MEMBRANE_TOKEN_BLACKLIST=
    # MEMBRANE_APP_ID_FIELD=
    # MEMBRANE_DATA_FIELD=
    # MEMBRANE_REDIRECT_URL_FIELD=
    # MEMBRANE_ENCODE_ALGORITHM=
    # MEMBRANE_ALLOWED_EMAIL_DOMAINS_PATTERN=
    # MEMBRANE_EMAIL_SUBJECT=
    # MEMBRANE_EMAIL_SEND_SUCCESS=
    # MEMBRANE_EMAIL_SEND_POLLER_WAIT_TIME=
    # MEMBRANE_EMAIL_SEND_TIMEOUT_SECONDS=
    # MEMBRANE_EMAIL_SEND_HTML_TEMPLATE=
    # MEMBRANE_GENERIC_500_ERROR_FIELD=
    # MEMBRANE_GENERIC_500_ERROR=
    # MEMBRANE_LOGGING_LEVEL=
    # MEMBRANE_LOGGING_FORMAT=
    # MEMBRANE_HEALTH_MESSAGE=
    # MEMBRANE_WORKERS=
    # MEMBRANE_KEEP_ALIVE=

3. Running the App with Docker

  1. Build the Docker image:

    docker build -t your_app_name .
  2. Set your desired port number:

    export PORT=<your_port_here>
  3. Run the Docker container:

    docker run -v $(pwd)/keys:/app/keys -p $PORT:$PORT -e PORT=$PORT --env-file .env your_app_name