Skip to content

Commit

Permalink
add api key expired message to the 403 when an api key is expired/inv…
Browse files Browse the repository at this point in the history
…alid (#532)
  • Loading branch information
ykeremy authored Jun 30, 2024
1 parent 6a6119b commit 27da621
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 10 additions & 9 deletions skyvern/forge/sdk/db/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,18 +566,19 @@ async def validate_org_auth_token(
organization_id: str,
token_type: OrganizationAuthTokenType,
token: str,
valid: bool | None = True,
) -> OrganizationAuthToken | None:
try:
async with self.Session() as session:
if token_obj := (
await session.scalars(
select(OrganizationAuthTokenModel)
.filter_by(organization_id=organization_id)
.filter_by(token_type=token_type)
.filter_by(token=token)
.filter_by(valid=True)
)
).first():
query = (
select(OrganizationAuthTokenModel)
.filter_by(organization_id=organization_id)
.filter_by(token_type=token_type)
.filter_by(token=token)
)
if valid is not None:
query = query.filter_by(valid=valid)
if token_obj := (await session.scalars(query)).first():
return convert_to_organization_auth_token(token_obj)
else:
return None
Expand Down
7 changes: 7 additions & 0 deletions skyvern/forge/sdk/services/org_auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,20 @@ async def _get_current_org_cached(x_api_key: str, db: AgentDB) -> Organization:
organization_id=organization.organization_id,
token_type=OrganizationAuthTokenType.api,
token=x_api_key,
valid=None,
)
if not api_key_db_obj:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid credentials",
)

if api_key_db_obj.valid is False:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Your API key has expired. Please retrieve the latest one from https://app.skyvern.com/settings",
)

# set organization_id in skyvern context and log context
context = skyvern_context.current()
if context:
Expand Down

0 comments on commit 27da621

Please sign in to comment.