Skip to content

fix: authenticates swagger ui to access secured endpoints#122

Merged
semmet95 merged 2 commits into
mainfrom
fix/swagger-ui-auth
May 29, 2026
Merged

fix: authenticates swagger ui to access secured endpoints#122
semmet95 merged 2 commits into
mainfrom
fix/swagger-ui-auth

Conversation

@semmet95
Copy link
Copy Markdown
Contributor

@semmet95 semmet95 commented May 29, 2026

Summary by CodeRabbit

  • New Features

    • Enhanced Swagger UI with automatic bearer token authorization support.
  • Improvements

    • Client-ID header now defaults to "swagger-ui" for API requests.
    • Token endpoint removed from API documentation view for clarity.
    • Improved Swagger UI rendering and error handling.

Review Change Stack

Signed-off-by: Amit Singh <singhamitch@outlook.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

Warning

Review limit reached

@semmet95, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 33 minutes and 43 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1e48fbaa-3e2c-41ad-a744-d137622d0d68

📥 Commits

Reviewing files that changed from the base of the PR and between 7abdbf0 and 282e5bd.

📒 Files selected for processing (1)
  • pkg/handlers/swagger-ui.html
📝 Walkthrough

Walkthrough

This PR refactors the Swagger UI endpoint to use an embedded HTML file instead of an inlined template. It adds OpenAPI spec runtime loading with automatic authorization, an auto-authorize plugin that fetches auth tokens and preauthorizes requests, and a request interceptor that injects the Client-ID header. The API spec now includes a default Client-ID value.

Changes

Swagger UI with Auto-Authorization

Layer / File(s) Summary
OpenAPI Client-ID header default
api/source-score.yaml
The ClientIDHeader parameter definition adds a default: swagger-ui value.
Swagger UI HTML page with auto-authorization
pkg/handlers/swagger-ui.html
New HTML page loads Swagger UI from CDN, fetches the OpenAPI spec from /swagger/spec, modifies it to remove the /auth/token endpoint, implements an AutoAuthorizePlugin that calls /auth/token with the Client-ID header to fetch a bearer token and preauthorize API requests, and registers a requestInterceptor to inject the Client-ID header for all token endpoint calls.
Go handler embedding and serving
pkg/handlers/swagger.go
Imports the embed package, embeds swagger-ui.html into a package-level variable, and updates ServeUI to return the embedded content as HTML instead of constructing an inline template.

Sequence Diagram

sequenceDiagram
  participant Browser as Browser/User
  participant SwaggerUI as Swagger UI Bundle
  participant Backend as Backend API
  Browser->>SwaggerUI: Load page
  SwaggerUI->>Backend: GET /swagger/spec
  Backend-->>SwaggerUI: OpenAPI spec (YAML)
  SwaggerUI->>SwaggerUI: Parse spec and remove /auth/token path
  Browser->>SwaggerUI: Click Authorize button
  SwaggerUI->>Backend: POST /auth/token (with Client-ID: swagger-ui)
  Backend-->>SwaggerUI: {token: "..."}
  SwaggerUI->>SwaggerUI: preauthorizeApiKey(BearerAuth, token)
  SwaggerUI->>Browser: Show authorization success
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A swagger of specs takes flight so neat,
With tokens fetched and headers sweet,
The rabbit's UI, now embeds with care,
Auto-auth magic floating in the air! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main objective of the changeset, which is to enable Swagger UI authentication for accessing secured endpoints.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/swagger-ui-auth

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@semmet95 semmet95 marked this pull request as ready for review May 29, 2026 16:09
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/handlers/swagger-ui.html`:
- Around line 38-39: Update the script tag in pkg/handlers/swagger-ui.html that
loads https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js to
include a Subresource Integrity attribute: compute the SHA-512 hash for that
exact minified file and add integrity="sha512-..." plus crossorigin="anonymous"
(keep existing referrerpolicy if present) so the script matches the other
swagger-ui-dist scripts using integrity; locate the script by the src URL and
add the integrity attribute with the exact computed hash for js-yaml.min.js
v4.1.0.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8404914f-9c75-4da3-965f-f8ffec27b485

📥 Commits

Reviewing files that changed from the base of the PR and between 29f60d8 and 7abdbf0.

📒 Files selected for processing (3)
  • api/source-score.yaml
  • pkg/handlers/swagger-ui.html
  • pkg/handlers/swagger.go

Comment thread pkg/handlers/swagger-ui.html
Comment thread pkg/handlers/swagger-ui.html
@semmet95
Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai

@cubic-dev-ai
Copy link
Copy Markdown

cubic-dev-ai Bot commented May 29, 2026

@cubic-dev-ai

@semmet95 I have started the AI code review. It will take a few minutes to complete.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread pkg/handlers/swagger-ui.html
Signed-off-by: Amit Singh <singhamitch@outlook.com>
@semmet95 semmet95 force-pushed the fix/swagger-ui-auth branch from 7d2dcfe to 282e5bd Compare May 29, 2026 16:35
@semmet95 semmet95 merged commit be76f3a into main May 29, 2026
4 checks passed
@semmet95 semmet95 deleted the fix/swagger-ui-auth branch May 29, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant