refactor(artstudio): consolidate 27 duplicated token-user-id auth checks into a helper (S1192) #677

Closed
opened 2026-07-17 19:24:48 +00:00 by spikerj · 1 comment
Owner

Found via S1192 triage. ArtStudioController repeated the exact same token-user-id auth check in 27 endpoints:

var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userId))
{
    return Unauthorized("User ID not found in token");
}

That is 27 copies of the same auth logic + error message (SonarQube flagged the 'User ID not found in token' literal as duplicated 27x under S1192). It is a DRY/maintainability smell: the claim type, the null handling, and the 401 message can only be changed consistently by editing 27 places.

Fix: extract a private TryGetUserId(out string userId, out ActionResult? error) helper (returns false + a 401 result when the id claim is absent) and replace all 27 blocks with if (!TryGetUserId(out var userId, out var authError)) return authError;. Behavior is byte-for-byte identical — same claim, same message, same 401. All 49 ArtStudioController tests pass; build clean. Net ~80 lines of duplication removed.

Follow-up: ArtStudioPresetsController has the same pattern x4 (deferred to keep this change scoped to one controller). SonarQube rule csharpsquid:S1192.

Found via S1192 triage. ArtStudioController repeated the exact same token-user-id auth check in 27 endpoints: ``` var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (string.IsNullOrEmpty(userId)) { return Unauthorized("User ID not found in token"); } ``` That is 27 copies of the same auth logic + error message (SonarQube flagged the 'User ID not found in token' literal as duplicated 27x under S1192). It is a DRY/maintainability smell: the claim type, the null handling, and the 401 message can only be changed consistently by editing 27 places. **Fix:** extract a private `TryGetUserId(out string userId, out ActionResult? error)` helper (returns false + a 401 result when the id claim is absent) and replace all 27 blocks with `if (!TryGetUserId(out var userId, out var authError)) return authError;`. Behavior is byte-for-byte identical — same claim, same message, same 401. All 49 ArtStudioController tests pass; build clean. Net ~80 lines of duplication removed. Follow-up: ArtStudioPresetsController has the same pattern x4 (deferred to keep this change scoped to one controller). SonarQube rule csharpsquid:S1192.
Author
Owner

Resolved in spikersoft-backend PR #376 (merged to master). Consolidated the 27 duplicated token-user-id auth checks into a TryGetUserId helper; behavior identical, all 49 controller tests pass. Closing.

Resolved in spikersoft-backend PR #376 (merged to master). Consolidated the 27 duplicated token-user-id auth checks into a TryGetUserId helper; behavior identical, all 49 controller tests pass. Closing.
Sign in to join this conversation.