auth_rails 1.0.2 → 1.1.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
  SHA256:
3
- metadata.gz: a80fe0179e20db84cc1966bd18331d98b3172c4cd5c3b652949ef7bc7a508b0a
4
- data.tar.gz: 6cc51206c4381735dd92f118c8268df20998739cad769bcd2e01211dabb20e76
3
+ metadata.gz: c69df81c88cacdf202e2c49cf329423ded23e509fc2dc67fd60d07fda1fbe12e
4
+ data.tar.gz: abbad690e4211d181950af7bf542e2df367b79c0c79c049f32c5192188106f4f
5
5
  SHA512:
6
- metadata.gz: e7d2ccb1bbd06e8cf115267a998cd0490ac8e9583e5a351ca61c486d0ceafd26fda66eee7995a84e01dd4dd82ed29fa29c4af865cbf2f232b445a48bd6d0bc86
7
- data.tar.gz: 7a2057151c16ea45d74eb88f9a501a232f5e714a713a22dc125b56fbb6f9710c0b02dd39d9364af8ab7a54e002b8cc511d95389d0d1467050470b970f2814ab1
6
+ metadata.gz: b97b6c42ed2386b526ce58bbc9e8fd2982e7b60357adebfb0c547f3b3cbf6d07f6cbca8e4cf13871a42f69935ae09daf2dc7528563dd024dcc95dc95e8611f5a
7
+ data.tar.gz: 758425cb02330e2efd68fedc8730e4b8879136098f0ecfb1d9543812e8220fdbc51f8ec1eaa15c19a09d17e29788c4968969121e08c9a6cf922a2ea4b60e005e
data/README.md CHANGED
@@ -165,6 +165,50 @@ module Api
165
165
  end
166
166
  ```
167
167
 
168
+ - In case your identifier is not email
169
+
170
+ ```rb
171
+ Rails.application.config.to_prepare do
172
+ AuthRails.configure do |config|
173
+ config.resource_class = User # required
174
+ config.identifier_name = :username # must be string or symbol, default is email
175
+ end
176
+ end
177
+ ```
178
+
179
+ - If you have a custom method to validate password
180
+
181
+ ```rb
182
+ Rails.application.config.to_prepare do
183
+ AuthRails.configure do |config|
184
+ config.resource_class = User # required
185
+ config.identifier_name = :username # must be string or symbol, default is email
186
+ config.authenticate = ->(resource, password) { resource.password == password } # must be a proc, validate password
187
+ end
188
+ end
189
+ ```
190
+
191
+ - Sometimes, you have a complex logic to get the user
192
+
193
+ ```rb
194
+ Rails.application.config.to_prepare do
195
+ AuthRails.configure do |config|
196
+ config.resource_class = User # required
197
+ config.identifier_name = :username # this one is sub in jwt
198
+ config.dig_params = ->(params) { params[:identifier] } # must be a proc, how to get identifier from params
199
+
200
+ # how to get user from identifier
201
+ # identifier default is params[<identifier_name>]
202
+ # or extract from dig_params
203
+ config.retrieve_resource = lambda { |identifier|
204
+ User.where(email: identifier)
205
+ .or(User.where(username: identifier))
206
+ .first
207
+ }
208
+ end
209
+ end
210
+ ```
211
+
168
212
  # Strategy list
169
213
 
170
214
  - allowed_token
@@ -4,9 +4,9 @@ module AuthRails
4
4
  module Api
5
5
  class AuthController < ApiController
6
6
  def create
7
- resource = AuthRails.resource_class.find_by(email: params[:email])
7
+ resource = AuthRails.retrieve_resource(params: params)
8
8
 
9
- raise AuthRails.error_class, :unauthenticated if resource.blank? || !resource.authenticate(params[:password])
9
+ raise AuthRails.error_class, :unauthenticated if resource.blank? || !AuthRails.authenticate(resource: resource, password: params[:password])
10
10
 
11
11
  respond_to_create(generate_token(resource))
12
12
  end
@@ -43,7 +43,7 @@ module AuthRails
43
43
 
44
44
  def payload(resource)
45
45
  {
46
- sub: resource.email
46
+ sub: resource.send(AuthRails.identifier_name)
47
47
  }
48
48
  end
49
49
 
@@ -10,7 +10,7 @@ module AuthRails
10
10
  secret_key: Configuration::Jwt::AccessToken.secret_key
11
11
  )
12
12
 
13
- CurrentAuth.user = AuthRails.resource_class.find_by(email: payload[:sub])
13
+ CurrentAuth.user = AuthRails.resource_class.find_by(AuthRails.identifier_name => payload[:sub])
14
14
 
15
15
  raise AuthRails.error_class, :unauthenticated unless CurrentAuth.user
16
16
  end
data/auth_rails.gemspec CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  .git
29
29
  .circleci
30
30
  appveyor
31
+ examples/
31
32
  Gemfile
32
33
  .rubocop.yml
33
34
  .vscode/settings.json
@@ -14,6 +14,10 @@ module AuthRails
14
14
  @resource_class ||= Config.resource_class
15
15
  end
16
16
 
17
+ def identifier_name
18
+ @identifier_name ||= Config.identifier_name.to_sym || :email
19
+ end
20
+
17
21
  def error_class
18
22
  @error_class ||= Config.error_class || Error
19
23
  end
@@ -21,5 +25,45 @@ module AuthRails
21
25
  def jwt_strategy
22
26
  @jwt_strategy ||= Configuration::Jwt.strategy || Strategies::BaseStrategy
23
27
  end
28
+
29
+ def authenticate(resource:, password:)
30
+ if Config.authenticate.present?
31
+ raise_if_not_proc(Config.authenticate, 'Config.authenticate')
32
+
33
+ Config.authenticate.call(resource, password)
34
+ else
35
+ raise 'Don\'t know how to authenticate resource with password' unless resource.respond_to?(:authenticate)
36
+
37
+ resource.authenticate(password)
38
+ end
39
+ end
40
+
41
+ def dig_params(params:)
42
+ if Config.dig_params.present?
43
+ raise_if_not_proc(Config.dig_params, 'Config.dig_params')
44
+
45
+ Config.dig_params.call(params)
46
+ else
47
+ params[AuthRails.identifier_name]
48
+ end
49
+ end
50
+
51
+ def retrieve_resource(params:)
52
+ identifier = dig_params(params: params)
53
+
54
+ if Config.retrieve_resource.present?
55
+ raise_if_not_proc(Config.retrieve_resource, 'Config.retrieve_resource')
56
+
57
+ return Config.retrieve_resource.call(identifier)
58
+ end
59
+
60
+ AuthRails.resource_class.find_by(AuthRails.identifier_name => identifier)
61
+ end
62
+
63
+ private
64
+
65
+ def raise_if_not_proc(source, name)
66
+ raise "#{name} must be a Proc" unless source.is_a?(Proc)
67
+ end
24
68
  end
25
69
  end
@@ -3,8 +3,12 @@
3
3
  module AuthRails
4
4
  class Config
5
5
  class << self
6
- attr_accessor :error_class,
7
- :resource_class
6
+ attr_accessor :dig_params,
7
+ :error_class,
8
+ :authenticate,
9
+ :resource_class,
10
+ :identifier_name,
11
+ :retrieve_resource
8
12
 
9
13
  def jwt
10
14
  yield Configuration::Jwt
@@ -11,7 +11,7 @@ module AuthRails
11
11
  .joins(:allowed_tokens)
12
12
  .where(allowed_tokens: symbolized_payload.slice(:jti, :aud))
13
13
  .where('allowed_tokens.exp > ?', Time.current)
14
- .find_by(email: symbolized_payload[:sub])
14
+ .find_by(AuthRails.identifier_name => symbolized_payload[:sub])
15
15
  end
16
16
 
17
17
  def gen_token(resource:, payload:, exp: nil, secret_key: nil, algorithm: nil)
@@ -8,7 +8,7 @@ module AuthRails
8
8
  symbolized_payload = payload.symbolize_keys
9
9
 
10
10
  AuthRails.resource_class
11
- .find_by(email: symbolized_payload[:sub])
11
+ .find_by(AuthRails.identifier_name => symbolized_payload[:sub])
12
12
  end
13
13
 
14
14
  def gen_token(payload:, exp: nil, secret_key: nil, algorithm: nil, jti: nil, **)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AuthRails
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auth_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alpha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-08 00:00:00.000000000 Z
11
+ date: 2024-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt