Skip to content

Commit 92fdbf8

Browse files
committed
added Jordan Brough as contributor, declared dependency on Echoe, added support for plaintext/unsigned JWTs as introduced by draft 03
1 parent 4e1df34 commit 92fdbf8

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# JWT
2-
A Ruby implementation of [JSON Web Token draft 01](http://self-issued.info/docs/draft-jones-json-web-token-01.html).
2+
A Ruby implementation of [JSON Web Token draft 06](http://self-issued.info/docs/draft-jones-json-web-token-06.html).
33

44
## Installing
55

@@ -37,14 +37,26 @@ Change the algorithm with by setting it in encode:
3737

3838
JWT.encode({"some" => "payload"}, "secret", "HS512")
3939

40-
## Tests
40+
**Plaintext**
41+
42+
We also support unsigned plaintext JWTs as introduced by draft 03 by explicitly specifying `nil` as the key and algorithm:
43+
44+
jwt = JWT.encode({"some" => "payload"}, nil, nil)
45+
JWT.decode(jwt, nil, nil)
46+
47+
## Development and Tests
48+
49+
We depend on [Echoe](http://rubygems.org/gems/echoe) for defining gemspec and performing releases to rubygems.org, which can be done with
50+
51+
rake release
4152

4253
The tests are written with rspec. Given you have rake and rspec, you can run tests with
4354

4455
rake test
4556

4657
## Contributors
4758

59+
* Jordan Brough <[email protected]>
4860
* Ilya Zhitomirskiy <[email protected]>
4961
* Daniel Grippi <[email protected]>
5062
* Jeff Lindsay <[email protected]>

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require 'rubygems'
22
require 'rake'
33
require 'echoe'
44

5-
Echoe.new('jwt', '0.1.3') do |p|
5+
Echoe.new('jwt', '0.1.4') do |p|
66
p.description = "JSON Web Token implementation in Ruby"
77
p.url = "http://github.com/progrium/ruby-jwt"
88
p.author = "Jeff Lindsay"

lib/jwt.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#
22
# JSON Web Token implementation
33
#
4-
# Minimum implementation based on this spec:
5-
# http://self-issued.info/docs/draft-jones-json-web-token-01.html
4+
# Should be up to date with the latest spec:
5+
# http://self-issued.info/docs/draft-jones-json-web-token-06.html
66

77
require "base64"
88
require "openssl"
@@ -43,29 +43,34 @@ def self.base64url_encode(str)
4343
end
4444

4545
def self.encode(payload, key, algorithm='HS256')
46+
algorithm ||= "none"
4647
segments = []
4748
header = {"typ" => "JWT", "alg" => algorithm}
4849
segments << base64url_encode(header.to_json)
4950
segments << base64url_encode(payload.to_json)
5051
signing_input = segments.join('.')
51-
signature = sign(algorithm, signing_input, key)
52-
segments << base64url_encode(signature)
52+
if algorithm != "none"
53+
signature = sign(algorithm, signing_input, key)
54+
segments << base64url_encode(signature)
55+
else
56+
segments << ""
57+
end
5358
segments.join('.')
5459
end
5560

5661
def self.decode(jwt, key=nil, verify=true)
5762
segments = jwt.split('.')
58-
raise JWT::DecodeError.new("Not enough or too many segments") unless segments.length == 3
63+
raise JWT::DecodeError.new("Not enough or too many segments") unless [2,3].include? segments.length
5964
header_segment, payload_segment, crypto_segment = segments
6065
signing_input = [header_segment, payload_segment].join('.')
6166
begin
6267
header = JSON.parse(base64url_decode(header_segment))
6368
payload = JSON.parse(base64url_decode(payload_segment))
64-
signature = base64url_decode(crypto_segment)
69+
signature = base64url_decode(crypto_segment) if verify
6570
rescue JSON::ParserError
6671
raise JWT::DecodeError.new("Invalid segment encoding")
6772
end
68-
if verify
73+
if verify == true
6974
algo = header['alg']
7075

7176
if ["HS256", "HS384", "HS512"].include?(algo)

spec/jwt.rb

+7
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@
5252
it "raises exception on unsupported crypto algorithm" do
5353
lambda { JWT.encode(@payload, "secret", 'HS1024') }.should raise_error(NotImplementedError)
5454
end
55+
56+
it "encodes and decodes plaintext JWTs" do
57+
jwt = JWT.encode(@payload, nil, nil)
58+
jwt.split('.').length.should == 2
59+
decoded_payload = JWT.decode(jwt, nil, nil)
60+
decoded_payload.should == @payload
61+
end
5562
end

0 commit comments

Comments
 (0)