Class: Decidim::ViaOberta::Verifications::ViaObertaHandler

Inherits:
AuthorizationHandler
  • Object
show all
Defined in:
app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb

Constant Summary collapse

DOCUMENT_TYPE =

VALID: 1=NIF, 2=NIE, 3=Passaport, 4=Altres VIAOBERTA: 1=NIF, 2=passaport, 3=permís residència, 4=NIE

{
  1 => :nif,
  2 => :nie,
  3 => :passport,
  4 => :others
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#response_codeObject (readonly)

Returns the value of attribute response_code.



24
25
26
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 24

def response_code
  @response_code
end

#response_errorObject (readonly)

Returns the value of attribute response_error.



24
25
26
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 24

def response_error
  @response_error
end

Instance Method Details

#census_responseObject



61
62
63
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 61

def census_response
  @census_response ||= ViaOberta::Api::Request.new(document_id: document_id, document_type: document_type, organization: organization).response
end

#document_idObject

use from previous authorization metadata. We don't allow user input here to prevent spoofing not memoized because @document_id would be defined if sent via form



37
38
39
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 37

def document_id
  trusted_authorization&.&.dig("uid") || impersonated_document_id
end

#document_typeObject

use from previous authorization metadata or use the one from the form as a fallback not memoized because @document_id would be defined if sent via form



43
44
45
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 43

def document_type
   || DOCUMENT_TYPE[attributes[:document_type]]
end

#document_type_from_metadataObject



47
48
49
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 47

def 
  @document_type_from_metadata ||= DOCUMENT_TYPE[trusted_authorization&.&.dig("extra", "identifier_type").to_i]
end

#document_type_stringObject



55
56
57
58
59
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 55

def document_type_string
  return "" unless document_type

  I18n.t("decidim.via_oberta.verifications.document_type.#{document_type}", default: document_type.to_s)
end

#document_typesObject



51
52
53
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 51

def document_types
  @document_types ||= DOCUMENT_TYPE.map { |k, v| [I18n.t("decidim.via_oberta.verifications.document_type.#{v}", default: v.to_s), k] }
end

#existing_via_oberta_identityObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 65

def existing_via_oberta_identity
  return unless tos_agreement

  return errors.add(:base, I18n.t("decidim.verifications.trusted_ids.errors.invalid_id")) if document_id.blank?
  return errors.add(:base, I18n.t("decidim.verifications.trusted_ids.errors.invalid_type")) if document_type.blank?

  return if census_response.found?

  errors.add(:base, I18n.t("decidim.verifications.trusted_ids.errors.invalid_census"))
  @response_error = census_response.error
  @response_code = census_response.code
end

#form_attributesObject

no public attributes



79
80
81
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 79

def form_attributes
  attributes.except(:id, :user, :document_id, :document_type).keys
end

#to_partial_pathObject



83
84
85
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 83

def to_partial_path
  "decidim/via_oberta/verifications/form"
end

#unique_idObject



26
27
28
29
30
31
32
33
# File 'app/forms/decidim/via_oberta/verifications/via_oberta_handler.rb', line 26

def unique_id
  return unless organization
  return unless census_response.success?

  Digest::SHA256.hexdigest(
    "#{document_id}-#{user&.decidim_organization_id}-#{Rails.application.secret_key_base}"
  )
end