Skip to content

Commit

Permalink
Merge pull request #174 from xamenrax/remove_typ_optional_parameter
Browse files Browse the repository at this point in the history
Remove 'typ' optional parameter
  • Loading branch information
excpt authored Nov 30, 2016
2 parents 45b89ba + 4a7c69e commit e36234c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ decoded_token = JWT.decode token, nil, false
# Array
# [
# {"data"=>"test"}, # payload
# {"typ"=>"JWT", "alg"=>"none"} # header
# {"alg"=>"none"} # header
# ]
puts decoded_token
```
Expand All @@ -79,7 +79,7 @@ decoded_token = JWT.decode token, hmac_secret, true, { :algorithm => 'HS256' }
# Array
# [
# {"data"=>"test"}, # payload
# {"typ"=>"JWT", "alg"=>"HS256"} # header
# {"alg"=>"HS256"} # header
# ]
puts decoded_token
```
Expand All @@ -104,7 +104,7 @@ decoded_token = JWT.decode token, rsa_public, true, { :algorithm => 'RS256' }
# Array
# [
# {"data"=>"test"}, # payload
# {"typ"=>"JWT", "alg"=>"RS256"} # header
# {"alg"=>"RS256"} # header
# ]
puts decoded_token
```
Expand All @@ -131,7 +131,7 @@ decoded_token = JWT.decode token, ecdsa_public, true, { :algorithm => 'ES256' }
# Array
# [
# {"test"=>"data"}, # payload
# {"typ"=>"JWT", "alg"=>"ES256"} # header
# {"alg"=>"ES256"} # header
# ]
puts decoded_token
```
Expand All @@ -152,6 +152,38 @@ used. JWT supports these reserved claim names:
- 'iat' (Issued At) Claim
- 'sub' (Subject) Claim

## Add custom header fields
Ruby-jwt gem supports custom [header fields] (https://tools.ietf.org/html/rfc7519#section-5)
To add custom header fields you need to pass `header_fields` parameter

```ruby
token = JWT.encode payload, key, algorithm='HS256', header_fields={}
```

**Example:**

```ruby
require 'jwt'

payload = {:data => 'test'}

# IMPORTANT: set nil as password parameter
token = JWT.encode payload, nil, 'none', { :typ => "JWT" }

# eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
puts token

# Set password to nil and validation to false otherwise this won't work
decoded_token = JWT.decode token, nil, false

# Array
# [
# {"data"=>"test"}, # payload
# {"typ"=>"JWT", "alg"=>"none"} # header
# ]
puts decoded_token
```

### Expiration Time Claim

From [Oauth JSON Web Token 4.1.4. "exp" (Expiration Time) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.4):
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def base64url_encode(str)
end

def encoded_header(algorithm = 'HS256', header_fields = {})
header = { 'typ' => 'JWT', 'alg' => algorithm }.merge(header_fields)
header = { 'alg' => algorithm }.merge(header_fields)
base64url_encode(encode_json(header))
end

Expand Down
23 changes: 17 additions & 6 deletions spec/integration/readme_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
token = JWT.encode payload, nil, 'none'
decoded_token = JWT.decode token, nil, false

expect(token).to eq 'eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.'
expect(token).to eq 'eyJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.'
expect(decoded_token).to eq [
{ 'data' => 'test' },
{ 'typ' => 'JWT', 'alg' => 'none' }
{ 'alg' => 'none' }
]
end

it 'HMAC' do
token = JWT.encode payload, 'my$ecretK3y', 'HS256'
decoded_token = JWT.decode token, 'my$ecretK3y', false

expect(token).to eq 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.ZxW8go9hz3ETCSfxFxpwSkYg_602gOPKearsf6DsxgY'
expect(token).to eq 'eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pNIWIL34Jo13LViZAJACzK6Yf0qnvT_BuwOxiMCPE-Y'
expect(decoded_token).to eq [
{ 'data' => 'test' },
{ 'typ' => 'JWT', 'alg' => 'HS256' }
{ 'alg' => 'HS256' }
]
end

Expand All @@ -37,7 +37,7 @@

expect(decoded_token).to eq [
{ 'data' => 'test' },
{ 'typ' => 'JWT', 'alg' => 'RS256' }
{ 'alg' => 'RS256' }
]
end

Expand All @@ -52,7 +52,7 @@

expect(decoded_token).to eq [
{ 'data' => 'test' },
{ 'typ' => 'JWT', 'alg' => 'ES256' }
{ 'alg' => 'ES256' }
]
end
end
Expand Down Expand Up @@ -176,6 +176,17 @@
end
end

context 'custom header fields' do
it 'with custom field' do
payload = { data: 'test' }

token = JWT.encode payload, nil, 'none', { typ: 'JWT' }
jwt_payload, header = JWT.decode token, nil, false

expect(header['typ']).to eq 'JWT'
end
end

it 'sub' do
sub = 'Subject'
sub_payload = { data: 'data', sub: sub }
Expand Down
14 changes: 7 additions & 7 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
'ES384_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec384-public.pem'))),
'ES512_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-private.pem'))),
'ES512_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-public.pem'))),
'NONE' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.',
'HS256' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.tCGvlClld0lbQ3NZaH8y53n5RSBr3zlS4Oy5bXqvzZQ',
'HS384' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.sj1gc01SawlJSrPZgmveifJ8CzZRYAWjejWm4FRaGaAISESJ9Ncf12fCz2vHrITm',
'HS512' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1c2VyX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PSeMtbe_5EyS94nFoEF9OQuhJYjUmp7agsBVa8FFEjVw5jEgVqkvERSj5hSY4nEiCAomdVxIKBfykyi0d12cgjhI7mBFwWkPku8XIPGZ7N8vpiSLdM68BnUqIK5qR7NAhtvT7iyLFgOqhZNUQ6Ret5VpQ',
'RS384' => 'eyJhbGciOiJSUzM4NCJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.Sfgk56moPghtsjaP4so6tOy3I553mgwX-5gByMC6dX8lpeWgsxSeAd_K8IyO7u4lwYOL0DSftnqO1HEOuN1AKyBbDvaTXz3u2xNA2x4NYLdW4AZA6ritbYcKLO5BHTXw5ueMbtA1jjGXP0zI_aK2iJTMBmB8SCF88RYBUH01Tyf4PlLj98pGL-v3prZd6kZkIeRJ3326h04hslcB5HQKmgeBk24QNLIoIC-CD329HPjJ7TtGx01lj-ehTBnwVbBGzYFAyoalV5KgvL_MDOfWPr1OYHnR5s_Fm6_3Vg4u6lBljvHOrmv4Nfx7d8HLgbo8CwH4qn1wm6VQCtuDd-uhRg',
'RS512' => 'eyJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.LIIAUEuCkGNdpYguOO5LoW4rZ7ED2POJrB0pmEAAchyTdIK4HKh1jcLxc6KyGwZv40njCgub3y72q6vcQTn7oD0zWFCVQRIDW1911Ii2hRNHuigiPUnrnZh1OQ6z65VZRU6GKs8omoBGU9vrClBU0ODqYE16KxYmE_0n4Xw2h3D_L1LF0IAOtDWKBRDa3QHwZRM9sHsHNsBuD5ye9KzDYN1YALXj64LBfA-DoCKfpVAm9NkRPOyzjR2X2C3TomOSJgqWIVHJucudKDDAZyEbO4RA5pI-UFYy1370p9bRajvtDyoBuLDCzoSkMyQ4L2DnLhx5CbWcnD7Cd3GUmnjjTA',
'ES256' => '',
'ES384' => '',
'ES512' => ''
Expand Down

0 comments on commit e36234c

Please sign in to comment.