Class: SelfSDK::Services::Authentication
- Inherits:
-
Object
- Object
- SelfSDK::Services::Authentication
- Defined in:
- lib/services/auth.rb
Overview
Input class to handle authentication requests on self network.
Instance Method Summary collapse
-
#generate_deep_link(callback, opts = {}) ⇒ String
Generates a deep link to authenticate with self app.
-
#generate_qr(opts = {}) ⇒ String
Generates a QR code so users can authenticate to your app.
-
#initialize(messaging, client) ⇒ SelfSDK::Services::Authentication
constructor
Creates a new authentication service.
-
#request(selfid, opts = {}, &block) ⇒ Object
Sends an authentication request to the specified selfid.
-
#subscribe(&block) ⇒ Object
Adds an observer for an authentication response.
Constructor Details
permalink #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.
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
permalink #generate_deep_link(callback, opts = {}) ⇒ String
Generates a deep link to authenticate with self app.
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 |
permalink #generate_qr(opts = {}) ⇒ String
Generates a QR code so users can authenticate to your app.
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 |
permalink #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.
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. 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. end # Otherwise the request is synchronous req.request end |
permalink #subscribe(&block) ⇒ Object
Adds an observer for an authentication response
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 |