-
Notifications
You must be signed in to change notification settings - Fork 0
feat(security): Move API Key to Cookies #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
211cddb
fix(security): move api key localstorage to cookies
Ayush8923 ce5a5f9
fix(*): fix the keystore routing if the user is not logged in
Ayush8923 adee97e
fix(*): handle few edge cases
Ayush8923 195d137
fix(security): code clenaps and coderabbit suggestions
Ayush8923 8dac10b
fix(security): code clenaps and reviewers suggestions
Ayush8923 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { apiClient } from "@/app/lib/apiClient"; | ||
| import { clearApiKeyCookies, setApiKeyCookies } from "@/app/lib/authCookie"; | ||
| import type { AddApiKeyRequest, ApiKeyMeta } from "@/app/lib/types/credentials"; | ||
|
|
||
| function maskKey(key: string): string { | ||
| const tail = key.slice(-4); | ||
| return `${"•".repeat(8)}${tail}`; | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| let body: AddApiKeyRequest; | ||
| try { | ||
| body = (await request.json()) as AddApiKeyRequest; | ||
| } catch { | ||
| return NextResponse.json( | ||
| { error: "Invalid request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const key = body.key?.trim(); | ||
| const label = body.label?.trim(); | ||
|
|
||
| if (!key || !label) { | ||
| return NextResponse.json( | ||
| { error: "Both a label and an API key are required" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const verifyRequest = new Request(request.url, { | ||
| headers: { | ||
| "X-API-KEY": key, | ||
| Cookie: request.headers.get("Cookie") || "", | ||
| }, | ||
| }); | ||
|
|
||
| let status: number; | ||
| try { | ||
| ({ status } = await apiClient(verifyRequest, "/api/v1/apikeys/verify")); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { error: "Failed to reach backend for API key verification" }, | ||
| { status: 502 }, | ||
| ); | ||
| } | ||
|
|
||
| if (status < 200 || status >= 300) { | ||
| return NextResponse.json( | ||
| { error: "Invalid API key. Please check the key and try again." }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
|
Ayush8923 marked this conversation as resolved.
|
||
|
|
||
| const meta: ApiKeyMeta = { | ||
| id: crypto.randomUUID(), | ||
| label, | ||
| masked: maskKey(key), | ||
| createdAt: new Date().toISOString(), | ||
| }; | ||
|
|
||
| const res = NextResponse.json({ data: meta }, { status: 200 }); | ||
| setApiKeyCookies(res, key, meta); | ||
| return res; | ||
| } | ||
|
Ayush8923 marked this conversation as resolved.
|
||
|
|
||
| export async function DELETE() { | ||
| const res = NextResponse.json({ success: true }, { status: 200 }); | ||
| clearApiKeyCookies(res); | ||
| return res; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.