A Streamlit application with secure PostgreSQL-based token storage using encryption and a REST API for external access.
- π Secure Token Storage: Tokens are encrypted before storing in PostgreSQL
- π€ User Authentication: Database-based user management
- π Auto-Generate Tokens: Automatic secure token generation
- βοΈ Manual Token Entry: Support for existing tokens
- π‘οΈ Encryption: Fernet encryption for all sensitive data
- π Dashboard: Token management and user information
- π REST API: Secure API for external applications to access tokens
- π JWT Authentication: Secure API authentication with JWT tokens
- Encryption at Rest: All tokens are encrypted using Fernet encryption
- Password Hashing: User passwords are hashed using SHA-256
- Database Security: PostgreSQL with proper connection handling
- Environment Variables: Sensitive configuration stored in .env files
- JWT Authentication: Secure API access with time-limited tokens
- Access Control: Users can only access their own tokens
- Python 3.7+
- PostgreSQL database
- pip (Python package manager)
-
Clone the repository
git clone <repository-url> cd streamlit
-
Install dependencies
pip install -r requirements.txt
-
Set up PostgreSQL
- Install PostgreSQL on your system
- Create a new database for the application
- Note down the database credentials
-
Configure environment variables
cp env_example.txt .env
Edit
.env
file with your database credentials:# Database Configuration DB_HOST=localhost DB_NAME=token_storage DB_USER=postgres DB_PASSWORD=your_password_here DB_PORT=5432 # Encryption Key (will be auto-generated on first run) ENCRYPTION_KEY=your_encryption_key_here # JWT Secret for API Authentication (generate this securely) JWT_SECRET=your_jwt_secret_key_here
-
Initialize the database
python setup_database.py
-
Run the application
streamlit run app.py
-
Start the API server (optional, for external access)
python api_server.py
The API server provides secure access to tokens for external applications:
python api_server.py
The API will be available at http://localhost:8000
Other applications can use the provided Python client to access tokens:
from token_client import SecureTokenClient
# Initialize client
client = SecureTokenClient("http://localhost:8000")
# Login
if client.login("testuser", "testpass123"):
# Get token for your application
token_data = client.get_token(
user_id="testuser",
application_name="my-app",
purpose="API integration"
)
if token_data:
token = token_data["token"]
print(f"Token: {token}")
You can also make direct HTTP calls to the API:
-
Login to get JWT token:
curl -X POST "http://localhost:8000/auth/login" \ -H "Content-Type: application/json" \ -d '{"username": "testuser", "password": "testpass123"}'
-
Access token using JWT:
curl -X POST "http://localhost:8000/tokens/access" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "Content-Type: application/json" \ -d '{"user_id": "testuser", "application_name": "my-app", "purpose": "API integration"}'
POST /auth/login
- Authenticate and get JWT tokenPOST /tokens/access
- Access a user's tokenGET /tokens/status/{user_id}
- Check token statusDELETE /tokens/{user_id}
- Delete user's tokenGET /health
- Health check
See example_usage.py
for complete examples of how to integrate with:
- GitHub API
- Custom APIs
- Token management
- Error handling
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE tokens (
id SERIAL PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
encrypted_token TEXT NOT NULL,
generation_method VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Start the application:
streamlit run app.py
- Login: Use the test credentials created during setup
- Username:
testuser
- Password:
testpass123
- Username:
- Choose token method: Auto-generate or manual entry
- Complete setup: View your dashboard
- Start API server:
python api_server.py
(for external access) - Use tokens in other apps: Use the client library or direct API calls
- The encryption key is stored in the
.env
file - Never commit the
.env
file to version control - Use a strong, randomly generated key in production
- Consider using a key management service for production
- Use a strong JWT secret in production
- JWT tokens expire after 24 hours by default
- Store JWT secrets securely
- Consider shorter expiration times for sensitive applications
- Use strong database passwords
- Restrict database access to application servers only
- Enable SSL connections for database communication
- Regular database backups with encryption
- Use HTTPS in production
- Implement rate limiting
- Monitor API access logs
- Consider API key authentication for additional security
- Use HTTPS in production
- Implement proper session management
- Add rate limiting for login attempts
- Consider adding two-factor authentication
βββ app.py # Main Streamlit application
βββ api_server.py # FastAPI server for external access
βββ token_client.py # Python client library
βββ example_usage.py # Usage examples
βββ database.py # Database and encryption logic
βββ setup_database.py # Database initialization script
βββ requirements.txt # Python dependencies
βββ env_example.txt # Environment variables template
βββ README.md # This file
- Update the database schema if needed
- Add new methods to
SecureTokenStorage
class - Update the Streamlit interface in
app.py
- Add corresponding API endpoints in
api_server.py
- Update the client library in
token_client.py
- Test thoroughly with the database
- Verify PostgreSQL is running
- Check database credentials in
.env
- Ensure database exists and is accessible
- Check firewall settings
- Verify the API server is running on port 8000
- Check JWT_SECRET is set in
.env
- Ensure proper network connectivity
- Check API server logs for errors
- Verify
ENCRYPTION_KEY
is set in.env
- Ensure the key is properly formatted
- Check for key corruption or changes
- Check Streamlit logs for error messages
- Verify all dependencies are installed
- Ensure proper file permissions
-
Use a production database
- Set up PostgreSQL with proper security
- Use connection pooling
- Enable SSL connections
-
Secure the environment
- Use strong encryption keys
- Secure environment variables
- Enable HTTPS for both Streamlit and API
-
API deployment
- Use a production WSGI server (Gunicorn)
- Set up reverse proxy (Nginx)
- Enable SSL/TLS
- Configure proper CORS settings
-
Monitoring and logging
- Set up application monitoring
- Configure database logging
- Monitor for security events
- Log API access for audit trails
-
Backup strategy
- Regular database backups
- Encrypted backup storage
- Test restore procedures
This project is licensed under the MIT License - see the LICENSE file for details.