omniauth-infinum 0.9.7

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34c9a53f0a650d22f963c93f462ecd28c26d74de
4
+ data.tar.gz: a54cbc44da5bcbddafa1a13ae66dfb42dfa938d1
5
+ SHA512:
6
+ metadata.gz: 2998ecce1785a94e4a19f6d0b9d80993373abc64aa145aba31e283c104635ce469ca19fc6361b277d3333b35671f46c863a2e57a643e18950fd3cabf9886cc04
7
+ data.tar.gz: 5ec424bc25650101372ee5ec4c9179ad7ac11f2f2c9b1f7c09352b3569505bfe4d3c121a707396f3ea439c934af8fc2f3ad1a002cfdd48a19b8bbeff53d73c87
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Readme.md ADDED
@@ -0,0 +1,176 @@
1
+ # Introduction
2
+
3
+ omniauth-infinum is a gem for integrating the Infinum ID Single Sign On system in a Ruby on Rails web application.
4
+
5
+ Anybody can register for an Infinum ID by visiting [https://accounts.infinum.co](https://accounts.infinum.co)
6
+
7
+ # Gem installation
8
+
9
+ The gem isn't hosted on Gemcutter, but rather on a private gem server, [http://gems.infinum.co](http://gems.infinum.co).
10
+
11
+ To use this private gem server, you need to add a new gem source
12
+
13
+ gem sources -a http://gems.infinum.co
14
+
15
+ Afer that, install the gem.
16
+
17
+ gem install omniauth-infinum
18
+
19
+ If you're including the gem in your Gemfile, you need to also manually specify the source
20
+
21
+ source 'http://gems.infinum.co'
22
+ gem "omniauth-infinum"
23
+
24
+
25
+ # Implementation
26
+
27
+ There is a possibility to implement the omniauth-infinum gem with a generator, or manually. If your model will be called other than User (such as Admin or similar), we advise you create everything manually.
28
+ Otherwise we reccommend you use the generator:
29
+
30
+
31
+ # Omniauth-infinum gem implementation with a generator:
32
+
33
+
34
+ Run the generator with:
35
+
36
+ rails g omniauth_infinum:install
37
+
38
+ Go to your config/initializers/omniauth-infinum.rb file and update the APP_ID and APP_SECRET.
39
+
40
+ Run the migration
41
+
42
+ rake db:migrate
43
+
44
+ And finish the implementation with adding the .new_from_omniauth class method to the User model:
45
+
46
+ def self.new_from_omniauth(omniauth)
47
+ user = User.find_by_uid(omniauth['uid']) || User.find_by_email(omniauth[:extra][:email]) || User.new(:uid => omniauth['uid'])
48
+ # refresh info
49
+ user.uid = omniauth['uid']
50
+ user.first_name = omniauth[:extra][:first_name]
51
+ user.last_name = omniauth[:extra][:last_name]
52
+ user.email = omniauth[:extra][:email]
53
+ user.avatar_url = omniauth[:extra][:avatar_url]
54
+ user.save
55
+ user
56
+ end
57
+
58
+ # Omniauth-infinum gem implementation manually:
59
+
60
+
61
+ ## Initializing
62
+
63
+ To initialize the omniauth provider, you need to add an initializer with a given secret and access key. Add this to <tt>config/initializers/omniauth.rb</tt>:
64
+
65
+ OmniAuth::Strategies::Infinum.setup do |config|
66
+ config.url = "http://accounts.infinum.co#{Rails.env.development? ? '.dev' : ''}"
67
+ end
68
+
69
+ APP_ID = 'some_key'
70
+ APP_SECRET = 'some_secret'
71
+
72
+ Rails.application.config.middleware.use OmniAuth::Builder do
73
+ provider :infinum, APP_ID, APP_SECRET
74
+ end
75
+
76
+ ## Routes
77
+
78
+ For the success/failure callbacks and logout functionality, you need to add some routes to the application:
79
+
80
+ get '/auth/:provider/callback', :to => 'omniauth#success'
81
+ get '/auth/failure', :to => 'omniauth#failure'
82
+ get '/logout', :to => 'omniauth#logout'
83
+
84
+ You'll also need to add a controller to handle the callbacks
85
+
86
+ class OmniauthController < ApplicationController
87
+ before_filter :authenticate_user!, :only => [ :logout ]
88
+
89
+ def success
90
+ session[:user_id] = env['omniauth.auth']
91
+ user = User.new_from_omniauth(session[:user_id])
92
+ redirect_to request.env['omniauth.origin'] || root_path, notice: "Successfully logged in"
93
+ end
94
+
95
+ def failure
96
+ flash[:alert] = params[:message]
97
+ #remember to add a template to render out or redirect to somewhere
98
+ end
99
+
100
+ # logout - Clear our rack session BUT essentially redirect to the provider to clean up the Devise session from there too !
101
+ def logout
102
+ session[:user_id] = nil
103
+ redirect_to "#{OmniAuth::Strategies::Infinum.url}/users/sign_out?redirect_to=#{root_url}", notice: 'You have successfully signed out!'
104
+ end
105
+ end
106
+
107
+ ## Model implementation
108
+
109
+ In the <tt>User</tt> model, you'll need to implement the <tt>new_from_omniauth</tt> method like so:
110
+
111
+ class User < ActiveRecord::Base
112
+ def self.new_from_omniauth(omniauth)
113
+ user = User.find_by_uid(omniauth['uid']) || User.find_by_email(omniauth[:extra][:email]) || User.new(:uid => omniauth['uid'])
114
+ # refresh info
115
+ user.uid = omniauth['uid']
116
+ user.first_name = omniauth[:extra][:first_name]
117
+ user.last_name = omniauth[:extra][:last_name]
118
+ user.email = omniauth[:extra][:email]
119
+ user.avatar_url = omniauth[:extra][:avatar_url]
120
+ user.save
121
+ end
122
+ end
123
+
124
+ ## Migrations
125
+
126
+ Your <tt>User</tt> model will also need to have a <tt>string</tt> field called <tt>uid</tt> which hold the user ide from the <tt>accounts.infinum.co</tt> database
127
+
128
+ rails g migration add_uid_to_users uid:string
129
+ rake db:migrate
130
+
131
+ ## Important notice
132
+
133
+ If the provider type is **private** the Infinum ID administrator will need to manually assign each user to each particular service when they register.
134
+
135
+ If the provider type is **public**, this is not necessary.
136
+
137
+ # That's it!
138
+
139
+ Give it a spin!
140
+
141
+ # Updating to 0.9.5 or higher
142
+
143
+ If you wish to change the development address of accounts.infinum.co, add this line to your <tt>config/initializers/omniauth.rb</tt> file:
144
+
145
+ OmniAuth::Strategies::Infinum.setup do |config|
146
+ config.url = Rails.env.development? ? "http://accounts.infinum.co.dev" :"http://accounts.infinum.co"
147
+ end
148
+
149
+
150
+ # TestHelpers
151
+
152
+ Omniauth-infinum provides two test helpers, <tt>sign_in(user)</tt> and <tt>sign_out</tt>
153
+
154
+ Require the test_helper file:
155
+
156
+ require 'omniauth-infinum/test_helpers'
157
+
158
+ Add the following to your `test/test_helper.rb` file:
159
+
160
+ ```ruby
161
+ class ActionController::TestCase
162
+ include OmniAuth::Infinum::TestHelpers
163
+ end
164
+ ```
165
+
166
+ If you're using RSpec, you can put the following inside a file named `spec/spec_helper.rb`:
167
+
168
+ ```ruby
169
+ RSpec.configure do |config|
170
+ config.include OmniAuth::Infinum::TestHelpers
171
+ end
172
+ ```
173
+
174
+ # Copyright
175
+
176
+ Copyright © 2013 Tomislav Car, Infinum
@@ -0,0 +1,35 @@
1
+ module OmniauthInfinum
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ def create_a_config_file
6
+ copy_file "config/initializers/omniauth-infinum.rb", "config/initializers/omniauth-infinum.rb"
7
+ end
8
+
9
+ def create_the_omniauth_controller
10
+ copy_file "app/controllers/omniauth_controller.rb", "app/controllers/omniauth_controller.rb"
11
+ end
12
+
13
+ def add_routes
14
+ routes = " get '/auth/:provider/callback', :to => 'omniauth#success'\n" +
15
+ " get '/auth/failure' , :to => 'omniauth#failure'\n" +
16
+ " get '/logout', :to => 'omniauth#logout'\n"
17
+
18
+ file_content = File.read('config/routes.rb')
19
+ file_content.sub!("routes.draw do\n", "routes.draw do\n\n#{routes}")
20
+ File.open("config/routes.rb", 'w') { |file| file.write(file_content)}
21
+ say_status "insert", "routes.rb file -> 'Success, failure and logout routes added.'", :green
22
+ end
23
+
24
+ def rails_generate_migration
25
+ copy_file "db/migrate/add_uid_to_users.rb", "db/migrate/#{migrationNumber}_add_uid_to_users.rb"
26
+ end
27
+
28
+ private
29
+
30
+ def migrationNumber
31
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ class OmniauthController < ApplicationController
2
+ before_filter :authenticate_user!, :only => [ :logout ]
3
+
4
+ def success
5
+ session[:user_id] = env['omniauth.auth']
6
+ user = User.new_from_omniauth(session[:user_id])
7
+ redirect_to request.env['omniauth.origin'] || root_path, notice: "Successfully logged in"
8
+ end
9
+
10
+ def failure
11
+ flash[:alert] = params[:message]
12
+ #remember to add a template to render out or redirect to somewhere
13
+ end
14
+
15
+ # logout - Clear our rack session BUT essentially redirect to the provider to clean up the Devise session from there too !
16
+ def logout
17
+ session[:user_id] = nil
18
+ redirect_to "#{OmniAuth::Strategies::Infinum.url}/users/sign_out?redirect_to=#{root_url}", notice: 'You have successfully signed out!'
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ OmniAuth::Strategies::Infinum.setup do |config|
2
+ config.url = "http://accounts.infinum.co#{Rails.env.development? ? '.dev' : ''}"
3
+ end
4
+
5
+ APP_ID = 'your_app_id'
6
+ APP_SECRET = 'your_app_secret'
7
+
8
+ Rails.application.config.middleware.use OmniAuth::Builder do
9
+ provider :infinum, APP_ID, APP_SECRET
10
+ end
@@ -0,0 +1,5 @@
1
+ class AddUidToUsers < ActiveRecord::Migration
2
+ def change
3
+ add_column :users, :uid, :string
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "omniauth-infinum/version"
2
+ require "omniauth-infinum/controller"
3
+ require 'omniauth/strategies/infinum'
4
+
5
+ if defined? ActionController::Base
6
+ ActionController::Base.send(:include, OmniAuth::Infinum::Controller)
7
+ end
@@ -0,0 +1,28 @@
1
+ module OmniAuth::Infinum::Controller
2
+ module InstanceMethods
3
+ def authenticate_user!
4
+ if current_user.blank?
5
+ respond_to do |format|
6
+ format.html {
7
+ redirect_to "/auth/infinum?origin=#{request.url}"
8
+ }
9
+ format.json {
10
+ render :json => { 'error' => 'Access Denied' }.to_json
11
+ }
12
+ end
13
+ end
14
+ end
15
+
16
+ def current_user
17
+ return nil unless session[:user_id]
18
+
19
+ @current_user ||= User.new_from_omniauth(session[:user_id])
20
+ end
21
+ end
22
+
23
+ def self.included(receiver)
24
+ receiver.send :include, InstanceMethods
25
+
26
+ receiver.helper_method :current_user
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module OmniAuth::Infinum
2
+ module TestHelpers
3
+
4
+ def sign_in(user)
5
+ session[:user_id] = OmniAuth::AuthHash.new({
6
+ :provider => 'infinum',
7
+ :uid => (user.try(:uid) || '75'),
8
+ :extra => {
9
+ :first_name => user.try(:first_name),
10
+ :last_name => user.try(:last_name),
11
+ :email => user.try(:email),
12
+ :avatar_url => user.try(:avatar_url)
13
+ }
14
+ })
15
+ end
16
+
17
+ def sign_out
18
+ session[:user_id] = nil
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Infinum
3
+ VERSION = "0.9.7"
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Infinum < OmniAuth::Strategies::OAuth2
6
+
7
+ # mattr_accessor :url
8
+ def self.url
9
+ @@url
10
+ end
11
+
12
+ def self.url=(some_value)
13
+ @@url = some_value
14
+ end
15
+
16
+ def self.development_environment?
17
+ if defined? Rails
18
+ Rails.env.development?
19
+ else
20
+ ENV['RACK_ENV'].nil? || ENV['RACK_ENV'] == "development"
21
+ end
22
+ end
23
+
24
+ @@url = "http://accounts.infinum.co#{development_environment? ? '.dev' : ''}"
25
+
26
+ def self.setup
27
+ yield self if block_given?
28
+
29
+ option :client_options, {
30
+ :site => @@url,
31
+ :authorize_url => "#{@@url}/auth/infinum/authorize",
32
+ :access_token_url => "#{@@url}/auth/infinum/access_token"
33
+ }
34
+ end
35
+
36
+ option :client_options, {
37
+ :site => @@url,
38
+ :authorize_url => "#{@@url}/auth/infinum/authorize",
39
+ :access_token_url => "#{@@url}/auth/infinum/access_token"
40
+ }
41
+
42
+ uid { raw_info['id'] }
43
+
44
+ info do
45
+ {
46
+ :email => raw_info['email']
47
+ }
48
+ end
49
+
50
+ extra do
51
+ {
52
+ :first_name => raw_info['extra']['first_name'],
53
+ :last_name => raw_info['extra']['last_name'],
54
+ :email => raw_info['extra']['email'],
55
+ :avatar_url => raw_info['extra']['avatar_url']
56
+ }
57
+ end
58
+
59
+ def raw_info
60
+ @raw_info ||= access_token.get("/auth/infinum/user.json?oauth_token=#{access_token.token}").parsed
61
+ end
62
+
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,118 @@
1
+ require 'cgi'
2
+ require 'uri'
3
+ require 'oauth2'
4
+ require 'omniauth'
5
+ require 'timeout'
6
+ require 'securerandom'
7
+ require 'omniauth-oauth2'
8
+
9
+
10
+ module OmniAuth
11
+ module Strategies
12
+ class OAuth2
13
+ include OmniAuth::Strategy
14
+
15
+ args [:client_id, :client_secret]
16
+
17
+ option :client_id, nil
18
+ option :client_secret, nil
19
+ option :client_options, {}
20
+ option :authorize_params, {}
21
+ option :authorize_options, [:scope]
22
+ option :token_params, {}
23
+ option :token_options, []
24
+ option :provider_ignores_state, false
25
+
26
+ attr_accessor :access_token
27
+
28
+ def client
29
+ ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
30
+ end
31
+
32
+ def callback_url
33
+ full_host + script_name + callback_path
34
+ end
35
+
36
+ credentials do
37
+ hash = {'token' => access_token.token}
38
+ hash.merge!('refresh_token' => access_token.refresh_token) if access_token.expires? && access_token.refresh_token
39
+ hash.merge!('expires_at' => access_token.expires_at) if access_token.expires?
40
+ hash.merge!('expires' => access_token.expires?)
41
+ hash
42
+ end
43
+
44
+ def request_phase
45
+ redirect client.auth_code.authorize_url({:redirect_uri => callback_url}.merge(authorize_params))
46
+ end
47
+
48
+ def authorize_params
49
+ options.authorize_params[:state] = SecureRandom.hex(24)
50
+ params = options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
51
+ if OmniAuth.config.test_mode
52
+ @env ||= {}
53
+ @env['rack.session'] ||= {}
54
+ end
55
+ session['omniauth.state'] = params[:state]
56
+ params
57
+ end
58
+
59
+ def token_params
60
+ options.token_params.merge(options.token_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
61
+ end
62
+
63
+
64
+
65
+ def callback_phase
66
+ if request.params['error'] || request.params['error_reason']
67
+ raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
68
+ end
69
+ if !options.provider_ignores_state && (request.params['state'].to_s.empty? || request.params['state'] != session.delete('omniauth.state'))
70
+ raise CallbackError.new(nil, :csrf_detected)
71
+ end
72
+
73
+ self.access_token = build_access_token
74
+ self.access_token = access_token.refresh! if access_token.expired?
75
+
76
+ super
77
+ rescue ::OAuth2::Error => e
78
+ fail!(e.code, e)
79
+ rescue CallbackError => e
80
+ fail!(e.error_reason,e)
81
+ rescue ::MultiJson::DecodeError => e
82
+ fail!(:invalid_response, e)
83
+ rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
84
+ fail!(:timeout, e)
85
+ rescue ::SocketError => e
86
+ fail!(:failed_to_connect, e)
87
+ end
88
+
89
+ protected
90
+
91
+ def deep_symbolize(hash)
92
+ hash.inject({}) do |h, (k,v)|
93
+ h[k.to_sym] = v.is_a?(Hash) ? deep_symbolize(v) : v
94
+ h
95
+ end
96
+ end
97
+
98
+ def build_access_token
99
+ verifier = request.params['code']
100
+ client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)))
101
+ end
102
+
103
+ # An error that is indicated in the OAuth 2.0 callback.
104
+ # This could be a `redirect_uri_mismatch` or other
105
+ class CallbackError < StandardError
106
+ attr_accessor :error, :error_reason, :error_uri
107
+
108
+ def initialize(error, error_reason=nil, error_uri=nil)
109
+ self.error = error
110
+ self.error_reason = error_reason
111
+ self.error_uri = error_uri
112
+ end
113
+ end
114
+
115
+ end
116
+ end
117
+ end
118
+ OmniAuth.config.add_camelization 'oauth2', 'OAuth2'
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../lib/omniauth-infinum/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Tomislav Car"]
5
+ gem.email = ["tomislav@infinum.hr"]
6
+ gem.description = %q{Official OmniAuth strategy for Infinum.}
7
+ gem.summary = %q{Official OmniAuth strategy for Infinum.}
8
+ gem.homepage = "https://infinum.codebasehq.com/projects/infinum-accounts/repositories/omniauth-infinum"
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "omniauth-infinum"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = OmniAuth::Infinum::VERSION
16
+
17
+ gem.add_dependency 'omniauth', '~> 1.0'
18
+ gem.add_dependency 'omniauth-oauth2', '~> 1.1'
19
+ #gem.add_development_dependency 'rspec', '~> 2.7'
20
+ #gem.add_development_dependency 'rack-test'
21
+ #gem.add_development_dependency 'simplecov'
22
+ #gem.add_development_dependency 'webmock'
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-infinum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.7
5
+ platform: ruby
6
+ authors:
7
+ - Tomislav Car
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ description: Official OmniAuth strategy for Infinum.
42
+ email:
43
+ - tomislav@infinum.hr
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Readme.md
50
+ - lib/generators/omniauth_infinum/install_generator.rb
51
+ - lib/generators/omniauth_infinum/templates/app/controllers/omniauth_controller.rb
52
+ - lib/generators/omniauth_infinum/templates/config/initializers/omniauth-infinum.rb
53
+ - lib/generators/omniauth_infinum/templates/db/migrate/add_uid_to_users.rb
54
+ - lib/omniauth-infinum.rb
55
+ - lib/omniauth-infinum/controller.rb
56
+ - lib/omniauth-infinum/test_helpers.rb
57
+ - lib/omniauth-infinum/version.rb
58
+ - lib/omniauth/strategies/infinum.rb
59
+ - lib/omniauth/strategies/oauth2.rb
60
+ - omniauth-infinum.gemspec
61
+ homepage: https://infinum.codebasehq.com/projects/infinum-accounts/repositories/omniauth-infinum
62
+ licenses: []
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Official OmniAuth strategy for Infinum.
84
+ test_files: []
85
+ has_rdoc: