rack-rpx 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/examples/login-app.rb +1 -0
- data/examples/login-hooks-sapp.rb +47 -0
- data/lib/rack-rpx.rb +57 -18
- data/spec/rack-rpx_spec.rb +32 -3
- data/spec/spec_helper.rb +10 -17
- data/test/rack_rpx_test.rb +24 -0
- data/test/test_helper.rb +10 -0
- metadata +6 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/examples/login-app.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'rack'
|
4
|
+
require 'haml'
|
5
|
+
require 'lib/rack-rpx'
|
6
|
+
|
7
|
+
use Rack::Session::Cookie
|
8
|
+
|
9
|
+
use Rack::Rpx, :port => '9393',
|
10
|
+
:api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
|
11
|
+
:callback_path => '/login_completed'
|
12
|
+
|
13
|
+
helpers do
|
14
|
+
include Rack::Rpx::Methods
|
15
|
+
end
|
16
|
+
|
17
|
+
class Rack::Rpx
|
18
|
+
def login(env)
|
19
|
+
puts "logging in ..."
|
20
|
+
env['rack.session']['user']= Rack::Rpx.credentials @req.params['token']
|
21
|
+
end
|
22
|
+
|
23
|
+
def logout(env)
|
24
|
+
puts "logging out ..."
|
25
|
+
env['rack.session'].delete 'user'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
get "/" do
|
30
|
+
"our current session => #{session.inspect}, params => #{params.inspect}<br/>, #{env.inspect}"
|
31
|
+
end
|
32
|
+
|
33
|
+
get "/login" do
|
34
|
+
haml :login
|
35
|
+
end
|
36
|
+
|
37
|
+
post "/login_completed" do
|
38
|
+
"our current #{session.inspect}"
|
39
|
+
end
|
40
|
+
|
41
|
+
get '/logout' do
|
42
|
+
"our current #{session.inspect}"
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
data/lib/rack-rpx.rb
CHANGED
@@ -6,19 +6,24 @@ module Rack #:nodoc:
|
|
6
6
|
# Note: this *requires* that a Rack::Session middleware be enabled
|
7
7
|
#
|
8
8
|
class Rpx
|
9
|
+
RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info"
|
10
|
+
# Raised if an incompatible session is being used.
|
11
|
+
class NoSession < RuntimeError; end
|
12
|
+
class LoginFailedError < RuntimeError; end
|
13
|
+
|
9
14
|
OPTIONS = {
|
10
|
-
:login_path => '/login',
|
11
15
|
:callback_path => '/login_completed',
|
16
|
+
:logout_path => '/logout',
|
12
17
|
:host => 'localhost',
|
13
18
|
:port => '80',
|
14
|
-
:rack_session => 'rack.session'
|
19
|
+
:rack_session => 'rack.session',
|
20
|
+
:name => 'default'
|
15
21
|
}
|
16
|
-
|
22
|
+
|
17
23
|
# Helper methods intended to be included in your Rails controller or
|
18
24
|
# in your Sinatra helpers block
|
19
25
|
module Methods
|
20
|
-
|
21
|
-
|
26
|
+
|
22
27
|
# This is *the* method you want to call.
|
23
28
|
#
|
24
29
|
# After you're authorized and redirected back to your #redirect_to path,
|
@@ -27,6 +32,20 @@ module Rack #:nodoc:
|
|
27
32
|
#
|
28
33
|
# You can use the token to make GET/POST/etc requests
|
29
34
|
def get_credentials(token)
|
35
|
+
Rack::Rpx.get_credentials(token)
|
36
|
+
end
|
37
|
+
|
38
|
+
def login_widget_url(app_name=nil)
|
39
|
+
"https://#{app_name}.rpxnow.com/openid/v2/signin?token_url=#{callback_url}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def callback_url
|
43
|
+
"http://#{env['HTTP_HOST']}#{OPTIONS[:callback_path]}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class << self
|
48
|
+
def credentials(token)
|
30
49
|
u = URI.parse(RPX_LOGIN_URL)
|
31
50
|
req = Net::HTTP::Post.new(u.path)
|
32
51
|
req.set_form_data({:token => token, :apiKey => OPTIONS[:api_key], :format => 'json', :extended => 'true'})
|
@@ -37,25 +56,45 @@ module Rack #:nodoc:
|
|
37
56
|
raise LoginFailedError, 'Cannot log in. Try another account!' unless json['stat'] == 'ok'
|
38
57
|
json
|
39
58
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize app, *options
|
62
|
+
@app = app
|
63
|
+
OPTIONS.merge! options.pop
|
64
|
+
OPTIONS.each do |k,v|
|
65
|
+
Rack::Rpx.send(:define_method, k.to_s) {OPTIONS[k]}
|
43
66
|
end
|
44
67
|
|
68
|
+
end
|
69
|
+
|
70
|
+
def call env
|
71
|
+
@req = Rack::Request.new env
|
72
|
+
raise NoSession, 'No compatible session.' unless env['rack.session']
|
45
73
|
|
46
|
-
|
47
|
-
"
|
48
|
-
|
74
|
+
if env['PATH_INFO'] == OPTIONS[:callback_path] && @req.post? then
|
75
|
+
token = @req.params["token"]
|
76
|
+
set_credentials(env, token) if OPTIONS[:set_credentials]
|
77
|
+
login(env)
|
78
|
+
elsif env['PATH_INFO'] == OPTIONS[:logout_path] then
|
79
|
+
logout(env)
|
80
|
+
end
|
81
|
+
@app.call(env)
|
49
82
|
end
|
50
83
|
|
51
|
-
def
|
52
|
-
|
53
|
-
arg_options = args.pop
|
54
|
-
OPTIONS.merge! arg_options
|
84
|
+
def set_credentials(env, token)
|
85
|
+
env['rack.session']['credentials'] = self.get_credentials(token)
|
55
86
|
end
|
87
|
+
|
88
|
+
# This is the method that you should override if you want to
|
89
|
+
# perform any operation just after the response from rpx now
|
90
|
+
#
|
91
|
+
# You can use the token to make GET/POST/etc requests
|
92
|
+
def login(env); end
|
93
|
+
|
94
|
+
|
95
|
+
# This is the method that you should override if you want to
|
96
|
+
# perform any operation just before you reach the logout_path
|
97
|
+
def logout(env); end
|
56
98
|
|
57
|
-
def call env
|
58
|
-
@app.call(env)
|
59
|
-
end
|
60
99
|
end
|
61
100
|
end
|
data/spec/rack-rpx_spec.rb
CHANGED
@@ -1,7 +1,36 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'rack/test'
|
2
3
|
|
3
|
-
describe "
|
4
|
-
|
5
|
-
|
4
|
+
describe Rack::Rpx, "basics" do
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
@lambda = lambda {|env| [200, {}, ["Hello World"]] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def app ; @app ; end
|
12
|
+
def app=(app) ; @app= app ; end
|
13
|
+
|
14
|
+
|
15
|
+
# TODO remove OPTIONS const, is just horrible
|
16
|
+
it 'should have a name' do
|
17
|
+
app = Rack::Rpx.new @lambda, :key => 'b', :secret => 'c', :name => 'olokun'
|
18
|
+
app.name.should == 'olokun'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'name should default to "default"' do
|
22
|
+
app= Rack::Rpx.new @lambda, :key => 'b', :secret => 'c'
|
23
|
+
app.name.should == 'default'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should be able to access an instance of Rack::Rpx by name from within Rack application' do
|
27
|
+
app = Rack::Rpx lambda {|env| [200, {}, [ env['rack.oauth'].keys.inspect ]] }, :key => 'b', :secret => 'c'
|
28
|
+
|
29
|
+
#rpx = Rack::Rpx.new @app, :key => 'b', :secret => 'c', :name => 'olokun'
|
30
|
+
get "/"
|
31
|
+
|
32
|
+
last_response.body.should contain("olokun")
|
33
|
+
|
6
34
|
end
|
35
|
+
|
7
36
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,26 +1,19 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
3
4
|
require 'rack-rpx'
|
5
|
+
require 'rack/test'
|
4
6
|
require 'spec'
|
5
7
|
require 'spec/autorun'
|
6
|
-
require '
|
7
|
-
require File.dirname(__FILE__) + '/../lib/rack-rpx'
|
8
|
+
require 'spec/interop/test'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
File.join File.dirname(__FILE__), 'data', "#{name}.yml"
|
17
|
-
end
|
18
|
-
|
19
|
-
# returns the string body of a file using data_path to get the path to the file
|
20
|
-
def data name
|
21
|
-
File.read data_path(name)
|
22
|
-
end
|
23
|
-
|
10
|
+
# set test environment
|
11
|
+
# set :environment, :test
|
12
|
+
# set :run, false
|
13
|
+
# set :raise_errors, true
|
14
|
+
# set :logging, false
|
15
|
+
|
16
|
+
# I dont know
|
24
17
|
def mock_request_token options = {}
|
25
18
|
YAML.load data(:unauthorized_request_token).sub('AUTH_PATH', options[:authorize_path] || '/oauth/authorize')
|
26
19
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
require 'rack/test'
|
3
|
+
|
4
|
+
|
5
|
+
class RackRpxTest < Test::Unit::TestCase
|
6
|
+
include Rack::Test::Methods
|
7
|
+
|
8
|
+
def app
|
9
|
+
app = Rack::Builder.app do
|
10
|
+
use Rack::Rpx, :port => '9393',
|
11
|
+
:api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
|
12
|
+
:callback_path => '/login_completed'
|
13
|
+
lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_redirect_logged_in_users_to_dashboard
|
18
|
+
get "/"
|
19
|
+
|
20
|
+
assert_equal "http://localhost:9393/login_completed", last_request.url
|
21
|
+
assert last_response.ok?
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rack-rpx'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
require 'spec/interop/test'
|
9
|
+
require 'test/spec'
|
10
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-rpx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Del Gallego
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-17 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- Rakefile
|
47
47
|
- VERSION
|
48
48
|
- examples/login-app.rb
|
49
|
+
- examples/login-hooks-sapp.rb
|
49
50
|
- examples/views/login.haml
|
50
51
|
- lib/rack-rpx.rb
|
51
52
|
- spec/rack-rpx_spec.rb
|
@@ -82,4 +83,7 @@ summary: Rack Middleware for RPX Now Authorization
|
|
82
83
|
test_files:
|
83
84
|
- spec/spec_helper.rb
|
84
85
|
- spec/rack-rpx_spec.rb
|
86
|
+
- test/test_helper.rb
|
87
|
+
- test/rack_rpx_test.rb
|
85
88
|
- examples/login-app.rb
|
89
|
+
- examples/login-hooks-sapp.rb
|