Class: SelfSDK::Services::Requester

Inherits:
Object
  • Object
show all
Defined in:
lib/services/requester.rb

Overview

Input class to handle fact requests on self network.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messaging, client) ⇒ SelfSDK::Services::Facts

Creates a new facts service. Facts service mainly manages fact requests against self users wanting to share their verified facts with your app.

Parameters:

  • messaging (SelfSDK::Messaging)

    messaging object.

  • client (SelfSDK::Client)

    http client object.



23
24
25
26
27
# File 'lib/services/requester.rb', line 23

def initialize(messaging, client)
  @messaging = messaging.client
  @messaging_service = messaging
  @client = client
end

Instance Attribute Details

#messagingObject (readonly)

Returns the value of attribute messaging.



13
14
15
# File 'lib/services/requester.rb', line 13

def messaging
  @messaging
end

Instance Method Details

Generates a deep link to authenticate with self app.

Parameters:

  • facts (Array)

    a list of facts to be requested.

  • callback (String)

    the callback identifier you’ll be redirected to if the app is not installed.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :selfid (String)

    the user selfid you want to authenticate.

  • :cid (String)

    The unique identifier of the authentication request.

Returns:

  • (String, String)

    conversation id or encoded body.



129
130
131
132
133
134
135
# File 'lib/services/requester.rb', line 129

def generate_deep_link(facts, callback, opts = {})
  opts[:request] = false
  selfid = opts.fetch(:selfid, "-")

  body = @client.jwt.encode(request(selfid, facts, opts))
  @client.jwt.build_dynamic_link(body, @client.env, callback)
end

#generate_qr(facts, opts = {}) ⇒ String

Generates a QR code so users can send facts to your app.

Parameters:

  • facts (Array)

    a list of facts to be requested.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :cid (String)

    The unique identifier of the authentication request.

  • :options (String)

    Options you want to share with the identity.

Returns:

  • (String, String)

    conversation id or encoded body.



114
115
116
117
118
119
# File 'lib/services/requester.rb', line 114

def generate_qr(facts, opts = {})
  opts[:request] = false
  selfid = opts.fetch(:selfid, "-")
  req = request(selfid, facts, opts)
  ::RQRCode::QRCode.new(req, level: 'l')
end

#request(selfid, facts, opts = {}, &block) ⇒ Object #request(selfid, facts, opts = {}) ⇒ Object

Sends a fact request to the specified selfid. An fact request allows your app to access trusted facts of your user with its permission.

Overloads:

  • #request(selfid, facts, opts = {}, &block) ⇒ Object

    Parameters:

    • selfid (string)

      the receiver of the authentication request.

    • opts (Hash) (defaults to: {})

      the options to authenticate.

    Options Hash (opts):

    • :cid (String)

      The unique identifier of the authentication request.

  • #request(selfid, facts, opts = {}) ⇒ Object

    Returns SelfSDK:::Messages::FactRequest.

    Parameters:

    • selfid (string)

      the receiver of the authentication request.

    • opts (Hash) (defaults to: {})

      the options to authenticate.

    Options Hash (opts):

    • :cid (String)

      The unique identifier of the authentication request.

    • :exp_timeout (Integer)

      timeout in seconds to expire the request.

    • :allowed_for (Integer)

      number of seconds for enabling recurrent requests.

    • :auth (Boolean)

      allows displaying the request as anuthentication request with facts.

    Returns:

    • (Object)

      SelfSDK:::Messages::FactRequest

Yields:

  • (request)

    Invokes the given block when a response is received. @return [Object] SelfSDK:::Messages::FactRequest



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/services/requester.rb', line 48

def request(selfid, facts, opts = {}, &block)
  SelfSDK.logger.info "authenticating #{selfid}"
  rq = opts.fetch(:request, true)

  req = SelfSDK::Messages::FactRequest.new(@messaging)
  req.populate(selfid, prepare_facts(facts), opts)

  body = @client.jwt.prepare(req.body)
  return body unless rq

  # when a block is given the request will always be asynchronous.
  if block_given?
    @messaging.set_observer(req, timeout: req.exp_timeout, &block)
    return req.send_message
  end

  if opts[:async] == true
    return req.send_message
  end

  # Otherwise the request is synchronous
  req.request
end

#request_via_intermediary(selfid, facts, opts = {}, &block) ⇒ Object

Sends a request through an intermediary. An intermediary is an entity trusted by the user and acting as a proxy between you and the recipient of your fact request. Intermediaries usually do not provide the original user facts, but they create its own assertions based on your request and the user’s facts.

@param selfid [string] the receiver of the authentication request.
@param [Hash] opts the options to authenticate.
@option opts [String] intermediary an intermediary identity to be used.
@return [Object] SelfSDK:::Messages::FactRequest


82
83
84
85
# File 'lib/services/requester.rb', line 82

def request_via_intermediary(selfid, facts, opts = {}, &block)
  opts[:intermediary] = opts.fetch(:intermediary, DEFAULT_INTERMEDIARY)
  request(selfid, facts, opts, &block)
end

#subscribe(auth, &block) ⇒ Object

Adds an observer for a fact response Whenever you receive a fact response registered observers will receive a notification.

@yield [request] Invokes the block with a fact response message.


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/services/requester.rb', line 91

def subscribe(auth, &block)
  if auth == true
    @auth_subscription = block
  else
    @fact_subscription = block
  end

  @messaging.subscribe :fact_response do |res|
    if res.auth_response?
      @auth_subscription&.call(res)
    else
      @fact_subscription&.call(res)
    end
  end
end