Developers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
DadOfTwo
Visitor

ByteArray FromBase64String not able to handle Valid JWT (RS512) tokens?

Hi all

Found what maybe an issue with the Roku ByteArray fromBase64StringtoAsciiString methods.

When passing in a value JSON Web Token to the ByteArray.fromBase64String method and converting it via ByteArray.toAsciiString the 'payload' value in the JWT is missing characters.

Here's an example of the issue, the tokens are generated from the https://jwt.io site.

sub init()
  'jwToken1 values'
  'header:
  '{
  ' "alg": "HS256",
  ' "typ": "JWT"
  '}
  'payload:
  '{
  ' "someKey": "someValue"
  '}
  'verify signature:
  'default on the https://jwt.io/ site'

  jwToken1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lS2V5Ijoic29tZVZhbHVlIn0._UeRDHCMD9aFDsAxucwn3b66Y7YkJ7eBsQh7ajEWXmE"
  tokenPayloadFromJwToken1 = getTokenPayloadAsObject(jwToken1)
  'tokenPayloadFromJwToken1 outputs INVALID JSON: {"someKey":"someValue

  jwToken2 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lS2V5Ijp0cnVlfQ.kxkYrH4gICavDvfJ2IoGvhUHlt2YLKHWk1DbwFN9qNA"
  tokenPayloadFromJwToken2 = getTokenPayloadAsObject(jwToken2)
  'tokenPayloadFromJwToken2 outputs INVALID JSON: {"someKey":true

  jwToken3 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lS2V5IjoxMjN9.DL8qyWMeqfMMLCTPN3RA9K08e-AkNW_ybPyywvrIIZ8"
  tokenPayloadFromJwToken3 = getTokenPayloadAsObject(jwToken3)
  'tokenPayloadFromJwToken3 outputs VALID JSON: {"someKey":123}

  jwToken4 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lS2V5IjoxMjM0fQ.BHOwW2P2Rs6hUMrxyFqMi9EbLVQGgvWfUdbXZHE2QA8"
  tokenPayloadFromJwToken4 = getTokenPayloadAsObject(jwToken4)
  'tokenPayloadFromJwToken4 outputs INVALID JSON: {"someKey":1234
end sub

function getTokenPayloadAsObject(token as String) as String
  tokenParts = token.split(".")
  ' XXX: the token we're receiving back is a JWT (RS512) base64 string consisting of 3 parts:
  '      1 (index 0). header: token type and algoritham'
  '      2 (index 1). payload: various json value pairs'
  '      3 (index 3). verify signature: details on the encoding/ secret format'
  payloadIndex = 1
  tokenPayload = tokenParts[payloadIndex]
  ba = CreateObject("roByteArray")
  ba.fromBase64String(tokenPayload)
  tokenPayloadString = ba.toAsciiString()
  return tokenPayloadString
end function



Thanks
Tags (1)
0 Kudos
3 REPLIES 3
NB_
Roku Guru

Re: ByteArray FromBase64String not able to handle Valid JWT (RS512) tokens?

it doesn't handle well un-padded base64, apparently. Make sure the length is divisible by 4, pad with '=' as needed:

Brightscript Debugger> s = "eyJzb21lS2V5Ijoic29tZVZhbHVlIn0"
Brightscript Debugger> b = createObject("roByteArray")
Brightscript Debugger> b.fromBase64String(s): ? b.toAsciiString(), len(s)  
{"someKey":"someValue            31
Brightscript Debugger> if len(s) mod 4 <> 0 then s += string(4 - len(s) mod 4, "=")  '"

Brightscript Debugger> b.fromBase64String(s): ? b.toAsciiString(), len(s), s
{"someKey":"someValue"}          32             eyJzb21lS2V5Ijoic29tZVZhbHVlIn0=
Tags (1)
0 Kudos
DadOfTwo
Visitor

Re: ByteArray FromBase64String not able to handle Valid JWT (RS512) tokens?

Nice explanation and temporary fix, thanks. 

Are there plans to fix this without needing to pad the token with equals? Also the ba.toAsciiString() method is still chopping off the trailing brace '}'... so need to manually check for that missing from the tokenPayloadString before passing it to the Native Roku ParseJson method.
Tags (1)
0 Kudos
DadOfTwo
Visitor

Re: ByteArray FromBase64String not able to handle Valid JWT (RS512) tokens?

Was applying it to the unpacked token, not the payload. Working now!

Thanks.
Tags (1)
0 Kudos
Community is Being Upgraded!

We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. Read more here.

Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 19th and you may notice reduced functionality. In the meantime, for additional assistance, visit our Support Site.

We're sorry for this disruption — we’re excited to share what’s next!