Hello Guys!
I am implementing a solution for KSeF in Python. I unfortunately don't speak Polish so I have to post my question in English.
I am trying to initiate an interactive session with a token generated via the web application for KSeF with all permissions granted.
No matter how I generate the token (logged in with NIP or PESEL of an authorized natural person) I get the error 'Nieprawidłowy token.' back.
Do you have any ideas as to what the matter could be? Feel free to ask for additional info and I will edit my post :)
Thanks in advance!
EDIT
Here is an example of a token with the status 'Active' in the web application (TEST Environment), but doesn't work in Postman:
84607417CB075E62EF9B99DB4F4FC02373377F5A3844B862D6ADA2AF32B12961
EDIT 2
1.Token generation from web app (Logged with NIP '9999999999')
2.AuthorisationChallenge
{
"contextIdentifier": {
"type": "onip",
"identifier": "9999999999"
}
}
Response
{
"timestamp": "2023-04-24T14:16:09.258Z",
"challenge": "20230424-CR-3775179898-22D3D1397C-6B"
}
3.Unix epoch timestamp -> 1682338569
4.Concatenation of AuthorisationChalllenge and Unix epoch timestamp -> 033477F29FF60A15F6371F5B7E9FC9A6A6781A0D3C2EBDE973E75EDE2BED8B96|1682338569
5.Encryption of concatenated string with Public key of KSeF Test system (Python code below)
def encrypt(token, timestamp):
e = 65537
n = 23406442688142992831583759844756089133712844262268877557835948537922690131301917528010472072980030525243164470704153404706424073919594735631248793604990149775500839426627798421278024993358446037655350061463757500035940998380555904273595946307684093782369149356087903235522664351435055995896215926900834823274455240788124709945700300545915276090426078078411011846599277153583946003396302217817245249811336711552038275781604699852026576581228046084296276086185170885235268125033156936166764556520982537544722946490123310277292524709687907821731708318317794956326577863648319840338909808057410474707559952504063561704659
pubkey = construct((n, e))
text = token+'|'+timestamp
cipher = PKCS1_v1_5.new(pubkey)
encrypted_text = cipher.encrypt(bytes(text, encoding='utf-8'))
return base64.b64encode(encrypted_text)
6.Encrypted token ->
fmh0LSIVVtaeafcEprVlkiNlD0SK+nTicoWXyYeKz8DA9oRBJq5IrhjOeUD4B5163Bl/TR+rg9T3OVCm40Cij4/0sdy7wSeuSUdlkmgvqxbft8awFNRrnapoGApaEplSdMAKBicBpoi2+aV8bDdBLEOcJU/o0nCCkfy6J1aprdt/U/R79tpTpoiYndo21D3w39bcNvnEnjhlI/eG5Eybrspmo9gV11JW3DAg7T+xVjM8j6D0YfW2e6r7BED6ldDhP1QS4g7eyq3+CcO9jTlbi8Fav1jCnDh6fdDQXNXLlcvv55CJ0u1f/tZ36lC7gWjlXzQ0D4yHRrrbndGgUjZjqA==
7.Xml result
<?xml version="1.0" encoding="utf-8"?>
<ns3:InitSessionTokenRequest xmlns="http://ksef.mf.gov.pl/schema/gtw/svc/online/types/2021/10/01/0001" xmlns:ns2="http://ksef.mf.gov.pl/schema/gtw/svc/types/2021/10/01/0001" xmlns:ns3="http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001">
<ns3:Context>
<Challenge>20230424-CR-3775179898-22D3D1397C-6B</Challenge>
<Identifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SubjectIdentifierByCompanyType">
<ns2:Identifier>9999999999</ns2:Identifier>
</Identifier>
<DocumentType>
<ns2:Service>KSeF</ns2:Service>
<ns2:FormCode>
<ns2:SystemCode>FA (1)</ns2:SystemCode>
<ns2:SchemaVersion>1-0E</ns2:SchemaVersion>
<ns2:TargetNamespace>http://crd.gov.pl/wzor/2021/11/29/11089/</ns2:TargetNamespace>
<ns2:Value>FA</ns2:Value>
</ns2:FormCode>
</DocumentType>
<Token>fmh0LSIVVtaeafcEprVlkiNlD0SK+nTicoWXyYeKz8DA9oRBJq5IrhjOeUD4B5163Bl/TR+rg9T3OVCm40Cij4/0sdy7wSeuSUdlkmgvqxbft8awFNRrnapoGApaEplSdMAKBicBpoi2+aV8bDdBLEOcJU/o0nCCkfy6J1aprdt/U/R79tpTpoiYndo21D3w39bcNvnEnjhlI/eG5Eybrspmo9gV11JW3DAg7T+xVjM8j6D0YfW2e6r7BED6ldDhP1QS4g7eyq3+CcO9jTlbi8Fav1jCnDh6fdDQXNXLlcvv55CJ0u1f/tZ36lC7gWjlXzQ0D4yHRrrbndGgUjZjqA==</Token>
</ns3:Context>
</ns3:InitSessionTokenRequest>
8.InitToken Response
{
"exception": {
"serviceCtx": "srvTEMFC",
"serviceCode": "20230424-EX-FC92C9006F-89D98183C2-B3",
"serviceName": "online.session.session.token.init",
"timestamp": "2023-04-24T14:39:49.200Z",
"referenceNumber": "20230424-SE-0ECA9C481E-71C6237998-97",
"exceptionDetailList": {
"exceptionCode": 21116,
"exceptionDescription": "Nieprawidłowy token."
}
}
}