Skip to content

timonrieger/auth-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Centralized Authentication Service

This project is a Flask-based centralized authentication service that provides user registration, login, and email confirmation functionalities. It uses the database-service as database schema.

Features

  • User registration with email confirmation
  • User login with password hashing
  • Email token generation and validation for account confirmation
  • Email and password changes
  • Password reset management
  • Redirect URLs for seamless UX
  • API Key management (creation and verification)
  • User management frontend to give the user full control about his account
  • Data export

Limitations

  • No user account deletion

Requirements

  • Flask
  • Flask-SQLAlchemy
  • Werkzeug
  • python-dotenv

Setup

  1. Clone the repository:

    git clone https://github.com/timonrieger/auth-service.git
    cd auth-service
  2. Create a virtual environment and activate it:

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  3. Install the required packages:

    pip install -r requirements.txt
  4. Create a .env file in the root directory and add your configuration settings. Use the database URL provided by your hosting service. Ensure the connection string matches the one used in the database:

    SECRET_KEY=your_secret_key
    DB_URI=your_database_uri
  5. Run the application:

    python3 -m main

Usage

Register a new user

Send a POST request to /register with the following parameters:

  • email
  • password
  • username
  • then (URL to redirect after account confirmation)
data = {"email": email, "password": password, "username": username, "then": "https://YOURDOMAIN/login"}
response = requests.post(f"{AUTH_URL}/register", json=data)

Login

Send a POST request to /login with the following parameters:

  • email
  • password
data = {"email": email, "password": password}
response = requests.post(url=AUTH_URL, json=data)

Create API Key

Send a POST request to /apikey/create with the following parameters:

  • id (user ID)
data = {"id": id}
response = requests.post(url=f"{AUTH_URL}/apikey/create", json=data)

Verify API Key

Send a GET request to /apikey/verify with the authorization header:

  • Authorization (no Bearer prefix)
headers = {'Authorization': token}
response = requests.get(url=f"{AUTH_URL}/apikey/verify", headers=headers)

Response

Returns status code 200 on success. Anything else is considered to be an error. The response also always contains a message.

response.status_code
response.json()['message']

Example

Your code might look like this for logging a user in (using flask and flask_login):

response = requests.post(url=f"{AUTH_URL}/login", json=data)
if response.status_code == 200:
	flash(response.json()['message'], "success")
	login_user(user)
	return redirect(url_for("home"))
flash(response.json()['message'], "error")

Configuration

You will have to change the email content in utils.py by updating the urls and my name. You can update anything else as well.

License

This project is licensed under the MIT License. See the LICENSE file for details.