oa-casport 0.1.0

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@oa-casport
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in oa-casport.gemspec
4
+ gemspec
data/README ADDED
File without changes
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,10 @@
1
+ $:.push File.expand_path('lib', __FILE__)
2
+
3
+ require "oa-casport/version"
4
+ require 'omniauth/core'
5
+
6
+ module OmniAuth
7
+ module Strategies
8
+ autoload :Casport, 'omniauth/strategies/casport'
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Casport
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,142 @@
1
+ require 'omniauth/core'
2
+ require 'httparty'
3
+ require 'redis'
4
+ require 'uri'
5
+
6
+ module OmniAuth
7
+ module Strategies
8
+ #
9
+ # Authentication to CASPORT
10
+ #
11
+ # @example Basic Usage
12
+ #
13
+ # use OmniAuth::Strategies::Casport, {
14
+ # :setup => true
15
+ # }
16
+ # @example Full Options Usage
17
+ #
18
+ # use OmniAuth::Strategies::Casport, {
19
+ # :setup => true,
20
+ # :cas_server => 'http://cas.slkdemos.com/users/',
21
+ # :format => '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
+ # }
27
+ class Casport
28
+
29
+ include OmniAuth::Strategy
30
+ include HTTParty
31
+
32
+ def initialize(app, options)
33
+ super(app, :casport)
34
+ @options = options
35
+ @options[:cas_server] ||= 'http://cas.dev/users'
36
+ @options[:format] ||= 'xml'
37
+ @options[:format_header] ||= 'application/xml'
38
+ end
39
+
40
+ def request_phase
41
+ # Can't get user data without their UID for the CASPORT server
42
+ raise "No UID set in request.env['omniauth.strategy'].options[:uid]" if @options[:uid].nil?
43
+ Casport.setup_httparty(@options)
44
+ redirect(callback_path)
45
+ end
46
+
47
+ def callback_phase
48
+ begin
49
+ raise 'We seemed to have misplaced your credentials... O_o' if user.nil?
50
+ super
51
+ rescue => e
52
+ redirect(request_path)
53
+ # fail!(:invalid_credentials, e)
54
+ end
55
+ call_app!
56
+ end
57
+
58
+ def auth_hash
59
+ # store user in a local var to avoid new method calls for each attribute
60
+ user_obj = user
61
+ begin
62
+ # convert all Java camelCase keys to Ruby snake_case, it just feels right!
63
+ user_obj = user_obj['userinfo'].inject({}){|memo, (k,v)| memo[k.gsub(/[A-Z]/){|c| '_'+c.downcase}] = v; memo}
64
+ rescue => e
65
+ fail!(:invalid_user, e)
66
+ end
67
+ OmniAuth::Utils.deep_merge(super, {
68
+ 'uid' => user_obj['uid'],
69
+ 'user_info' => {
70
+ 'name' => user_obj['full_name'],
71
+ 'email' => user_obj['email']
72
+ },
73
+ 'extra' => {'user_hash' => user_obj}
74
+ })
75
+ end
76
+
77
+ # Set HTTParty params that we need to set after initialize is called
78
+ # These params come from @options within initialize and include the following:
79
+ # :ssl_ca_file - SSL CA File for SSL connections
80
+ # :format - 'json', 'xml', 'html', etc. || Defaults to 'xml'
81
+ # :format_header - :format Header string || Defaults to 'application/xml'
82
+ # :pem_cert - /path/to/a/pem_formatted_certificate.pem for SSL connections
83
+ # :pem_cert_pass - plaintext password, not recommended!
84
+ def self.setup_httparty(opts)
85
+ format opts[:format].to_sym
86
+ headers 'Accept' => opts[:format_header]
87
+ if opts[:ssl_ca_file]
88
+ ssl_ca_file opts[:ssl_ca_file]
89
+ if opts[:pem_cert_pass]
90
+ pem File.read(opts[:pem_cert]), opts[:pem_cert_pass]
91
+ else
92
+ pem File.read(opts[:pem_cert])
93
+ end
94
+ end
95
+ end
96
+
97
+ def user
98
+ # Can't get user data without a UID from the application
99
+ begin
100
+ raise "No UID set in request.env['omniauth.strategy'].options[:uid]" if @options[:uid].nil?
101
+ @options[:uid] = @options[:uid].to_s
102
+ rescue => e
103
+ fail!(:uid_not_found, e)
104
+ end
105
+
106
+ url = URI.escape("#{@options[:cas_server]}/#{@options[:uid]}.#{@options[:format]}")
107
+ begin
108
+ raise Errno::ECONNREFUSED if @options[:redis_options] == 'disabled'
109
+ cache = @options[:redis_options].nil? ? Redis.new : Redis.new(@options[:redis_options])
110
+ unless @user = (cache.get @options[:uid])
111
+ # User is not in the cache
112
+ # Retrieving the user data from CASPORT
113
+ # {'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
118
+ end
119
+ # If we can't connect to Redis...
120
+ rescue Errno::ECONNREFUSED => e
121
+ @user ||= Casport.get(url).parsed_response
122
+ end
123
+ @user = nil if user_empty?
124
+ @user
125
+ end
126
+
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."
136
+ end
137
+ is_empty == true ? true : nil
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "oa-casport/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "oa-casport"
7
+ s.version = OmniAuth::Casport::VERSION
8
+ s.authors = ["Jesus Jackson", "Steven Haddox"]
9
+ s.email = ["jesusejackson@gmail.com", "stevenhaddox@shortmail.com"]
10
+ s.homepage = "http://oa-casport.slkdemos.com"
11
+ s.summary = %q{OmniAuth gem for internal casport server}
12
+ s.description = %q{ Simple gem to enable rack powered Ruby apps to authenticate internally via CASPORT with ease}
13
+ s.rubyforge_project = "oa-casport"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'oa-core'
21
+ s.add_dependency 'httparty'
22
+ s.add_dependency 'redis'
23
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oa-casport
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jesus Jackson
9
+ - Steven Haddox
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-07-19 00:00:00 -04:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: oa-core
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: httparty
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: "0"
37
+ type: :runtime
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: redis
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id003
50
+ description: " Simple gem to enable rack powered Ruby apps to authenticate internally via CASPORT with ease"
51
+ email:
52
+ - jesusejackson@gmail.com
53
+ - stevenhaddox@shortmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - .gitignore
62
+ - .rvmrc
63
+ - Gemfile
64
+ - README
65
+ - Rakefile
66
+ - lib/oa-casport.rb
67
+ - lib/oa-casport/version.rb
68
+ - lib/omniauth/strategies/casport.rb
69
+ - oa-casport.gemspec
70
+ has_rdoc: true
71
+ homepage: http://oa-casport.slkdemos.com
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project: oa-casport
94
+ rubygems_version: 1.6.2
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: OmniAuth gem for internal casport server
98
+ test_files: []
99
+