From b5151b9e7322247692dd392223f9d0e09c0ca487 Mon Sep 17 00:00:00 2001 From: Bodacious Date: Fri, 16 Aug 2024 13:23:10 +0100 Subject: [PATCH] Move decision to update the license outside of the license validation service. Signed-off-by: Bodacious --- .../v1/licenses/actions/validations_controller.rb | 11 ++++++++--- app/services/license_validation_service.rb | 15 ++------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/v1/licenses/actions/validations_controller.rb b/app/controllers/api/v1/licenses/actions/validations_controller.rb index f65ae973a6..9571bcfb7f 100644 --- a/app/controllers/api/v1/licenses/actions/validations_controller.rb +++ b/app/controllers/api/v1/licenses/actions/validations_controller.rb @@ -14,14 +14,14 @@ def quick_validate_by_id # FIXME(ezekg) Skipping :touch on origin is not a good idea, since # the origin header can be set by anybody. - valid, detail, code = LicenseValidationService.call(license: license, scope: false, skip_touch: request.headers['origin'] == 'https://app.keygen.sh') + valid, detail, code = LicenseValidationService.call(license: license, scope: false) meta = { ts: Time.current, # Included so customer has a signed ts to utilize elsewhere valid:, detail:, code:, } - + license.persist_last_validated_attributes! unless origin_is_keygen? Keygen.logger.info "[license.quick-validate] account_id=#{current_account.id} license_id=#{license&.id} validation_valid=#{valid} validation_detail=#{detail} validation_code=#{code}" Current.resource = license if @@ -73,6 +73,7 @@ def validate_by_id detail:, code:, } + license.persist_last_validated_attributes! if nonce = validation_meta[:nonce] meta[:nonce] = nonce @@ -155,7 +156,7 @@ def validate_by_key detail:, code:, } - + license.persist_last_validated_attributes! if nonce = validation_meta[:nonce] meta[:nonce] = nonce end @@ -191,5 +192,9 @@ def set_license Current.resource = license end + + def origin_is_keygen? + request.headers['origin'] == 'https://app.keygen.sh' + end end end diff --git a/app/services/license_validation_service.rb b/app/services/license_validation_service.rb index 03437767a8..a184733f80 100644 --- a/app/services/license_validation_service.rb +++ b/app/services/license_validation_service.rb @@ -2,19 +2,16 @@ class LicenseValidationService < BaseService - def initialize(license:, scope: nil, skip_touch: false) + def initialize(license:, scope: nil) @account = license&.account @product = license&.product @license = license @scope = scope - @skip_touch = skip_touch self.license.set_last_validated_attributes(last_validated_at: Time.current) end def call - res = validate! - touch! unless skip_touch? - res + validate! end private @@ -24,8 +21,6 @@ def call :license, :scope - def skip_touch? = !!@skip_touch - def validate! return [false, "does not exist", :NOT_FOUND] if license.nil? @@ -412,10 +407,4 @@ def validate! # All good return [true, "is valid", :VALID] end - - def touch! - return if skip_touch? || license.nil? - - license.persist_last_validated_attributes! - end end