Skip to content

Commit

Permalink
Move decision to update the license outside of the license validation…
Browse files Browse the repository at this point in the history
… service.

Signed-off-by: Bodacious <[email protected]>
  • Loading branch information
Bodacious committed Jan 26, 2025
1 parent 113eb71 commit b5151b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +73,7 @@ def validate_by_id
detail:,
code:,
}
license.persist_last_validated_attributes!

if nonce = validation_meta[:nonce]
meta[:nonce] = nonce
Expand Down Expand Up @@ -155,7 +156,7 @@ def validate_by_key
detail:,
code:,
}

license.persist_last_validated_attributes!
if nonce = validation_meta[:nonce]
meta[:nonce] = nonce
end
Expand Down Expand Up @@ -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
15 changes: 2 additions & 13 deletions app/services/license_validation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?

Expand Down Expand Up @@ -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

0 comments on commit b5151b9

Please sign in to comment.