lib/yoti/activity_details.rb in yoti-1.4.0 vs lib/yoti/activity_details.rb in yoti-1.5.0
- old
+ new
@@ -1,84 +1,186 @@
require 'net/http'
module Yoti
- # Encapsulates the user profile data
+ #
+ # Details of an activity between a user and the application.
+ #
class ActivityDetails
- # @return [String] the outcome of the profile request, eg: SUCCESS
+ #
+ # The outcome of the profile request, eg: SUCCESS
+ #
+ # @return [String]
+ #
attr_reader :outcome
+ #
# @deprecated replaced by :remember_me_id
- # @return [String] the Yoti ID
+ #
+ # @return [String]
+ #
attr_reader :user_id
- # @return [String] the Remember Me ID
+ #
+ # Return the Remember Me ID, which is a unique, stable identifier for
+ # a user in the context of an application.
+ #
+ # You can use it to identify returning users. This value will be different
+ # for the same user in different applications.
+ #
+ # @return [String]
+ #
attr_reader :remember_me_id
- # @return [String] the Parent Remember Me ID
+ #
+ # Return the Parent Remember Me ID, which is a unique, stable identifier for a
+ # user in the context of an organisation.
+ #
+ # You can use it to identify returning users. This value is consistent for a
+ # given user across different applications belonging to a single organisation.
+ #
+ # @return [String]
+ #
attr_reader :parent_remember_me_id
- # @return [Hash] the decoded profile attributes
+ #
+ # The decoded profile attributes
+ #
+ # @deprecated replaced by :profile
+ #
+ # @return [Hash]
+ #
attr_reader :user_profile
- # @return [String] the selfie in base64 format
+ #
+ # Base64 encoded selfie image
+ #
+ # @return [String]
+ #
attr_reader :base64_selfie_uri
- # @return [Boolean] the age under/over attribute
+ #
+ # The age under/over attribute
+ #
+ # @return [Boolean]
+ #
attr_reader :age_verified
+ #
+ # Receipt ID identifying a completed activity
+ #
+ # @return [String]
+ #
+ attr_reader :receipt_id
+
+ #
+ # Time and date of the sharing activity
+ #
+ # @return [Time]
+ #
+ attr_reader :timestamp
+
+ #
# @param receipt [Hash] the receipt from the API request
# @param decrypted_profile [Object] Protobuf AttributeList decrypted object containing the profile attributes
- def initialize(receipt, decrypted_profile = nil)
+ #
+ def initialize(receipt, decrypted_profile = nil, decrypted_application_profile = nil)
@remember_me_id = receipt['remember_me_id']
@user_id = @remember_me_id
+ @receipt_id = receipt['receipt_id']
@parent_remember_me_id = receipt['parent_remember_me_id']
@outcome = receipt['sharing_outcome']
- @user_profile = {}
- @extended_profile = {}
- process_decrypted_profile(decrypted_profile)
+ @timestamp = receipt['timestamp'] ? Time.parse(receipt['timestamp']) : nil
+ @extended_user_profile = process_decrypted_profile(decrypted_profile)
+ @extended_application_profile = process_decrypted_profile(decrypted_application_profile)
+ @user_profile = @extended_user_profile.map do |name, attribute|
+ [name, attribute.value]
+ end.to_h
end
- # @return [Hash] a JSON of the address
+ #
+ # The user's structured postal address as JSON
+ #
+ # @deprecated replaced by Profile.structured_postal_address
+ #
+ # @return [Hash]
+ #
def structured_postal_address
- @user_profile['structured_postal_address']
+ user_profile['structured_postal_address']
end
- # @return [Profile] of Yoti user
+ #
+ # The user profile with shared attributes and anchor information, returned
+ # by Yoti if the request was successful
+ #
+ # @return [Profile]
+ #
def profile
- Yoti::Profile.new(@extended_profile)
+ Yoti::Profile.new(@extended_user_profile)
end
+ #
+ # Profile of an application, with convenience methods to access well-known attributes
+ #
+ # @return [ApplicationProfile]
+ #
+ def application_profile
+ Yoti::ApplicationProfile.new(@extended_application_profile)
+ end
+
protected
+ #
+ # Process the decrypted profile into key-value hash
+ #
+ # @param [Yoti::Protobuf::Attrpubapi::AttributeList] decrypted_profile
+ #
+ # @return [Hash]
+ #
def process_decrypted_profile(decrypted_profile)
- return nil unless decrypted_profile.is_a?(Object)
- return nil unless decrypted_profile.respond_to?(:attributes)
+ return {} unless decrypted_profile.is_a?(Object)
+ return {} unless decrypted_profile.respond_to?(:attributes)
+ profile_data = {}
decrypted_profile.attributes.each do |attribute|
begin
- process_attribute(attribute)
+ profile_data[attribute.name] = process_attribute(attribute)
process_age_verified(attribute)
rescue StandardError => e
Yoti::Log.logger.warn("#{e.message} (Attribute: #{attribute.name})")
end
end
+ profile_data
end
+ #
+ # Converts protobuf attribute into Attribute
+ #
+ # @param [Yoti::Protobuf::Attrpubapi::Attribute] attribute
+ #
+ # @return [Attribute, nil]
+ #
def process_attribute(attribute)
+ # Application Logo can be empty, return nil when this occurs.
+ return nil if attribute.name == Yoti::Attribute::APPLICATION_LOGO && attribute.value == ''
+
attr_value = Yoti::Protobuf.value_based_on_content_type(attribute.value, attribute.content_type)
attr_value = Yoti::Protobuf.value_based_on_attribute_name(attr_value, attribute.name)
# Handle selfies for backwards compatibility.
if attribute.name == Yoti::Attribute::SELFIE && attr_value.is_a?(Yoti::Image)
@base64_selfie_uri = attr_value.base64_content
attr_value = attr_value.content
end
anchors_list = Yoti::AnchorProcessor.new(attribute.anchors).process
- @extended_profile[attribute.name] = Yoti::Attribute.new(attribute.name, attr_value, anchors_list['sources'], anchors_list['verifiers'])
- @user_profile[attribute.name] = attr_value
+ Yoti::Attribute.new(attribute.name, attr_value, anchors_list['sources'], anchors_list['verifiers'], anchors_list)
end
+ #
+ # Processes age verification
+ #
+ # @param [Yoti::Protobuf::Attrpubapi::Attribute] attribute
+ #
def process_age_verified(attribute)
@age_verified = attribute.value == 'true' if Yoti::AgeProcessor.is_age_verification(attribute.name)
end
end
end