Skip to content

Commit

Permalink
improved PLCOperation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Mar 16, 2024
1 parent 617c116 commit 2652018
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/didkit/plc_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,50 @@ class PLCOperation
class FormatError < StandardError
end

attr_reader :did, :created_at, :type, :pds_endpoint, :handles
attr_reader :json, :did, :created_at, :type, :pds_endpoint, :handles

def initialize(json)
@json = json
@did = json['did']
raise FormatError, "Missing DID" if @did.nil?
raise FormatError, "Invalid DID" unless @did.is_a?(String) && @did.start_with?('did:')
raise FormatError, "Missing DID: #{json}" if @did.nil?
raise FormatError, "Invalid DID: #{@did}" unless @did.is_a?(String) && @did.start_with?('did:')

timestamp = json['createdAt']
raise FormatError, "Missing createdAt" if timestamp.nil?
raise FormatError, "Invalid createdAt" unless timestamp.is_a?(String)
raise FormatError, "Missing createdAt: #{json}" if timestamp.nil?
raise FormatError, "Invalid createdAt: #{timestamp.inspect}" unless timestamp.is_a?(String)

@created_at = Time.parse(timestamp)

operation = json['operation']
raise FormatError, "Missing operation key" if operation.nil?
raise FormatError, "Invalid operation data" unless operation.is_a?(Hash)
raise FormatError, "Missing operation key: #{json}" if operation.nil?
raise FormatError, "Invalid operation data: #{operation.inspect}" unless operation.is_a?(Hash)

type = operation['type']
raise FormatError, "Missing type" if type.nil?
raise FormatError, "Missing operation type: #{json}" if type.nil?

@type = type.to_sym
return unless @type == :plc_operation

services = operation['services']
raise FormatError, "Missing services key" if services.nil?
raise FormatError, "Invalid services data" unless services.is_a?(Hash)
raise FormatError, "Missing services key: #{json}" if services.nil?
raise FormatError, "Invalid services data: #{services}" unless services.is_a?(Hash)

if pds = services['atproto_pds']
raise FormatError, "Invalid PDS data" unless pds.is_a?(Hash)
raise FormatError, "Missing PDS type" unless pds['type']
raise FormatError, "Invalid PDS type" unless pds['type'] == 'AtprotoPersonalDataServer'
raise FormatError, "Missing PDS endpoint" unless pds['endpoint']
raise FormatError, "Invalid PDS endpoint" unless pds['endpoint'].is_a?(String) && pds['endpoint'] =~ %r(://)
raise FormatError, "Invalid PDS data: #{pds.inspect}" unless pds.is_a?(Hash)
raise FormatError, "Missing PDS type: #{pds.inspect}" unless pds['type']
raise FormatError, "Invalid PDS type: #{pds['type']}" unless pds['type'] == 'AtprotoPersonalDataServer'

@pds_endpoint = pds['endpoint']
endpoint = pds['endpoint']
raise FormatError, "Missing PDS endpoint: #{pds.inspect}" unless endpoint
raise FormatError, "Invalid PDS endpoint: #{endpoint}" unless endpoint.is_a?(String) && endpoint =~ %r(://)

@pds_endpoint = endpoint
end

if aka = operation['alsoKnownAs']
raise FormatError, "Invalid alsoKnownAs" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }

@handles = aka.map { |x| x.gsub('at://', '') }
else
Expand Down

0 comments on commit 2652018

Please sign in to comment.