jwtb 2.0.0.beta2.bsk1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +20 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +411 -0
- data/Gemfile +4 -0
- data/LICENSE +7 -0
- data/Manifest +8 -0
- data/README.md +443 -0
- data/Rakefile +11 -0
- data/jwtb.gemspec +31 -0
- data/lib/jwtb.rb +67 -0
- data/lib/jwtb/decode.rb +45 -0
- data/lib/jwtb/default_options.rb +14 -0
- data/lib/jwtb/encode.rb +51 -0
- data/lib/jwtb/error.rb +15 -0
- data/lib/jwtb/signature.rb +146 -0
- data/lib/jwtb/verify.rb +84 -0
- data/lib/jwtb/version.rb +24 -0
- data/spec/fixtures/certs/ec256-private.pem +8 -0
- data/spec/fixtures/certs/ec256-public.pem +4 -0
- data/spec/fixtures/certs/ec256-wrong-private.pem +8 -0
- data/spec/fixtures/certs/ec256-wrong-public.pem +4 -0
- data/spec/fixtures/certs/ec384-private.pem +9 -0
- data/spec/fixtures/certs/ec384-public.pem +5 -0
- data/spec/fixtures/certs/ec384-wrong-private.pem +9 -0
- data/spec/fixtures/certs/ec384-wrong-public.pem +5 -0
- data/spec/fixtures/certs/ec512-private.pem +10 -0
- data/spec/fixtures/certs/ec512-public.pem +6 -0
- data/spec/fixtures/certs/ec512-wrong-private.pem +10 -0
- data/spec/fixtures/certs/ec512-wrong-public.pem +6 -0
- data/spec/fixtures/certs/rsa-1024-private.pem +15 -0
- data/spec/fixtures/certs/rsa-1024-public.pem +6 -0
- data/spec/fixtures/certs/rsa-2048-private.pem +27 -0
- data/spec/fixtures/certs/rsa-2048-public.pem +9 -0
- data/spec/fixtures/certs/rsa-2048-wrong-private.pem +27 -0
- data/spec/fixtures/certs/rsa-2048-wrong-public.pem +9 -0
- data/spec/fixtures/certs/rsa-4096-private.pem +51 -0
- data/spec/fixtures/certs/rsa-4096-public.pem +14 -0
- data/spec/integration/readme_examples_spec.rb +216 -0
- data/spec/jwtb/verify_spec.rb +190 -0
- data/spec/jwtb_spec.rb +233 -0
- data/spec/spec_helper.rb +28 -0
- metadata +225 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Jeff Lindsay
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,443 @@
|
|
1
|
+
# JWT
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/jwt/ruby-jwt.svg)](https://travis-ci.org/jwt/ruby-jwt)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/jwt/ruby-jwt/badges/gpa.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/jwt/ruby-jwt/badges/coverage.svg)](https://codeclimate.com/github/jwt/ruby-jwt/coverage)
|
6
|
+
[![Issue Count](https://codeclimate.com/github/jwt/ruby-jwt/badges/issue_count.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
|
7
|
+
|
8
|
+
A pure ruby implementation of the [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) standard with additional support for the secp256k1 curve used by bitcoin.
|
9
|
+
|
10
|
+
If you have further questions related to development or usage, join us: [ruby-jwt google group](https://groups.google.com/forum/#!forum/ruby-jwt).
|
11
|
+
|
12
|
+
## Announcements
|
13
|
+
|
14
|
+
* Ruby 1.9.3 support will be dropped by December 31st, 2016.
|
15
|
+
* Version 1.5.3 yanked. See: [#132](https://github.com/jwt/ruby-jwt/issues/132) and [#133](https://github.com/jwt/ruby-jwt/issues/133)
|
16
|
+
|
17
|
+
## Installing
|
18
|
+
|
19
|
+
### Using Rubygems:
|
20
|
+
```bash
|
21
|
+
sudo gem install jwt-blockstack
|
22
|
+
```
|
23
|
+
|
24
|
+
### Using Bundler:
|
25
|
+
Add the following to your Gemfile
|
26
|
+
```
|
27
|
+
gem 'jwt-blockstack'
|
28
|
+
```
|
29
|
+
And run `bundle install`
|
30
|
+
|
31
|
+
## Algorithms and Usage
|
32
|
+
|
33
|
+
The JWT spec supports NONE, HMAC, RSASSA, ECDSA and RSASSA-PSS algorithms for cryptographic signing. Currently the jwt gem supports NONE, HMAC, RSASSA and ECDSA. If you are using cryptographic signing, you need to specify the algorithm in the options hash whenever you call JWT.decode to ensure that an attacker [cannot bypass the algorithm verification step](https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/).
|
34
|
+
|
35
|
+
See: [ JSON Web Algorithms (JWA) 3.1. "alg" (Algorithm) Header Parameter Values for JWS](https://tools.ietf.org/html/rfc7518#section-3.1)
|
36
|
+
|
37
|
+
**NONE**
|
38
|
+
|
39
|
+
* none - unsigned token
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'jwtb'
|
43
|
+
|
44
|
+
payload = {:data => 'test'}
|
45
|
+
|
46
|
+
# IMPORTANT: set nil as password parameter
|
47
|
+
token = JWTB.encode payload, nil, 'none'
|
48
|
+
|
49
|
+
# eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
|
50
|
+
puts token
|
51
|
+
|
52
|
+
# Set password to nil and validation to false otherwise this won't work
|
53
|
+
decoded_token = JWTB.decode token, nil, false
|
54
|
+
|
55
|
+
# Array
|
56
|
+
# [
|
57
|
+
# {"data"=>"test"}, # payload
|
58
|
+
# {"alg"=>"none"} # header
|
59
|
+
# ]
|
60
|
+
puts decoded_token
|
61
|
+
```
|
62
|
+
|
63
|
+
**HMAC** (default: HS256)
|
64
|
+
|
65
|
+
* HS256 - HMAC using SHA-256 hash algorithm (default)
|
66
|
+
* HS512256 - HMAC using SHA-512/256 hash algorithm (only available with RbNaCl; see note below)
|
67
|
+
* HS384 - HMAC using SHA-384 hash algorithm
|
68
|
+
* HS512 - HMAC using SHA-512 hash algorithm
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
hmac_secret = 'my$ecretK3y'
|
72
|
+
|
73
|
+
token = JWTB.encode payload, hmac_secret, 'HS256'
|
74
|
+
|
75
|
+
# eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.ZxW8go9hz3ETCSfxFxpwSkYg_602gOPKearsf6DsxgY
|
76
|
+
puts token
|
77
|
+
|
78
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :algorithm => 'HS256' }
|
79
|
+
|
80
|
+
# Array
|
81
|
+
# [
|
82
|
+
# {"data"=>"test"}, # payload
|
83
|
+
# {"alg"=>"HS256"} # header
|
84
|
+
# ]
|
85
|
+
puts decoded_token
|
86
|
+
```
|
87
|
+
|
88
|
+
Note: If [RbNaCl](https://github.com/cryptosphere/rbnacl) is loadable, ruby-jwt will use it for HMAC-SHA256, HMAC-SHA512/256, and HMAC-SHA512. RbNaCl enforces a maximum key size of 32 bytes for these algorithms.
|
89
|
+
|
90
|
+
**RSA**
|
91
|
+
|
92
|
+
* RS256 - RSA using SHA-256 hash algorithm
|
93
|
+
* RS384 - RSA using SHA-384 hash algorithm
|
94
|
+
* RS512 - RSA using SHA-512 hash algorithm
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
rsa_private = OpenSSL::PKey::RSA.generate 2048
|
98
|
+
rsa_public = rsa_private.public_key
|
99
|
+
|
100
|
+
token = JWTB.encode payload, rsa_private, 'RS256'
|
101
|
+
|
102
|
+
# eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ0ZXN0IjoiZGF0YSJ9.c2FynXNyi6_PeKxrDGxfS3OLwQ8lTDbWBWdq7oMviCy2ZfFpzvW2E_odCWJrbLof-eplHCsKzW7MGAntHMALXgclm_Cs9i2Exi6BZHzpr9suYkrhIjwqV1tCgMBCQpdeMwIq6SyKVjgH3L51ivIt0-GDDPDH1Rcut3jRQzp3Q35bg3tcI2iVg7t3Msvl9QrxXAdYNFiS5KXH22aJZ8X_O2HgqVYBXfSB1ygTYUmKTIIyLbntPQ7R22rFko1knGWOgQCoYXwbtpuKRZVFrxX958L2gUWgb4jEQNf3fhOtkBm1mJpj-7BGst00o8g_3P2zHy-3aKgpPo1XlKQGjRrrxA
|
103
|
+
puts token
|
104
|
+
|
105
|
+
decoded_token = JWTB.decode token, rsa_public, true, { :algorithm => 'RS256' }
|
106
|
+
|
107
|
+
# Array
|
108
|
+
# [
|
109
|
+
# {"data"=>"test"}, # payload
|
110
|
+
# {"alg"=>"RS256"} # header
|
111
|
+
# ]
|
112
|
+
puts decoded_token
|
113
|
+
```
|
114
|
+
|
115
|
+
**ECDSA**
|
116
|
+
|
117
|
+
* ES256 - ECDSA using P-256 and SHA-256
|
118
|
+
* ES384 - ECDSA using P-384 and SHA-384
|
119
|
+
* ES512 - ECDSA using P-521 and SHA-512
|
120
|
+
* ES256K - ECDSA using secp256k1
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
ecdsa_key = OpenSSL::PKey::EC.new 'secp256k1'
|
124
|
+
ecdsa_key.generate_key
|
125
|
+
ecdsa_public = OpenSSL::PKey::EC.new ecdsa_key
|
126
|
+
ecdsa_public.private_key = nil
|
127
|
+
|
128
|
+
token = JWTB.encode payload, ecdsa_key, 'ES256K'
|
129
|
+
|
130
|
+
# eyJhbGciOiJFUzI1NksifQ.eyJkYXRhIjoidGVzdCJ9.SlW6tqCxv-R5488_aRpY2hB6yR3thDT_206zNpThS8RZFbrYyLtpD3xg5kHLHh3NRGUGQkzMlndnkzJYohslSA
|
131
|
+
puts token
|
132
|
+
|
133
|
+
decoded_token = JWTB.decode token, ecdsa_public, true, { :algorithm => 'ES256K' }
|
134
|
+
|
135
|
+
# Array
|
136
|
+
# [
|
137
|
+
# {"test"=>"data"}, # payload
|
138
|
+
# {"alg"=>"ES256K"} # header
|
139
|
+
# ]
|
140
|
+
puts decoded_token
|
141
|
+
```
|
142
|
+
|
143
|
+
**RSASSA-PSS**
|
144
|
+
|
145
|
+
Not implemented.
|
146
|
+
|
147
|
+
## Support for reserved claim names
|
148
|
+
JSON Web Token defines some reserved claim names and defines how they should be
|
149
|
+
used. JWT supports these reserved claim names:
|
150
|
+
|
151
|
+
- 'exp' (Expiration Time) Claim
|
152
|
+
- 'nbf' (Not Before Time) Claim
|
153
|
+
- 'iss' (Issuer) Claim
|
154
|
+
- 'aud' (Audience) Claim
|
155
|
+
- 'jti' (JWT ID) Claim
|
156
|
+
- 'iat' (Issued At) Claim
|
157
|
+
- 'sub' (Subject) Claim
|
158
|
+
|
159
|
+
## Add custom header fields
|
160
|
+
Ruby-jwt gem supports custom [header fields] (https://tools.ietf.org/html/rfc7519#section-5)
|
161
|
+
To add custom header fields you need to pass `header_fields` parameter
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
token = JWTB.encode payload, key, algorithm='HS256', header_fields={}
|
165
|
+
```
|
166
|
+
|
167
|
+
**Example:**
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
require 'jwt'
|
171
|
+
|
172
|
+
payload = {:data => 'test'}
|
173
|
+
|
174
|
+
# IMPORTANT: set nil as password parameter
|
175
|
+
token = JWTB.encode payload, nil, 'none', { :typ => "JWT" }
|
176
|
+
|
177
|
+
# eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
|
178
|
+
puts token
|
179
|
+
|
180
|
+
# Set password to nil and validation to false otherwise this won't work
|
181
|
+
decoded_token = JWTB.decode token, nil, false
|
182
|
+
|
183
|
+
# Array
|
184
|
+
# [
|
185
|
+
# {"data"=>"test"}, # payload
|
186
|
+
# {"typ"=>"JWT", "alg"=>"none"} # header
|
187
|
+
# ]
|
188
|
+
puts decoded_token
|
189
|
+
```
|
190
|
+
|
191
|
+
### Expiration Time Claim
|
192
|
+
|
193
|
+
From [Oauth JSON Web Token 4.1.4. "exp" (Expiration Time) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.4):
|
194
|
+
|
195
|
+
> The `exp` (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the `exp` claim requires that the current date/time MUST be before the expiration date/time listed in the `exp` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
196
|
+
|
197
|
+
**Handle Expiration Claim**
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
exp = Time.now.to_i + 4 * 3600
|
201
|
+
exp_payload = { :data => 'data', :exp => exp }
|
202
|
+
|
203
|
+
token = JWTB.encode exp_payload, hmac_secret, 'HS256'
|
204
|
+
|
205
|
+
begin
|
206
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :algorithm => 'HS256' }
|
207
|
+
rescue JWT::ExpiredSignature
|
208
|
+
# Handle expired token, e.g. logout user or deny access
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
**Adding Leeway**
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
exp = Time.now.to_i - 10
|
216
|
+
leeway = 30 # seconds
|
217
|
+
|
218
|
+
exp_payload = { :data => 'data', :exp => exp }
|
219
|
+
|
220
|
+
# build expired token
|
221
|
+
token = JWTB.encode exp_payload, hmac_secret, 'HS256'
|
222
|
+
|
223
|
+
begin
|
224
|
+
# add leeway to ensure the token is still accepted
|
225
|
+
decoded_token = JWT.decode token, hmac_secret, true, { :exp_leeway => leeway, :algorithm => 'HS256' }
|
226
|
+
rescue JWTB::ExpiredSignature
|
227
|
+
# Handle expired token, e.g. logout user or deny access
|
228
|
+
end
|
229
|
+
```
|
230
|
+
|
231
|
+
### Not Before Time Claim
|
232
|
+
|
233
|
+
From [Oauth JSON Web Token 4.1.5. "nbf" (Not Before) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.5):
|
234
|
+
|
235
|
+
> The `nbf` (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. The processing of the `nbf` claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the `nbf` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
236
|
+
|
237
|
+
**Handle Not Before Claim**
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
nbf = Time.now.to_i - 3600
|
241
|
+
nbf_payload = { :data => 'data', :nbf => nbf }
|
242
|
+
|
243
|
+
token = JWTB.encode nbf_payload, hmac_secret, 'HS256'
|
244
|
+
|
245
|
+
begin
|
246
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :algorithm => 'HS256' }
|
247
|
+
rescue JWTB::ImmatureSignature
|
248
|
+
# Handle invalid token, e.g. logout user or deny access
|
249
|
+
end
|
250
|
+
```
|
251
|
+
|
252
|
+
**Adding Leeway**
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
nbf = Time.now.to_i + 10
|
256
|
+
leeway = 30
|
257
|
+
|
258
|
+
nbf_payload = { :data => 'data', :nbf => nbf }
|
259
|
+
|
260
|
+
# build expired token
|
261
|
+
token = JWTB.encode nbf_payload, hmac_secret, 'HS256'
|
262
|
+
|
263
|
+
begin
|
264
|
+
# add leeway to ensure the token is valid
|
265
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :nbf_leeway => leeway, :algorithm => 'HS256' }
|
266
|
+
rescue JWTB::ImmatureSignature
|
267
|
+
# Handle invalid token, e.g. logout user or deny access
|
268
|
+
end
|
269
|
+
```
|
270
|
+
|
271
|
+
### Issuer Claim
|
272
|
+
|
273
|
+
From [Oauth JSON Web Token 4.1.1. "iss" (Issuer) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.1):
|
274
|
+
|
275
|
+
> The `iss` (issuer) claim identifies the principal that issued the JWT. The processing of this claim is generally application specific. The `iss` value is a case-sensitive string containing a ***StringOrURI*** value. Use of this claim is OPTIONAL.
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
iss = 'My Awesome Company Inc. or https://my.awesome.website/'
|
279
|
+
iss_payload = { :data => 'data', :iss => iss }
|
280
|
+
|
281
|
+
token = JWTB.encode iss_payload, hmac_secret, 'HS256'
|
282
|
+
|
283
|
+
begin
|
284
|
+
# Add iss to the validation to check if the token has been manipulated
|
285
|
+
decoded_token = JWT.decode token, hmac_secret, true, { :iss => iss, :verify_iss => true, :algorithm => 'HS256' }
|
286
|
+
rescue JWTB::InvalidIssuerError
|
287
|
+
# Handle invalid token, e.g. logout user or deny access
|
288
|
+
end
|
289
|
+
```
|
290
|
+
|
291
|
+
### Audience Claim
|
292
|
+
|
293
|
+
From [Oauth JSON Web Token 4.1.3. "aud" (Audience) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.3):
|
294
|
+
|
295
|
+
> The `aud` (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the `aud` claim when this claim is present, then the JWT MUST be rejected. In the general case, the `aud` value is an array of case-sensitive strings, each containing a ***StringOrURI*** value. In the special case when the JWT has one audience, the `aud` value MAY be a single case-sensitive string containing a ***StringOrURI*** value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
aud = ['Young', 'Old']
|
299
|
+
aud_payload = { :data => 'data', :aud => aud }
|
300
|
+
|
301
|
+
token = JWT.encode aud_payload, hmac_secret, 'HS256'
|
302
|
+
|
303
|
+
begin
|
304
|
+
# Add aud to the validation to check if the token has been manipulated
|
305
|
+
decoded_token = JWT.decode token, hmac_secret, true, { :aud => aud, :verify_aud => true, :algorithm => 'HS256' }
|
306
|
+
rescue JWTB::InvalidAudError
|
307
|
+
# Handle invalid token, e.g. logout user or deny access
|
308
|
+
puts 'Audience Error'
|
309
|
+
end
|
310
|
+
```
|
311
|
+
|
312
|
+
### JWT ID Claim
|
313
|
+
|
314
|
+
From [Oauth JSON Web Token 4.1.7. "jti" (JWT ID) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.7):
|
315
|
+
|
316
|
+
> The `jti` (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The `jti` claim can be used to prevent the JWT from being replayed. The `jti` value is a case-sensitive string. Use of this claim is OPTIONAL.
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
# Use the secret and iat to create a unique key per request to prevent replay attacks
|
320
|
+
jti_raw = [hmac_secret, iat].join(':').to_s
|
321
|
+
jti = Digest::MD5.hexdigest(jti_raw)
|
322
|
+
jti_payload = { :data => 'data', :iat => iat, :jti => jti }
|
323
|
+
|
324
|
+
token = JWTB.encode jti_payload, hmac_secret, 'HS256'
|
325
|
+
|
326
|
+
begin
|
327
|
+
# If :verify_jti is true, validation will pass if a JTI is present
|
328
|
+
#decoded_token = JWT.decode token, hmac_secret, true, { :verify_jti => true, :algorithm => 'HS256' }
|
329
|
+
# Alternatively, pass a proc with your own code to check if the JTI has already been used
|
330
|
+
decoded_token = JWT.decode token, hmac_secret, true, { :verify_jti => proc { |jti| my_validation_method(jti) }, :algorithm => 'HS256' }
|
331
|
+
rescue JWTB::InvalidJtiError
|
332
|
+
# Handle invalid token, e.g. logout user or deny access
|
333
|
+
puts 'Error'
|
334
|
+
end
|
335
|
+
|
336
|
+
```
|
337
|
+
|
338
|
+
### Issued At Claim
|
339
|
+
|
340
|
+
From [Oauth JSON Web Token 4.1.6. "iat" (Issued At) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.6):
|
341
|
+
|
342
|
+
> The `iat` (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
343
|
+
|
344
|
+
**Handle Issued At Claim**
|
345
|
+
|
346
|
+
```ruby
|
347
|
+
iat = Time.now.to_i
|
348
|
+
iat_payload = { :data => 'data', :iat => iat }
|
349
|
+
|
350
|
+
token = JWTB.encode iat_payload, hmac_secret, 'HS256'
|
351
|
+
|
352
|
+
begin
|
353
|
+
# Add iat to the validation to check if the token has been manipulated
|
354
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :verify_iat => true, :algorithm => 'HS256' }
|
355
|
+
rescue JWTB::InvalidIatError
|
356
|
+
# Handle invalid token, e.g. logout user or deny access
|
357
|
+
end
|
358
|
+
```
|
359
|
+
|
360
|
+
**Adding Leeway**
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
iat = Time.now.to_i + 10
|
364
|
+
leeway = 30 # seconds
|
365
|
+
|
366
|
+
iat_payload = { :data => 'data', :iat => iat }
|
367
|
+
|
368
|
+
# build token issued in the future
|
369
|
+
token = JWTB.encode iat_payload, hmac_secret, 'HS256'
|
370
|
+
|
371
|
+
begin
|
372
|
+
# add leeway to ensure the token is accepted
|
373
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { :iat_leeway => leeway, :verify_iat => true, :algorithm => 'HS256' }
|
374
|
+
rescue JWTB::InvalidIatError
|
375
|
+
# Handle invalid token, e.g. logout user or deny access
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
379
|
+
### Subject Claim
|
380
|
+
|
381
|
+
From [Oauth JSON Web Token 4.1.2. "sub" (Subject) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.2):
|
382
|
+
|
383
|
+
> The `sub` (subject) claim identifies the principal that is the subject of the JWT. The Claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific. The sub value is a case-sensitive string containing a ***StringOrURI*** value. Use of this claim is OPTIONAL.
|
384
|
+
|
385
|
+
```ruby
|
386
|
+
sub = 'Subject'
|
387
|
+
sub_payload = { :data => 'data', :sub => sub }
|
388
|
+
|
389
|
+
token = JWTB.encode sub_payload, hmac_secret, 'HS256'
|
390
|
+
|
391
|
+
begin
|
392
|
+
# Add sub to the validation to check if the token has been manipulated
|
393
|
+
decoded_token = JWTB.decode token, hmac_secret, true, { 'sub' => sub, :verify_sub => true, :algorithm => 'HS256' }
|
394
|
+
rescue JWTB::InvalidSubError
|
395
|
+
# Handle invalid token, e.g. logout user or deny access
|
396
|
+
end
|
397
|
+
```
|
398
|
+
|
399
|
+
# Development and Tests
|
400
|
+
|
401
|
+
We depend on [Bundler](http://rubygems.org/gems/bundler) for defining gemspec and performing releases to rubygems.org, which can be done with
|
402
|
+
|
403
|
+
```bash
|
404
|
+
rake release
|
405
|
+
```
|
406
|
+
|
407
|
+
The tests are written with rspec. Given you have installed the dependencies via bundler, you can run tests with
|
408
|
+
|
409
|
+
```bash
|
410
|
+
bundle exec rspec
|
411
|
+
```
|
412
|
+
|
413
|
+
**If you want a release cut with your PR, please include a version bump according to [Semantic Versioning](http://semver.org/)**
|
414
|
+
|
415
|
+
## Contributors
|
416
|
+
|
417
|
+
* Jordan Brough <github.jordanb@xoxy.net>
|
418
|
+
* Ilya Zhitomirskiy <ilya@joindiaspora.com>
|
419
|
+
* Daniel Grippi <daniel@joindiaspora.com>
|
420
|
+
* Jeff Lindsay <progrium@gmail.com>
|
421
|
+
* Bob Aman <bob@sporkmonger.com>
|
422
|
+
* Micah Gates <github@mgates.com>
|
423
|
+
* Rob Wygand <rob@wygand.com>
|
424
|
+
* Ariel Salomon (Oscil8)
|
425
|
+
* Paul Battley <pbattley@gmail.com>
|
426
|
+
* Zane Shannon [@zshannon](https://github.com/zshannon)
|
427
|
+
* Brian Fletcher [@punkle](https://github.com/punkle)
|
428
|
+
* Alex [@ZhangHanDong](https://github.com/ZhangHanDong)
|
429
|
+
* John Downey [@jtdowney](https://github.com/jtdowney)
|
430
|
+
* Adam Greene [@skippy](https://github.com/skippy)
|
431
|
+
* Tim Rudat [@excpt](https://github.com/excpt) <timrudat@gmail.com> - Maintainer
|
432
|
+
|
433
|
+
## License
|
434
|
+
|
435
|
+
MIT
|
436
|
+
|
437
|
+
Copyright (c) 2011 Jeff Lindsay
|
438
|
+
|
439
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
440
|
+
|
441
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
442
|
+
|
443
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|