Class: Braintrust::Server::Middleware::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/braintrust/server/middleware/auth.rb

Overview

Auth middleware that validates requests using a pluggable strategy. Sets env with the authentication result on success.

Instance Method Summary collapse

Constructor Details

#initialize(app, strategy:) ⇒ Auth

Returns a new instance of Auth.



11
12
13
14
# File 'lib/braintrust/server/middleware/auth.rb', line 11

def initialize(app, strategy:)
  @app = app
  @strategy = strategy
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/braintrust/server/middleware/auth.rb', line 16

def call(env)
  auth_result = @strategy.authenticate(env)
  unless auth_result
    return [401, {"content-type" => "application/json"},
      [JSON.dump({"error" => "Unauthorized"})]]
  end

  env["braintrust.auth"] = auth_result
  @app.call(env)
end