mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-04-27 08:19:34 -05:00
Cosmetic improvement
This commit is contained in:
+6
-14
@@ -73,23 +73,15 @@ type Server struct {
|
||||
var _ puzzle.Engine = (*Server)(nil)
|
||||
|
||||
type apiKeyOwnerSource struct {
|
||||
Store db.Implementor
|
||||
key *dbgen.APIKey
|
||||
Store db.Implementor
|
||||
cachedKey *dbgen.APIKey
|
||||
}
|
||||
|
||||
var _ puzzle.OwnerIDSource = (*apiKeyOwnerSource)(nil)
|
||||
|
||||
func (a *apiKeyOwnerSource) cachedApiKey(ctx context.Context) (*dbgen.APIKey, bool) {
|
||||
if a.key != nil {
|
||||
return a.key, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (a *apiKeyOwnerSource) apiKey(ctx context.Context) (*dbgen.APIKey, error) {
|
||||
if apiKey, ok := ctx.Value(common.APIKeyContextKey).(*dbgen.APIKey); ok {
|
||||
a.key = apiKey
|
||||
a.cachedKey = apiKey
|
||||
return apiKey, nil
|
||||
}
|
||||
|
||||
@@ -97,7 +89,7 @@ func (a *apiKeyOwnerSource) apiKey(ctx context.Context) (*dbgen.APIKey, error) {
|
||||
// this is the "postponed" DB access mentioned in APIKey() middleware
|
||||
key, err := a.Store.Impl().RetrieveAPIKey(ctx, secret)
|
||||
if err != nil {
|
||||
a.key = key
|
||||
a.cachedKey = key
|
||||
}
|
||||
return key, err
|
||||
}
|
||||
@@ -440,7 +432,7 @@ func (s *Server) recaptchaVerifyHandler(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if apiKey, ok := ownerSource.cachedApiKey(ctx); ok {
|
||||
if apiKey := ownerSource.cachedKey; apiKey != nil {
|
||||
// if we are not cached, then we will recheck via "delayed" mechanism of OwnerIDSource
|
||||
// when rate limiting is cleaned up (due to inactivity) we should still be able to access on defaults
|
||||
interval := float64(time.Second) / apiKey.RequestsPerSecond
|
||||
@@ -491,7 +483,7 @@ func (s *Server) pcVerifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if apiKey, ok := ownerSource.cachedApiKey(ctx); ok {
|
||||
if apiKey := ownerSource.cachedKey; apiKey != nil {
|
||||
// if we are not cached, then we will recheck via "delayed" mechanism of OwnerIDSource
|
||||
// when rate limiting is cleaned up (due to inactivity) we should still be able to access on defaults
|
||||
interval := float64(time.Second) / apiKey.RequestsPerSecond
|
||||
|
||||
Reference in New Issue
Block a user