LoginLlama Python SDK for detecting suspicious logins
Project description
LoginLlama API Client
This Python module provides an interface to interact with the LoginLlama API, which offers login status checks for users based on various parameters.
Sign up for free at loginllama.app.
Installation
Install via pip. Requires Python 3.6 or higher.
pip install loginllama.py
Usage
First, import the necessary classes:
from loginllama.loginllama import LoginLlama
Initialization
To initialize the LoginLlama
class, you can either provide an API token directly or set it in the environment variable LOGINLLAMA_API_KEY
.
loginllama = LoginLlama("YOUR_API_TOKEN")
Or, if using the environment variable LOGINLLAMA_API_KEY
:
loginllama = LoginLlama()
# Pulls from the environment variable LOGINLLAMA_API_KEY
Checking Login Status
The primary function provided by this module is check_login
, which checks the login status of a user based on various parameters.
Parameters:
request
(optional): A Django or Flask request object. If provided, the IP address and user agent will be extracted from this object.ip_address
(optional): The IP address of the user. If not provided and therequest
object is given, it will be extracted from the request.user_agent
(optional): The user agent string of the user. If not provided and therequest
object is given, it will be extracted from the request.identity_key
: The unique identity key for the user. This is a required parameter.
Return Value:
The function returns a LoginCheck
object. This object contains the result of the login check, including the status, a message, and any applicable codes indicating the reason for the status.
Examples:
Using IP address and user agent directly:
loginCheckResult = loginllama.check_login(
ip_address="192.168.1.1",
user_agent="Mozilla/5.0",
identity_key="user123"
)
print(loginCheckResult.status, loginCheckResult.message, loginCheckResult.codes)
Using a Flask request object:
from flask import Flask, request
app = Flask(__name__)
loginllama = LoginLlama(api_token='your_api_token')
@app.route('/login_check', methods=['POST'])
def login_check():
identity_key = request.form.get('identity_key')
if not identity_key:
return "identity_key is required", 400
try:
login_check_result = loginllama.check_login(request=request, identity_key=identity_key)
return {
"status": login_check_result.status,
"message": login_check_result.message,
"codes": [code.value for code in login_check_result.codes]
}
except ValueError as e:
return str(e), 400
except Exception as e:
return str(e), 500
if __name__ == '__main__':
app.run()
Using a Django request object:
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from loginllama.loginllama import LoginLlama
@csrf_exempt
def login_check(request):
if request.method == 'POST':
identity_key = request.POST.get('identity_key')
if not identity_key:
return JsonResponse({"error": "identity_key is required"}, status=400)
try:
loginllama = LoginLlama(api_token='your_api_token')
login_check_result = loginllama.check_login(request=request, identity_key=identity_key)
return JsonResponse({
"status": login_check_result.status,
"message": login_check_result.message,
"codes": [code.value for code in login_check_result.codes]
})
except ValueError as e:
return JsonResponse({"error": str(e)}, status=400)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)
return JsonResponse({"error": "Invalid request method"}, status=405)
Error Handling
The check_login
function will raise exceptions if any of the required parameters (ip_address
, user_agent
, or identity_key
) are missing (if request
is not provided).
API Endpoint
The default API endpoint used by this module is https://loginllama.app/api/v1
.
Login Status Codes
The module provides an enumeration LoginCheckStatus
that lists various possible status codes returned by the LoginLlama API, such as VALID
, IP_ADDRESS_SUSPICIOUS
, KNOWN_BOT
, etc.
Contributing
If you find any issues or have suggestions for improvements, please open an issue or submit a pull request. Your contributions are welcome!
License
This module is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file loginllama-0.1.tar.gz
.
File metadata
- Download URL: loginllama-0.1.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b890653f6b9db196d1dfe5c60ba0a5160fa8ab5446ed3264ab4eea7631dde04 |
|
MD5 | d0fcc1c1f74fb0fea6885d8ea2fe9b39 |
|
BLAKE2b-256 | 9bb54b2228b20f7dec4392cdd35c5972133678727537ce9544f34917187f823c |
File details
Details for the file loginllama-0.1-py3-none-any.whl
.
File metadata
- Download URL: loginllama-0.1-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a0df6b4bbf3c7c427d0269c4b65ea7847f2ee952d5263d9242a524d00e39b3 |
|
MD5 | 58f44214af38060f853fa58fe1420322 |
|
BLAKE2b-256 | 1036aedf955e56134a0b7390b30f868bfd0b0892ac3bc50372c0c7a460cfc724 |