@@ -8,30 +8,20 @@ module FastMcpJwtAuth
88 module RackTransportPatch
99 @patch_applied = false
1010
11- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1211 def self . apply_patch!
13- if @patch_applied
14- FastMcpJwtAuth . logger &.debug "FastMcpJwtAuth: RackTransport patch already applied, skipping"
15- return
16- end
17-
18- unless defined? ( FastMcp ::Transports ::RackTransport )
19- FastMcpJwtAuth . logger &.debug "FastMcpJwtAuth: FastMcp::Transports::RackTransport not defined yet, skipping patch"
20- return
21- end
22-
23- unless FastMcpJwtAuth . config . enabled
24- FastMcpJwtAuth . logger &.debug "FastMcpJwtAuth: JWT authentication disabled, skipping patch"
25- return
26- end
12+ return FastMcpJwtAuth . log_debug ( "RackTransport patch already applied, skipping" ) if @patch_applied
13+ return FastMcpJwtAuth . log_debug ( "FastMcp::Transports::RackTransport not defined yet, skipping patch" ) unless defined? ( FastMcp ::Transports ::RackTransport )
14+ return FastMcpJwtAuth . log_debug ( "JWT authentication disabled, skipping patch" ) unless FastMcpJwtAuth . config . enabled
2715
28- FastMcpJwtAuth . logger &.info "FastMcpJwtAuth: Applying JWT authentication patch to FastMcp::Transports::RackTransport"
16+ apply_patch_to_transport
17+ end
2918
19+ def self . apply_patch_to_transport
20+ FastMcpJwtAuth . log_info "Applying JWT authentication patch to FastMcp::Transports::RackTransport"
3021 patch_transport_class
3122 @patch_applied = true
32- FastMcpJwtAuth . logger &. info "FastMcpJwtAuth: JWT authentication patch applied successfully"
23+ FastMcpJwtAuth . log_info " JWT authentication patch applied successfully"
3324 end
34- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
3525
3626 def self . patch_transport_class
3727 FastMcp ::Transports ::RackTransport . prepend ( JwtAuthenticationPatch )
@@ -56,24 +46,22 @@ def authenticate_user_from_jwt(request)
5646 auth_header = request . env [ "HTTP_AUTHORIZATION" ]
5747 return unless auth_header &.start_with? ( "Bearer " )
5848
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 )
49+ auth_header . sub ( "Bearer " , "" ) . tap do | jwt_token |
50+ FastMcpJwtAuth . log_debug " Extracted JWT token from Authorization header"
51+ authenticate_user_with_token ( jwt_token )
52+ end
6353 rescue StandardError => e
64- FastMcpJwtAuth . logger &. warn "FastMcpJwtAuth: JWT token authentication failed: #{ e . message } "
54+ FastMcpJwtAuth . log_warn " JWT token authentication failed: #{ e . message } "
6555 end
6656
6757 def authenticate_user_with_token ( jwt_token )
6858 return unless FastMcpJwtAuth . config . jwt_decoder
6959
70- decoded_token = FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token )
71- return unless decoded_token
72-
73- return unless token_valid? ( decoded_token )
60+ FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token ) &.then do |decoded_token |
61+ return unless token_valid? ( decoded_token )
7462
75- user = find_user_from_token ( decoded_token )
76- assign_current_user ( user ) if user
63+ find_user_from_token ( decoded_token ) &. tap { | user | assign_current_user ( user ) }
64+ end
7765 end
7866
7967 def token_valid? ( decoded_token )
@@ -85,9 +73,9 @@ def token_valid?(decoded_token)
8573 def find_user_from_token ( decoded_token )
8674 return unless FastMcpJwtAuth . config . user_finder
8775
88- user = FastMcpJwtAuth . config . user_finder . call ( decoded_token )
89- FastMcpJwtAuth . logger &. debug "FastMcpJwtAuth: Authenticated user: #{ user } " if user
90- user
76+ FastMcpJwtAuth . config . user_finder . call ( decoded_token ) . tap do | user |
77+ FastMcpJwtAuth . log_debug " Authenticated user: #{ user } " if user
78+ end
9179 end
9280
9381 def assign_current_user ( user )
0 commit comments