Skip to content

Commit fc46268

Browse files
committed
chore: improve code style
fix minors codacy issues
1 parent bdf68ee commit fc46268

File tree

13 files changed

+458
-478
lines changed

13 files changed

+458
-478
lines changed

app/controllers/api/v1/feedbacks_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def vote
5959

6060
def set_feedback
6161
# Feedback is a public board — all authenticated users can vote on any item.
62-
# Intentionally unscoped. nosemgrep: ruby.rails.security.brakeman.check-unscoped-find.check-unscoped-find
63-
@feedback = Feedback.find(params[:id])
62+
# Intentionally cross-org: users vote on any feedback regardless of their org.
63+
# nosemgrep: ruby.rails.security.brakeman.check-unscoped-find.check-unscoped-find
64+
@feedback = Feedback.find(params[:id]) # brakeman:ignore:UnscopedFind
6465
end
6566

6667
def feedback_params

app/controllers/concerns/authenticatable.rb

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,61 @@ module Authenticatable
1414

1515
private
1616

17-
def authenticate_request! # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17+
def authenticate_request!
1818
token = extract_token_from_header
1919

2020
if token.nil?
2121
render_unauthorized('Missing authentication token')
2222
return
2323
end
2424

25-
begin
26-
@jwt_payload = JwtService.decode(token)
27-
28-
# Reject refresh tokens used as access tokens.
29-
# Access tokens for users carry type: 'access'.
30-
# Access tokens for players carry entity_type: 'player' AND type: 'access'.
31-
# Refresh tokens carry type: 'refresh' and must never authenticate a request.
32-
unless valid_access_token_type?(@jwt_payload)
33-
raise JwtService::TokenInvalidError, 'Invalid token type'
34-
end
35-
36-
if @jwt_payload[:entity_type] == 'player'
37-
# ── Player token ──────────────────────────────────────────────────────
38-
# Free agents (auto-cadastro via ArenaBR) têm organization_id: nil
39-
@current_player = Player.unscoped.find(@jwt_payload[:player_id])
40-
41-
org_id = @jwt_payload[:organization_id]
42-
@current_organization = org_id.present? ? Organization.find(org_id) : nil
43-
44-
Current.organization_id = @current_organization&.id
45-
org_label = @current_organization&.id || 'free_agent'
46-
Rails.logger.info("[AUTH] Player token: player_id=#{@current_player.id} org=#{org_label}")
47-
return
48-
end
49-
50-
# ── Regular user token ────────────────────────────────────────────────
51-
# Bypass RLS for authentication queries - we need to find the user before we can set RLS context
52-
@current_user = User.unscoped.find(@jwt_payload[:user_id])
53-
@current_organization = @current_user.organization
54-
55-
# Set request-scoped attributes for OrganizationScoped models (thread-safe)
56-
Current.organization_id = @current_organization.id
57-
Current.user_id = @current_user.id
58-
Current.user_role = @current_user.role
59-
60-
# Debug log in production to verify Current is being set
61-
Rails.logger.info("[AUTH] Set Current.organization_id=#{Current.organization_id} for user #{@current_user.email}")
62-
63-
# Update last login time (uses update_column which skips callbacks/audit logs)
64-
@current_user.update_last_login! if should_update_last_login?
65-
rescue JwtService::AuthenticationError => e
66-
Rails.logger.error("JWT Authentication error: #{e.class} - #{e.message}")
67-
render_unauthorized(e.message)
68-
rescue ActiveRecord::RecordNotFound => e
69-
Rails.logger.error("User not found during authentication: #{e.message}")
70-
render_unauthorized('User not found')
71-
rescue StandardError => e
72-
Rails.logger.error("Unexpected authentication error: #{e.class} - #{e.message}")
73-
Rails.logger.error(e.backtrace.join("\n"))
74-
render json: {
75-
error: {
76-
code: 'INTERNAL_ERROR',
77-
message: 'An internal error occurred'
78-
}
79-
}, status: :internal_server_error
25+
perform_authentication(token)
26+
end
27+
28+
def perform_authentication(token)
29+
@jwt_payload = JwtService.decode(token)
30+
31+
# Reject refresh tokens used as access tokens.
32+
# Refresh tokens carry type: 'refresh' and must never authenticate a request.
33+
raise JwtService::TokenInvalidError, 'Invalid token type' unless valid_access_token_type?(@jwt_payload)
34+
35+
if @jwt_payload[:entity_type] == 'player'
36+
authenticate_player_token
37+
else
38+
authenticate_user_token
8039
end
40+
rescue JwtService::AuthenticationError => e
41+
Rails.logger.error("JWT Authentication error: #{e.class} - #{e.message}")
42+
render_unauthorized(e.message)
43+
rescue ActiveRecord::RecordNotFound => e
44+
Rails.logger.error("User not found during authentication: #{e.message}")
45+
render_unauthorized('User not found')
46+
rescue StandardError => e
47+
Rails.logger.error("Unexpected authentication error: #{e.class} - #{e.message}")
48+
Rails.logger.error(e.backtrace.join("\n"))
49+
render json: { error: { code: 'INTERNAL_ERROR', message: 'An internal error occurred' } },
50+
status: :internal_server_error
51+
end
52+
53+
def authenticate_player_token
54+
# Free agents (auto-cadastro via ArenaBR) têm organization_id: nil
55+
@current_player = Player.unscoped.find(@jwt_payload[:player_id])
56+
org_id = @jwt_payload[:organization_id]
57+
@current_organization = org_id.present? ? Organization.find(org_id) : nil
58+
Current.organization_id = @current_organization&.id
59+
org_label = @current_organization&.id || 'free_agent'
60+
Rails.logger.info("[AUTH] Player token: player_id=#{@current_player.id} org=#{org_label}")
61+
end
62+
63+
def authenticate_user_token
64+
# Bypass RLS for authentication queries - we need to find the user before we can set RLS context
65+
@current_user = User.unscoped.find(@jwt_payload[:user_id])
66+
@current_organization = @current_user.organization
67+
Current.organization_id = @current_organization.id
68+
Current.user_id = @current_user.id
69+
Current.user_role = @current_user.role
70+
Rails.logger.info("[AUTH] Set Current.organization_id=#{Current.organization_id} for user #{@current_user.email}")
71+
@current_user.update_last_login! if should_update_last_login?
8172
end
8273

8374
def extract_token_from_header

app/modules/admin/controllers/status_incidents_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ def require_admin_access
111111
end
112112

113113
def set_incident
114-
# StatusIncidents are platform-wide (not org-scoped) — intentionally unscoped.
115-
# This endpoint requires admin or owner role (see require_admin_access before_action).
116-
# nosemgrep: ruby.rails.security.brakeman.check-unscoped-find
117-
@incident = StatusIncident.find(params[:id])
114+
@incident = StatusIncident.find(params[:id]) # brakeman:ignore:UnscopedFind # nosemgrep
118115
end
119116

120117
def create_params

0 commit comments

Comments
 (0)