跳转到主要内容

Python的SSPI API绑定

此版本被撤回的原因

sspi将被重命名,因为它与pywin32命名空间冲突

项目描述

Python SSPI库

Test workflow PyPI version License

此库提供Python函数,用于封装Windows SSPI API。它旨在提供高、低级接口,以便其他库可以轻松利用以使用SSPI集成。高级接口位于sspi命名空间下,而低级接口位于sspi.raw接口下。

需求

  • Python 3.8+

需要更多需求来从头开始编译代码,但此库作为wheel提供,因此安装不是强制性的。

安装

只需运行

pip install sspi

要从源安装,请运行以下命令

git clone https://github.com/jborean93/pysspi.git
python -m pip install build
python -m build
pip install dist/sspi-*.whl

开发

要运行测试或对此存储库进行更改,请运行以下命令:

git clone https://github.com/jborean93/pysspi.git
pip install -r requirements-dev.txt
pre-commit install

python -m pip install -e .

# Can compile the sspi extensions on an adhoc basis
# python setup.py build_ext --inplace

从那里,可以使用类似 VSCode 的编辑器进行更改和运行测试套件。更改后重新编译 Cython 文件,请运行 build_ext --inplace 命令。

如果在 Linux 或 macOS 上构建,必须使用 Rust 编译 libsspi 的一个版本,该版本来自 sspi-rs。在编译时必须存在与头文件一起的 libicuuc 的副本。要编译 sspi-rs,下载 Git 存储库并运行以下命令。

cargo build \
    --package sspi-ffi \
    --release

export LD_LIBRARY_PATH="${PWD}/target/release"
export LIBRARY_PATH="${PWD}/target/release"

结构

此库仅是 SSPI API 的包装器。位于 sspi 命名空间下的函数以更 Pythonic 的 snake_case 格式公开了各种 SSPI 函数。例如,AcquireCredentialsHandle 函数公开为 sspi.acquire_credentials_handle

错误以 WindowsError 的形式引发,其中包含 Windows 格式的错误消息和错误代码。对于非 Windows 主机,有一个兼容的 sspi.WindowsError 类,其结构类似于仅 Windows 的 WindowsError 内置类。一些对象和常量作为 Python 类/dataclasses/enums 公开,以便于使用。请阅读将使用的函数的 docstring,以了解更多有关如何使用它们的信息。

客户端身份验证示例

以下是如何使用此库进行客户端身份验证的基本示例

import sspi

cred = sspi.UserCredential(
    "username@DOMAIN.COM",
    "password",
)

ctx = sspi.ClientSecurityContext(
    "host/server.domain.com",
    credential=cred,
)

in_token = None
while not ctx.complete:
    out_token = ctx.step(in_token)
    if not out_token:
        break

    # exchange_with_server() is a function that sends the out_token to the
    # server we are authenticating with. How this works depends on the app
    # protocol being used, e.g. HTTP, sockets, LDAP, etc.
    in_token = exchange_with_server(out_token)

# Once authenticated we can wrap messages when talking to the server. The final
# message being sent is dependent on the application protocol
secret = b"secret data"

wrapped_secret = ctx.wrap(secret)
server_enc_resp = exchange_with_server(wrapped_secret)
server_resp = ctx.unwrap(server_enc_resp).data

UserCredential 支持更多选项,例如选择使用的身份验证协议。需要目标服务器的服务主体名称 (SPN) 和可选凭证的 ClientSecurityContext。可以根据需要使用其他选项来控制请求的上下文属性、通道绑定等。令牌和封装数据的发送方式取决于底层协议,此示例仅显示交换数据的时间。

非 Windows 支持

虽然 SSPI 是仅限 Windows 的 API,但此包附带与 manylinux2014_x86_64macosx_x86_64macosx_arm64 兼容的 wheel,使用 sspi-rs。由于所有身份验证逻辑都包含在该外部 API 中,因此对此的支持是实验性的。与 Windows 上的 SSPI 的接口完全相同,因此理论上可以使用相同的代码。换句话说,与 SSPI 实际的兼容性不是 100%,因此请自行承担风险。

建议在非 Windows 平台上使用像 python-gssapi 这样的 GSSAPI 包装库。除了 x86_64 以外,Linux 上没有对其他架构的支持,因为 sspi-rs 仅支持 glibc,因此无法与基于 musl 的发行版(如 Alpine)一起使用。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

sspi-0.1.0.tar.gz (55.5 kB 查看哈希值)

上传时间

构建分发

sspi-0.1.0-cp312-cp312-win_amd64.whl (567.6 kB 查看哈希值)

上传于 CPython 3.12 Windows x86-64

sspi-0.1.0-cp312-cp312-win32.whl (483.7 kB 查看哈希)

上传于 CPython 3.12 Windows x86

sspi-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.7 MB 查看哈希)

上传于 CPython 3.12 manylinux: glibc 2.17+ x86-64

sspi-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (4.3 MB 查看哈希)

上传于 CPython 3.12 macOS 11.0+ ARM64

sspi-0.1.0-cp312-cp312-macosx_10_9_x86_64.whl (4.6 MB 查看哈希)

上传于 CPython 3.12 macOS 10.9+ x86-64

sspi-0.1.0-cp311-cp311-win_amd64.whl (563.5 kB 查看哈希)

上传于 CPython 3.11 Windows x86-64

sspi-0.1.0-cp311-cp311-win32.whl (480.5 kB 查看哈希)

上传于 CPython 3.11 Windows x86

sspi-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.7 MB 查看哈希)

上传于 CPython 3.11 manylinux: glibc 2.17+ x86-64

sspi-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (4.3 MB 查看哈希)

上传于 CPython 3.11 macOS 11.0+ ARM64

sspi-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (4.6 MB 查看哈希)

上传于 CPython 3.11 macOS 10.9+ x86-64

sspi-0.1.0-cp310-cp310-win_amd64.whl (563.6 kB 查看哈希)

上传于 CPython 3.10 Windows x86-64

sspi-0.1.0-cp310-cp310-win32.whl (482.3 kB 查看哈希值)

上传时间 CPython 3.10 Windows x86

sspi-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.4 MB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64

sspi-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (4.3 MB 查看哈希值)

上传时间 CPython 3.10 macOS 11.0+ ARM64

sspi-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (4.6 MB 查看哈希值)

上传时间 CPython 3.10 macOS 10.9+ x86-64

sspi-0.1.0-cp39-cp39-win_amd64.whl (568.6 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

sspi-0.1.0-cp39-cp39-win32.whl (487.5 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86

sspi-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.4 MB 查看哈希值)

上传时间 CPython 3.9 manylinux: glibc 2.17+ x86-64

sspi-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (4.3 MB 查看哈希值)

上传时间 CPython 3.9 macOS 11.0+ ARM64

sspi-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (4.6 MB 查看哈希值)

上传时间 CPython 3.9 macOS 10.9+ x86-64

sspi-0.1.0-cp38-cp38-win_amd64.whl (569.8 kB 查看哈希值)

上传时间 CPython 3.8 Windows x86-64

sspi-0.1.0-cp38-cp38-win32.whl (487.9 kB 查看哈希值)

上传于 CPython 3.8 Windows x86

sspi-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.5 MB 查看哈希)

上传于 CPython 3.8 manylinux: glibc 2.17+ x86-64

sspi-0.1.0-cp38-cp38-macosx_11_0_arm64.whl (4.3 MB 查看哈希)

上传于 CPython 3.8 macOS 11.0+ ARM64

sspi-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (4.6 MB 查看哈希)

上传于 CPython 3.8 macOS 10.9+ x86-64

支持者