Skip to content

Commit

Permalink
fix: check if refresh token exists
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn committed Nov 11, 2024
1 parent d2f2428 commit 452a1a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions go/pkg/repos/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ func (a *Auth) DeleteRefreshToken(ctx context.Context, refreshToken string) erro
_, err := orm.Auths(orm.AuthWhere.RefreshToken.EQ(null.StringFrom(refreshToken))).UpdateAll(ctx, a.db, orm.M{orm.AuthColumns.RefreshToken: nil})
return err
}

func (a *Auth) RefreshTokenExists(ctx context.Context, refreshToken string) (bool, error) {
return orm.Auths(orm.AuthWhere.RefreshToken.EQ(null.StringFrom(refreshToken))).Exists(ctx, a.db)
}
12 changes: 11 additions & 1 deletion go/rpc/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,20 @@ func (h *handler) RefreshToken(ctx context.Context, _ *connect.Request[v1.Refres

refreshToken, ok := ctx.Value(jwt.ContextKeyRefreshToken).(string)
if !ok {
log.Warn("refresh token not found")
log.Warn("refresh token not provided")
return nil, connect.NewError(connect.CodeUnauthenticated, http.ErrNoCookie)
}

exists, err := h.repo.RefreshTokenExists(ctx, refreshToken)
if err != nil {
log.Error("refresh token check failed", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, errors.New(""))
}
if !exists {
log.Warn("refresh token not found")
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("refresh token not found"))
}

claims, err := h.jwt.ClaimsFromToken(refreshToken, jwt.TokenTypeRefresh)
if err != nil {
log.Error("token parsing failed", zap.Error(err))
Expand Down

0 comments on commit 452a1a4

Please sign in to comment.