Skip to content

Commit

Permalink
Consistency with gw.applyPoliciesAndSave
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric authored and titpetric committed Aug 17, 2023
1 parent 9e10598 commit 792aa50
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState,
if apiSpec == nil {
logger.WithField("api_id", apiId).Warn("Can't find active API, storing anyway")
}
gw.checkAndApplyTrialPeriod(keyName, newSession, isHashed)

if apiSpec != nil {
gw.checkAndApplyTrialPeriod(keyName, newSession, isHashed)
}

// Lets reset keys if they are edited by admin
if apiSpec == nil || !apiSpec.DontSetQuotasOnCreate {
Expand All @@ -351,11 +354,11 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState,
gw.GlobalSessionManager.ResetQuota(keyName, newSession, isHashed)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
}

// apply polices (if any) and save key
if err := gw.applyPoliciesAndSave(keyName, newSession, apiSpec, isHashed); err != nil {
return err
}
// apply polices (if any) and save key
if err := gw.applyPoliciesAndSave(keyName, newSession, apiSpec, isHashed); err != nil {
return err
}
}
} else {
Expand Down Expand Up @@ -1970,9 +1973,7 @@ func (gw *Gateway) createKeyHandler(w http.ResponseWriter, r *http.Request) {
newSession.LastUpdated = strconv.Itoa(int(time.Now().Unix()))
newSession.DateCreated = time.Now()

mw := BaseMiddleware{Gw: gw}
// TODO: handle apply policies error
mw.ApplyPolicies(newSession)
sessionManager := gw.GlobalSessionManager

if len(newSession.AccessRights) > 0 {
// reset API-level limit to nil if any has a zero-value
Expand All @@ -1981,28 +1982,18 @@ func (gw *Gateway) createKeyHandler(w http.ResponseWriter, r *http.Request) {
apiSpec := gw.getApiSpec(apiID)
if apiSpec != nil {
gw.checkAndApplyTrialPeriod(newKey, newSession, false)
// If we have enabled HMAC checking for keys, we need to generate a secret for the client to use
if !apiSpec.DontSetQuotasOnCreate {
// Reset quota by default
gw.GlobalSessionManager.ResetQuota(newKey, newSession, false)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
// apply polices (if any) and save key
if err := gw.applyPoliciesAndSave(newKey, newSession, apiSpec, false); err != nil {
doJSONWrite(w, http.StatusInternalServerError, apiError("Failed to create key - "+err.Error()))
return
}
} else {
// Use fallback
sessionManager := gw.GlobalSessionManager
}

if apiSpec == nil || !apiSpec.DontSetQuotasOnCreate {
// Reset quota by default
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
sessionManager.ResetQuota(newKey, newSession, false)
// apply polices (if any) and save key
err := sessionManager.UpdateSession(newKey, newSession, -1, false)
if err != nil {
doJSONWrite(w, http.StatusInternalServerError, apiError("Failed to create key - "+err.Error()))
return
}
}

// apply polices (if any) and save key
if err := gw.applyPoliciesAndSave(newKey, newSession, apiSpec, false); err != nil {
doJSONWrite(w, http.StatusInternalServerError, apiError("Failed to create key - "+err.Error()))
return
}
}
} else {
Expand All @@ -2024,8 +2015,8 @@ func (gw *Gateway) createKeyHandler(w http.ResponseWriter, r *http.Request) {
for _, spec := range gw.apisByID {
gw.checkAndApplyTrialPeriod(newKey, newSession, false)
if !spec.DontSetQuotasOnCreate {
// Reset quote by default
gw.GlobalSessionManager.ResetQuota(newKey, newSession, false)
// Reset quota by default
sessionManager.ResetQuota(newKey, newSession, false)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
if err := gw.applyPoliciesAndSave(newKey, newSession, spec, false); err != nil {
Expand Down

0 comments on commit 792aa50

Please sign in to comment.