Skip to content

Commit

Permalink
fix: return error if session id does not exist (#1538)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* return error if session id doesn't exist in the db

## What is the current behavior?

Please link any relevant issues here.

## What is the new behavior?

Feel free to include screenshots if it includes visual changes.

## Additional context

Add any other context or screenshots.
  • Loading branch information
kangmingtay committed Apr 15, 2024
1 parent 348a1da commit 91e9eca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ func (a *API) maybeLoadUserOrSession(ctx context.Context) (context.Context, erro
return ctx, forbiddenError(ErrorCodeBadJWT, "invalid claim: session_id claim must be a UUID").WithInternalError(err)
}
session, err = models.FindSessionByID(db, sessionId, false)
if err != nil && !models.IsNotFoundError(err) {
return ctx, forbiddenError(ErrorCodeSessionNotFound, "Session from session_id claim in JWT does not exist")
if err != nil {
if models.IsNotFoundError(err) {
return ctx, forbiddenError(ErrorCodeSessionNotFound, "Session from session_id claim in JWT does not exist")
}
return ctx, err
}
ctx = withSession(ctx, session)
}
Expand Down
13 changes: 13 additions & 0 deletions internal/api/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ func (ts *AuthTestSuite) TestMaybeLoadUserOrSession() {
ExpectedUser: u,
ExpectedSession: s,
},
{
Desc: "Session ID doesn't exist",
UserJwtClaims: &AccessTokenClaims{
StandardClaims: jwt.StandardClaims{
Subject: u.ID.String(),
},
Role: "authenticated",
SessionId: "73bf9ee0-9e8c-453b-b484-09cb93e2f341",
},
ExpectedError: forbiddenError(ErrorCodeSessionNotFound, "Session from session_id claim in JWT does not exist"),
ExpectedUser: u,
ExpectedSession: nil,
},
}

for _, c := range cases {
Expand Down

0 comments on commit 91e9eca

Please sign in to comment.