Skip to main content

A module for managing API authentication using 1Password and more.

Project description

OAuthManager

OAuthManager is a Python package for managing API authentication using 1Password Service Accounts and securely caching credentials.

Features

  • Manage API credentials securely via 1Password.
  • Cache API credentials locally using encryption for faster retrieval.
  • Designed to be extensible for any API that requires authentication.

Requirements

  • Python 3.11 or later
  • 1Password Service Account
  • 1Password CLI
  • An environment variable for your 1Password service account token (OP_SERVICE_ACCOUNT_TOKEN).
  • cryptography Python package

Installation

  1. Install Python and Pip

    Ensure you have Python 3.11 installed on your system. You can download it from python.org.

  2. Install the OAuthManager package

    You can install the OAuthManager package directly from PyPI:

    pip install oauthmanager
    

Setting Up 1Password Service Account

1. Create a 1Password Account

To begin, sign up for a 1Password account. You will need to be on a 1Password Teams, Business, or Enterprise plan to create service accounts.

2. Create a 1Password Service Account

A service account allows OAuthManager to securely retrieve your API credentials. Follow the steps below to create one:

Create Service Account via 1Password Website

  1. Sign in to your account on 1Password.com.
  2. Select Developer Tools in the sidebar.
  3. Under Infrastructure Secrets Management, select Other.
  4. Click Create a Service Account and follow the instructions:
    • Choose a name for the service account.
    • Choose whether the service account can create vaults.
    • Choose the vaults that the service account can access (make sure it can access your API vault).
    • Click Create Account.
    • Click Save in 1Password to store your service account token.

⚠️ Important: The service account token is shown only once during the creation process. Save it immediately in your 1Password account.

3. Set Environment Variables

Platform-Specific Instructions

You will need to set two environment variables:

  • OP_SERVICE_ACCOUNT_TOKEN: The service account token created in the previous step.
  • OP_CACHE: Enable caching for faster retrieval.

macOS/Linux

  1. Open your terminal.

  2. Use the following command to open your .bashrc, .bash_profile, or .zshrc (depending on your shell):

    nano ~/.bashrc
    
  3. Add the following lines to export your service account token and enable caching:

    export OP_SERVICE_ACCOUNT_TOKEN="your-service-account-token"
    export OP_CACHE="true"
    
  4. Save the file and run:

    source ~/.bashrc
    

Windows

  1. Open the Command Prompt as Administrator.

  2. Set the environment variables:

    setx OP_SERVICE_ACCOUNT_TOKEN "your-service-account-token"
    setx OP_CACHE "true"
    

    This sets the variables globally. Restart the terminal for them to take effect.

Usage

Once you’ve installed OAuthManager and set up the necessary environment variables, you can start retrieving credentials from 1Password.

Example Code:

from oauthmanager import AuthManager, OnePasswordAuthManager

# Initialize the Auth Manager
auth_manager = OnePasswordAuthManager()

# Retrieve credentials for Spotify
spotify_creds = auth_manager.get_credentials("Spotify", "client_id", "client_secret", "uri")

# Use the credentials
print(f"Client ID: {spotify_creds['client_id']}")
print(f"Client Secret: {spotify_creds['client_secret']}")
print(f"Redirect URI: {spotify_creds['uri']}")

Caching

By default, the OAuthManager caches credentials locally using encryption. To refresh the cache, simply delete the credentials_cache.json file, or let it expire after 24 hours.

License

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

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

oauthmanager-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oauthmanager-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file oauthmanager-0.1.1.tar.gz.

File metadata

  • Download URL: oauthmanager-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.6.0

File hashes

Hashes for oauthmanager-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cc98f5d9255ea047b0f29be95eb355ebd4ee0eb8459b00cfae11473362672151
MD5 781dd2e2e6ff4a652c04b8043bb1421c
BLAKE2b-256 b8dd9bc942201e1a070d6c56faffaf324ddf5a815d9c7ab30ba4402ab9870ffe

See more details on using hashes here.

File details

Details for the file oauthmanager-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: oauthmanager-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.6.0

File hashes

Hashes for oauthmanager-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dbdc21744865a8d88d40a440380b7cbed4f6bd0564c554bc8277a9db5e99c3d7
MD5 4edd1f5164dad9fb004c5aea681c9462
BLAKE2b-256 e737e281726183f798c7c0af6826db5ba28cbbbb13529af0e46fdd4ecc65f54f

See more details on using hashes here.

Supported by

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