跳转到主要内容

Python的SSPI API绑定

项目描述

Python SSPI库

Test workflow PyPI version License

该库提供了封装Windows SSPI API的Python函数。它被设计成既可以作为高层接口也可以作为底层接口,方便其他库利用以进行SSPI集成。高层接口位于sspilib命名空间下,而底层接口位于sspilib.raw接口下。

需求

  • Python 3.8+

编译代码需要更多的需求,但该库以wheel形式提供,所以安装时不是必须的。

安装

只需运行以下命令

pip install sspilib

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

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

开发

要运行测试或修改此存储库,请运行以下命令

git clone https://github.com/jborean93/sspilib.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编译来自sspi-rslibsspi版本。必须在编译时提供与头文件一起的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的封装。位于sspilib下的高层API公开了易于使用的Python API来使用SSPI。位于sspilib.raw命名空间下的函数以更Pythonic的snake_case格式公开了各种SSPI函数。例如,AcquireCredentialsHandle函数公开为sspilib.raw.acquire_credentials_handle

错误以WindowsError的形式抛出,其中包含Windows格式化的错误消息和错误代码。对于非Windows宿主,有一个兼容的sspilib.WindowsError类,其结构与Windows特有的WindowsError内置结构相似。一些对象和常量作为Python类/dataclasses/enums公开,以便于使用。请阅读将要使用的函数的docstring以了解更多有关它们的使用方法。

客户端认证示例

以下是一个使用此库进行客户端认证的基本示例

import sspilib

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

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

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支持更多选项,例如选择使用的认证协议。需要使用ClientSecurityContext提供的凭证和目标服务器的主服务名(SPN)。根据需要,其他选项可以用来控制请求的上下文属性、通道绑定等。令牌和包装数据的发送方式取决于底层协议,此示例仅显示何时交换数据。

非Windows支持

虽然SSPI是仅适用于Windows的API,但此软件包附带与manylinux2014_x86_64macosx_x86_64macosx_arm64兼容的wheel,这些wheel使用sspi-rs。此支持是实验性的,因为所有的认证逻辑都包含在该外部API中。sspi-rs的接口与Windows上的SSPI完全相同,因此理论上可以使用相同的代码。然而,必须指出,与实际SSPI的兼容性并非100%,因此请自行承担风险。

建议在非Windows平台(如python-gssapi)上使用封装GSSAPI的库。在Linux上除x86_64以外的任何架构上都没有支持,因为sspi-rs仅支持glibc,因此它不能与基于musl的发行版(如Alpine)一起使用。

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定选择哪个,请了解有关安装软件包的更多信息。

源代码分发

sspilib-0.2.0.tar.gz (55.8 kB 查看哈希值)

上传时间: 源码

构建版本

sspilib-0.2.0-cp313-cp313-win_arm64.whl (469.0 kB 查看哈希值)

上传时间: CPython 3.13 Windows ARM64

sspilib-0.2.0-cp313-cp313-win_amd64.whl (563.7 kB 查看哈希值)

上传时间: CPython 3.13 Windows x86-64

sspilib-0.2.0-cp313-cp313-win32.whl (483.6 kB 查看哈希值)

上传时间: CPython 3.13 Windows x86

sspilib-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB 查看哈希值)

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

sspilib-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传时间: CPython 3.13 macOS 11.0+ ARM64

sspilib-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl (4.9 MB 查看哈希值)

上传时间: CPython 3.13 macOS 10.13+ x86-64

sspilib-0.2.0-cp312-cp312-win_arm64.whl (471.4 kB 查看哈希值)

上传时间: CPython 3.12 Windows ARM64

sspilib-0.2.0-cp312-cp312-win_amd64.whl (569.2 kB 查看哈希值)

上传时间: CPython 3.12 Windows x86-64

sspilib-0.2.0-cp312-cp312-win32.whl (487.3 kB 查看哈希值)

上传时间: CPython 3.12 Windows x86

sspilib-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.6 MB 查看哈希值)

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

sspilib-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传于 CPython 3.12 macOS 11.0+ ARM64

sspilib-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl (4.9 MB 查看哈希值)

上传于 CPython 3.12 macOS 10.13+ x86-64

sspilib-0.2.0-cp311-cp311-win_arm64.whl (472.5 kB 查看哈希值)

上传于 CPython 3.11 Windows ARM64

sspilib-0.2.0-cp311-cp311-win_amd64.whl (564.8 kB 查看哈希值)

上传于 CPython 3.11 Windows x86-64

sspilib-0.2.0-cp311-cp311-win32.whl (485.4 kB 查看哈希值)

上传于 CPython 3.11 Windows x86

sspilib-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.6 MB 查看哈希值)

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

sspilib-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传于 CPython 3.11 macOS 11.0+ ARM64

sspilib-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl (4.9 MB 查看哈希值)

上传于 CPython 3.11 macOS 10.9+ x86-64

sspilib-0.2.0-cp310-cp310-win_arm64.whl (473.6 kB 查看哈希值)

上传于 CPython 3.10 Windows ARM64

sspilib-0.2.0-cp310-cp310-win_amd64.whl (565.3 kB 查看哈希值)

上传于 CPython 3.10 Windows x86-64

sspilib-0.2.0-cp310-cp310-win32.whl (487.7 kB 查看哈希值)

上传于 CPython 3.10 Windows x86

sspilib-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB 查看哈希值)

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

sspilib-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传于 CPython 3.10 macOS 11.0+ ARM64

sspilib-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl (4.9 MB 查看哈希值)

上传于 CPython 3.10 macOS 10.9+ x86-64

sspilib-0.2.0-cp39-cp39-win_arm64.whl (478.4 kB 查看哈希值)

上传于 CPython 3.9 Windows ARM64

sspilib-0.2.0-cp39-cp39-win_amd64.whl (570.1 kB 查看哈希值)

上传于 CPython 3.9 Windows x86-64

sspilib-0.2.0-cp39-cp39-win32.whl (492.5 kB 查看哈希值)

上传于 CPython 3.9 Windows x86

sspilib-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB 查看哈希值)

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

sspilib-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

sspilib-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl (4.9 MB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ x86-64

sspilib-0.2.0-cp38-cp38-win_amd64.whl (572.1 kB 查看哈希值)

上传于 CPython 3.8 Windows x86-64

sspilib-0.2.0-cp38-cp38-win32.whl (493.0 kB 查看哈希值)

上传于 CPython 3.8 Windows x86

sspilib-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.4 MB 查看哈希值)

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

sspilib-0.2.0-cp38-cp38-macosx_11_0_arm64.whl (4.5 MB 查看哈希值)

上传于 CPython 3.8 macOS 11.0+ ARM64

sspilib-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl (4.9 MB 查看哈希值)

上传于 CPython 3.8 macOS 10.9+ x86-64

支持者