Skip to content

Commit 35866bf

Browse files
committed
Cache invalid API key
1 parent e99d1f1 commit 35866bf

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

api/pkg/middlewares/http_request_logger_middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func HTTPRequestLogger(tracer telemetry.Tracer, logger telemetry.Logger) fiber.H
2222

2323
statusCode := c.Response().StatusCode()
2424
span.AddEvent(fmt.Sprintf("finished handling request with traceID: [%s], statusCode: [%d]", span.SpanContext().TraceID().String(), statusCode))
25-
if statusCode >= 300 && len(c.Request().Body()) > 0 {
25+
if statusCode >= 300 && len(c.Request().Body()) > 0 && statusCode != 401 {
2626
ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("http.status [%d], body [%s]", statusCode, string(c.Request().Body()))))
2727
}
2828

api/pkg/repositories/gorm_user_repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,16 @@ func (repository *gormUserRepository) LoadAuthContext(ctx context.Context, apiKe
154154
defer span.End()
155155

156156
if authUser, found := repository.cache.Get(apiKey); found {
157+
if authUser.IsNoop() {
158+
return authUser, repository.tracer.WrapErrorSpan(span, stacktrace.NewError(fmt.Sprintf("user with api key [%s] does not exist", apiKey)))
159+
}
157160
return authUser, nil
158161
}
159162

160163
user := new(entities.User)
161164
err := repository.db.WithContext(ctx).Where("api_key = ?", apiKey).First(user).Error
162165
if errors.Is(err, gorm.ErrRecordNotFound) {
166+
repository.cache.SetWithTTL(apiKey, entities.AuthContext{}, 1, 2*time.Hour)
163167
msg := fmt.Sprintf("user with api key [%s] does not exist", apiKey)
164168
return entities.AuthContext{}, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg))
165169
}

0 commit comments

Comments
 (0)