oa-casport 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ # oa-casport
2
+
3
+ The goal of this gem is to allow CASPORT integartion with your rack-based application easily through OmniAuth.
4
+
5
+ ## Installation
6
+
7
+ Add the following line to your Gemfile:
8
+
9
+ gem 'oa-casport'
10
+
11
+ ## Configuration Parameters:
12
+
13
+ Configuration within the initializer for OmniAuth:
14
+
15
+ # @example Basic Usage
16
+ #
17
+ # use OmniAuth::Strategies::Casport, {
18
+ # :setup => true
19
+ # }
20
+ #
21
+ # @example Full Options Usage
22
+ #
23
+ # use OmniAuth::Strategies::Casport, {
24
+ # :setup => true,
25
+ # :cas_server => 'http://cas.slkdemos.com/users/',
26
+ # :format => 'json', 'xml' || Defaults to 'xml'
27
+ # :format_header => 'application/xml', 'application/json' || Defaults to 'application/xml'
28
+ # :ssl_ca_file => 'path/to/ca_file.crt',
29
+ # :pem_cert => '/path/to/cert.pem',
30
+ # :pem_cert_pass => 'keep it secret, keep it safe.',
31
+ # :redis_options => 'disabled'
32
+ # }
33
+
34
+ ## Example Applications
35
+
36
+ You can see how to set it up and use it with a Rails 3 application at: [https://github.com/stevenhaddox/oa-casport-rails3](https://github.com/stevenhaddox/oa-casport-rails3)
37
+
38
+ \#TODO: You can see how to set it up and use it with a Sinatra application at: [https://github.com/stevenhaddox/oa-casport-sinatra](https://github.com/stevenhaddox/oa-casport-sinatra)
39
+
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Casport
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -2,6 +2,7 @@ require 'omniauth/core'
2
2
  require 'httparty'
3
3
  require 'redis'
4
4
  require 'uri'
5
+ require 'yaml'
5
6
 
6
7
  module OmniAuth
7
8
  module Strategies
@@ -11,19 +12,20 @@ module OmniAuth
11
12
  # @example Basic Usage
12
13
  #
13
14
  # use OmniAuth::Strategies::Casport, {
14
- # :setup => true
15
- # }
15
+ # :setup => true
16
+ # }
16
17
  # @example Full Options Usage
17
18
  #
18
19
  # use OmniAuth::Strategies::Casport, {
19
- # :setup => true,
20
- # :cas_server => 'http://cas.slkdemos.com/users/',
21
- # :format => 'json', 'xml', 'html', etc. || Defaults to 'xml'
22
- # :format_header => 'application/xml',
23
- # :ssl_ca_file => 'path/to/ca_file.crt',
24
- # :pem_cert => '/path/to/cert.pem',
25
- # :pem_cert_pass => 'keep it secret, keep it safe.'
26
- # }
20
+ # :setup => true,
21
+ # :cas_server => 'http://cas.slkdemos.com/users/',
22
+ # :format => 'json', 'xml', 'html', etc. || Defaults to 'xml'
23
+ # :format_header => 'application/xml',
24
+ # :ssl_ca_file => 'path/to/ca_file.crt',
25
+ # :pem_cert => '/path/to/cert.pem',
26
+ # :pem_cert_pass => 'keep it secret, keep it safe.',
27
+ # :redis_options => 'disabled' or options_hash || Defaults to {:host => '127.0.0.1', :port => 6739}
28
+ # }
27
29
  class Casport
28
30
 
29
31
  include OmniAuth::Strategy
@@ -65,7 +67,7 @@ module OmniAuth
65
67
  fail!(:invalid_user, e)
66
68
  end
67
69
  OmniAuth::Utils.deep_merge(super, {
68
- 'uid' => user_obj['uid'],
70
+ 'uid' => user_obj['dn'],
69
71
  'user_info' => {
70
72
  'name' => user_obj['full_name'],
71
73
  'email' => user_obj['email']
@@ -83,7 +85,9 @@ module OmniAuth
83
85
  # :pem_cert_pass - plaintext password, not recommended!
84
86
  def self.setup_httparty(opts)
85
87
  format opts[:format].to_sym
86
- headers 'Accept' => opts[:format_header]
88
+ headers 'Accept' => opts[:format_header]
89
+ headers 'Content-Type' => opts[:format_header]
90
+ headers 'X-XSRF-UseProtection' => 'false' if opts[:format] == 'json'
87
91
  if opts[:ssl_ca_file]
88
92
  ssl_ca_file opts[:ssl_ca_file]
89
93
  if opts[:pem_cert_pass]
@@ -98,12 +102,19 @@ module OmniAuth
98
102
  # Can't get user data without a UID from the application
99
103
  begin
100
104
  raise "No UID set in request.env['omniauth.strategy'].options[:uid]" if @options[:uid].nil?
101
- @options[:uid] = @options[:uid].to_s
105
+ # Fix DN order (if we have a DN) for CASPORT to work properly
106
+ if @options[:uid].include?('/') or @options[:uid].include?(',')
107
+ # Convert '/' to ',' and split on ','
108
+ @options[:uid] = @options[:uid].gsub('/',',').split(',').reject{|array| array.all? {|el| el.nil? || el.strip.empty? }}
109
+ # See if the DN is in the order CASPORT expects (and fix it if needed)
110
+ @options[:uid] = @options[:uid].reverse if @options[:uid].first.downcase.include? 'c='
111
+ # Join our array of DN elements back together with a comma as expected by CASPORT
112
+ @options[:uid] = @options.join ','
113
+ end
102
114
  rescue => e
103
115
  fail!(:uid_not_found, e)
104
116
  end
105
117
 
106
- url = URI.escape("#{@options[:cas_server]}/#{@options[:uid]}.#{@options[:format]}")
107
118
  begin
108
119
  raise Errno::ECONNREFUSED if @options[:redis_options] == 'disabled'
109
120
  cache = @options[:redis_options].nil? ? Redis.new : Redis.new(@options[:redis_options])
@@ -111,30 +122,33 @@ module OmniAuth
111
122
  # User is not in the cache
112
123
  # Retrieving the user data from CASPORT
113
124
  # {'userinfo' => {{'uid' => UID}, {'fullName' => NAME},...}},
114
- @user = Casport.get(url).parsed_response
115
- cache.set @options[:uid], @user
116
- # CASPORT expiration time for user (24 hours => 1440 seconds)
117
- cache.expire @options[:uid], 1440
125
+ get_user
126
+ if @user
127
+ # Set Redis object for the user, and expire after 24 hours
128
+ cache.set @options[:uid], @user.to_yaml
129
+ cache.expire @options[:uid], 1440
130
+ end
131
+ else
132
+ # We found our user in the cache, let's parse it into a Ruby object
133
+ @user = YAML::load(@user)
118
134
  end
119
135
  # If we can't connect to Redis...
120
136
  rescue Errno::ECONNREFUSED => e
121
- @user ||= Casport.get(url).parsed_response
137
+ get_user
122
138
  end
123
- @user = nil if user_empty?
124
139
  @user
125
140
  end
126
141
 
127
- # Investigate user_obj to see if it's empty (or anti-pattern data)
128
- def user_empty?
129
- is_empty = false
130
- is_empty = true if @user.nil?
131
- is_empty = true if @user.empty?
132
- # If it isn't empty yet, let's convert it into a Hash object for easy parsing via eval
133
- unless @user.class == Hash
134
- is_empty = true
135
- raise "String returned when a Hash was expected."
142
+ # Query for the user against CASPORT, return as nil or parsed object
143
+ def get_user
144
+ return if @user # no extra http calls
145
+ url = URI.escape("#{@options[:cas_server]}/#{@options[:uid]}.#{@options[:format]}")
146
+ response = Casport.get(url)
147
+ if response.success?
148
+ @user = response.parsed_response
149
+ else
150
+ @user = nil
136
151
  end
137
- is_empty == true ? true : nil
138
152
  end
139
153
 
140
154
  end
@@ -20,4 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.add_dependency 'oa-core'
21
21
  s.add_dependency 'httparty'
22
22
  s.add_dependency 'redis'
23
+
24
+ s.add_development_dependency 'rspec'
25
+ s.add_development_dependency 'fakeweb'
23
26
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + '../lib/omniauth/strategies/casport.rb')
3
+
4
+ desribe ".user" do
5
+ let(:app) { lamdba { |env| [200, {}, ['Test']] } }
6
+
7
+ it "should have correct xml returned" do
8
+ result = {'userinfo' => {'name' => 'Tyler Durden'}}
9
+ userinfo = '<userinfo><name>Tyler Durden</name></userinfo>'
10
+ FakeWeb.register_uri(:get, 'http://cas.dev/dn', :body => userinfo)
11
+ options = {:dn => 'dn', :cas_server => 'http://cas.dev/'}
12
+ user = OmniAuth::Strategies::Casport.new(app, options).user
13
+ user.should == result
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'fakeweb'
5
+ require 'oa-casport'
6
+ require 'httparty'
7
+ require 'redis'
8
+
9
+ RSpec.configure do |config|
10
+ # some (optional) config here
11
+ end
12
+
13
+ before(:each) do
14
+ FakeWeb.clean_registry
15
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: oa-casport
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesus Jackson
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-07-19 00:00:00 -04:00
14
+ date: 2011-07-28 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,28 @@ dependencies:
47
47
  version: "0"
48
48
  type: :runtime
49
49
  version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ - !ruby/object:Gem::Dependency
62
+ name: fakeweb
63
+ prerelease: false
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id005
50
72
  description: " Simple gem to enable rack powered Ruby apps to authenticate internally via casport with ease"
51
73
  email:
52
74
  - jesusejackson@gmail.com
@@ -61,12 +83,14 @@ files:
61
83
  - .gitignore
62
84
  - .rvmrc
63
85
  - Gemfile
64
- - README
86
+ - README.md
65
87
  - Rakefile
66
88
  - lib/oa-casport.rb
67
89
  - lib/oa-casport/version.rb
68
90
  - lib/omniauth/strategies/casport.rb
69
91
  - oa-casport.gemspec
92
+ - spec/casport_spec.rb
93
+ - spec/spec_helper.rb
70
94
  has_rdoc: true
71
95
  homepage: https://github.com/stevenhaddox/oa-casport
72
96
  licenses: []
@@ -95,5 +119,6 @@ rubygems_version: 1.6.2
95
119
  signing_key:
96
120
  specification_version: 3
97
121
  summary: OmniAuth gem for internal casport server
98
- test_files: []
99
-
122
+ test_files:
123
+ - spec/casport_spec.rb
124
+ - spec/spec_helper.rb
data/README DELETED
@@ -1,30 +0,0 @@
1
- # oa-casport is a custom strategy for authentication with Casport that works with the OmniAuth gem
2
-
3
- ## Examples:
4
-
5
- You can see how to set it up and use it with a Rails 3 application at: [https://github.com/stevenhaddox/oa-casport-rails3](https://github.com/stevenhaddox/oa-casport-rails3)
6
-
7
- \#TODO: You can see how to set it up and use it with a Sinatra application at: [https://github.com/stevenhaddox/oa-casport-sinatra](https://github.com/stevenhaddox/oa-casport-sinatra)
8
-
9
- ## Configuration Parameters:
10
-
11
- Configuration within the initializer for OmniAuth:
12
-
13
- # @example Basic Usage
14
- #
15
- # use OmniAuth::Strategies::Casport, {
16
- # :setup => true
17
- # }
18
- # @example Full Options Usage
19
- #
20
- # use OmniAuth::Strategies::Casport, {
21
- # :setup => true,
22
- # :cas_server => 'http://cas.slkdemos.com/users/',
23
- # :format => 'json', 'xml', 'html', etc. || Defaults to 'xml'
24
- # :format_header => 'application/xml',
25
- # :ssl_ca_file => 'path/to/ca_file.crt',
26
- # :pem_cert => '/path/to/cert.pem',
27
- # :pem_cert_pass => 'keep it secret, keep it safe.'
28
- # }
29
-
30
-