Created
March 13, 2016 18:43
-
-
Save havenwood/3b98192d6122a4c9b1a4 to your computer and use it in GitHub Desktop.
Unencrypted, plaintext JWTs in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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