Class: SSO::Server::Middleware::PassportExchange

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/sso/server/middleware/passport_exchange.rb

Overview

Hands out the Passport when presented with the corresponding Access Token.

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #logger, #progname, #warn

Constructor Details

#initialize(app) ⇒ PassportExchange

Returns a new instance of PassportExchange.



9
10
11
# File 'lib/sso/server/middleware/passport_exchange.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sso/server/middleware/passport_exchange.rb', line 13

def call(env)
  request = Rack::Request.new(env)
  remote_ip = request.env['action_dispatch.remote_ip'].to_s
  device_id = request.params['device_id']

  if !(request.post? && request.path == passports_path)
    debug { "I'm not interested in this #{request.request_method.inspect} request to #{request.path.inspect} I only care for POST #{passports_path.inspect}" }
    return @app.call(env)
  end

  token = request.params['access_token']
  debug { "Detected incoming Passport creation request for access token #{token.inspect}" }
  access_token = ::Doorkeeper::AccessToken.find_by_token token

  unless access_token
    return json_code :access_token_not_found
  end

  unless access_token.valid?
    return json_code :access_token_invalid
  end

  finding = ::SSO::Server::Passports.find_by_access_token_id(access_token.id)
  if finding.failure?
    # This should never happen. Every Access Token should be connected to a Passport.
    return json_code :passport_not_found
  end
  passport = finding.object

  ::SSO::Server::Passports.update_activity passport_id: passport.id, request: request

  debug { "Attaching user and chip to passport #{passport.inspect}" }
  passport.load_user!
  passport.create_chip!

  payload = { success: true, code: :here_is_your_passport, passport: passport.export }
  debug { "Created Passport #{passport.id}, sending it including user #{passport.user.inspect}}" }

  [200, { 'Content-Type' => 'application/json' }, [payload.to_json]]
end

#json_code(code) ⇒ Object



54
55
56
# File 'lib/sso/server/middleware/passport_exchange.rb', line 54

def json_code(code)
  [200, { 'Content-Type' => 'application/json' }, [{ success: true, code: code }.to_json]]
end

#passports_pathObject



58
59
60
# File 'lib/sso/server/middleware/passport_exchange.rb', line 58

def passports_path
  OmniAuth::Strategies::SSO.passports_path
end