Class: SelfSDK::Services::Facts

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

Overview

Input class to handle authentication requests on self network.

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.

[View source]

19
20
21
22
23
# File 'lib/services/facts.rb', line 19

def initialize(messaging, client)
  @messaging = messaging.client
  @messaging_service = messaging
  @client = client
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 url you’ll be redirected 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.

[View source]

110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/services/facts.rb', line 110

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

  if @client.env.empty?
    return "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app"
  elsif @client.env == 'development'
    return "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app.dev"
  end
  "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app.#{@client.env}"
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.

[View source]

95
96
97
98
99
100
# File 'lib/services/facts.rb', line 95

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 = {}) {|request| ... } ⇒ 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 = {}) {|request| ... } ⇒ 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.

    Yields:

    • (request)

      Invokes the block with a street name for each result.

    Returns:

    • (Object)

      SelfSDK:::Messages::FactRequest

  • #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.

    Returns:

    • (Object)

      SelfSDK:::Messages::FactRequest

[View source]

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/services/facts.rb', line 42

def request(selfid, facts, opts = {}, &block)
  SelfSDK.logger.info "authenticating #{selfid}"
  rq = opts.fetch(:request, true)
  if rq
    raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
  end

  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

  # 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
[View source]

75
76
77
78
# File 'lib/services/facts.rb', line 75

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

#subscribe(&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.
[View source]

84
85
86
# File 'lib/services/facts.rb', line 84

def subscribe(&block)
  @messaging.subscribe(:fact_response, &block)
end