challah-jwt 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d0e70a00bf7f163dcc79d200a28b5848f1bbb78
4
- data.tar.gz: ef354f85f0201bd2b5159f772fe6beb3d3022549
3
+ metadata.gz: 7c3fcba21e9b182c4522ceef036a576dc980409d
4
+ data.tar.gz: ae4e5f372f24f542bf9c1018461395e7af1e7347
5
5
  SHA512:
6
- metadata.gz: 822500c6516ae7b2f909a423daaab1471afeb27adbf7e5c8d5ccc609c0987478a5e47987394e5696009f6bf50db8e3fd1d43f99f2fca14f1de7e839facd02472
7
- data.tar.gz: 9911d5cb1aa541dae8ee9bd4daaa381efabcc5274b08987efcc37386498b7be465d9b60d31446cb5033ba9cc2253e4bf8834eae3c53b451036453467aee83145
6
+ metadata.gz: 9cc59e0ab65029e3e5534453ff9b307915195d220817e9dd83da0bd6dfced1087aeaefab55562a0ae19b4f2bbd9a50bf88f5248d33483bba51ae93d0fdb9e4a1
7
+ data.tar.gz: '0379a709decc5837e6b5188f2a67222b4bba2e0782aa13027707bc23162d97b25b97481c6558a6b9e0a87565f8aa9b2835b23ba2d19bad96ae4b4eda97d28d67'
data/README.md CHANGED
@@ -47,6 +47,34 @@ GET /
47
47
  Authorization: Bearer adi8e98uie.saxbbbgudinocgeigc84y9834.8ui9odeion
48
48
  ```
49
49
 
50
+ ### Tokenizer
51
+
52
+ Challah-JWT adds a few methods to your User model that make it easy to tokenize and look up users:
53
+
54
+ ```ruby
55
+ user = User.first
56
+ # => #<User id=1...>
57
+
58
+ # Convert the user to a JWT
59
+ jwt = user.to_jwt
60
+
61
+ # Look up user by JWT
62
+ user = User.find_by_jwt(jwt)
63
+ ```
64
+
65
+ The tokenizer only includes the user's ID in the payload by deafult, to override this behavior, override the `jwt_attrs` method in your user model:
66
+
67
+ ```ruby
68
+ class User < ApplicationRecord
69
+ include Challah::Jwt::Tokenizer
70
+
71
+ def jwt_attrs
72
+ # make sure you include id, otherwise the lookup will fail
73
+ serializable_hash.slice("id", "email", "status")
74
+ end
75
+ end
76
+ ```
77
+
50
78
  ## Development
51
79
 
52
80
  After checking out the repo, run `bin/setup` to install dependencies. You can
@@ -0,0 +1,23 @@
1
+ module Challah
2
+ module Jwt
3
+ class Configuration
4
+ attr_accessor :use_api_key
5
+
6
+ def initialize(options = {})
7
+ @use_api_key = options.fetch(:use_api_key, false)
8
+ end
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configuration=(config)
16
+ @configuration = config
17
+ end
18
+
19
+ def self.configure(&block)
20
+ yield(configuration)
21
+ end
22
+ end
23
+ end
@@ -8,8 +8,14 @@ module Challah
8
8
  class_methods do
9
9
  def find_by_jwt(token)
10
10
  payload = ::JWT.decode(token, Challah::Jwt.secret_key)
11
- model_id = payload.dig(0, jwt_root, "id")
12
- find(model_id)
11
+ id = payload.dig(0, jwt_root, "id")
12
+
13
+ if Challah::Jwt.configuration.use_api_key
14
+ api_key = payload.dig(0, jwt_root, "api_key")
15
+ find_by(id: id, api_key: api_key)
16
+ else
17
+ find_by(id: id)
18
+ end
13
19
  rescue JWT::DecodeError
14
20
  nil
15
21
  end
@@ -24,7 +30,11 @@ module Challah
24
30
  end
25
31
 
26
32
  def jwt_attrs
27
- serializable_hash.slice("id")
33
+ if Challah::Jwt.configuration.use_api_key
34
+ serializable_hash.slice("id", "api_key")
35
+ else
36
+ serializable_hash.slice("id")
37
+ end
28
38
  end
29
39
 
30
40
  def jwt_payload
@@ -1,5 +1,5 @@
1
1
  module Challah
2
2
  module Jwt
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/challah/jwt.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "challah"
2
2
  require "jwt"
3
3
 
4
+ require "challah/jwt/configuration"
4
5
  require "challah/jwt/technique"
5
6
  require "challah/jwt/tokenizer"
6
7
  require "challah/jwt/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: challah-jwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phillip Ridlen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-19 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: challah
@@ -82,6 +82,7 @@ files:
82
82
  - challah-jwt.gemspec
83
83
  - lib/challah-jwt.rb
84
84
  - lib/challah/jwt.rb
85
+ - lib/challah/jwt/configuration.rb
85
86
  - lib/challah/jwt/technique.rb
86
87
  - lib/challah/jwt/tokenizer.rb
87
88
  - lib/challah/jwt/version.rb