module WarpEngine::AdminApi::Authenticator
Public Instance Methods
Source
# File lib/warp_engine/admin_api/authenticator.rb, line 39 def bearer_token(request) header = request.headers["Authorization"].to_s return nil unless header.start_with?("Bearer ") header.delete_prefix("Bearer ").strip.presence end
The token from the Authorization: Bearer header of request, or nil.
Source
# File lib/warp_engine/admin_api/authenticator.rb, line 11 def call(request) configured = AdminApi.config.authenticator principal = case configured when nil, :application_token, "application_token" then from_application_token(request) else configured.call(request) end principal.is_a?(Principal) ? principal : nil rescue StandardError => e Rails.logger.error("[WarpEngine::AdminApi] authenticator #{e.class}: #{e.message}") nil end
Authenticates request with the configured Configuration#authenticator: the default :application_token uses from_application_token, anything else is called with the request. Returns the Principal, or nil when the request is not authenticated or the authenticator raises (the error is logged).
# File lib/warp_engine/admin_api/authenticator.rb, line 26 def from_application_token(request) token = bearer_token(request) return nil if token.blank? record = ApplicationToken.authenticate(token) return nil if record.nil? || !record.admin? return nil unless record.owner_type == AdminApi.config.owner_class_name record.touch_last_used! Principal.from_token(record) end
Authenticates the bearer token of request as an admin WarpEngine::ApplicationToken owned by the configured owner class. Updates the tokenβs last used time and returns its Principal, or nil.