Class: Rack::Rpx

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-rpx.rb

Overview

Rack Middleware for integrating RPX Now into your application

Note: this requires that a Rack::Session middleware be enabled

Defined Under Namespace

Modules: Methods Classes: LoginFailedError, NoSession

Constant Summary collapse

RPX_LOGIN_URL =
"https://rpxnow.com/api/v2/auth_info"
OPTIONS =
{
  :callback_path   => '/login_completed',
  :logout_path     => '/logout', 
  :host            => 'localhost',
  :port            => '80',
  :rack_session    => 'rack.session',
  :name            => 'default'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *options) ⇒ Rpx

Returns a new instance of Rpx.



61
62
63
64
65
66
67
# File 'lib/rack-rpx.rb', line 61

def initialize app, *options
  @app = app
  OPTIONS.merge! options.pop
  OPTIONS.each do |k,v|
    Rack::Rpx.send(:define_method, k.to_s) {OPTIONS[k]}
  end      
end

Class Method Details

.credentials(token) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack-rpx.rb', line 48

def credentials(token)
  u = URI.parse(RPX_LOGIN_URL)
  req = Net::HTTP::Post.new(u.path)
  req.set_form_data({:token => token, :apiKey => OPTIONS[:api_key], :format => 'json', :extended => 'true'})
  http = Net::HTTP.new(u.host,u.port)
  http.use_ssl = true if u.scheme == 'https'
  json = JSON.parse(http.request(req).body)
  
  raise LoginFailedError, 'Cannot log in. Try another account! #{json.inspect}' unless json['stat'] == 'ok'
  json
end

Instance Method Details

#call(env) ⇒ Object

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack-rpx.rb', line 69

def call env      
  @req = Rack::Request.new env 
  raise NoSession, 'No compatible session.' unless env['rack.session']
  
  if env['PATH_INFO'] ==  OPTIONS[:callback_path] &&  @req.post? then 
    token = @req.params["token"]
    set_credentials(env, token) if OPTIONS[:set_credentials] 
    (env)
  elsif env['PATH_INFO'] == OPTIONS[:logout_path] then
    logout(env)
  end        
  @app.call(env)     
end

#login(env) ⇒ Object

This is the method that you should override if you want to perform any operation just after the response from rpx now

You can use the token to make GET/POST/etc requests



91
# File 'lib/rack-rpx.rb', line 91

def (env);  end

#logout(env) ⇒ Object

This is the method that you should override if you want to perform any operation just before you reach the logout_path



96
# File 'lib/rack-rpx.rb', line 96

def logout(env); end

#set_credentials(env, token) ⇒ Object



83
84
85
# File 'lib/rack-rpx.rb', line 83

def set_credentials(env, token)
  env['rack.session']['credentials'] = self.get_credentials(token)
end