Class: RackJwtAegis::ResponseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_jwt_aegis/response_builder.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ResponseBuilder

Returns a new instance of ResponseBuilder.

Since:

  • 0.1.0



7
8
9
# File 'lib/rack_jwt_aegis/response_builder.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#build_error_body(message, status_code) ⇒ Object (private)

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack_jwt_aegis/response_builder.rb', line 43

def build_error_body(message, status_code)
  error_data = {
    error: message,
    status: status_code,
    timestamp: Time.now.iso8601,
  }

  # Add additional context in debug mode
  if @config.debug_mode?
    error_data[:middleware] = 'rack_jwt_aegis'
    error_data[:version] = RackJwtAegis::VERSION
  end

  JSON.generate(error_data)
end

#error_response(message, status_code) ⇒ Object

Since:

  • 0.1.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack_jwt_aegis/response_builder.rb', line 25

def error_response(message, status_code)
  response_body = build_error_body(message, status_code)

  [
    status_code,
    {
      'Content-Type' => 'application/json',
      'Content-Length' => response_body.bytesize.to_s,
      'Cache-Control' => 'no-cache, no-store, must-revalidate',
      'Pragma' => 'no-cache',
      'Expires' => '0',
    },
    [response_body],
  ]
end

#forbidden_response(message = nil) ⇒ Object

Since:

  • 0.1.0



18
19
20
21
22
23
# File 'lib/rack_jwt_aegis/response_builder.rb', line 18

def forbidden_response(message = nil)
  error_response(
    message || @config.forbidden_response[:error] || 'Access denied',
    403,
  )
end

#unauthorized_response(message = nil) ⇒ Object

Since:

  • 0.1.0



11
12
13
14
15
16
# File 'lib/rack_jwt_aegis/response_builder.rb', line 11

def unauthorized_response(message = nil)
  error_response(
    message || @config.unauthorized_response[:error] || 'Authentication required',
    401,
  )
end