build endpoints
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
module WarpEngine
|
||||
# Token-hitelesítés a publikáló (/build/*) endpointokhoz.
|
||||
# A hitelesítési forrás kizárólagos: :database módban a shared secret nem
|
||||
# érvényes, :env módban a DB-tokenek nem.
|
||||
module UpdateAuthentication
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
attr_reader :current_application_token
|
||||
|
||||
# A token kizárólag az X-Update-Secret headerből jöhet — URL-ben a secret
|
||||
# proxy- és access-logokba szivárogna.
|
||||
def update_authorized?(required_scope:)
|
||||
token = request.headers["X-Update-Secret"].presence
|
||||
return false if token.blank?
|
||||
|
||||
case WarpEngine.config.application_token_source
|
||||
when :database then database_token_authorized?(token, required_scope)
|
||||
else env_secret_authorized?(token)
|
||||
end
|
||||
end
|
||||
|
||||
def env_secret_authorized?(token)
|
||||
expected = WarpEngine.config.update_secret
|
||||
# Konfigurálatlan secret esetén az endpoint zárva marad.
|
||||
expected.present? && ActiveSupport::SecurityUtils.secure_compare(token, expected)
|
||||
end
|
||||
|
||||
def database_token_authorized?(token, required_scope)
|
||||
if WarpEngine.config.application_token_owner_class.blank?
|
||||
Rails.logger.error("[#{self.class.name}] application_token_source=:database, de application_token_owner_class nincs beállítva — minden kérés elutasítva")
|
||||
return false
|
||||
end
|
||||
|
||||
record = WarpEngine::ApplicationToken.authenticate(token, required_scope: required_scope)
|
||||
return false if record.nil?
|
||||
|
||||
record.touch_last_used!
|
||||
@current_application_token = record
|
||||
true
|
||||
end
|
||||
|
||||
# Owner-kényszer: csak :database módban (van token) és bekapcsolt
|
||||
# enforce_software_ownership mellett szűr. Owner nélküli software a
|
||||
# backfillig szabad préda — a kényszer bekapcsolása előtt kell backfillelni.
|
||||
def software_ownership_authorized?(name)
|
||||
return true unless WarpEngine.config.enforce_software_ownership
|
||||
|
||||
token = current_application_token
|
||||
return true if token.nil? || token.unrestricted?
|
||||
|
||||
software = WarpEngine::Software.find_by(name: name)
|
||||
return true if software.nil? || software.owner_id.nil?
|
||||
|
||||
software.owner_type == token.owner_type && software.owner_id == token.owner_id
|
||||
end
|
||||
|
||||
# Az először publikált (vagy backfill előtti, gazdátlan) software a beküldő
|
||||
# token ownerét kapja. Unrestricted (belső) token nem foglal ownert.
|
||||
def claim_software_ownership(name)
|
||||
token = current_application_token
|
||||
return if token.nil? || token.unrestricted?
|
||||
|
||||
software = WarpEngine::Software.find_by(name: name)
|
||||
return if software.nil? || software.owner_id.present?
|
||||
|
||||
software.update_columns(owner_type: token.owner_type, owner_id: token.owner_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user