pysapsso2
A pure python library for generating SAP Assertion/Logon tickets.
SAP Logon Tickets are authentication tokens accepted by :
- SAP NetWeaver ABAP (SAP GUI, Webdynpro/Fiori, Web Services)
- SAP NetWeaver Java (Web)
This library is an opensource alternative to SAPSSOEXT (304450 - Single-Sign-On with SAP logon tickets in non-SAP systems)
Installation
pip install pysapsso2
How to generate an SAP shortcut
cf. examples/generate_shortcut.py
How to parse an SAP Logon Ticket
from pysapsso2 import SapTicket
ticket = SapTicket.from_b64(cookie_value)
print(ticket.user, ticket.source_sid, ticket.source_client, ticket.expires_at)
How to verify an SAP Logon Ticket
from pysapsso2 import SapTicket, SapTicketHandler
ticket = SapTicket.from_b64(cookie_value)
handler = SapTicketHandler("SSO", "000", certificate=trusted_cert_pem)
handler.verify(ticket) # raises SignatureError / CertificateError
verify() checks, in order:
- the payload digest against the
message_digestsigned attribute, - that the ticket names the supplied certificate as its signer,
- that the certificate is within its validity period (
check_certificate_validity=Falseto skip,at_time=to pin the moment), - the signature over the signed attributes.
The certificate must come from the caller. A ticket may embed a certificate,
but anybody can embed one they hold the key for, so it is never used as a trust
anchor. pysapsso2.crypto.get_dsa_key() exposes the embedded key for inspection
only.
Signing algorithms
| Key | Key sizes | Digest |
|---|---|---|
| DSA | 1024 – 4096 | sha1, sha256 |
| RSA (PKCS#1 v1.5) | 1024 – 4096 | sha1, sha256 |
| ECDSA | P-256, P-384, P-521 | sha1, sha256 |
Every combination in the table was accepted by a live SAP NetWeaver ABAP system.
DSA with sha1 is the default, because that is what SAPSSOEXT emits. Use it if
you need a ticket that SAPSSOEXT can also read. Otherwise prefer RSA or ECDSA
with sha256.
Pick the algorithm on the handler:
from Crypto.PublicKey import RSA
from pysapsso2 import SapTicketHandler
handler = SapTicketHandler(
"SSO",
"000",
private_key=RSA.import_key(key_pem),
certificate=cert_pem,
digest_algorithm="sha256",
)
ticket = handler.new("DEMOUSER")
digest_algorithm defaults to "sha1". The handler picks the signer from the
key type. You can also build the signer yourself with
pysapsso2.DsaSigner, pysapsso2.RsaSigner or pysapsso2.signer_for_key() and
pass it as private_key; the handler's digest_algorithm is then ignored,
because the signer carries its own.
Verification needs no configuration. verify() reads the digest algorithm from
the ticket and the key algorithm from the certificate.
How to generate an SAP Logon Ticket with SAPSSOEXT
For reference here are commands to generate a new key and self-signed certificate for testing SAPSSOEXT:
# Generate a DSA key
openssl dsaparam -out sapsso2.key -genkey 1024
# Generate a certificate
openssl req -x509 -new -sha1 -subj "/CN=pysapsso2" -key sapsso2.key -out sapsso2.crt
# Bundle it in a P12 and convert it to SAP PSE
openssl pkcs12 -export -in sapsso2.crt -inkey sapsso2.key -out sapsso2.p12
sapgenpse import_p12 -p sapsso2.pse sapsso2.p12
# Create an SAP Assertion Ticket using SAPSSOEXT
java -cp .\sapssoext.jar com.mysap.sso.SSO2Ticket -i ticket.txt -c -mysid SSO -mycli 000 -exsid ERP -excli 100 -p sapsso2.pse
Notes:
- SAPSSOEXT signs with a DSA key only when the key's subgroup
qis 160 bits. The key size itself does not matter — 1024, 2048, 3072 and 4096 all sign withq= 160, whileq= 224 andq= 256 fail at every size with "MySapCreateAssertionTicket failed: standard error= 9, ssf error= 27".openssl dsaparampicksq= 224 above 1024 bits, so ask forq= 160 explicitly when you build a larger key for SAPSSOEXT. RSA has no such limit: 1024 through 4096 all sign. - SAPSSOEXT also signs RSA keys, 1024 to 4096 bit, with a SHA-1 digest. It cannot sign with an EC key on any curve — that gives
standard error= 9, ssf error= 10. - NetWeaver ABAP accepts DSA, RSA and ECDSA up to 4096 bits, with SHA-1 and SHA-256. See the matrix under "Signing algorithms" for exactly what was tested. NetWeaver Java was not tested.
Development
This project uses uv.
# Create the virtualenv and install the project with its dev dependencies
uv sync
# Run the tests / linter
uv run pytest
uv run ruff check
# Run an example
uv run examples/generate_shortcut.py
# Build the sdist and wheel
uv build
tests/keys/ holds throwaway self-signed keys, one pair per row of the
algorithm matrix. They are committed, so a clone runs the suite straight away.
To replace them, or to add a key size or curve, run
pwsh tools/make_test_keys.ps1 -Force. It needs openssl; on Windows it falls
back to the one shipped with Git.
sapsso2.key and sapsso2.crt are the exception: the script never touches
them. That key signed the SAPSSOEXT reference ticket pinned in
tests/test_sapticket.py, so a regenerated key would not verify that
signature.
Testing against SAPSSOEXT
uv run pytest checks this library against itself. The suite in
tests/test_oracle.py checks it against SAPSSOEXT, SAP's closed-source
reference implementation. That is the only way to catch a disagreement with SAP,
so run it after any change to the wire format, the signature, or the validity
fields. When the two disagree, SAPSSOEXT is right.
These tests are skipped by default, because SAPSSOEXT is not redistributable and its native libraries are Windows amd64 only.
The pytest oracle suite pins DSA/SHA-1 only, because that is what this library emits by default. The other combinations are covered by the offline round-trip tests and by live NetWeaver runs. See docs/interop.md for what SAPSSOEXT and NetWeaver each accept.
Setup
Put these two files in internal/ (gitignored — do not commit them):
| File | Where it comes from |
|---|---|
sapssoext.jar |
SAP note 304450 |
SAPCRYPTOLIB*.SAR |
SAP Software Center, Windows x86_64 build |
SAPCAR*.EXE |
SAP Software Center, Windows x86_64 build |
The SAR and SAPCAR must be the Windows builds. The Linux ones are ELF binaries
and will not run; bootstrap.py checks this and says so.
You also need a JDK (javac, not just java) and openssl on PATH.
# One-off: extract SAPCRYPTOLIB, generate a test key, certificate and PSE,
# and compile the Java helper. Everything lands in internal/oracle/.
uv run tools/oracle/bootstrap.py
# Run the interop suite
uv run pytest -m oracle
# Run everything
uv run pytest -m "oracle or not oracle"
bootstrap.py is idempotent — re-running skips work already done. Pass
--force to rebuild. The key it generates is 1024-bit DSA, which matches the
reference fixture. A larger DSA key also works, as long as its subgroup q is
160 bits; q = 224 and q = 256 give standard error=9, ssf error=27 at every
key size.
What the oracle records
tools/oracle/oracle.py maps SAPSSOEXT's (standard, ssf) error pairs to
meanings, so a failing test says why rather than just failing. SAP documents
none of them. The table is in
docs/interop.md.
Further reading
docs/interop.md records what SAP actually does with a ticket, measured rather than quoted: which keys and digests each consumer signs and validates, the DSA subgroup rule, the error pairs, and the validity and base64 behaviour.
TODO
- Add pyright type checking and a CI workflow
Metadata
Release files for pysapsso2 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pysapsso2-1.0.0.tar.gz | 72.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pysapsso2-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 91.5 kB
Release files / pysapsso2-1.0.0.tar.gz
| Download URL | pysapsso2-1.0.0.tar.gz |
|---|---|
| Size | 72.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
082f65b98511a6249f68901ef9ae999e67b7454d5923060e7a743549358aa096
|
|
BLAKE2b-256 checksum How to use checksums |
1e079a1b79527fe072422f113a0638fa646f8c0c4fb6fde5a75ad0ec01a4a3f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / pysapsso2-1.0.0-py3-none-any.whl
| Download URL | pysapsso2-1.0.0-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b29cee9d587463c84dcdb1cc93f5ac4e387ba4d88c62676c52e7caf23190700
|
|
BLAKE2b-256 checksum How to use checksums |
1af2ed52d9d2a335a22386306bc5a0b38cd20f83564a5e056d097a6ed538a75b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|