Skip to content

Commit

Permalink
swagger fixups (#252)
Browse files Browse the repository at this point in the history
* πŸ€πŸ’» fix url for swagger ui
* πŸ” auth fixups for validating bearer token
* πŸ€πŸ” typo in error msg
  • Loading branch information
Flip-Liquid committed Apr 23, 2024
1 parent 941bdab commit 9d3f38b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion spec/oas_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
name: MIT
url: https://opensource.org/licenses/MIT
servers:
- url: vote.optimisim.io/api/v1
- url: https://vote.optimism.io/api/v1
description: Base URL for optimism production
security:
- bearerAuth: []
Expand Down
13 changes: 10 additions & 3 deletions src/app/lib/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { validate as validateUuid } from "uuid";
let prismaModule: any;

const HASH_FN = "sha256";
const REASON_NO_TOKEN = "No token provided in 'authorization' header";
const REASON_NO_TOKEN = "No token provided in 'Authorization' header";
const REASON_INVALID_API_KEY = "Invalid API Key";
const REASON_DISABLED_USER = "User disabled";

Expand All @@ -15,8 +15,15 @@ export type AuthResponse = {
reason?: string;
};

export function extractBearerToken(token?: string | null) {
if (token && token.split(" ")[0] === "Bearer") {
return token.split(" ")[1];
}
return null;
}

export function hasApiKey(request: NextRequest): AuthResponse {
const token = request.headers.get("authorization");
const token = extractBearerToken(request.headers.get("Authorization"));
let authResponse: AuthResponse = { authenticated: true, reason: "" };

if (!token) {
Expand Down Expand Up @@ -52,7 +59,7 @@ export async function authenticateApiUser(

let authResponse: AuthResponse = hasApiKey(request);

const key = request.headers.get("authorization");
const key = extractBearerToken(request.headers.get("Authorization"));

if (!key) {
return authResponse;
Expand Down

0 comments on commit 9d3f38b

Please sign in to comment.