Skip to main content

github-fine-grained-token-client

pipeline status docs pypi supported python versions

Library and CLI tool for creating and managing fine-grained GitHub tokens.

Purpose

GitHub allows the creation of per-project access tokens with fine-grained permissions but doesn't currently have an API to do so.

This tool seeks to provide a client exposing this functionality anyway by whatever means necessary. More specifically, for now this means simulating requests to the relevant parts of the web interface closely enough to how a browser would perform them.

Installation

To install from PyPI:

pip3 install github-fine-grained-token-client

Command-line tool usage

To use the CLI tool, you'll need to install some optional dependencies first:

pip3 install 'github-fine-grained-token-client[cli]'

To create a token yourtokenname with read-only permissions (the default) for your public GitHub repository yourrepo:

github-fine-grained-token-client create --repositories yourrepo yourtokenname

There are more commands and options - please refer to the docs.

Usage as a library

Basic example script:

import asyncio
from datetime import timedelta
from os import environ

from github_fine_grained_token_client import (
    BlockingPromptTwoFactorOtpProvider,
    GithubCredentials,
    SelectRepositories,
    async_client,
)

credentials = GithubCredentials(environ["GITHUB_USER"], environ["GITHUB_PASS"])
assert credentials.username and credentials.password


async def main() -> str:
    async with async_client(
        credentials=credentials,
        # 2FA will be mandatory on GitHub starting at some point in 2023
        two_factor_otp_provider=BlockingPromptTwoFactorOtpProvider(),
    ) as session:
        token = await session.create_token(
            "my token",
            expires=timedelta(days=364),
            scope=SelectRepositories(["my-project"]),
        )
    return token


token = asyncio.run(main())

print(token)

More information

For more detailed usage information and the API reference, refer to the documentation.

License

MIT License, see LICENSE file.

Changelog

  • 1.0.9: Move keyring functionality to separate module, fixing library use without the cli or all extras installed.
  • 1.0.8: Add py.typed marker (allows typechecking when used as a dependency).
  • 1.0.7: Fix expiration date parsing in presence of refresh advice.

Metadata

Release files for github-fine-grained-token-client 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for github-fine-grained-token-client 1.0.9
File Size Uploaded Provenance Actions
github_fine_grained_token_client-1.0.9.tar.gz 23.7 kB Without provenance Details for github_fine_grained_token_client-1.0.9.tar.gz

Release files / github_fine_grained_token_client-1.0.9.tar.gz

Download URL github_fine_grained_token_client-1.0.9.tar.gz
Size 23.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e27dfbf78c99fc32d232a78a100947ddf4ce332fc01aca65e079c9c5317b5c96
BLAKE2b-256 checksum
How to use checksums
6938da9e3e8825d6c8bb30ff1ec7446c7860fc9591a4449b293ab4a888ed5952
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Security for github-fine-grained-token-client 1.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page