oa-cadun 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,4 +7,4 @@ require 'nokogiri'
7
7
  require 'date'
8
8
  require 'oa-core'
9
9
  require 'cadun'
10
- require 'omni_auth/strategies/cadun'
10
+ require 'omni_auth/strategies/cadun'
@@ -2,7 +2,7 @@ module OACadun
2
2
  module VERSION
3
3
  MAJOR = 3
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY] * '.'
8
8
  end
@@ -6,16 +6,15 @@ module OmniAuth
6
6
  module Strategies
7
7
  class Cadun
8
8
  include OmniAuth::Strategy
9
- include ::Cadun
10
9
 
11
10
  def initialize(app, options = {})
12
- Config.load_file(options[:config])
11
+ ::Cadun::Config.load_file(options[:config])
13
12
 
14
13
  super(app, :cadun, options)
15
14
  end
16
15
 
17
16
  def request_phase
18
- redirect "#{Config.login_url}/#{service_id}?url=#{callback_url}"
17
+ redirect "#{::Cadun::Config.login_url}/#{service_id}?url=#{callback_url}"
19
18
  end
20
19
 
21
20
  def callback_phase
@@ -30,16 +29,8 @@ module OmniAuth
30
29
  self.class.build_auth_hash(user, request)
31
30
  end
32
31
 
33
- def self.build_auth_hash(user, request = nil)
34
- hash = { :provider => "cadun", :uid => user.id, :user_info => user.to_hash.merge(:birthday => user.birthday.strftime('%d/%m/%Y')) }
35
- hash[:user_info].merge!(:GLBID => request.params['GLBID'], :url => request.params['url']) if request
36
-
37
- hash
38
- end
39
-
40
- protected
41
32
  def user
42
- @user ||= User.new(:glb_id => request.params['GLBID'], :ip => env['REMOTE_ADDR'], :service_id => service_id)
33
+ @user ||= ::Cadun::User.new(:glb_id => request.params['GLBID'], :ip => client_ip, :service_id => service_id)
43
34
  end
44
35
 
45
36
  def service_id
@@ -54,6 +45,21 @@ module OmniAuth
54
45
  callback_url = "#{scheme}://#{uri}#{port}/auth/cadun/callback"
55
46
  URI.escape(callback_url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
56
47
  end
48
+
49
+ def client_ip
50
+ if env['HTTP_X_FORWARDED_FOR'] and not env['HTTP_X_FORWARDED_FOR'].empty?
51
+ env['HTTP_X_FORWARDED_FOR'].split(',').last.strip
52
+ else
53
+ env['REMOTE_ADDR']
54
+ end
55
+ end
56
+
57
+ def self.build_auth_hash(user, request = nil)
58
+ hash = { :provider => "cadun", :uid => user.id, :user_info => user.to_hash.merge(:birthday => user.birthday.strftime('%d/%m/%Y')) }
59
+ hash[:user_info].merge!(:GLBID => request.params['GLBID'], :url => request.params['url']) if request
60
+
61
+ hash
62
+ end
57
63
  end
58
64
  end
59
- end
65
+ end
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.require_paths = %w(lib)
17
17
 
18
18
  s.add_dependency 'oa-core'
19
- s.add_dependency 'cadun', '0.3.0'
19
+ s.add_dependency 'cadun', '0.3.3'
20
20
  s.add_development_dependency 'rack'
21
21
  s.add_development_dependency 'rspec'
22
22
  s.add_development_dependency 'rr'
@@ -15,13 +15,11 @@ describe OmniAuth::Strategies::Cadun do
15
15
  end
16
16
 
17
17
  describe "status" do
18
- subject { @status }
19
- specify { should == 302 }
18
+ it { @status.should == 302 }
20
19
  end
21
20
 
22
21
  describe "headers" do
23
- subject { @headers }
24
- specify { should include("Location" => "https://login.dev.globoi.com/login/1?url=http%3A%2F%2Ftest.localhost%2Fauth%2Fcadun%2Fcallback") }
22
+ it { @headers.should include("Location" => "https://login.dev.globoi.com/login/1?url=http%3A%2F%2Ftest.localhost%2Fauth%2Fcadun%2Fcallback") }
25
23
  end
26
24
  end
27
25
 
@@ -33,13 +31,11 @@ describe OmniAuth::Strategies::Cadun do
33
31
  end
34
32
 
35
33
  describe "status" do
36
- subject { @status }
37
- specify { should == 302 }
34
+ it { @status.should == 302 }
38
35
  end
39
36
 
40
37
  describe "headers" do
41
- subject { @headers }
42
- specify { should include("Location" => "https://login.dev.globoi.com/login/1?url=http%3A%2F%2Ftest.localhost%3A8080%2Fauth%2Fcadun%2Fcallback") }
38
+ it { @headers.should include("Location" => "https://login.dev.globoi.com/login/1?url=http%3A%2F%2Ftest.localhost%3A8080%2Fauth%2Fcadun%2Fcallback") }
43
39
  end
44
40
  end
45
41
  end
@@ -47,69 +43,92 @@ describe OmniAuth::Strategies::Cadun do
47
43
  describe "#callback_phase" do
48
44
  context "when the authorization fails" do
49
45
  before do
50
- FakeWeb.register_uri :put, "http://isp-authenticator.dev.globoi.com:8280/ws/rest/autorizacao",
51
- :body => nil
52
-
53
- strategy.call! Rack::MockRequest.env_for("http://localhost/auth/cadun/callback", "rack.session" => {})
46
+ stub_fail_requests
47
+ strategy.call! Rack::MockRequest.env_for("http://localhost/auth/cadun/callback?GLBID=GLBID", "rack.session" => {}, "REMOTE_ADDR" => "127.0.0.1")
54
48
  end
55
49
 
56
- subject { strategy.callback_phase }
57
-
58
- specify { strategy.env['omniauth.auth'].should be_nil }
59
- specify { strategy.env['omniauth.error.type'].should == :invalid_credentials }
50
+ it { strategy.env['omniauth.auth'].should be_nil }
51
+ it { strategy.env['omniauth.error'].message.should == "not authorized" }
52
+ it { strategy.env['omniauth.error.type'].should == :invalid_credentials }
60
53
  end
61
54
 
62
55
  context "when the authorization succeeds" do
63
56
  before do
64
57
  stub_requests
65
- strategy.call! Rack::MockRequest.env_for("http://localhost/auth/cadun/callback", "rack.session" => {})
58
+ strategy.call! Rack::MockRequest.env_for("http://localhost/auth/cadun/callback?GLBID=GLBID", "rack.session" => {}, "REMOTE_ADDR" => "127.0.0.1")
66
59
  end
67
60
 
68
- subject { strategy.callback_phase }
69
-
70
- specify { strategy.env['omniauth.auth'].should_not be_nil }
71
- specify { strategy.env['omniauth.error.type'].should be_nil }
61
+ it { strategy.env['omniauth.auth'].should_not be_nil }
62
+ it { strategy.env['omniauth.error'].should be_nil }
63
+ it { strategy.env['omniauth.error.type'].should be_nil }
72
64
  end
73
65
  end
74
66
 
75
67
  describe "#auth_hash" do
76
68
  before do
77
69
  stub_requests
78
- strategy.call! Rack::MockRequest.env_for("http://localhost?GLBID=GLBID&url=/go_back", "rack.session" => {})
70
+ strategy.call! Rack::MockRequest.env_for("http://localhost?GLBID=GLBID&url=/go_back", "rack.session" => {}, "REMOTE_ADDR" => "127.0.0.1")
79
71
  end
80
72
 
81
- subject { strategy.auth_hash }
82
-
83
73
  describe ":uid" do
84
- specify { subject[:uid].should == "21737810" }
74
+ it { strategy.auth_hash[:uid].should == "21737810" }
85
75
  end
86
76
 
87
77
  describe ":provider" do
88
- specify { subject[:provider].should == "cadun" }
78
+ it { strategy.auth_hash[:provider].should == "cadun" }
89
79
  end
90
80
 
91
81
  describe ":user_info" do
92
- specify { subject[:user_info].should include(:address => "Rua Uruguai, 59") }
93
- specify { subject[:user_info].should include(:birthday => "22/02/1983") }
94
- specify { subject[:user_info].should include(:city => "Rio de Janeiro") }
95
- specify { subject[:user_info].should include(:country => "Brasil") }
96
- specify { subject[:user_info].should include(:cpf => "09532034765") }
97
- specify { subject[:user_info].should include(:email => "fab1@spam.la") }
98
- specify { subject[:user_info].should include(:gender => "MASCULINO") }
99
- specify { subject[:user_info].should include(:GLBID => "GLBID") }
100
- specify { subject[:user_info].should include(:cadun_id => "21737810") }
101
- specify { subject[:user_info].should include(:mobile => "21 99999999") }
102
- specify { subject[:user_info].should include(:name => "Fabricio Rodrigo Lopes") }
103
- specify { subject[:user_info].should include(:neighborhood => "Andaraí") }
104
- specify { subject[:user_info].should include(:login => "fabricio_fab1") }
105
- specify { subject[:user_info].should include(:phone => "21 22881060") }
106
- specify { subject[:user_info].should include(:state => "RJ") }
107
- specify { subject[:user_info].should include(:status => "ATIVO") }
108
- specify { subject[:user_info].should include(:url => "/go_back") }
109
- specify { subject[:user_info].should include(:user_type => "NAO_ASSINANTE") }
110
- specify { subject[:user_info].should include(:zipcode => "20510060") }
111
- specify { subject[:user_info].should include(:complement => "807") }
82
+ subject { strategy.auth_hash[:user_info] }
83
+
84
+ it { should include(:address => "Rua Uruguai, 59") }
85
+ it { should include(:birthday => "22/02/1983") }
86
+ it { should include(:city => "Rio de Janeiro") }
87
+ it { should include(:country => "Brasil") }
88
+ it { should include(:cpf => "09532034765") }
89
+ it { should include(:email => "fab1@spam.la") }
90
+ it { should include(:gender => "MASCULINO") }
91
+ it { should include(:GLBID => "GLBID") }
92
+ it { should include(:cadun_id => "21737810") }
93
+ it { should include(:mobile => "21 99999999") }
94
+ it { should include(:name => "Fabricio Rodrigo Lopes") }
95
+ it { should include(:neighborhood => "Andaraí") }
96
+ it { should include(:login => "fabricio_fab1") }
97
+ it { should include(:phone => "21 22881060") }
98
+ it { should include(:state => "RJ") }
99
+ it { should include(:status => "ATIVO") }
100
+ it { should include(:url => "/go_back") }
101
+ it { should include(:user_type => "NAO_ASSINANTE") }
102
+ it { should include(:zipcode => "20510060") }
103
+ it { should include(:complement => "807") }
104
+ end
105
+ end
106
+
107
+ describe "#client_ip" do
108
+ it 'should return ip from REMOTE_ADDR when it comes alone' do
109
+ strategy.call! Rack::MockRequest.env_for("http://test.localhost/auth/cadun", "rack.session" => {}, 'REMOTE_ADDR' => '200.201.0.15')
110
+ strategy.client_ip.should == '200.201.0.15'
111
+ end
112
+
113
+ it 'should return ip from REMOTE_ADDR when HTTP_X_FORWARDED_FOR is empty' do
114
+ strategy.call! Rack::MockRequest.env_for("http://test.localhost/auth/cadun", "rack.session" => {}, 'REMOTE_ADDR' => '200.201.0.20', 'HTTP_X_FORWARDED_FOR' => '')
115
+ strategy.client_ip.should == '200.201.0.20'
116
+ end
117
+
118
+ it 'should return ip from HTTP_X_FORWARDED_FOR when it comes alone' do
119
+ strategy.call! Rack::MockRequest.env_for("http://test.localhost/auth/cadun", "rack.session" => {}, 'HTTP_X_FORWARDED_FOR' => '201.10.0.15')
120
+ strategy.client_ip.should == '201.10.0.15'
121
+ end
122
+
123
+ it 'should return ip from HTTP_X_FORWARDED_FOR even if REMOTE_ADDR is present' do
124
+ strategy.call! Rack::MockRequest.env_for("http://test.localhost/auth/cadun", "rack.session" => {}, 'REMOTE_ADDR' => '200.201.0.15', 'HTTP_X_FORWARDED_FOR' => '201.10.0.16')
125
+ strategy.client_ip.should == '201.10.0.16'
126
+ end
127
+
128
+ it 'should always return the last ip from HTTP_X_FORWARDED_FOR' do
129
+ strategy.call! Rack::MockRequest.env_for("http://test.localhost/auth/cadun", "rack.session" => {}, 'HTTP_X_FORWARDED_FOR' => '201.10.0.15, 201.10.0.16, 201.10.0.17')
130
+ strategy.client_ip.should == '201.10.0.17'
112
131
  end
113
132
  end
114
133
 
115
- end
134
+ end
@@ -24,4 +24,12 @@ def stub_requests
24
24
 
25
25
  FakeWeb.register_uri :get, "http://isp-authenticator.dev.globoi.com:8280/cadunii/ws/resources/pessoa/21737810",
26
26
  :body => File.join(File.dirname(__FILE__), "support", "fixtures", "pessoa.xml")
27
- end
27
+ end
28
+
29
+ def stub_fail_requests
30
+ FakeWeb.register_uri :put, "http://isp-authenticator.dev.globoi.com:8280/ws/rest/autorizacao",
31
+ :body => File.join(File.dirname(__FILE__), "support", "fixtures", "autorizacao_fail.xml")
32
+
33
+ FakeWeb.register_uri :get, "http://isp-authenticator.dev.globoi.com:8280/cadunii/ws/resources/pessoa/21737810",
34
+ :body => File.join(File.dirname(__FILE__), "support", "fixtures", "pessoa.xml")
35
+ end
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <usuarioAutorizado>
3
+ <emailPrincipal>fab1@spam.la</emailPrincipal>
4
+ <glbId>1484e00106ea401d57902541631200e8a6d44556132366c754c4261655666625537614531655252536e6262626c63676676436c6c316744544d5636617651707a6d417a49756b6e3830415a4a394f36773a303a66616231407370616d2e6c61</glbId>
5
+ <ip>10.2.25.160</ip>
6
+ <login>fab1@spam.la</login>
7
+ <servicoID>2626</servicoID>
8
+ <status>NAO_AUTORIZADO</status>
9
+ <statusUsuario>ATIVO</statusUsuario>
10
+ <tipoUsuario>NAO_ASSINANTE</tipoUsuario>
11
+ <username>fabricio_fab1</username>
12
+ <usuarioID>21737810</usuarioID>
13
+ </usuarioAutorizado>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: oa-cadun
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.2.0
5
+ version: 3.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bruno
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-05-30 00:00:00 Z
15
+ date: 2011-06-08 00:00:00 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: oa-core
@@ -33,7 +33,7 @@ dependencies:
33
33
  requirements:
34
34
  - - "="
35
35
  - !ruby/object:Gem::Version
36
- version: 0.3.0
36
+ version: 0.3.3
37
37
  type: :runtime
38
38
  version_requirements: *id002
39
39
  - !ruby/object:Gem::Dependency
@@ -104,6 +104,7 @@ files:
104
104
  - spec/omni_auth/strategies/cadun_spec.rb
105
105
  - spec/spec_helper.rb
106
106
  - spec/support/fixtures/autorizacao.xml
107
+ - spec/support/fixtures/autorizacao_fail.xml
107
108
  - spec/support/fixtures/config.yml
108
109
  - spec/support/fixtures/pessoa.xml
109
110
  homepage: https://github.com/azisaka/oa-cadun