Skip to main content

Python wrapper for cern-sso-cli - CERN SSO authentication

Project description

cern-sso-python

Python wrapper for cern-sso-cli - CERN SSO authentication.

Installation

  1. Install the CLI (v0.24.0+): See cern-sso-cli installation instructions

  2. Install the Python package:

    pip install cern-sso-python
    

Quick Start

from cern_sso import get_cookies, get_token, device_flow

# Get cookies for a URL (requires Kerberos ticket or will prompt)
jar = get_cookies("https://gitlab.cern.ch")

# With 2FA OTP
jar = get_cookies("https://gitlab.cern.ch", otp="123456")

# Get an OAuth2 access token
token = get_token(client_id="my-app", redirect_uri="https://my-app/callback")

# Device flow for headless servers
token = device_flow(client_id="my-app")

Usage

Cookies

from cern_sso import get_cookies, load_cookies

# Authenticate and get cookies
jar = get_cookies("https://gitlab.cern.ch", otp="123456")

# Use with urllib
import urllib.request
opener = urllib.request.build_opener(
    urllib.request.HTTPCookieProcessor(jar)
)
response = opener.open("https://gitlab.cern.ch/api/v4/user")

# Save cookies to file
jar = get_cookies("https://gitlab.cern.ch", file="cookies.txt")

# Load existing cookies
jar = load_cookies("cookies.txt")

With requests

from cern_sso import get_cookies, to_requests_jar
import requests

jar = get_cookies("https://gitlab.cern.ch")
req_jar = to_requests_jar(jar)  # Requires: pip install requests
response = requests.get("https://gitlab.cern.ch/api/v4/user", cookies=req_jar)

OAuth2 Tokens

from cern_sso import get_token

token = get_token(client_id="my-app", redirect_uri="https://my-app/callback")

# Access token properties
print(token.access_token)
print(token.token_type)      # "Bearer"
print(token.expires_at)      # datetime when token expires
print(token.is_expired)      # bool

# Dict access (oauthlib compatible)
print(token["access_token"])

# Use with requests-oauthlib
from requests_oauthlib import OAuth2Session
session = OAuth2Session(token=token)

Device Flow

For headless servers without Kerberos:

from cern_sso import device_flow

token = device_flow(client_id="my-app")
# CLI will print: Go to https://auth.cern.ch/device and enter code: XXXX-YYYY
# After authenticating in browser, token is returned

print(token.access_token)
print(token.refresh_token)

Advanced: Custom Client

from cern_sso import CERNSSOClient

client = CERNSSOClient(
    cli_path="/custom/path/cern-sso-cli",
    quiet=False,  # Show CLI output
)
jar = client.get_cookies("https://gitlab.cern.ch")

2FA Options

from cern_sso import get_cookies

# Force OTP method (even if WebAuthn is default)
jar = get_cookies("https://gitlab.cern.ch", use_otp=True)

# Force WebAuthn with PIN
jar = get_cookies("https://gitlab.cern.ch", use_webauthn=True, webauthn_pin="1234")

# Specify user and OTP together
jar = get_cookies("https://gitlab.cern.ch", user="alice", otp="123456")

# Use 1Password CLI to get OTP
jar = get_cookies("https://gitlab.cern.ch", otp_command="op item get CERN --otp")
Parameter Description
user Kerberos username
otp 6-digit OTP code
otp_command Command to fetch OTP
otp_retries Max retry attempts
use_otp Force OTP method
use_webauthn Force WebAuthn method
webauthn_pin FIDO2 key PIN
webauthn_device Path to FIDO2 device

Keytab Authentication

For automated environments, you can use Kerberos keytabs:

from cern_sso import get_cookies
import os

# Using KRB5_KTNAME env var (recommended)
os.environ["KRB5_KTNAME"] = "/path/to/keytab"
jar = get_cookies("https://gitlab.cern.ch")

# Explicit keytab file
jar = get_cookies("https://gitlab.cern.ch", keytab="/path/to/keytab")

# Force keytab authentication
jar = get_cookies("https://gitlab.cern.ch", use_keytab=True)

# Force credential cache
jar = get_cookies("https://gitlab.cern.ch", use_ccache=True)

# Custom Kerberos config
jar = get_cookies("https://gitlab.cern.ch", krb5_config="/path/to/krb5.conf")
Parameter Description
keytab Path to Kerberos keytab file
use_keytab Force keytab authentication
use_password Force password authentication
use_ccache Force credential cache authentication
krb5_config Kerberos config source ('embedded', 'system', or file path)

API Reference

get_cookies(url, **kwargs) -> MozillaCookieJar

Authenticate and return session cookies.

Parameter Type Description
url str Target URL to authenticate against
file str | Path Save cookies to file (optional)
user str Kerberos username
otp str 6-digit OTP code
otp_command str Command to fetch OTP
use_otp bool Force OTP method
use_webauthn bool Force WebAuthn (security key) method
webauthn_pin str FIDO2 security key PIN
keytab str Path to Kerberos keytab file
use_keytab bool Force keytab authentication
use_password bool Force password authentication
use_ccache bool Force credential cache authentication
krb5_config str Kerberos config source ('embedded', 'system', or file path)
force bool Force re-authentication
insecure bool Skip certificate validation

get_token(client_id, redirect_uri, **kwargs) -> TokenResult

Get OAuth2 access token via Authorization Code flow. Accepts same authentication parameters as get_cookies.

device_flow(client_id, **kwargs) -> TokenResult

Get OAuth2 tokens via Device Authorization Grant (for headless environments).

CERNSSOClient(cli_path=None, quiet=True)

Low-level client for direct CLI invocation.

Parameter Type Description
cli_path str Path to cern-sso-cli binary (auto-detect if None)
quiet bool Suppress CLI progress output (default: True)

Exceptions

Exception Description
CERNSSOError Base exception
CLINotFoundError cern-sso-cli not found in PATH
CLIVersionError CLI version too old (requires ≥0.24.0)
AuthenticationError Authentication failed
CookieError Cookie file operations failed

Requirements

License

GPL-3.0 - see LICENSE

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

cern_sso_python-0.2.0.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

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

cern_sso_python-0.2.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file cern_sso_python-0.2.0.tar.gz.

File metadata

  • Download URL: cern_sso_python-0.2.0.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cern_sso_python-0.2.0.tar.gz
Algorithm Hash digest
SHA256 af4edff36d5f69e2fe33184ea3656f9cb4018c06f7c1a993bd3ebd41b74edf4e
MD5 03e9cce3827f1e1d644a033c6a4344b0
BLAKE2b-256 04027aa727b74861c0f7c6a64d8939c51b984ba0cfc4d17abc78cd07496a79c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cern_sso_python-0.2.0.tar.gz:

Publisher: publish.yml on clelange/cern-sso-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cern_sso_python-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cern_sso_python-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 135b374acc4c3de5e5dc1a2616908d0921190b3b52eaf78e54a188238cbe76f7
MD5 2e4134d40373aa7b4617a8a8f899d0a5
BLAKE2b-256 024fa9ac4b27424b144939ed32cd6525a2f47e363f4277500ea803084e1ae275

See more details on using hashes here.

Provenance

The following attestation bundles were made for cern_sso_python-0.2.0-py3-none-any.whl:

Publisher: publish.yml on clelange/cern-sso-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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