@@ -8,28 +8,30 @@ module FastMcpJwtAuth
88 module RackTransportPatch
99 @patch_applied = false
1010
11+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1112 def self . apply_patch!
1213 if @patch_applied
13- FastMcpJwtAuth . log_debug " RackTransport patch already applied, skipping"
14+ FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: RackTransport patch already applied, skipping"
1415 return
1516 end
1617
1718 unless defined? ( FastMcp ::Transports ::RackTransport )
18- FastMcpJwtAuth . log_debug " FastMcp::Transports::RackTransport not defined yet, skipping patch"
19+ FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: FastMcp::Transports::RackTransport not defined yet, skipping patch"
1920 return
2021 end
2122
2223 unless FastMcpJwtAuth . config . enabled
23- FastMcpJwtAuth . log_debug " JWT authentication disabled, skipping patch"
24+ FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: JWT authentication disabled, skipping patch"
2425 return
2526 end
2627
27- FastMcpJwtAuth . log_info " Applying JWT authentication patch to FastMcp::Transports::RackTransport"
28+ FastMcpJwtAuth . logger &. info "FastMcpJwtAuth: Applying JWT authentication patch to FastMcp::Transports::RackTransport"
2829
2930 patch_transport_class
3031 @patch_applied = true
31- FastMcpJwtAuth . log_info " JWT authentication patch applied successfully"
32+ FastMcpJwtAuth . logger &. info "FastMcpJwtAuth: JWT authentication patch applied successfully"
3233 end
34+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
3335
3436 def self . patch_transport_class
3537 FastMcp ::Transports ::RackTransport . prepend ( JwtAuthenticationPatch )
@@ -54,22 +56,24 @@ def authenticate_user_from_jwt(request)
5456 auth_header = request . env [ "HTTP_AUTHORIZATION" ]
5557 return unless auth_header &.start_with? ( "Bearer " )
5658
57- auth_header . sub ( "Bearer " , "" ) . tap do | jwt_token |
58- FastMcpJwtAuth . log_debug " Extracted JWT token from Authorization header"
59- authenticate_user_with_token ( jwt_token )
60- end
59+ jwt_token = auth_header . sub ( "Bearer " , "" )
60+ FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: Extracted JWT token from Authorization header"
61+
62+ authenticate_user_with_token ( jwt_token )
6163 rescue StandardError => e
62- FastMcpJwtAuth . log_warn " JWT token authentication failed: #{ e . message } "
64+ FastMcpJwtAuth . logger &. warn "FastMcpJwtAuth: JWT token authentication failed: #{ e . message } "
6365 end
6466
6567 def authenticate_user_with_token ( jwt_token )
6668 return unless FastMcpJwtAuth . config . jwt_decoder
6769
68- FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token ) &.tap do |decoded_token |
69- next unless token_valid? ( decoded_token )
70+ decoded_token = FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token )
71+ return unless decoded_token
72+
73+ return unless token_valid? ( decoded_token )
7074
71- find_user_from_token ( decoded_token ) &. tap { | user | assign_current_user ( user ) }
72- end
75+ user = find_user_from_token ( decoded_token )
76+ assign_current_user ( user ) if user
7377 end
7478
7579 def token_valid? ( decoded_token )
@@ -81,9 +85,9 @@ def token_valid?(decoded_token)
8185 def find_user_from_token ( decoded_token )
8286 return unless FastMcpJwtAuth . config . user_finder
8387
84- FastMcpJwtAuth . config . user_finder . call ( decoded_token ) &. tap do | user |
85- FastMcpJwtAuth . log_debug " Authenticated user: #{ user } "
86- end
88+ user = FastMcpJwtAuth . config . user_finder . call ( decoded_token )
89+ FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: Authenticated user: #{ user } " if user
90+ user
8791 end
8892
8993 def assign_current_user ( user )
0 commit comments