Add a server-side API for avatar and header images: uploads are cropped with Vips (400x400 and 1500x500, GIFs passed through, EXIF stripped, transparency flattened to white) and stored as one pending attachment per field. Saving the profile flushes them to the configured Blossom server, authorized by a user-signed kind-24242 token whose x tags cover the stored hashes.
110 lines
3.7 KiB
Ruby
110 lines
3.7 KiB
Ruby
module Settings
|
|
class NostrMediaController < ApplicationController
|
|
before_action :authenticate_user!
|
|
|
|
AUTH_KIND = 24242
|
|
CLOCK_SKEW = 5.minutes
|
|
EXPIRATION_WINDOW = 24.hours
|
|
MAX_TAGS = 20
|
|
MAX_TAG_BYTES = 512
|
|
RATE_LIMIT = 6
|
|
RATE_WINDOW = 5.minutes
|
|
|
|
def create
|
|
return render_error("This account has no Nostr public key", :forbidden) if current_user.nostr_pubkey.blank?
|
|
return render_error("Media server is not configured", :unprocessable_entity) if BlossomManagerService.server_url.blank?
|
|
return render_error("Too many upload attempts. Please try again later.", :too_many_requests) if rate_limited?
|
|
|
|
auth_event = verified_auth_event(params[:signed_event])
|
|
return render_error("Invalid authorization", :unprocessable_entity) if auth_event.nil?
|
|
|
|
hashes = authorized_hashes(auth_event)
|
|
uploads = []
|
|
|
|
pending_images.each do |field, attachment|
|
|
bytes = attachment.blob.download
|
|
sha256 = Digest::SHA256.hexdigest(bytes)
|
|
next unless hashes.include?(sha256)
|
|
|
|
descriptor = BlossomManager::UploadBlob.call(
|
|
bytes: bytes, content_type: attachment.blob.content_type, sha256: sha256, auth_event: auth_event
|
|
)
|
|
return render_error("Upload failed", :bad_gateway) if descriptor.nil?
|
|
|
|
uploads << { field: field, url: descriptor["url"] }
|
|
end
|
|
|
|
render json: { uploads: uploads }
|
|
rescue ActionController::ParameterMissing, ArgumentError, TypeError, JSON::ParserError
|
|
render_error("Invalid upload", :unprocessable_entity)
|
|
end
|
|
|
|
private
|
|
|
|
def pending_images
|
|
{
|
|
"picture" => current_user.nostr_picture,
|
|
"banner" => current_user.nostr_banner
|
|
}.select { |_field, attachment| attachment.attached? }
|
|
end
|
|
|
|
def verified_auth_event(raw)
|
|
return nil if raw.blank?
|
|
|
|
data = (raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw.to_h).symbolize_keys
|
|
event = Nostr::Event.new(**data.slice(:id, :pubkey, :created_at, :kind, :tags, :content, :sig))
|
|
return nil unless event.kind == AUTH_KIND && event.pubkey == current_user.nostr_pubkey
|
|
return nil unless created_at_valid?(event.created_at)
|
|
return nil unless NostrManager::VerifySignedEvent.call(event: event, allowed_kinds: [AUTH_KIND])
|
|
return nil unless within_tag_limits?(event.tags)
|
|
return nil unless auth_tags_valid?(event.tags)
|
|
|
|
event
|
|
rescue JSON::ParserError, ArgumentError, TypeError, NoMethodError
|
|
nil
|
|
end
|
|
|
|
def created_at_valid?(created_at)
|
|
created_at.is_a?(Integer) && created_at <= (Time.current.to_i + CLOCK_SKEW.to_i)
|
|
end
|
|
|
|
def within_tag_limits?(tags)
|
|
tags = Array(tags)
|
|
return false if tags.size > MAX_TAGS
|
|
|
|
tags.all? do |tag|
|
|
tag.is_a?(Array) &&
|
|
tag.all? { |value| value.is_a?(String) } &&
|
|
tag.sum(&:bytesize) <= MAX_TAG_BYTES
|
|
end
|
|
end
|
|
|
|
def auth_tags_valid?(tags)
|
|
verbs = tags.select { |tag| tag[0] == "t" }.map { |tag| tag[1] }
|
|
return false unless verbs.include?("upload")
|
|
|
|
expiration = tags.find { |tag| tag[0] == "expiration" }&.[](1)
|
|
return false unless expiration.to_s.match?(/\A\d+\z/)
|
|
|
|
expiration.to_i.between?(Time.current.to_i, (Time.current.to_i + EXPIRATION_WINDOW.to_i))
|
|
end
|
|
|
|
def authorized_hashes(event)
|
|
Array(event.tags).select { |tag| tag[0] == "x" }.map { |tag| tag[1] }
|
|
end
|
|
|
|
def rate_limited?
|
|
key = "nostr_media:#{current_user.id}"
|
|
count = Rails.cache.read(key).to_i
|
|
return true if count >= RATE_LIMIT
|
|
|
|
Rails.cache.write(key, count + 1, expires_in: RATE_WINDOW)
|
|
false
|
|
end
|
|
|
|
def render_error(message, status)
|
|
render json: { error: message }, status: status
|
|
end
|
|
end
|
|
end
|