Class: BetterAuth::Router

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, endpoints, rate_limiter: RateLimiter.new) ⇒ Router

Returns a new instance of Router.



11
12
13
14
15
16
# File 'lib/better_auth/router.rb', line 11

def initialize(context, endpoints, rate_limiter: RateLimiter.new)
  @context = context
  @endpoints = endpoints
  @rate_limiter = rate_limiter
  @origin_check = Middleware::OriginCheck.new
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/better_auth/router.rb', line 9

def context
  @context
end

#endpointsObject (readonly)

Returns the value of attribute endpoints.



9
10
11
# File 'lib/better_auth/router.rb', line 9

def endpoints
  @endpoints
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



9
10
11
# File 'lib/better_auth/router.rb', line 9

def rate_limiter
  @rate_limiter
end

Class Method Details

.check_endpoint_conflicts(options, logger) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/better_auth/router.rb', line 18

def self.check_endpoint_conflicts(options, logger)
  registry = Hash.new { |hash, key| hash[key] = [] }
  options.plugins.each do |plugin|
    plugin.fetch(:endpoints, {}).each do |key, endpoint|
      next unless endpoint.respond_to?(:path) && endpoint.path

      registry[endpoint.path] << {
        plugin_id: plugin[:id].to_s,
        endpoint_key: key.to_s,
        methods: endpoint.methods
      }
    end
  end

  conflicts = registry.filter_map do |path, entries|
    conflict_methods = conflicting_methods(entries)
    next if conflict_methods.empty?

    {
      path: path,
      plugins: entries.map { |entry| entry[:plugin_id] }.uniq,
      methods: conflict_methods
    }
  end

  return if conflicts.empty?

  message = "Endpoint path conflicts detected! Multiple plugins are trying to use the same endpoint paths with conflicting HTTP methods:\n"
  message += conflicts.map do |conflict|
    "  - \"#{conflict[:path]}\" [#{conflict[:methods].join(", ")}] used by plugins: #{conflict[:plugins].join(", ")}"
  end.join("\n")
  log(logger, :error, message)
end

Instance Method Details

#call(env) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/better_auth/router.rb', line 52

def call(env)
  request = Rack::Request.new(env)
  context.prepare_for_request!(request) if context.respond_to?(:prepare_for_request!)

  route_path = route_path_for(request.path_info)
  return not_found unless route_path

  query = parse_query(request)
  endpoint, params, allowed_methods = find_endpoint(route_path, request.request_method)
  return run_on_response_chain(not_found) unless endpoint
  return run_on_response_chain(method_not_allowed(allowed_methods)) unless endpoint.matches_method?(request.request_method)
  return run_on_response_chain(unsupported_media_type) unless allowed_media_type?(request, endpoint)

  body = parse_body(request)
  endpoint_context = build_endpoint_context(request, route_path, query, body, params)

  response = @origin_check.call(endpoint_context)
  return run_on_response_chain(response) if response

  response = run_plugin_middlewares(endpoint_context)
  return run_on_response_chain(response) if response

  return run_on_response_chain(not_found) if disabled_path?(route_path)

  request = run_on_request_chain(request)
  return run_on_response_chain(request) if rack_response?(request)

  response = rate_limiter.call(request, context, route_path)
  return run_on_response_chain(response) if response

  endpoint_context = rebuild_endpoint_context(endpoint_context, request, route_path, params)
  result = API.new(context, endpoints).execute(endpoint, endpoint_context)
  response = result.response.is_a?(APIError) ? error_response(result.response, headers: result.headers) : result.to_rack_response
  run_on_response_chain(response)
rescue APIError => error
  error_response(error)
rescue JSON::ParserError
  error_response(APIError.new("BAD_REQUEST", message: "Invalid JSON body"))
end