omniauth_uoc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc611eca086f22bcba097b2c09d0ab66687df525
4
+ data.tar.gz: 35e4491db80795ad7cf0e858ab296942ed654d37
5
+ SHA512:
6
+ metadata.gz: 48b0b51b585c7ae15b5d5763a7717385aa3c3760af761cd211f56c4d6f4c1f48d134f46cf0e3ffde0c0e261c03646b766c1a5dd3bc59fe3005a0b1ebc993ed19
7
+ data.tar.gz: 661a33a3619a9acdd57acf7979b72eccb2fc32dff3b35c6df0f6a8357d50baffa4edf14efcc20c46260f4bd1f44b95f2aca1f26b777161b33916c7334eddf98c
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth_uoc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 rromerogar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # omniauth_uoc
2
+
3
+ The omniauth_uoc library is an OmniAuth provider that supports authentication against UOC REST apis
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'omniauth', '~> 1.0'
10
+ gem 'omniauth_uoc'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install omniauth_uoc
19
+
20
+ ## Usage
21
+
22
+ You will need to configure OmniAuth to use your uoc authentication. This is generally done in Rails in the config/initializers/omniauth.rb with:
23
+
24
+ Rails.application.config.middleware.use OmniAuth::Builder do
25
+ provider :uoc, :uoc_server_url=>"https://cv.uoc.edu/"
26
+ end
27
+
28
+ ## References
29
+
30
+ * OmniAuth: https://github.com/intridea/omniauth/
31
+ * Especially thanks to [Rob Di Marco](https://github.com/robdimarco) and his [omniauth_crowd](https://github.com/robdimarco/omniauth_crowd) project that it was inspiration for me.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/rromerogar/omniauth_uoc/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
40
+
41
+ ## Copyright
42
+
43
+ Copyright (c) 2014 Universitat Oberta de Catalunya. See LICENSE.txt for further details.
44
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,35 @@
1
+ require 'rack'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Uoc
6
+ class Configuration
7
+ DEFAULT_SESSION_URL = '%s/webapps/campusGateway/sessions'
8
+
9
+ attr_reader :session_url
10
+
11
+ def initialize(params)
12
+ parse_params params
13
+ end
14
+
15
+ private
16
+ IS_NOT_URL_ERROR_MESSAGE = '%s is not a valid URL'
17
+
18
+ def parse_params(options)
19
+ unless options.include?(:uoc_server_url)
20
+ raise ArgumentError.new('Either :uoc_server_url MUST be provided')
21
+ end
22
+
23
+ @session_url = options[:uoc_session_url] || DEFAULT_SESSION_URL % options[:uoc_server_url]
24
+
25
+ validate_is_url 'session URL', @session_url
26
+ end
27
+
28
+ def validate_is_url(name, possibly_a_url)
29
+ url = URI.parse(possibly_a_url) rescue nil
30
+ raise ArgumentError.new(IS_NOT_URL_ERROR_MESSAGE % name) unless url.kind_of?(URI::HTTP)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,77 @@
1
+ require 'multi_xml'
2
+ require 'faraday'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Uoc
7
+ class UocValidator
8
+ DEFAULT_CONTENT_TYPE = 'application/xml'
9
+ SESSION_REQUEST_BODY = <<-BODY.strip
10
+ <session>
11
+ <name>%s</name>
12
+ <password>%s</password>
13
+ </session>
14
+ BODY
15
+
16
+ def initialize(configuration, username, password)
17
+ @configuration, @username, @password = configuration, username, password
18
+ @session_uri = URI.parse(@configuration.session_url)
19
+ MultiXml.parser = :nokogiri
20
+ end
21
+
22
+ def user_info
23
+ retrieve_user_info!
24
+ end
25
+
26
+ private
27
+ def retrieve_user_info!
28
+ response = make_session_request
29
+ user_info = nil
30
+ unless response.status.to_i != 201 || response.body.nil? || response.body == ''
31
+ xml = MultiXml.parse(response.body)
32
+ if(xml['session']['authenticated'] == 'true')
33
+ user_info = {
34
+ :name => xml['session']['fullname'],
35
+ :email => xml['session']['email'],
36
+ :nickname =>xml['session']['login'],
37
+ :extra => {
38
+ :s => xml['session']['id'],
39
+ :user_id => xml['session']['userId'],
40
+ :user_number => xml['session']['userNumber'],
41
+ :lang => xml['session']['lang'],
42
+ :locale => xml['session']['locale'],
43
+ }
44
+ }
45
+ end
46
+ end
47
+ if user_info.nil?
48
+ OmniAuth.logger.send(:warn, "(crowd) [retrieve_user_info!] response code: #{response.status.to_s}")
49
+ OmniAuth.logger.send(:warn, "(crowd) [retrieve_user_info!] response body: #{response.body}")
50
+ end
51
+ user_info
52
+ end
53
+
54
+ def make_session_request
55
+ conn = Faraday.new(:url => @session_uri.to_s) do |faraday|
56
+ faraday.request :url_encoded # form-encode POST params
57
+ faraday.response :logger # log requests to STDOUT
58
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
59
+ end
60
+
61
+ conn.post do |req|
62
+ req.headers['Content-Type'] = DEFAULT_CONTENT_TYPE
63
+ req.body = make_session_request_body(@username, @password)
64
+ end
65
+ end
66
+
67
+ def make_session_request_body(username,password)
68
+ request_body = MultiXml.parse(SESSION_REQUEST_BODY)
69
+ request_body['session']['name'] = username
70
+ request_body['session']['password'] = password
71
+ request_body['session'].to_xml :root => :session
72
+ end
73
+
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,53 @@
1
+ require 'omniauth'
2
+ module OmniAuth
3
+ module Strategies
4
+ class Uoc
5
+ include OmniAuth::Strategy
6
+
7
+ autoload :Configuration, 'omniauth/strategies/uoc/configuration'
8
+ autoload :UocValidator, 'omniauth/strategies/uoc/uoc_validator'
9
+
10
+ def initialize(app, options = {}, &block)
11
+ options.symbolize_keys!()
12
+ super(app, {:name=> :uoc}.merge(options), &block)
13
+ @configuration = OmniAuth::Strategies::Uoc::Configuration.new(options)
14
+ end
15
+
16
+ protected
17
+
18
+ def request_phase
19
+ if env['REQUEST_METHOD'] == 'GET'
20
+ get_credentials
21
+ else
22
+ session['omniauth.uoc'] = {'username' => request['username'], 'password' => request['password']}
23
+ redirect callback_url
24
+ end
25
+ end
26
+
27
+ def get_credentials
28
+ OmniAuth::Form.build(:title => (options[:title] || 'UOC Authentication')) do
29
+ text_field 'Login', 'username'
30
+ password_field 'Password', 'password'
31
+ end.to_response
32
+ end
33
+
34
+ def callback_phase
35
+ creds = session.delete 'omniauth.uoc'
36
+ return fail!(:no_credentials) unless creds
37
+ validator = UocValidator.new(@configuration, creds['username'], creds['password'])
38
+ @user_info = validator.user_info
39
+
40
+ return fail!(:invalid_credentials) if @user_info.nil? || @user_info.empty?
41
+
42
+ super
43
+ end
44
+
45
+ def auth_hash
46
+ OmniAuth::Utils.deep_merge(super, {
47
+ 'uid' => @user_info[:extra][:id],
48
+ 'info' => @user_info
49
+ })
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module OmniauthUoc
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require 'omniauth/strategies/uoc'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth_uoc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'omniauth_uoc'
8
+ spec.version = OmniauthUoc::VERSION
9
+ spec.authors = ['rromerogar']
10
+ spec.email = ['rromerogar@uoc.edu']
11
+ spec.summary = %q{ OmniAuth provider that supports authentication against UOC REST apis. }
12
+ spec.homepage = 'http://github.com/rromerogar/omniauth_uoc'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_dependency 'omniauth', '~> 1.0'
23
+ spec.add_dependency 'multi_xml', '~> 0.5.5'
24
+ spec.add_dependency 'faraday', '~> 0.9.0'
25
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth_uoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rromerogar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: omniauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_xml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.0
83
+ description:
84
+ email:
85
+ - rromerogar@uoc.edu
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/omniauth/strategies/uoc.rb
96
+ - lib/omniauth/strategies/uoc/configuration.rb
97
+ - lib/omniauth/strategies/uoc/uoc_validator.rb
98
+ - lib/omniauth_uoc.rb
99
+ - lib/omniauth_uoc/version.rb
100
+ - omniauth_uoc.gemspec
101
+ homepage: http://github.com/rromerogar/omniauth_uoc
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.11
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: OmniAuth provider that supports authentication against UOC REST apis.
125
+ test_files: []