Refactor Nostr settings/connect
* Use NIP-42 auth event instead of short text note * Verify event ID and signature using the nostr gem instead of custom code
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
module NostrManager
|
||||
class ValidateId < NostrManagerService
|
||||
def initialize(event:)
|
||||
@event = Nostr::Event.new(**event)
|
||||
end
|
||||
|
||||
def call
|
||||
@event.id == Digest::SHA256.hexdigest(JSON.generate(@event.serialize))
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/services/nostr_manager/verify_auth.rb
Normal file
17
app/services/nostr_manager/verify_auth.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module NostrManager
|
||||
class VerifyAuth < NostrManagerService
|
||||
def initialize(event:, challenge:)
|
||||
@event = event
|
||||
@challenge_expected = challenge
|
||||
@site_expected = Setting.accounts_domain
|
||||
end
|
||||
|
||||
def call
|
||||
site_given = @event.tags.find{|t| t[0] == "site"}[1]
|
||||
challenge_given = @event.tags.find{|t| t[0] == "challenge"}[1]
|
||||
|
||||
site_given == @site_expected &&
|
||||
challenge_given == @challenge_expected
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,17 +0,0 @@
|
||||
module NostrManager
|
||||
class VerifySignature < NostrManagerService
|
||||
def initialize(event:)
|
||||
@event = Nostr::Event.new(**event)
|
||||
end
|
||||
|
||||
def call
|
||||
Schnorr.check_sig!(
|
||||
[@event.id].pack('H*'),
|
||||
[@event.pubkey].pack('H*'),
|
||||
[@event.sig].pack('H*')
|
||||
)
|
||||
rescue Schnorr::InvalidSignatureError
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user