Skip to main content

A python authentication package to work with postgresql, inspired from devise gem for ruby.

Project description

AuthMe

A python authentication package to work with postgresql, inspired by devise from ruby.

PyPi Link - https://pypi.org/project/AuthMe/

Features

  1. Regular Auth Flows : Signup, Login, Logout, Password reset
  2. Invitations (coming soon)

Essential Things to keep handy, before using

  1. A postgresql database, ready to accept connections
  2. A secret token to encrypt all tokens
  3. All codes returned, are standard http response codes

Installation -

pip install AuthMe

Essential Setup

from AuthMe import AuthMe

db_config = {
    'dbname': 'your-db-name',
    'user': 'your-db-user',
    'password': 'your-db-password',
    'host': 'your-db-host',
    'port': 'your-db-port',
}

auth = AuthMe(db_config, "your-secret-token")

To initialize the database with the necessary tables

auth.initialize_database()

Signup User

# Below are the necessary attributes. Add more attributes, as needed by your application, but first, add those columns in the database
user_attributes = {
    'email': 'mail@example.com',
    'password': 'Password1$',
    'first_name': 'John',
    'last_name': 'Doe'
}
origin_host = "localhost"
ip_address = "127.0.0.1"

auth.signup(user_attributes, origin_host, ip_address)

Response

Signup Success -

# A Python Object
namespace(code=200, token='eyJhb...Hg3TF4', message='Success')

Signup Failure -

# A Python Object
namespace(code=409, token=None, message='User already exists')

Login User

auth.login(email, password, origin_host, ip_address)

Response

Login Success -

# A Python Object
namespace(code=200, token='eyJhbGc...NAM_Gg', message='Success')

Login Failure -

# A Python Object
namespace(code=401, token=None, message='Invalid credentials')

Logout User

auth.logout(token)

Response

# A Python Object
namespace(code=200, token=None, message='Logout success')

Authenticate User

auth.authenticate(token, origin_host, ip_address)

Response

# Boolean
True || False

Current User

auth.current_user(token)

Response

# A Python Object containing all the details of user
namespace(id=1, email='mail@example.com', encrypted_password='f13...0b3', first_name='John', last_name='Doe', created_at=datetime.datetime(2024, 1, 7, 11, 45, 59, 628334), updated_at=datetime.datetime(2024, 1, 7, 11, 45, 59, 628334), reset_password_token=None, reset_password_sent_at=None, reset_password_at=datetime.datetime(2024, 1, 7, 11, 49, 40, 733419), active=True)

NOTE : Like devise, first authenticate the token, then check for current user always. authenticate method can be invoked as a dependency method, on all the protected routes (both Fast Api and Flask support Depends), and if it returns False, throw 401 error.

Generate Reset Password

auth.reset_password_token("mail@example.com")

Response

# A reset token, which can be sent out via mail or whatever seems convenient
eyJh...s4Ac

Generate Password from Reset Token

auth.reset_password(reset_token, "Password1$")

Response

# Boolean
True || False

Features under development -

  1. Config for AuthMe class object :
    revoke_tokens_upon_password_reset : True || False, Default : False
    allow_multiple_sessions : True || False, Default : True
    auth_token_expiry : n second, Default : 86400 second (1 day)
    password_reset_token_expiry : n second, Default : 86400 second (1 day)
  2. Password update for loggedin user

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

AuthMe-0.1.5.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file AuthMe-0.1.5.tar.gz.

File metadata

  • Download URL: AuthMe-0.1.5.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for AuthMe-0.1.5.tar.gz
Algorithm Hash digest
SHA256 07b3317fce7124658bbcfb873f8eecf02ea1c84cf98dd9b90913110d55fafe2c
MD5 151acc63a80c74863cf905f54b713fbe
BLAKE2b-256 fa5e7510aed922aaebfa562d009985c4e6c54c094ce2fa73b39d41ce722e129f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page