コンテンツにスキップ

pytilpack.msal

必要なextra

pip install pytilpack[msal]

pytilpack.msal

MSAL関連のユーティリティなど。

SimpleTokenCredential(access_token, expires_in)

Bases: TokenCredential

トークンクラス。

ソースコード位置: pytilpack/msal.py
def __init__(self, access_token: str, expires_in: int):
    self.access_token = access_token
    self.expires: datetime.datetime = datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=expires_in)
    self.expires_on: int = int(self.expires.timestamp())

get_token(*args, **kwargs)

アクセストークンを取得する。

ソースコード位置: pytilpack/msal.py
def get_token(self, *args, **kwargs) -> azure.core.credentials.AccessToken:
    """アクセストークンを取得する。"""
    del args, kwargs
    return azure.core.credentials.AccessToken(self.access_token, self.expires_on)

FileTokenCache(cache_path)

MSALのトークンキャッシュ。

使用例::

token_cache = pytilpack.msal_.FileTokenCache("path/to/cache.json")
client_app = msal.ConfidentialClientApplication(
    client_id="your_client_id",
    client_credential="your_client_secret",
    token_cache=token_cache.get(),
)
...
token_cache.save()

ソースコード位置: pytilpack/msal.py
def __init__(self, cache_path: pathlib.Path | str):
    self.cache_path = pathlib.Path(cache_path)
    self.cache = msal.SerializableTokenCache()
    with _file_token_cache_lock:
        if self.cache_path.exists():
            self.cache.deserialize(self.cache_path.read_text("utf-8", errors="replace"))

get()

キャッシュを取得する。

ソースコード位置: pytilpack/msal.py
def get(self) -> msal.SerializableTokenCache:
    """キャッシュを取得する。"""
    return self.cache

save()

キャッシュをファイルに保存する。

ソースコード位置: pytilpack/msal.py
def save(self) -> None:
    """キャッシュをファイルに保存する。"""
    with _file_token_cache_lock:
        if self.cache.has_state_changed:
            self.cache_path.write_text(self.cache.serialize(), "utf-8", errors="replace")

load_pem_certificate(certificate_path)

PEM形式の証明書データを読み込み、秘密鍵と指紋を返す。

ソースコード位置: pytilpack/msal.py
def load_pem_certificate(certificate_path: pathlib.Path | str) -> dict:
    """PEM形式の証明書データを読み込み、秘密鍵と指紋を返す。"""
    return load_pem_certificate_data(pathlib.Path(certificate_path).read_bytes())

load_pem_certificate_data(certificate_data)

PEM形式の証明書データを読み込み、秘密鍵と指紋を返す。

ソースコード位置: pytilpack/msal.py
def load_pem_certificate_data(certificate_data: bytes) -> dict:
    """PEM形式の証明書データを読み込み、秘密鍵と指紋を返す。"""
    cert = cryptography.x509.load_pem_x509_certificate(certificate_data, cryptography.hazmat.backends.default_backend())
    fingerprint = cert.fingerprint(cryptography.hazmat.primitives.hashes.SHA1())
    return {
        "private_key": certificate_data,
        "thumbprint": binascii.hexlify(fingerprint).decode("utf-8"),
    }