Class: Railspress::Api::V1::AgentKeyExchangesController

Inherits:
BaseController
  • Object
show all
Includes:
ActionController::HttpAuthentication::Token::ControllerMethods
Defined in:
app/controllers/railspress/api/v1/agent_key_exchanges_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_key

Instance Method Summary collapse

Instance Method Details

#createObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/railspress/api/v1/agent_key_exchanges_controller.rb', line 12

def create
  api_key, plain_api_token = current_agent_bootstrap_key.exchange!(ip_address: request.remote_ip)

  render json: {
    data: {
      api_key: {
        id: api_key.id,
        name: api_key.name,
        token: plain_api_token,
        expires_at: api_key.expires_at
      },
      bootstrap: {
        id: current_agent_bootstrap_key.id,
        used_at: current_agent_bootstrap_key.used_at
      }
    }
  }, status: :created
rescue Railspress::AgentBootstrapKey::ExchangeError
  render_error("Unauthorized", status: :unauthorized)
rescue ActiveRecord::RecordInvalid => e
  render_validation_errors(e.record)
end