@@ -43,38 +43,111 @@ def handle_mcp_request(request, env)
4343 private
4444
4545 def authenticate_user_from_jwt ( request )
46- auth_header = request . env [ "HTTP_AUTHORIZATION" ]
47- return unless auth_header &.start_with? ( "Bearer " )
48-
49- auth_header . sub ( "Bearer " , "" ) . tap do |jwt_token |
50- FastMcpJwtAuth . log_debug "Extracted JWT token from Authorization header"
46+ extract_jwt_token ( request ) &.tap do |jwt_token |
47+ FastMcpJwtAuth . log_debug "Extracted JWT token from Authorization header (length: #{ jwt_token . length } chars)"
5148 authenticate_user_with_token ( jwt_token )
5249 end
5350 rescue StandardError => e
54- FastMcpJwtAuth . log_warn "JWT token authentication failed: #{ e . message } "
51+ log_authentication_error ( e )
52+ end
53+
54+ def extract_jwt_token ( request )
55+ auth_header = request . env [ "HTTP_AUTHORIZATION" ]
56+ return log_and_return ( "No Authorization header found in request" ) unless auth_header
57+ return log_and_return ( "Authorization header present but not Bearer token format: #{ auth_header [ 0 ..20 ] } ..." ) unless auth_header . start_with? ( "Bearer " )
58+
59+ auth_header . sub ( "Bearer " , "" )
60+ end
61+
62+ def log_and_return ( message )
63+ FastMcpJwtAuth . log_debug message
64+ nil
65+ end
66+
67+ def log_authentication_error ( exception )
68+ FastMcpJwtAuth . log_error "JWT token authentication failed with exception: #{ exception . class . name } - #{ exception . message } "
69+ FastMcpJwtAuth . log_debug "JWT authentication error backtrace: #{ exception . backtrace &.first ( 3 ) &.join ( "; " ) } "
5570 end
5671
5772 def authenticate_user_with_token ( jwt_token )
58- return unless FastMcpJwtAuth . config . jwt_decoder
73+ return FastMcpJwtAuth . log_warn ( "JWT decoder not configured, skipping token authentication" ) unless FastMcpJwtAuth . config . jwt_decoder
74+
75+ decode_and_authenticate_token ( jwt_token )
76+ rescue StandardError => e
77+ FastMcpJwtAuth . log_error "JWT token decoding failed: #{ e . class . name } - #{ e . message } "
78+ end
79+
80+ def decode_and_authenticate_token ( jwt_token )
81+ FastMcpJwtAuth . log_debug "Attempting to decode JWT token"
82+ return FastMcpJwtAuth . log_warn ( "JWT decoder returned nil - token may be invalid or malformed" ) unless ( decoded_token = FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token ) )
5983
60- FastMcpJwtAuth . config . jwt_decoder . call ( jwt_token ) &.then do |decoded_token |
61- return unless token_valid? ( decoded_token )
84+ FastMcpJwtAuth . log_debug "JWT token decoded successfully, checking validity"
85+ return unless token_valid? ( decoded_token )
86+
87+ FastMcpJwtAuth . log_debug "JWT token validation passed, looking up user"
88+ authenticate_found_user ( find_user_from_token ( decoded_token ) )
89+ end
6290
63- find_user_from_token ( decoded_token ) &.tap { |user | assign_current_user ( user ) }
91+ def authenticate_found_user ( user )
92+ if user
93+ FastMcpJwtAuth . log_debug "Setting current user context"
94+ assign_current_user ( user )
95+ FastMcpJwtAuth . log_info "User authentication completed successfully"
96+ else
97+ FastMcpJwtAuth . log_warn "Authentication failed: no user found for token"
6498 end
6599 end
66100
67101 def token_valid? ( decoded_token )
68- return true unless FastMcpJwtAuth . config . token_validator
102+ return log_debug_and_return_true? ( "No token validator configured, considering token valid" ) unless FastMcpJwtAuth . config . token_validator
69103
70- FastMcpJwtAuth . config . token_validator . call ( decoded_token )
104+ validate_decoded_token ( decoded_token )
105+ rescue StandardError => e
106+ FastMcpJwtAuth . log_error "Token validation failed with exception: #{ e . class . name } - #{ e . message } "
107+ false
108+ end
109+
110+ def validate_decoded_token ( decoded_token )
111+ FastMcpJwtAuth . log_debug "Running token validation"
112+ FastMcpJwtAuth . config . token_validator . call ( decoded_token ) . tap do |valid |
113+ log_validation_result ( valid )
114+ end
115+ end
116+
117+ def log_validation_result ( valid )
118+ if valid
119+ FastMcpJwtAuth . log_debug "Token validation passed"
120+ else
121+ FastMcpJwtAuth . log_warn "Token validation failed - validator returned falsy value"
122+ end
123+ end
124+
125+ def log_debug_and_return_true? ( message )
126+ FastMcpJwtAuth . log_debug message
127+ true
71128 end
72129
73130 def find_user_from_token ( decoded_token )
74- return unless FastMcpJwtAuth . config . user_finder
131+ return FastMcpJwtAuth . log_warn ( "User finder not configured, cannot authenticate user" ) unless FastMcpJwtAuth . config . user_finder
132+
133+ lookup_user_from_decoded_token ( decoded_token )
134+ rescue StandardError => e
135+ FastMcpJwtAuth . log_error "User lookup failed with exception: #{ e . class . name } - #{ e . message } "
136+ nil
137+ end
75138
139+ def lookup_user_from_decoded_token ( decoded_token )
140+ FastMcpJwtAuth . log_debug "Looking up user from decoded token"
76141 FastMcpJwtAuth . config . user_finder . call ( decoded_token ) . tap do |user |
77- FastMcpJwtAuth . log_debug "Authenticated user: #{ user } " if user
142+ log_user_lookup_result ( user )
143+ end
144+ end
145+
146+ def log_user_lookup_result ( user )
147+ if user
148+ FastMcpJwtAuth . log_debug "User found successfully: #{ user } "
149+ else
150+ FastMcpJwtAuth . log_warn "User finder returned nil - user may not exist or be inactive"
78151 end
79152 end
80153
0 commit comments