NetTalk Central
NetTalk Web Server => Web Server - Ask For Help => Topic started by: ntnewbies on July 20, 2021, 01:51:35 AM
-
hi
i need to Create the JWT token (https://jwt.io/) with the following claims:
{
"iss": "your_api_key",
"ist": "project",
"iat": current_timestamp_in_seconds,
"exp": expire_timestamp_in_seconds,
"jti": "jwt_nonce"
}
i was told to run a phyton script to get the token
import jwt
import time
import uuid
print jwt.encode({"iss": "my-account-API-key",
"iat": int(time.time()),
"exp": int(time.time()) + 180,
"ist": "project",
"jti": str(uuid.uuid4())},
'my-API-secret',
algorithm='HS256')
how to do this in nettalk??
jason
c11
nt11.51
-
Hi Jason,
This is me creating a JWT token for Twilio's Chat API system.
CreateTwilioToken PROCEDURE (STRING lAccountSID,STRING lAPIKey,STRING lAPISecret,STRING lServiceSID,STRING lPushSID,STRING lIdentity)
stHeader StringTheory
stPayload StringTheory
stToEncrypt StringTheory
stSignature StringTheory
Crypto Cryptonite
CODE
stHeader.SetValue('{{"typ":"JWT","alg":"HS256","cty":"twilio-fpa;v=1"}')
stPayload.SetValue('{{"jti":"'&CLIP(lAPIKey)&'-'&RANDOM(100000000,999999999)&'",'&|
'"iss":"'&CLIP(lAPIKey)&'",'&|
'"sub":"'&CLIP(lAccountSID)&'",'&|
'"iat":'&TimestampZ()-(GETINI('Server','Timezone',0,GLO:INIFilename)*3600)&','&|
'"exp":'&TimestampZ()+3600-(GETINI('Server','Timezone',0,GLO:INIFilename)*3600)&','&|
'"grants":{{'&|
'"identity":"'&CLIP(lIdentity)&'",'&|
'"chat":{{'&|
'"service_sid":"'&CLIP(lServiceSID)&'",'&|
'"push_credential_sid":"'&CLIP(lPushSID)&'"'&|
'}}}')
stHeader.Base64Encode(1)
stHeader.Replace('+','-')
stHeader.Replace('/','_')
stHeader.Replace('=','')
stPayload.Base64Encode(1)
stPayload.Replace('+','-')
stPayload.Replace('/','_')
stPayload.Replace('=','')
stToEncrypt.SetValue(stHeader.GetValue()&'.'&stPayload.GetValue())
Crypto.MakeHMAC(stToEncrypt,CLIP(lAPISecret),cs:CALG_SHA_256,0)
stToEncrypt.Base64Encode(1)
stToEncrypt.Replace('+','-')
stToEncrypt.Replace('/','_')
stToEncrypt.Replace('=','')
RETURN stHeader.GetValue()&'.'&stPayload.GetValue()&'.'&stToEncrypt.GetValue()
I'm in a hurry tonight getting a build out. But, if you want the exact code, ask me on Slack.
Regards
Bill
-
Hi Jason,
I thought I should post the solution here incase anyone was interested (after we worked through it on Slack).
TestVonage PROCEDURE ! Declare Procedure
lToken STRING(1024)
lPayload STRING(1024)
lSecret STRING(255)
lAPIKey STRING(255)
CODE
lSecret = 'b845bb0a8b2xxxxxxxxxxxxxx30c108da7bd755b'
lAPIKey = 'xxxxxxx'
lPayload = '{{"iss": "'&CLIP(lAPIKey)&'","iat": '&TimestampZ()&',"exp": '&TimestampZ()+180&',"ist": "Inhabit","jti": "'&RandomString(16)&'"}'
lToken = CreateJWTToken(CLIP(lPayload),CLIP(lSecret))
WriteDebugInfo('Payload: '&CLIP(lPayload))
WriteDebugInfo('Token: '&CLIP(lToken))
CreateJWTToken FUNCTION (STRING lPayload,STRING lSecret) ! Declare Procedure
stHeader StringTheory
stPayload StringTheory
stToEncrypt StringTheory
stSignature StringTheory
Crypto Cryptonite
CODE ! Begin processed code
stHeader.SetValue('{{"typ":"JWT","alg":"HS256"}')
stPayload.SetValue(CLIP(lPayload))
stHeader.Base64Encode(1)
stHeader.Replace('+','-')
stHeader.Replace('/','_')
stHeader.Replace('=','')
stPayload.Base64Encode(1)
stPayload.Replace('+','-')
stPayload.Replace('/','_')
stPayload.Replace('=','')
stToEncrypt.SetValue(stHeader.GetValue()&'.'&stPayload.GetValue())
Crypto.MakeHMAC(stToEncrypt,CLIP(lSecret),cs:CALG_SHA_256,0)
stToEncrypt.Base64Encode(1)
stToEncrypt.Replace('+','-')
stToEncrypt.Replace('/','_')
stToEncrypt.Replace('=','')
RETURN stHeader.GetValue()&'.'&stPayload.GetValue()&'.'&stToEncrypt.GetValue()
A quick dirty demo, but it can be verified at https://jwt.io.
Regards
Bill
-
Hi Bill,
just FYI, the following lines;
stPayload.Base64Encode(1)
stPayload.Replace('+','-')
stPayload.Replace('/','_')
stPayload.Replace('=','')
can be replaced by
stPayload.Base64Encode(st:URLSafe + st:NoPadding')
as per;
https://www.capesoft.com/docs/StringTheory3/StringTheory.htm#stBase64Encode
Also see;
https://www.capesoft.com/docs/NetTalk12/NetTalkUtilityFunctions.htm#NetMakeHMAC
which would remove the dependency on Cryptonite.
Cheers
Bruce
Cheers
Bruce
-
Hi Bruce,
Awesome! Thanks. I'll clean up my code.
Regards
Bill
-
thank you very much bill and bruce.
jason