A keyring backend for Posit Package Manager using OIDC device flow.
Project description
Posit Device Flow Keyring Backend
This is a custom Python keyring backend for the Posit Package Manager. It allows the handling of OAuth2 device flow and token exchange to securely store and retrieve Package Manager tokens.
- Implements OAuth 2.0 device flow for user authentication if no token exists in the system keyring.
- Stores the token securely in the system keyring after successful authentication.
- If the Package Manager token is expired, it will automatically go through the device flow again to refresh the token.
- Ability to bypass the device flow by setting the
PACKAGEMANAGER_IDENTITY_TOKEN_FILEenvironment variable to a file containing the identity token. This will allow the backend to directly exchange the identity token for a Package Manager token without user interaction. - Supports multiple platforms (macOS, Windows, Linux) using the appropriate keyring system keyring backend.
Installation
To use this keyring backend, you need to install posit-keyring:
# create a virtual environment
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
# install the package, this will install the keyring as a dependency if not already installed
pip install posit-keyring
It is good to verify that the keyring package is installed correctly and that the backend is set up properly.
# verify you are using the Python keyring package in the virtual environment
which keyring # On Windows use `where keyring`
# verify the keyring backend is set up correctly
keyring --list-backends
# You should see posit_keyring.backends.PackageManagerKeyring
Now you can use this keyring backend for installing and uploading packages with the Posit Package Manager.
Setup
Environment Variables
Package Manager
To set up the Posit Package Manager keyring backend, you need to ensure that the PACKAGEMANAGER_ADDRESS environment variable is set to the URL of your PPM instance. This can be done in your shell configuration file (e.g., .bashrc, .zshrc, or .bash_profile).
export PACKAGEMANAGER_ADDRESS="https://your-ppm-instance.com"
# Reload your shell configuration
source ~/.bashrc # or source ~/.zshrc, etc.
Or on Windows, set the environment variable in PowerShell or Command Prompt.
$env:PACKAGEMANAGER_ADDRESS = "https://your-ppm-instance.com"
Identity Token
A common pattern is to store the identity token in a file, which can be referenced by the PACKAGEMANAGER_IDENTITY_TOKEN_FILE environment variable. This file should contain the token string.
# Create a file to store the identity token
echo "your-identity-token" > ~/identity_token.txt
export PACKAGEMANAGER_IDENTITY_TOKEN_FILE=~/identity_token.txt
# Reload your shell configuration
source ~/.bashrc # or source ~/.zshrc, etc.
Or on Windows, set the environment variable in PowerShell or Command Prompt.
New-Item -Path $env:USERPROFILE -Name "identity_token.txt" -ItemType "file" -Value "your-identity-token"
$env:PACKAGEMANAGER_IDENTITY_TOKEN_FILE = "$env:USERPROFILE\identity_token.txt"
Setting this will allow the keyring backend to read the identity token from the specified file. If you do not set this variable, the backend will prompt you to authenticate using the OAuth2 device flow. Setting it skips the flow entirely and directly exchanges the identity token for a Package Manager token.
Posit Package Manager Configuration
In Package Manager, you will want to create an authenticated Python repository. This can be done by running the following command:
# create an authenticated Python repository in Posit Package Manager
rspm create repo --name=pypi-auth --type=python --authenticated
# subscribe the PyPI mirror to the new authenticated repository
rspm subscribe --repo=pypi-auth --source=pypi
# if you have local packages you want to upload, you can also create a local repository
rspm create source --name=local-python-src --type=local-python
rspm subscribe --repo=pypi-auth --source=local-python
Pip Configuration
Then you can set your pip configuration to use the new authenticated repository by creating or editing the pip.conf file (or pip.ini on Windows) in your home directory:
# ~/.config/pip/pip.conf (Linux/macOS)
[global]
index-url = https://__token__@your-ppm-instance.com/pypi-auth/latest/simple/
# %APPDATA%\pip\pip.ini (Windows)
[global]
index-url = https://__token__@your-ppm-instance.com/pypi-auth/latest/simple/
Alternatively, you can set the PIP_INDEX_URL environment variable to point to your PPM instance:
export PIP_INDEX_URL="https://__token__@your-ppm-instance.com/pypi-auth/latest/simple/"
# Reload your shell configuration if saved in .bashrc or .zshrc
source ~/.bashrc # or source ~/.zshrc, etc.
Or on Windows, set the environment variable in PowerShell or Command Prompt.
$env:PIP_INDEX_URL = "https://__token__@your-ppm-instance.com/pypi-auth/latest/simple/"
[!WARNING] If everything is configured properly,
keyringshould give this backend the highest priority automatically. Sometimes akeyringrc.cfgfile exists that causes issues. You can delete it to force the keyring to use the correct backend:rm ~/.config/python_keyring/keyringrc.cfg # Linux/macOS del %APPDATA%\python_keyring\keyringrc.cfg # WindowsYou can also manually specify the backend in your
pip.conforpip.inifile with:[global] keyring_backend = posit_keyring.backends.PackageManagerKeyringOr with an environment variable (mentioned in these docs):
export PYTHON_KEYRING_BACKEND="posit_keyring.backends.PackageManagerKeyring" # Linux/macOS $env:PYTHON_KEYRING_BACKEND="posit_keyring.backends.PackageManagerKeyring" # Windows
Twine Configuration
If you are using twine to upload packages, you can also configure it to use the Posit Package Manager keyring backend by creating or editing the pypirc file in your home directory:
# ~/.pypirc (Linux/macOS)
[distutils]
index-servers =
package-manager
[package-manager]
repository = https://your-ppm-instance.com/upload/pypi/local-python-src
username = __token__
# %APPDATA%\.pypirc (Windows)
[distutils]
index-servers =
package-manager
[package-manager]
repository = https://your-ppm-instance.com/upload/pypi/local-python-src
username = __token__
Alternatively, you can set the TWINE_REPOSITORY_URL environment variable to point to your PPM instance:
export TWINE_REPOSITORY_URL="https://your-ppm-instance.com/upload/pypi/local-python-src"
export TWINE_USERNAME="__token__"
# Reload your shell configuration if saved in .bashrc or .zshrc
source ~/.bashrc # or source ~/.zshrc, etc.
Or on Windows, set the environment variables in PowerShell or Command Prompt.
$env:TWINE_REPOSITORY_URL = "https://your-ppm-instance.com/upload/pypi/local-python-src"
$env:TWINE_USERNAME = "__token__"
Usage
Once the backend is set up, you can use pip and twine commands to install and upload packages, and the keyring backend will handle authentication the authentication flow automatically.
# Install a package
pip install your-package
# Upload a package
twine upload dist/* # include `-r package-manager` if you configured twine with `.pypirc`
Development
Prerequisites
Cloning the Repository
To get posit-keyring locally, you can clone the repository:
git clone https://github.com/posit-dev/posit-keyring.git
cd posit-keyring
Setting up the Development Environment
There is a just task to set up the development environment, which will create a virtual environment and install the required dependencies:
# Create the virtual environment with the required dependencies from the uv.lock file
just sync
# Activate the created virtual environment
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
To run the package locally in editable mode:
just install
You can also run linting and type checking with:
# Run linting
just lint
# Run type checking
just type
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file posit_keyring-0.2.0.tar.gz.
File metadata
- Download URL: posit_keyring-0.2.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8c1139636ca0338cb4ea45c491f8c49b07d4465d331e4e36919d02ea1918b39
|
|
| MD5 |
245a3d2f5a0ba7924987f5141e0e14a4
|
|
| BLAKE2b-256 |
0ae77ad86441077cbef146901eba8537a33af5e09a4f6cd0c2c9b90a8b643ec1
|
Provenance
The following attestation bundles were made for posit_keyring-0.2.0.tar.gz:
Publisher:
release.yml on posit-dev/posit-keyring
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
posit_keyring-0.2.0.tar.gz -
Subject digest:
d8c1139636ca0338cb4ea45c491f8c49b07d4465d331e4e36919d02ea1918b39 - Sigstore transparency entry: 270370589
- Sigstore integration time:
-
Permalink:
posit-dev/posit-keyring@deff219303fb91c797db092cfc82da7774561de0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/posit-dev
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@deff219303fb91c797db092cfc82da7774561de0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file posit_keyring-0.2.0-py3-none-any.whl.
File metadata
- Download URL: posit_keyring-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4704e446d8a18beb12739c6be372b04f921e91c5e3f0b60621bb0781ce2876cd
|
|
| MD5 |
6c672346dbe5dc1203c064920a71f101
|
|
| BLAKE2b-256 |
39e515b6574e3432b19e6176521d5d5173a601f067a59460a3ce83ab25aab2f3
|
Provenance
The following attestation bundles were made for posit_keyring-0.2.0-py3-none-any.whl:
Publisher:
release.yml on posit-dev/posit-keyring
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
posit_keyring-0.2.0-py3-none-any.whl -
Subject digest:
4704e446d8a18beb12739c6be372b04f921e91c5e3f0b60621bb0781ce2876cd - Sigstore transparency entry: 270370592
- Sigstore integration time:
-
Permalink:
posit-dev/posit-keyring@deff219303fb91c797db092cfc82da7774561de0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/posit-dev
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@deff219303fb91c797db092cfc82da7774561de0 -
Trigger Event:
release
-
Statement type: