Class: BetterAuth::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/context.rb

Defined Under Namespace

Classes: DirectAPIRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Context

Returns a new instance of Context.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/better_auth/context.rb', line 19

def initialize(configuration)
  @app_name = configuration.app_name
  @canonical_base_url = configuration.context_base_url
  @base_url = @canonical_base_url
  @version = BetterAuth::VERSION
  @options = configuration
  @social_providers = configuration.social_providers
  @auth_cookies = Cookies.get_cookies(configuration)
  @cookies = @auth_cookies
  @adapter = configuration.database
  @internal_adapter = nil
  @logger = configuration.logger
  @session_config = configuration.session
  @rate_limit_config = configuration.rate_limit
  @trusted_origins = configuration.trusted_origins
  @explicit_trusted_origins = configuration.explicit_trusted_origins
  @secret = configuration.secret
  @secret_config = configuration.secret_config
  @current_session = nil
  @new_session = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



151
152
153
154
155
156
# File 'lib/better_auth/context.rb', line 151

def method_missing(name, *arguments, &block)
  variable_name = :"@#{name}"
  return instance_variable_get(variable_name) if arguments.empty? && instance_variable_defined?(variable_name)

  super
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def adapter
  @adapter
end

#app_nameObject (readonly)

Returns the value of attribute app_name.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def app_name
  @app_name
end

#canonical_base_urlObject (readonly)

Returns the value of attribute canonical_base_url.



55
56
57
# File 'lib/better_auth/context.rb', line 55

def canonical_base_url
  @canonical_base_url
end

#internal_adapterObject (readonly)

Returns the value of attribute internal_adapter.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def internal_adapter
  @internal_adapter
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def options
  @options
end

#rate_limit_configObject (readonly)

Returns the value of attribute rate_limit_config.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def rate_limit_config
  @rate_limit_config
end

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def secret
  @secret
end

#secret_configObject (readonly)

Returns the value of attribute secret_config.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def secret_config
  @secret_config
end

#session_configObject (readonly)

Returns the value of attribute session_config.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def session_config
  @session_config
end

#social_providersObject (readonly)

Returns the value of attribute social_providers.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def social_providers
  @social_providers
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/better_auth/context.rb', line 7

def version
  @version
end

Instance Method Details

#apply_plugin_context!(attributes) ⇒ Object



135
136
137
138
139
# File 'lib/better_auth/context.rb', line 135

def apply_plugin_context!(attributes)
  normalize_context(attributes).each do |key, value|
    instance_variable_set("@#{key}", value) if plugin_context_attribute?(key)
  end
end

#auth_cookiesObject



65
66
67
# File 'lib/better_auth/context.rb', line 65

def auth_cookies
  runtime_fetch(:auth_cookies, @auth_cookies)
end

#base_urlObject



47
48
49
# File 'lib/better_auth/context.rb', line 47

def base_url
  runtime_fetch(:base_url, @base_url)
end

#clear_runtime!Object



184
185
186
# File 'lib/better_auth/context.rb', line 184

def clear_runtime!
  Thread.current[runtime_key] = nil
end

#cookiesObject



69
70
71
# File 'lib/better_auth/context.rb', line 69

def cookies
  runtime_fetch(:cookies, @cookies)
end


123
124
125
# File 'lib/better_auth/context.rb', line 123

def create_auth_cookie(cookie_name, override_attributes = {})
  Cookies.create_cookie(options, cookie_name.to_s, override_attributes)
end

#current_sessionObject



73
74
75
# File 'lib/better_auth/context.rb', line 73

def current_session
  runtime_fetch(:current_session, @current_session)
end

#explicit_trusted_originsObject



61
62
63
# File 'lib/better_auth/context.rb', line 61

def explicit_trusted_origins
  runtime_fetch(:explicit_trusted_origins, @explicit_trusted_origins)
end

#new_sessionObject



77
78
79
# File 'lib/better_auth/context.rb', line 77

def new_session
  runtime_fetch(:new_session, @new_session)
end

#passwordObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/better_auth/context.rb', line 98

def password
  config = {
    min_password_length: options.email_and_password[:min_password_length],
    max_password_length: options.email_and_password[:max_password_length]
  }
  password_config = options.email_and_password[:password] || {}

  {
    config: config,
    hash: ->(value) { Password.hash(value, hasher: password_config[:hash], algorithm: options.password_hasher) },
    verify: lambda do |password:, hash:|
      Password.verify(
        password: password,
        hash: hash,
        verifier: password_config[:verify],
        algorithm: options.password_hasher
      )
    end,
    check_password: lambda do |value|
      length = value.to_s.length
      length.between?(config[:min_password_length].to_i, config[:max_password_length].to_i)
    end
  }
end

#prepare_for_api_call!(source) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/better_auth/context.rb', line 170

def prepare_for_api_call!(source)
  runtime = request_runtime
  runtime[:current_session] = nil
  runtime[:new_session] = nil
  runtime[:base_url] = serving_base_url(source)
  runtime[:trusted_origins] = current_trusted_origins(request_for_callbacks(source))
end

#prepare_for_request!(request) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/better_auth/context.rb', line 162

def prepare_for_request!(request)
  runtime = request_runtime
  runtime[:current_session] = nil
  runtime[:new_session] = nil
  runtime[:base_url] = serving_base_url(request)
  runtime[:trusted_origins] = current_trusted_origins(request)
end

#refresh_from_options!Object



141
142
143
144
145
146
147
148
149
# File 'lib/better_auth/context.rb', line 141

def refresh_from_options!
  @social_providers = options.social_providers
  @session_config = options.session
  @rate_limit_config = options.rate_limit
  @trusted_origins = options.trusted_origins
  @explicit_trusted_origins = options.explicit_trusted_origins
  @secret = options.secret
  @secret_config = options.secret_config
end

#reset_runtime!Object



178
179
180
181
182
# File 'lib/better_auth/context.rb', line 178

def reset_runtime!
  Thread.current[runtime_key] = nil if request_runtime?
  @current_session = nil
  @new_session = nil
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/better_auth/context.rb', line 158

def respond_to_missing?(name, include_private = false)
  instance_variable_defined?(:"@#{name}") || super
end

#run_in_background(task) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/better_auth/context.rb', line 89

def run_in_background(task)
  handler = options.advanced.dig(:background_tasks, :handler)
  if handler.respond_to?(:call)
    handler.call(task)
  elsif task.respond_to?(:call)
    task.call
  end
end

#set_adapter(adapter) ⇒ Object



127
128
129
# File 'lib/better_auth/context.rb', line 127

def set_adapter(adapter)
  @adapter = adapter
end

#set_current_session(session) ⇒ Object



85
86
87
# File 'lib/better_auth/context.rb', line 85

def set_current_session(session)
  runtime_store(:current_session, session) || @current_session = session
end

#set_internal_adapter(adapter) ⇒ Object



131
132
133
# File 'lib/better_auth/context.rb', line 131

def set_internal_adapter(adapter)
  @internal_adapter = adapter
end

#set_new_session(session) ⇒ Object



81
82
83
# File 'lib/better_auth/context.rb', line 81

def set_new_session(session)
  runtime_store(:new_session, session) || @new_session = session
end


51
52
53
# File 'lib/better_auth/context.rb', line 51

def token_link_base_url
  base_url
end

#trusted_origin?(url, allow_relative_paths: false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/better_auth/context.rb', line 41

def trusted_origin?(url, allow_relative_paths: false)
  trusted_origins.any? do |origin|
    Configuration.matches_origin_pattern?(url, origin, allow_relative_paths: allow_relative_paths)
  end
end

#trusted_originsObject



57
58
59
# File 'lib/better_auth/context.rb', line 57

def trusted_origins
  runtime_fetch(:trusted_origins, @trusted_origins)
end