Class: SelfSDK::Services::Authentication

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

Overview

Input class to handle authentication requests on self network.

Instance Method Summary collapse

Constructor Details

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

Creates a new authentication service. Authentication service mainly manages authentication requests against self users wanting to authenticate on your app.

Parameters:

  • messaging (SelfSDK::Messaging)

    messaging object.

  • client (SelfSDK::Client)

    http client object.

[View source]

17
18
19
20
21
# File 'lib/services/auth.rb', line 17

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:

  • 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]

84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/services/auth.rb', line 84

def generate_deep_link(callback, opts = {})
  opts[:request] = false
  selfid = opts.fetch(:selfid, "-")
  body = @client.jwt.encode(request(selfid, 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(opts = {}) ⇒ String

Generates a QR code so users can authenticate to your app.

Parameters:

  • 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]

70
71
72
73
74
75
# File 'lib/services/auth.rb', line 70

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

#request(selfid, opts = {}) {|request| ... } ⇒ String #request(selfid, opts = {}) ⇒ String

Sends an authentication request to the specified selfid. An authentication requests allows your users to authenticate on your app using a secure self app.

Overloads:

  • #request(selfid, opts = {}) {|request| ... } ⇒ String

    Returns conversation id or encoded body.

    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 an authentication response for each result.

    Returns:

    • (String, String)

      conversation id or encoded body.

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

    Returns conversation id or encoded body.

    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.

    Returns:

    • (String, String)

      conversation id or encoded body.

[View source]

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

def request(selfid, 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::AuthenticationReq.new(@messaging)
  req.populate(selfid, opts)

  body = @client.jwt.prepare(req.body)
  return body unless rq
  return req.send_message if opts.fetch(:async, false)

  # 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

#subscribe(&block) ⇒ Object

Adds an observer for an authentication response

[View source]

98
99
100
101
102
103
# File 'lib/services/auth.rb', line 98

def subscribe(&block)
  @messaging.subscribe :authentication_response do |res|
    valid_payload(res.input)
    yield(res)
  end
end