File: __init__.py

package info (click to toggle)
python-id 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: python: 905; makefile: 80; sh: 5
file content (114 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2022 The Sigstore Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
API for retrieving OIDC tokens.
"""

from __future__ import annotations

import base64
import binascii
import json
from typing import Callable

__version__ = "1.6.1"


class IdentityError(Exception):
    """
    Raised on any OIDC token format or claim error.
    """

    pass


class AmbientCredentialError(IdentityError):
    """
    Raised when an ambient credential should be present, but
    can't be retrieved (e.g. network failure).
    """

    pass


class GitHubOidcPermissionCredentialError(AmbientCredentialError):
    """
    Raised when the current GitHub Actions environment doesn't have permission
    to retrieve an OIDC token.
    """

    pass


def _validate_credential(credential: str, audience: str) -> None:
    # Decode credential to verify it roughly looks like a token and contains
    # the correct audience
    try:
        _, payload, _ = credential.split(".")
        decoded_payload = base64.urlsafe_b64decode(payload + "==").decode("utf-8")
        payload_json = json.loads(decoded_payload)
    except (ValueError, binascii.Error, json.decoder.JSONDecodeError) as e:
        raise AmbientCredentialError("Malformed token") from e

    if not isinstance(payload_json, dict):
        raise AmbientCredentialError("Malformed token payload (JWT is not a JSON object)")
    if "aud" not in payload_json:
        raise AmbientCredentialError("Malformed token payload (audience claim is missing)")
    if payload_json["aud"] != audience:
        raise AmbientCredentialError(
            f"Token audience claim mismatch (expected {audience}, got {payload_json['aud']})"
        )


def detect_credential(audience: str) -> str | None:
    """
    Try each ambient credential detector, returning the first one to succeed
    or `None` if all fail.

    Raises `AmbientCredentialError` if any detector fails internally (i.e.
    detects a credential, but cannot retrieve it).
    """
    from ._internal.oidc.ambient import (
        detect_buildkite,
        detect_circleci,
        detect_gcp,
        detect_github,
        detect_gitlab,
    )

    detectors: list[Callable[..., str | None]] = [
        detect_github,
        detect_gcp,
        detect_buildkite,
        detect_gitlab,
        detect_circleci,
    ]
    for detector in detectors:
        credential = detector(audience)
        if credential is not None:
            _validate_credential(credential, audience)
            return credential
    return None


def decode_oidc_token(token: str) -> tuple[str, str, str]:
    # Split the token into its three parts: header, payload, and signature
    header, payload, signature = token.split(".")

    # Decode base64-encoded header and payload
    decoded_header = base64.urlsafe_b64decode(header + "==").decode("utf-8")
    decoded_payload = base64.urlsafe_b64decode(payload + "==").decode("utf-8")

    return decoded_header, decoded_payload, signature