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
# File 'lib/sso/server/middleware/passport_exchange.rb', line 13

def call(env)
  request = Rack::Request.new(env)

  unless 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 exchange request for access token #{token.inspect}" }
  access_token = ::Doorkeeper::AccessToken.find_by_token token

  return json_error :access_token_not_found unless access_token
  return json_error :access_token_invalid unless access_token.valid?

  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_error :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_error(code) ⇒ Object



47
48
49
# File 'lib/sso/server/middleware/passport_exchange.rb', line 47

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

#passports_pathObject



51
52
53
# File 'lib/sso/server/middleware/passport_exchange.rb', line 51

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