standard_id 0.3.1 → 0.3.2

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: a164420e3b36d754be94711721ead8cb4d29efed42fd6e75b0060e1c8c7c3bce
4
- data.tar.gz: cded0bf7e847cc77ed0f6aa7c69647435c847d62ae02c2af1466778a507f88f4
3
+ metadata.gz: 0f0a6bc5ee9c7ea15e8f9ea7bee06f7cb080da52fade31a85bb0d0ead5207552
4
+ data.tar.gz: 8dffbedfccd7f2434fbf77befe544ebb0c2dc55e4bbbbfb105c11254543e8103
5
5
  SHA512:
6
- metadata.gz: 0b9b506d8014bd8d8cd380b1f5300a87d2d6825e073e8b959c67cbfeac3eb577abe671c9dac330833c8d27ee010b87786f533ac4e247459d691dd956d5053faf
7
- data.tar.gz: ec1a9973e61d08fff4579099dbcac25ad8355d140994c51c2621559d4aed39486d2fdd891b451ade775799f792c3db19916d555d3a50f3bc84d07ed77c1f0a52
6
+ metadata.gz: dd96b17327a9468bd2128dea33826450009949e73b6579e36f7c422cd5f9e8aa36a1201de4019e523f79f709539a2cc2e4f8c92dabb15fdef0505488fb3c9cb1
7
+ data.tar.gz: 74e0c84b638ecb2ab7dbc7f7303617e8b2614fc62f9c8218f33da27136f267f41f6ca676afdff2197395eff5410b538fc186f3c9c9f1c7148cd43bc653b22b57
@@ -2,6 +2,12 @@ module StandardId
2
2
  module ApiAuthentication
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ if StandardId.config.alias_current_user
7
+ define_method(:current_user) { current_account }
8
+ end
9
+ end
10
+
5
11
  delegate :current_session, :current_account, :revoke_current_session!, to: :session_manager
6
12
 
7
13
  private
@@ -0,0 +1,36 @@
1
+ module StandardId
2
+ # Sets Sentry user context from the current authenticated account.
3
+ #
4
+ # This is a standalone concern that host apps can include in their
5
+ # ApplicationController to automatically set Sentry user context
6
+ # for each request. It eliminates the need for apps to write
7
+ # their own SentryContext boilerplate.
8
+ #
9
+ # Safe to include even when the Sentry gem is not installed -- the
10
+ # callback is a no-op if `Sentry` is not defined.
11
+ #
12
+ # @example
13
+ # class ApplicationController < ActionController::Base
14
+ # include StandardId::WebAuthentication
15
+ # include StandardId::SentryContext
16
+ # end
17
+ module SentryContext
18
+ extend ActiveSupport::Concern
19
+
20
+ included do
21
+ before_action :set_standard_id_sentry_context
22
+ end
23
+
24
+ private
25
+
26
+ def set_standard_id_sentry_context
27
+ return unless defined?(Sentry)
28
+ return unless respond_to?(:current_account, true) && current_account.present?
29
+
30
+ context = { id: current_account.id }
31
+ context[:session_id] = current_session.id if respond_to?(:current_session, true) && current_session.present?
32
+
33
+ Sentry.set_user(context)
34
+ end
35
+ end
36
+ end
@@ -12,7 +12,7 @@ module StandardId
12
12
  return unless defined?(::Current)
13
13
 
14
14
  ::Current.request_id = request.request_id if ::Current.respond_to?(:request_id=)
15
- ::Current.ip_address = request.remote_ip if ::Current.respond_to?(:ip_address=)
15
+ ::Current.ip_address = StandardId::Utils::IpNormalizer.normalize(request.remote_ip) if ::Current.respond_to?(:ip_address=)
16
16
  ::Current.user_agent = request.user_agent if ::Current.respond_to?(:user_agent=)
17
17
  end
18
18
  end
@@ -5,6 +5,11 @@ module StandardId
5
5
  included do
6
6
  include StandardId::InertiaSupport
7
7
  helper_method :current_account, :authenticated?
8
+
9
+ if StandardId.config.alias_current_user
10
+ define_method(:current_user) { current_account }
11
+ helper_method :current_user
12
+ end
8
13
  end
9
14
 
10
15
  delegate :current_session, :current_account, :revoke_current_session!, to: :session_manager
@@ -19,7 +19,7 @@ module StandardId
19
19
  target: email,
20
20
  code: generate_otp_code,
21
21
  expires_at: 10.minutes.from_now,
22
- ip_address: request.remote_ip,
22
+ ip_address: StandardId::Utils::IpNormalizer.normalize(request.remote_ip),
23
23
  user_agent: request.user_agent
24
24
  )
25
25
 
@@ -19,7 +19,7 @@ module StandardId
19
19
  target: phone,
20
20
  code: generate_otp_code,
21
21
  expires_at: 10.minutes.from_now,
22
- ip_address: request.remote_ip,
22
+ ip_address: StandardId::Utils::IpNormalizer.normalize(request.remote_ip),
23
23
  user_agent: request.user_agent
24
24
  )
25
25
 
@@ -12,9 +12,7 @@ module StandardId
12
12
 
13
13
  def current_account
14
14
  return unless current_session
15
- @current_account ||= StandardId.account_class
16
- .find_by(id: current_session.account_id)
17
- &.tap { |a| a.strict_loading!(false) }
15
+ @current_account ||= load_current_account
18
16
  end
19
17
 
20
18
  def revoke_current_session!
@@ -28,6 +26,12 @@ module StandardId
28
26
 
29
27
  private
30
28
 
29
+ def load_current_account
30
+ scope = StandardId.account_class
31
+ scope = StandardId.config.account_scope.call(scope) if StandardId.config.account_scope
32
+ scope.find_by(id: current_session.account_id)&.tap { |a| a.strict_loading!(false) }
33
+ end
34
+
31
35
  def load_current_session
32
36
  return @current_session if @current_session.present?
33
37
 
@@ -10,7 +10,7 @@ module StandardId
10
10
  def create_device_session(account, device_id: nil, device_agent: nil)
11
11
  StandardId::DeviceSession.create!(
12
12
  account:,
13
- ip_address: @request.remote_ip,
13
+ ip_address: StandardId::Utils::IpNormalizer.normalize(@request.remote_ip),
14
14
  device_id: device_id || SecureRandom.uuid,
15
15
  device_agent: device_agent || @request.user_agent,
16
16
  expires_at: StandardId::DeviceSession.expiry
@@ -21,7 +21,7 @@ module StandardId
21
21
  StandardId::ServiceSession.create!(
22
22
  account:,
23
23
  owner:,
24
- ip_address: @request.remote_ip,
24
+ ip_address: StandardId::Utils::IpNormalizer.normalize(@request.remote_ip),
25
25
  service_name:,
26
26
  service_version:,
27
27
  metadata: metadata || {},
@@ -14,8 +14,10 @@ StandardConfig.schema.draw do
14
14
  field :issuer, type: :string, default: nil
15
15
  field :login_url, type: :string, default: nil
16
16
  field :allowed_post_logout_redirect_uris, type: :array, default: []
17
+ field :account_scope, type: :any, default: nil
17
18
  field :use_inertia, type: :boolean, default: false
18
19
  field :inertia_component_namespace, type: :string, default: "standard_id"
20
+ field :alias_current_user, type: :boolean, default: false
19
21
  end
20
22
 
21
23
  scope :events do
@@ -35,7 +35,7 @@ module StandardId
35
35
  target: username,
36
36
  code: code,
37
37
  expires_at: StandardId.config.passwordless.code_ttl.seconds.from_now,
38
- ip_address: request.remote_ip,
38
+ ip_address: StandardId::Utils::IpNormalizer.normalize(request.remote_ip),
39
39
  user_agent: request.user_agent
40
40
  )
41
41
  cc
@@ -0,0 +1,16 @@
1
+ module StandardId
2
+ module Utils
3
+ class IpNormalizer
4
+ IPV6_LOCALHOST = "::1"
5
+ IPV4_LOCALHOST = "127.0.0.1"
6
+
7
+ class << self
8
+ def normalize(ip)
9
+ return ip unless ip == IPV6_LOCALHOST
10
+
11
+ IPV4_LOCALHOST
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -15,7 +15,7 @@ module StandardId
15
15
  end
16
16
 
17
17
  def current_account
18
- Current.account ||= current_session&.account&.tap { |a| a.strict_loading!(false) }
18
+ Current.account ||= load_current_account
19
19
  end
20
20
 
21
21
  def sign_in_account(account)
@@ -50,6 +50,19 @@ module StandardId
50
50
 
51
51
  private
52
52
 
53
+ def load_current_account
54
+ if StandardId.config.account_scope
55
+ account_id = current_session&.account_id
56
+ return unless account_id
57
+
58
+ scope = StandardId.account_class
59
+ scope = StandardId.config.account_scope.call(scope)
60
+ scope.find_by(id: account_id)&.tap { |a| a.strict_loading!(false) }
61
+ else
62
+ current_session&.account&.tap { |a| a.strict_loading!(false) }
63
+ end
64
+ end
65
+
53
66
  def load_current_session
54
67
  Current.session ||= load_session_from_session_token
55
68
  Current.session ||= load_session_from_remember_token
@@ -10,7 +10,7 @@ module StandardId
10
10
  def create_browser_session(account)
11
11
  StandardId::BrowserSession.create!(
12
12
  account: account,
13
- ip_address: request.remote_ip,
13
+ ip_address: StandardId::Utils::IpNormalizer.normalize(request.remote_ip),
14
14
  user_agent: request.user_agent,
15
15
  expires_at: StandardId::BrowserSession.expiry
16
16
  )
data/lib/standard_id.rb CHANGED
@@ -40,6 +40,7 @@ require "standard_id/passwordless/base_strategy"
40
40
  require "standard_id/passwordless/email_strategy"
41
41
  require "standard_id/passwordless/sms_strategy"
42
42
  require "standard_id/utils/callable_parameter_filter"
43
+ require "standard_id/utils/ip_normalizer"
43
44
 
44
45
  require "concurrent/delay"
45
46
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim
@@ -84,6 +84,7 @@ files:
84
84
  - app/controllers/concerns/standard_id/inertia_rendering.rb
85
85
  - app/controllers/concerns/standard_id/inertia_support.rb
86
86
  - app/controllers/concerns/standard_id/passwordless_strategy.rb
87
+ - app/controllers/concerns/standard_id/sentry_context.rb
87
88
  - app/controllers/concerns/standard_id/set_current_request_details.rb
88
89
  - app/controllers/concerns/standard_id/social_authentication.rb
89
90
  - app/controllers/concerns/standard_id/web/social_login_params.rb
@@ -208,6 +209,7 @@ files:
208
209
  - lib/standard_id/provider_registry.rb
209
210
  - lib/standard_id/providers/base.rb
210
211
  - lib/standard_id/utils/callable_parameter_filter.rb
212
+ - lib/standard_id/utils/ip_normalizer.rb
211
213
  - lib/standard_id/version.rb
212
214
  - lib/standard_id/web/authentication_guard.rb
213
215
  - lib/standard_id/web/session_manager.rb