Skip to content

Commit

Permalink
PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike12345567 committed Mar 16, 2023
1 parent a42bdf1 commit ce86edf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 12 additions & 0 deletions packages/backend-core/src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export abstract class BudibaseError extends Error {
export enum ErrorCode {
USAGE_LIMIT_EXCEEDED = "usage_limit_exceeded",
FEATURE_DISABLED = "feature_disabled",
INVALID_API_KEY = "invalid_api_key",
HTTP = "http",
}

Expand Down Expand Up @@ -85,3 +86,14 @@ export class FeatureDisabledError extends HTTPError {
}
}
}

// AUTH

export class InvalidAPIKeyError extends BudibaseError {
constructor() {
super(
"Invalid API key - may need re-generated, or user doesn't exist",
ErrorCode.INVALID_API_KEY
)
}
}
9 changes: 3 additions & 6 deletions packages/backend-core/src/middleware/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { decrypt } from "../security/encryption"
import * as identity from "../context/identity"
import env from "../environment"
import { Ctx, EndpointMatcher } from "@budibase/types"
import { InvalidAPIKeyError, ErrorCode } from "../errors"

const ONE_MINUTE = env.SESSION_UPDATE_PERIOD
? parseInt(env.SESSION_UPDATE_PERIOD)
Expand Down Expand Up @@ -68,11 +69,7 @@ async function checkApiKey(apiKey: string, populateUser?: Function) {
user: await getUser(userId, tenantId, populateUser),
}
} else {
throw {
message:
"Invalid API key - may need re-generated, or user doesn't exist",
name: "InvalidApiKey",
}
throw new InvalidAPIKeyError()
}
})
}
Expand Down Expand Up @@ -175,7 +172,7 @@ export default function (
// invalid token, clear the cookie
if (err?.name === "JsonWebTokenError") {
clearCookie(ctx, Cookie.Auth)
} else if (err?.name === "InvalidApiKey") {
} else if (err?.code === ErrorCode.INVALID_API_KEY) {
ctx.throw(403, err.message)
}
// allow configuring for public access
Expand Down

0 comments on commit ce86edf

Please sign in to comment.