Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 13, 2016 18:43
Show Gist options
  • Save havenwood/3b98192d6122a4c9b1a4 to your computer and use it in GitHub Desktop.
Save havenwood/3b98192d6122a4c9b1a4 to your computer and use it in GitHub Desktop.
Unencrypted, plaintext JWTs in Ruby
require 'base64'
require 'json'
module PlainTextJWT
HEADER = Base64.strict_encode64({typ: 'JWT', alg: 'none'}.to_json)
HEADER_SIZE = HEADER.size
module_function
def encode payload
"#{HEADER}.#{Base64.strict_encode64 JSON.unparse payload}."
end
def decode payload
JSON.parse Base64.strict_decode64 payload[HEADER_SIZE.next..-2]
end
end
PlainTextJWT.encode [42]
#=> "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0=.WzQyXQ==."
PlainTextJWT.decode "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0=.WzQyXQ==."
#=> [42]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment