sealights-rspec-agent 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/agent/config.rb +6 -6
  3. data/agent/dependencies/faraday-0.17.0/LICENSE.md +20 -0
  4. data/agent/dependencies/faraday-0.17.0/README.md +384 -0
  5. data/agent/dependencies/faraday-0.17.0/lib/faraday.rb +248 -0
  6. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter.rb +55 -0
  7. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/em_http.rb +243 -0
  8. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/em_synchrony.rb +106 -0
  9. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
  10. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/excon.rb +82 -0
  11. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/httpclient.rb +128 -0
  12. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/net_http.rb +152 -0
  13. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/net_http_persistent.rb +68 -0
  14. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/patron.rb +95 -0
  15. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/rack.rb +58 -0
  16. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/sl_em_http_ssl_patch.rb +56 -0
  17. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/test.rb +213 -0
  18. data/agent/dependencies/faraday-0.17.0/lib/faraday/adapter/typhoeus.rb +12 -0
  19. data/agent/dependencies/faraday-0.17.0/lib/faraday/autoload.rb +84 -0
  20. data/agent/dependencies/faraday-0.17.0/lib/faraday/connection.rb +484 -0
  21. data/agent/dependencies/faraday-0.17.0/lib/faraday/error.rb +66 -0
  22. data/agent/dependencies/faraday-0.17.0/lib/faraday/middleware.rb +37 -0
  23. data/agent/dependencies/faraday-0.17.0/lib/faraday/options.rb +373 -0
  24. data/agent/dependencies/faraday-0.17.0/lib/faraday/parameters.rb +198 -0
  25. data/agent/dependencies/faraday-0.17.0/lib/faraday/rack_builder.rb +237 -0
  26. data/agent/dependencies/faraday-0.17.0/lib/faraday/request.rb +114 -0
  27. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/authorization.rb +41 -0
  28. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/basic_authentication.rb +13 -0
  29. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/instrumentation.rb +36 -0
  30. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/multipart.rb +68 -0
  31. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/retry.rb +212 -0
  32. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/token_authentication.rb +15 -0
  33. data/agent/dependencies/faraday-0.17.0/lib/faraday/request/url_encoded.rb +36 -0
  34. data/agent/dependencies/faraday-0.17.0/lib/faraday/response.rb +97 -0
  35. data/agent/dependencies/faraday-0.17.0/lib/faraday/response/logger.rb +80 -0
  36. data/agent/dependencies/faraday-0.17.0/lib/faraday/response/raise_error.rb +21 -0
  37. data/agent/dependencies/faraday-0.17.0/lib/faraday/upload_io.rb +67 -0
  38. data/agent/dependencies/faraday-0.17.0/lib/faraday/utils.rb +326 -0
  39. data/agent/dependencies/jwt-2.2.1/AUTHORS +84 -0
  40. data/agent/dependencies/jwt-2.2.1/Appraisals +14 -0
  41. data/agent/dependencies/jwt-2.2.1/CHANGELOG.md +570 -0
  42. data/agent/dependencies/jwt-2.2.1/Gemfile +3 -0
  43. data/agent/dependencies/jwt-2.2.1/LICENSE +7 -0
  44. data/agent/dependencies/jwt-2.2.1/README.md +489 -0
  45. data/agent/dependencies/jwt-2.2.1/Rakefile +11 -0
  46. data/agent/dependencies/jwt-2.2.1/lib/jwt.rb +30 -0
  47. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/ecdsa.rb +35 -0
  48. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/eddsa.rb +23 -0
  49. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/hmac.rb +33 -0
  50. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/ps.rb +43 -0
  51. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/rsa.rb +19 -0
  52. data/agent/dependencies/jwt-2.2.1/lib/jwt/algos/unsupported.rb +16 -0
  53. data/agent/dependencies/jwt-2.2.1/lib/jwt/base64.rb +19 -0
  54. data/agent/dependencies/jwt-2.2.1/lib/jwt/claims_validator.rb +33 -0
  55. data/agent/dependencies/jwt-2.2.1/lib/jwt/decode.rb +100 -0
  56. data/agent/dependencies/jwt-2.2.1/lib/jwt/default_options.rb +15 -0
  57. data/agent/dependencies/jwt-2.2.1/lib/jwt/encode.rb +68 -0
  58. data/agent/dependencies/jwt-2.2.1/lib/jwt/error.rb +20 -0
  59. data/agent/dependencies/jwt-2.2.1/lib/jwt/json.rb +18 -0
  60. data/agent/dependencies/jwt-2.2.1/lib/jwt/jwk.rb +31 -0
  61. data/agent/dependencies/jwt-2.2.1/lib/jwt/jwk/key_finder.rb +57 -0
  62. data/agent/dependencies/jwt-2.2.1/lib/jwt/jwk/rsa.rb +47 -0
  63. data/agent/dependencies/jwt-2.2.1/lib/jwt/security_utils.rb +57 -0
  64. data/agent/dependencies/jwt-2.2.1/lib/jwt/signature.rb +52 -0
  65. data/agent/dependencies/jwt-2.2.1/lib/jwt/verify.rb +98 -0
  66. data/agent/dependencies/jwt-2.2.1/lib/jwt/version.rb +24 -0
  67. data/agent/dependencies/jwt-2.2.1/ruby-jwt.gemspec +34 -0
  68. data/agent/dependencies/multipart-post-2.1.1/Gemfile +6 -0
  69. data/agent/dependencies/multipart-post-2.1.1/History.txt +64 -0
  70. data/agent/dependencies/multipart-post-2.1.1/LICENSE +21 -0
  71. data/agent/dependencies/multipart-post-2.1.1/Manifest.txt +9 -0
  72. data/agent/dependencies/multipart-post-2.1.1/README.md +127 -0
  73. data/agent/dependencies/multipart-post-2.1.1/Rakefile +6 -0
  74. data/agent/dependencies/multipart-post-2.1.1/lib/composite_io.rb +108 -0
  75. data/agent/dependencies/multipart-post-2.1.1/lib/multipart_post.rb +9 -0
  76. data/agent/dependencies/multipart-post-2.1.1/lib/multipartable.rb +48 -0
  77. data/agent/dependencies/multipart-post-2.1.1/lib/net/http/post/multipart.rb +28 -0
  78. data/agent/dependencies/multipart-post-2.1.1/lib/parts.rb +126 -0
  79. data/agent/dependencies/multipart-post-2.1.1/multipart-post.gemspec +23 -0
  80. data/agent/http_client.rb +46 -0
  81. data/agent/listener.rb +1 -1
  82. data/agent/sealights-rspec-agent.rb +2 -2
  83. data/agent/tia.rb +5 -1
  84. metadata +80 -3
  85. data/agent/rest-client-wrapper.rb +0 -27
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -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.
@@ -0,0 +1,489 @@
1
+ # JWT
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jwt.svg)](https://badge.fury.io/rb/jwt)
4
+ [![Build Status](https://travis-ci.org/jwt/ruby-jwt.svg)](https://travis-ci.org/jwt/ruby-jwt)
5
+ [![Code Climate](https://codeclimate.com/github/jwt/ruby-jwt/badges/gpa.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
6
+ [![Test Coverage](https://codeclimate.com/github/jwt/ruby-jwt/badges/coverage.svg)](https://codeclimate.com/github/jwt/ruby-jwt/coverage)
7
+ [![Issue Count](https://codeclimate.com/github/jwt/ruby-jwt/badges/issue_count.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
8
+ [![Ebert](https://ebertapp.io/github/jwt/ruby-jwt.svg)](https://ebertapp.io/github/jwt/ruby-jwt)
9
+
10
+ A ruby implementation of the [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) standard.
11
+
12
+ If you have further questions related to development or usage, join us: [ruby-jwt google group](https://groups.google.com/forum/#!forum/ruby-jwt).
13
+
14
+ ## Announcements
15
+
16
+ * Ruby 1.9.3 support was dropped at December 31st, 2016.
17
+ * 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)
18
+
19
+ ## Installing
20
+
21
+ ### Using Rubygems:
22
+ ```bash
23
+ sudo gem install jwt
24
+ ```
25
+
26
+ ### Using Bundler:
27
+ Add the following to your Gemfile
28
+ ```
29
+ gem 'jwt'
30
+ ```
31
+ And run `bundle install`
32
+
33
+ ## Algorithms and Usage
34
+
35
+ 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/). **It is strongly recommended that you hard code the algorithm, as you may leave yourself vulnerable by dynamically picking the algorithm**
36
+
37
+ See: [ JSON Web Algorithms (JWA) 3.1. "alg" (Algorithm) Header Parameter Values for JWS](https://tools.ietf.org/html/rfc7518#section-3.1)
38
+
39
+ **NONE**
40
+
41
+ * none - unsigned token
42
+
43
+ ```ruby
44
+ require 'jwt'
45
+
46
+ payload = { data: 'test' }
47
+
48
+ # IMPORTANT: set nil as password parameter
49
+ token = JWT.encode payload, nil, 'none'
50
+
51
+ # eyJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
52
+ puts token
53
+
54
+ # Set password to nil and validation to false otherwise this won't work
55
+ decoded_token = JWT.decode token, nil, false
56
+
57
+ # Array
58
+ # [
59
+ # {"data"=>"test"}, # payload
60
+ # {"alg"=>"none"} # header
61
+ # ]
62
+ puts decoded_token
63
+ ```
64
+
65
+ **HMAC**
66
+
67
+ * HS256 - HMAC using SHA-256 hash algorithm
68
+ * HS512256 - HMAC using SHA-512-256 hash algorithm (only available with RbNaCl; see note below)
69
+ * HS384 - HMAC using SHA-384 hash algorithm
70
+ * HS512 - HMAC using SHA-512 hash algorithm
71
+
72
+ ```ruby
73
+ hmac_secret = 'my$ecretK3y'
74
+
75
+ token = JWT.encode payload, hmac_secret, 'HS256'
76
+
77
+ # eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pNIWIL34Jo13LViZAJACzK6Yf0qnvT_BuwOxiMCPE-Y
78
+ puts token
79
+
80
+ decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
81
+
82
+ # Array
83
+ # [
84
+ # {"data"=>"test"}, # payload
85
+ # {"alg"=>"HS256"} # header
86
+ # ]
87
+ puts decoded_token
88
+ ```
89
+
90
+ 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.
91
+
92
+ [RbNaCl](https://github.com/cryptosphere/rbnacl) requires
93
+ [libsodium](https://github.com/jedisct1/libsodium), it can be installed
94
+ on MacOS with `brew install libsodium`.
95
+
96
+ **RSA**
97
+
98
+ * RS256 - RSA using SHA-256 hash algorithm
99
+ * RS384 - RSA using SHA-384 hash algorithm
100
+ * RS512 - RSA using SHA-512 hash algorithm
101
+
102
+ ```ruby
103
+ rsa_private = OpenSSL::PKey::RSA.generate 2048
104
+ rsa_public = rsa_private.public_key
105
+
106
+ token = JWT.encode payload, rsa_private, 'RS256'
107
+
108
+ # eyJhbGciOiJSUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.GplO4w1spRgvEJQ3-FOtZr-uC8L45Jt7SN0J4woBnEXG_OZBSNcZjAJWpjadVYEe2ev3oUBFDYM1N_-0BTVeFGGYvMewu8E6aMjSZvOpf1cZBew-Vt4poSq7goG2YRI_zNPt3af2lkPqXD796IKC5URrEvcgF5xFQ-6h07XRDpSRx1ECrNsUOt7UM3l1IB4doY11GzwQA5sHDTmUZ0-kBT76ZMf12Srg_N3hZwphxBtudYtN5VGZn420sVrQMdPE_7Ni3EiWT88j7WCr1xrF60l8sZT3yKCVleG7D2BEXacTntB7GktBv4Xo8OKnpwpqTpIlC05dMowMkz3rEAAYbQ
109
+ puts token
110
+
111
+ decoded_token = JWT.decode token, rsa_public, true, { algorithm: 'RS256' }
112
+
113
+ # Array
114
+ # [
115
+ # {"data"=>"test"}, # payload
116
+ # {"alg"=>"RS256"} # header
117
+ # ]
118
+ puts decoded_token
119
+ ```
120
+
121
+ **ECDSA**
122
+
123
+ * ES256 - ECDSA using P-256 and SHA-256
124
+ * ES384 - ECDSA using P-384 and SHA-384
125
+ * ES512 - ECDSA using P-521 and SHA-512
126
+
127
+ ```ruby
128
+ ecdsa_key = OpenSSL::PKey::EC.new 'prime256v1'
129
+ ecdsa_key.generate_key
130
+ ecdsa_public = OpenSSL::PKey::EC.new ecdsa_key
131
+ ecdsa_public.private_key = nil
132
+
133
+ token = JWT.encode payload, ecdsa_key, 'ES256'
134
+
135
+ # eyJhbGciOiJFUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.AlLW--kaF7EX1NMX9WJRuIW8NeRJbn2BLXHns7Q5TZr7Hy3lF6MOpMlp7GoxBFRLISQ6KrD0CJOrR8aogEsPeg
136
+ puts token
137
+
138
+ decoded_token = JWT.decode token, ecdsa_public, true, { algorithm: 'ES256' }
139
+
140
+ # Array
141
+ # [
142
+ # {"test"=>"data"}, # payload
143
+ # {"alg"=>"ES256"} # header
144
+ # ]
145
+ puts decoded_token
146
+ ```
147
+
148
+ **EDDSA**
149
+
150
+ In order to use this algorithm you need to add the `RbNaCl` gem to you `Gemfile`.
151
+
152
+ ```ruby
153
+ gem 'rbnacl'
154
+ ```
155
+
156
+ For more detailed installation instruction check the official [repository](https://github.com/cryptosphere/rbnacl) on GitHub.
157
+
158
+ * ED25519
159
+
160
+ ```ruby
161
+ private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
162
+ public_key = private_key.verify_key
163
+ token = JWT.encode payload, private_key, 'ED25519'
164
+
165
+ # eyJhbGciOiJFRDI1NTE5In0.eyJkYXRhIjoidGVzdCJ9.6xIztXyOupskddGA_RvKU76V9b2dCQUYhoZEVFnRimJoPYIzZ2Fm47CWw8k2NTCNpgfAuxg9OXjaiVK7MvrbCQ
166
+ puts token
167
+
168
+ decoded_token = JWT.decode token, public_key, true, { algorithm: 'ED25519' }
169
+ # Array
170
+ # [
171
+ # {"test"=>"data"}, # payload
172
+ # {"alg"=>"ED25519"} # header
173
+ # ]
174
+
175
+ ```
176
+
177
+ **RSASSA-PSS**
178
+
179
+ In order to use this algorithm you need to add the `openssl` gem to you `Gemfile` with a version greater or equal to `2.1`.
180
+
181
+ ```ruby
182
+ gem 'openssl', '~> 2.1'
183
+ ```
184
+
185
+ * PS256 - RSASSA-PSS using SHA-256 hash algorithm
186
+ * PS384 - RSASSA-PSS using SHA-384 hash algorithm
187
+ * PS512 - RSASSA-PSS using SHA-512 hash algorithm
188
+
189
+ ```ruby
190
+ rsa_private = OpenSSL::PKey::RSA.generate 2048
191
+ rsa_public = rsa_private.public_key
192
+
193
+ token = JWT.encode payload, rsa_private, 'PS256'
194
+
195
+ # eyJhbGciOiJQUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.KEmqagMUHM-NcmXo6818ZazVTIAkn9qU9KQFT1c5Iq91n0KRpAI84jj4ZCdkysDlWokFs3Dmn4MhcXP03oJKLFgnoPL40_Wgg9iFr0jnIVvnMUp1kp2RFUbL0jqExGTRA3LdAhuvw6ZByGD1bkcWjDXygjQw-hxILrT1bENjdr0JhFd-cB0-ps5SB0mwhFNcUw-OM3Uu30B1-mlFaelUY8jHJYKwLTZPNxHzndt8RGXF8iZLp7dGb06HSCKMcVzhASGMH4ZdFystRe2hh31cwcvnl-Eo_D4cdwmpN3Abhk_8rkxawQJR3duh8HNKc4AyFPo7SabEaSu2gLnLfN3yfg
196
+ puts token
197
+
198
+ decoded_token = JWT.decode token, rsa_public, true, { algorithm: 'PS256' }
199
+
200
+ # Array
201
+ # [
202
+ # {"data"=>"test"}, # payload
203
+ # {"alg"=>"PS256"} # header
204
+ # ]
205
+ puts decoded_token
206
+ ```
207
+
208
+ ## Support for reserved claim names
209
+ JSON Web Token defines some reserved claim names and defines how they should be
210
+ used. JWT supports these reserved claim names:
211
+
212
+ - 'exp' (Expiration Time) Claim
213
+ - 'nbf' (Not Before Time) Claim
214
+ - 'iss' (Issuer) Claim
215
+ - 'aud' (Audience) Claim
216
+ - 'jti' (JWT ID) Claim
217
+ - 'iat' (Issued At) Claim
218
+ - 'sub' (Subject) Claim
219
+
220
+ ## Add custom header fields
221
+ Ruby-jwt gem supports custom [header fields](https://tools.ietf.org/html/rfc7519#section-5)
222
+ To add custom header fields you need to pass `header_fields` parameter
223
+
224
+ ```ruby
225
+ token = JWT.encode payload, key, algorithm='HS256', header_fields={}
226
+ ```
227
+
228
+ **Example:**
229
+
230
+ ```ruby
231
+ require 'jwt'
232
+
233
+ payload = { data: 'test' }
234
+
235
+ # IMPORTANT: set nil as password parameter
236
+ token = JWT.encode payload, nil, 'none', { typ: 'JWT' }
237
+
238
+ # eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJkYXRhIjoidGVzdCJ9.
239
+ puts token
240
+
241
+ # Set password to nil and validation to false otherwise this won't work
242
+ decoded_token = JWT.decode token, nil, false
243
+
244
+ # Array
245
+ # [
246
+ # {"data"=>"test"}, # payload
247
+ # {"typ"=>"JWT", "alg"=>"none"} # header
248
+ # ]
249
+ puts decoded_token
250
+ ```
251
+
252
+ ### Expiration Time Claim
253
+
254
+ From [Oauth JSON Web Token 4.1.4. "exp" (Expiration Time) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.4):
255
+
256
+ > 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.
257
+
258
+ **Handle Expiration Claim**
259
+
260
+ ```ruby
261
+ exp = Time.now.to_i + 4 * 3600
262
+ exp_payload = { data: 'data', exp: exp }
263
+
264
+ token = JWT.encode exp_payload, hmac_secret, 'HS256'
265
+
266
+ begin
267
+ decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
268
+ rescue JWT::ExpiredSignature
269
+ # Handle expired token, e.g. logout user or deny access
270
+ end
271
+ ```
272
+
273
+ **Adding Leeway**
274
+
275
+ ```ruby
276
+ exp = Time.now.to_i - 10
277
+ leeway = 30 # seconds
278
+
279
+ exp_payload = { data: 'data', exp: exp }
280
+
281
+ # build expired token
282
+ token = JWT.encode exp_payload, hmac_secret, 'HS256'
283
+
284
+ begin
285
+ # add leeway to ensure the token is still accepted
286
+ decoded_token = JWT.decode token, hmac_secret, true, { exp_leeway: leeway, algorithm: 'HS256' }
287
+ rescue JWT::ExpiredSignature
288
+ # Handle expired token, e.g. logout user or deny access
289
+ end
290
+ ```
291
+
292
+ ### Not Before Time Claim
293
+
294
+ From [Oauth JSON Web Token 4.1.5. "nbf" (Not Before) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.5):
295
+
296
+ > 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.
297
+
298
+ **Handle Not Before Claim**
299
+
300
+ ```ruby
301
+ nbf = Time.now.to_i - 3600
302
+ nbf_payload = { data: 'data', nbf: nbf }
303
+
304
+ token = JWT.encode nbf_payload, hmac_secret, 'HS256'
305
+
306
+ begin
307
+ decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
308
+ rescue JWT::ImmatureSignature
309
+ # Handle invalid token, e.g. logout user or deny access
310
+ end
311
+ ```
312
+
313
+ **Adding Leeway**
314
+
315
+ ```ruby
316
+ nbf = Time.now.to_i + 10
317
+ leeway = 30
318
+
319
+ nbf_payload = { data: 'data', nbf: nbf }
320
+
321
+ # build expired token
322
+ token = JWT.encode nbf_payload, hmac_secret, 'HS256'
323
+
324
+ begin
325
+ # add leeway to ensure the token is valid
326
+ decoded_token = JWT.decode token, hmac_secret, true, { nbf_leeway: leeway, algorithm: 'HS256' }
327
+ rescue JWT::ImmatureSignature
328
+ # Handle invalid token, e.g. logout user or deny access
329
+ end
330
+ ```
331
+
332
+ ### Issuer Claim
333
+
334
+ From [Oauth JSON Web Token 4.1.1. "iss" (Issuer) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.1):
335
+
336
+ > 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.
337
+
338
+ You can pass multiple allowed issuers as an Array, verification will pass if one of them matches the `iss` value in the payload.
339
+
340
+ ```ruby
341
+ iss = 'My Awesome Company Inc. or https://my.awesome.website/'
342
+ iss_payload = { data: 'data', iss: iss }
343
+
344
+ token = JWT.encode iss_payload, hmac_secret, 'HS256'
345
+
346
+ begin
347
+ # Add iss to the validation to check if the token has been manipulated
348
+ decoded_token = JWT.decode token, hmac_secret, true, { iss: iss, verify_iss: true, algorithm: 'HS256' }
349
+ rescue JWT::InvalidIssuerError
350
+ # Handle invalid token, e.g. logout user or deny access
351
+ end
352
+ ```
353
+
354
+ ### Audience Claim
355
+
356
+ From [Oauth JSON Web Token 4.1.3. "aud" (Audience) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.3):
357
+
358
+ > 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.
359
+
360
+ ```ruby
361
+ aud = ['Young', 'Old']
362
+ aud_payload = { data: 'data', aud: aud }
363
+
364
+ token = JWT.encode aud_payload, hmac_secret, 'HS256'
365
+
366
+ begin
367
+ # Add aud to the validation to check if the token has been manipulated
368
+ decoded_token = JWT.decode token, hmac_secret, true, { aud: aud, verify_aud: true, algorithm: 'HS256' }
369
+ rescue JWT::InvalidAudError
370
+ # Handle invalid token, e.g. logout user or deny access
371
+ puts 'Audience Error'
372
+ end
373
+ ```
374
+
375
+ ### JWT ID Claim
376
+
377
+ From [Oauth JSON Web Token 4.1.7. "jti" (JWT ID) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.7):
378
+
379
+ > 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.
380
+
381
+ ```ruby
382
+ # Use the secret and iat to create a unique key per request to prevent replay attacks
383
+ jti_raw = [hmac_secret, iat].join(':').to_s
384
+ jti = Digest::MD5.hexdigest(jti_raw)
385
+ jti_payload = { data: 'data', iat: iat, jti: jti }
386
+
387
+ token = JWT.encode jti_payload, hmac_secret, 'HS256'
388
+
389
+ begin
390
+ # If :verify_jti is true, validation will pass if a JTI is present
391
+ #decoded_token = JWT.decode token, hmac_secret, true, { verify_jti: true, algorithm: 'HS256' }
392
+ # Alternatively, pass a proc with your own code to check if the JTI has already been used
393
+ decoded_token = JWT.decode token, hmac_secret, true, { verify_jti: proc { |jti| my_validation_method(jti) }, algorithm: 'HS256' }
394
+ rescue JWT::InvalidJtiError
395
+ # Handle invalid token, e.g. logout user or deny access
396
+ puts 'Error'
397
+ end
398
+ ```
399
+
400
+ ### Issued At Claim
401
+
402
+ From [Oauth JSON Web Token 4.1.6. "iat" (Issued At) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.6):
403
+
404
+ > 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. The `leeway` option is not taken into account when verifying this claim. The `iat_leeway` option was removed in version 2.2.0. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
405
+
406
+ **Handle Issued At Claim**
407
+
408
+ ```ruby
409
+ iat = Time.now.to_i
410
+ iat_payload = { data: 'data', iat: iat }
411
+
412
+ token = JWT.encode iat_payload, hmac_secret, 'HS256'
413
+
414
+ begin
415
+ # Add iat to the validation to check if the token has been manipulated
416
+ decoded_token = JWT.decode token, hmac_secret, true, { verify_iat: true, algorithm: 'HS256' }
417
+ rescue JWT::InvalidIatError
418
+ # Handle invalid token, e.g. logout user or deny access
419
+ end
420
+ ```
421
+
422
+ ### Subject Claim
423
+
424
+ From [Oauth JSON Web Token 4.1.2. "sub" (Subject) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.2):
425
+
426
+ > 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.
427
+
428
+ ```ruby
429
+ sub = 'Subject'
430
+ sub_payload = { data: 'data', sub: sub }
431
+
432
+ token = JWT.encode sub_payload, hmac_secret, 'HS256'
433
+
434
+ begin
435
+ # Add sub to the validation to check if the token has been manipulated
436
+ decoded_token = JWT.decode token, hmac_secret, true, { sub: sub, verify_sub: true, algorithm: 'HS256' }
437
+ rescue JWT::InvalidSubError
438
+ # Handle invalid token, e.g. logout user or deny access
439
+ end
440
+ ```
441
+
442
+ ### JSON Web Key (JWK)
443
+
444
+ JWK is a JSON structure representing a cryptographic key. Currently only supports RSA public keys.
445
+
446
+ ```ruby
447
+ jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048))
448
+ payload, headers = { data: 'data' }, { kid: jwk.kid }
449
+
450
+ token = JWT.encode(payload, jwk.keypair, 'RS512', headers)
451
+
452
+ # The jwk loader would fetch the set of JWKs from a trusted source
453
+ jwk_loader = ->(options) do
454
+ @cached_keys = nil if options[:invalidate] # need to reload the keys
455
+ @cached_keys ||= { keys: [jwk.export] }
456
+ end
457
+
458
+ begin
459
+ JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwk_loader})
460
+ rescue JWT::JWKError
461
+ # Handle problems with the provided JWKs
462
+ rescue JWT::DecodeError
463
+ # Handle other decode related issues e.g. no kid in header, no matching public key found etc.
464
+ end
465
+ ```
466
+
467
+ # Development and Tests
468
+
469
+ We depend on [Bundler](http://rubygems.org/gems/bundler) for defining gemspec and performing releases to rubygems.org, which can be done with
470
+
471
+ ```bash
472
+ rake release
473
+ ```
474
+
475
+ The tests are written with rspec. Given you have installed the dependencies via bundler, you can run tests with
476
+
477
+ ```bash
478
+ bundle exec rspec
479
+ ```
480
+
481
+ **If you want a release cut with your PR, please include a version bump according to [Semantic Versioning](http://semver.org/)**
482
+
483
+ ## Contributors
484
+
485
+ See `AUTHORS` file.
486
+
487
+ ## License
488
+
489
+ See `LICENSE` file.