-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:Beetrack/rack-es-logger into dev…
…elop
- Loading branch information
Showing
7 changed files
with
75 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'jwt' | ||
require 'es_logger/configuration' | ||
|
||
module EsLogger | ||
class Response | ||
def self.call(env) | ||
request = ::Rack::Request.new(env) | ||
is_json = request&.media_type&.downcase == 'application/json' | ||
body_stream = is_json ? request.body.read : nil | ||
request.body.rewind if body_stream | ||
JWT_REGEX = Regexp.new('^(Bearer\s)?(?<token>[a-zA-Z0-9\-_.]+)$') | ||
|
||
def self.call(env) | ||
payload = { | ||
remote_address: env['REMOTE_ADDR'], | ||
request_method: env['REQUEST_METHOD'], | ||
path: env['PATH_INFO'], | ||
query_string_params: ::Rack::Utils.parse_nested_query(env['QUERY_STRING']), | ||
params: is_json && body_stream.length.positive? ? JSON.parse(body_stream) : nil | ||
params: request_params(env), | ||
headers: http_headers(env).to_json, | ||
authorization: decode_jwt_token(env), | ||
query_string_params: query_string(env) | ||
} | ||
|
||
controller = env['action_controller.instance'] | ||
return payload unless (controller = env['action_controller.instance']) | ||
|
||
payload.merge!( | ||
'controller_name' => controller.controller_name, | ||
'controller_action' => controller.action_name, | ||
'content_type' => controller.content_type || :none | ||
) | ||
end | ||
|
||
def self.query_string(env) | ||
::Rack::Utils.parse_nested_query(env['QUERY_STRING']) | ||
end | ||
|
||
def self.request_params(env) | ||
request = ::Rack::Request.new(env) | ||
return unless request&.media_type | ||
|
||
is_json = request.media_type.downcase == 'application/json' | ||
return unless is_json | ||
|
||
request.body.rewind if (body_stream = request.body.read) | ||
return unless body_stream.length.positive? | ||
|
||
JSON.parse(body_stream) | ||
end | ||
|
||
def self.http_headers(env) | ||
env.select { |k, _| k.to_s.start_with? 'HTTP_' } | ||
end | ||
|
||
def self.decode_jwt_token(env) | ||
jwt = EsLogger.configuration.jwt | ||
return if jwt.nil? | ||
|
||
if controller | ||
payload.merge!( | ||
'controller_name' => controller.controller_name, | ||
'controller_action' => controller.action_name, | ||
'content_type' => controller.content_type || :none | ||
) | ||
end | ||
jwt_key = "HTTP_#{jwt.upcase}" | ||
jwt_value = env[jwt_key].to_s.match(JWT_REGEX)[:token] | ||
return if jwt_value.nil? | ||
|
||
payload | ||
JWT.decode(jwt_value, nil, false).first | ||
rescue JWT::DecodeError | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module EsLogger | ||
VERSION = '1.3.3' | ||
VERSION = '1.5.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters