so_auth 0.0.1

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: df48a035162f12dc8998c24c00c50ffe88cc5b12
4
+ data.tar.gz: 7b6da2e588124c146478da31d9c6e1bcaf1b980b
5
+ SHA512:
6
+ metadata.gz: 22df3215c82a6eab1e715832e4e3f44cf7cec49ac083f1e7c103ad34dca85bff3fe3307b82883fa4d0f425f9efa3ab6f285cb1ba3c729932c7e2452dddc60078
7
+ data.tar.gz: 91b4554806a55e41ab53804ed3a58a4eedaa7b6e86d74f5d07647cb0b7a8d450079b50e2238bbdc60ee69202fdea809e9bb89320cfca9ec68c669a85e53f9379
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # SoAuth
2
+
3
+ A Rails engine that makes it easy to delegate authentication for a Rails
4
+ site to SoAuthProvider.
5
+
6
+
7
+ Usage
8
+ ==============
9
+
10
+ ## Add `so_auth` to the `Gemfile`
11
+
12
+ ```ruby
13
+ gem 'so_auth'
14
+ ```
15
+
16
+ ## Generate an initilizer
17
+
18
+ Run this command
19
+
20
+ ```bash
21
+ rails generate so_auth:install
22
+ ```
23
+
24
+ This will create the following files
25
+
26
+ ```
27
+ config/initializers/omniauth.rb
28
+ ```
29
+
30
+ ## Create a new application in your `SoAuthProvider` instance
31
+
32
+ Go to the `/oauth/applications` endpoint on the `SoAuthProvider`
33
+ installation that you want to integrate with. For development this will
34
+ probably be `http://localhost:3000/oauth/applications`.
35
+
36
+ Create a new application, and set the callback URL to
37
+ `http://localhost:3001/auth/so/callback`. Change the port if you
38
+ plan to run your client app on a differnt port. (See the optional
39
+ section below.)
40
+
41
+ After creating the app make note of the Application Id and the
42
+ Secret.
43
+
44
+ ## Set some environment variables for your client
45
+
46
+ In your new client project (where you installed this gem), you should
47
+ set some environment variables. Using something like `foreman` is
48
+ probably the best so that you can just set them in a `.env` file.
49
+
50
+ ```
51
+ AUTH_PROVIDER_URL=http://localhost:3000
52
+ AUTH_PROVIDER_APPLICATION_ID=1234
53
+ AUTH_PROVIDER_SECRET=5678
54
+ AUTH_PROVIDER_ME_URL=/oauth/me.json
55
+ ```
56
+
57
+ Be sure to use the Application Id you got in the last step as
58
+ `AUTH_APPLICATION_APPLICATION_ID` and the Secret as `AUTH_APPLICATION_SECRET`.
59
+
60
+ ## Create a `User` model
61
+
62
+ If you haven't already done it, you should create a `User` model
63
+
64
+ ```bash
65
+ rails generate model user email:string
66
+ ```
67
+
68
+ Then be sure to run migrations.
69
+
70
+ ```bash
71
+ rake db:migrate; rake db:test:prepare
72
+ ```
73
+
74
+ ## Update `ApplicationController`
75
+
76
+ Change your `ApplicationController` to inhereit from
77
+ `SoAuth::ApplicationController`. The first line should look like this.
78
+
79
+ ```ruby
80
+ class ApplicationController < SoAuth::ApplicationController
81
+ ```
82
+
83
+ ## Protect some stuff in a controller
84
+
85
+ Use a `before_filter` to protect some controller actions.
86
+
87
+ ```ruby
88
+ before_filter :login_required
89
+ ```
90
+
91
+
92
+
93
+
94
+
95
+ ## OPTIONAL : Change the default port of your new project
96
+
97
+ Since we're relying on `so_auth_provider` to provide authentication, we need
98
+ to run our new project on a different port in development. Open up `config/boot.rb`
99
+ and add this to the bottom of the file. If you want to use a port other
100
+ than `3001` just change the port as appropriate.
101
+
102
+ ```ruby
103
+ # Setting default port to 3001
104
+ require 'rails/commands/server'
105
+ module Rails
106
+ class Server
107
+ alias :default_options_alias :default_options
108
+ def default_options
109
+ default_options_alias.merge!(:Port => 3001)
110
+ end
111
+ end
112
+ end
113
+ ```
114
+
115
+ Or you could just run your development server on a different port:
116
+
117
+ ```
118
+ rails s -p 3001
119
+ ```
120
+
121
+ or
122
+
123
+ ```
124
+ unicorn -p 3001 -c ./config/unicorn.rb
125
+ ```
126
+
127
+ or whatever.
128
+
129
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SoAuth'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,66 @@
1
+ module SoAuth
2
+ class ApplicationController < ActionController::Base
3
+
4
+ protect_from_forgery
5
+
6
+ before_filter :check_cookie
7
+ def check_cookie
8
+ if !cookie_valid?
9
+ session[:user_id] = nil
10
+ end
11
+ end
12
+
13
+ def cookie_valid?
14
+ cookies[:so_auth].present? && session[:user_id].present? && cookies[:so_auth].to_s == session[:user_id].to_s
15
+ end
16
+
17
+ def login_required
18
+ if !current_user
19
+ not_authorized
20
+ end
21
+ end
22
+
23
+ def not_authorized
24
+ respond_to do |format|
25
+ format.html{ auth_redirect }
26
+ format.json{ head :unauthorized }
27
+ end
28
+ end
29
+
30
+ def auth_redirect
31
+ observable_redirect_to "/auth/so?origin=#{request.protocol}#{request.host_with_port}#{request.fullpath}"
32
+ end
33
+
34
+ def current_user
35
+ return nil unless session[:user_id]
36
+ @current_user ||= User.find_by_id(session[:user_id])
37
+ end
38
+
39
+ def signed_in?
40
+ current_user.present?
41
+ end
42
+
43
+ helper_method :signed_in?
44
+ helper_method :current_user
45
+
46
+
47
+
48
+
49
+ private
50
+
51
+ # These two methods help with testing
52
+ def integration_test?
53
+ Rails.env.test? && defined?(Cucumber::Rails)
54
+ end
55
+
56
+ def observable_redirect_to(url)
57
+ if integration_test?
58
+ render :text => "If this wasn't an integration test, you'd be redirected to: #{url}"
59
+ else
60
+ redirect_to url
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+
@@ -0,0 +1,40 @@
1
+ class SoAuth::UserSessionsController < SoAuth::ApplicationController
2
+ before_filter :login_required, :only => [ :destroy ]
3
+
4
+ respond_to :html
5
+
6
+ # omniauth callback method
7
+ def create
8
+ omniauth = env['omniauth.auth']
9
+
10
+ user = User.find_by_id(omniauth['uid'])
11
+ if not user
12
+ # New user registration
13
+ user = User.new
14
+ user.id = omniauth['uid']
15
+ end
16
+ user.email = omniauth['info']['email'] if user.respond_to?(:email)
17
+ user.save
18
+
19
+ session[:user_id] = user.id
20
+
21
+ flash[:notice] = "Successfully logged in"
22
+ redirect_to request.env['omniauth.origin'] || root_path
23
+ end
24
+
25
+ # Omniauth failure callback
26
+ def failure
27
+ flash[:notice] = params[:message]
28
+ redirect_to root_path
29
+ end
30
+
31
+ # logout - Clear our rack session BUT essentially redirect to the provider
32
+ # to clean up the Devise session from there too !
33
+ def destroy
34
+ reset_session
35
+ flash[:notice] = 'You have successfully signed out!'
36
+ redirect_to "#{ENV['AUTH_PROVIDER_URL']}/users/sign_out"
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,8 @@
1
+ <%
2
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3
+ rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4
+ std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
5
+ %>
6
+ default: <%= std_opts %> features
7
+ wip: --tags @wip:3 --wip features
8
+ rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ Rails.application.routes.draw do
2
+ # omniauth
3
+ get '/auth/:provider/callback', :to => 'so_auth/user_sessions#create'
4
+ get '/auth/failure', :to => 'so_auth/user_sessions#failure'
5
+
6
+ # Custom logout
7
+ post '/logout', :to => 'so_auth/user_sessions#destroy'
8
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate config and initializer for a client of SoAuthProvider.
3
+
4
+ Example:
5
+ rails generate so_auth:install
6
+
7
+ This will create:
8
+ config/initializers/omniauth.rb
@@ -0,0 +1,8 @@
1
+ class SoAuth::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_initializer_file
5
+ copy_file "omniauth.rb", "config/initializers/omniauth.rb"
6
+ end
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ # If you need to pull the app_id and app_secret from a different spot
2
+ # this is the place to do it
3
+ APP_ID = ENV['AUTH_PROVIDER_APPLICATION_ID'] || "not_a_real_id"
4
+ APP_SECRET = ENV['AUTH_PROVIDER_SECRET'] || "not_a_real_secret"
5
+
6
+ Rails.application.config.middleware.use OmniAuth::Builder do
7
+ provider :so, APP_ID, APP_SECRET
8
+ end
@@ -0,0 +1,41 @@
1
+ require 'omniauth-oauth2'
2
+ module OmniAuth
3
+ module Strategies
4
+ class So < OmniAuth::Strategies::OAuth2
5
+
6
+ CUSTOM_PROVIDER_URL = ENV['AUTH_PROVIDER_URL'] || "http://custom-provider-goes-here"
7
+ CUSTOM_PROVIDER_ME_URL = ENV['AUTH_PROVIDER_ME_URL'] || "/oauth/me.json"
8
+
9
+ option :client_options, {
10
+ :site => CUSTOM_PROVIDER_URL,
11
+ :authorize_url => "#{CUSTOM_PROVIDER_URL}/oauth/authorize",
12
+ :access_token_url => "#{CUSTOM_PROVIDER_URL}/oauth/token"
13
+ }
14
+
15
+ uid {
16
+ raw_info['id']
17
+ }
18
+
19
+ info do
20
+ {
21
+ :email => raw_info['email'],
22
+ :admin => raw_info['admin']
23
+ }
24
+ end
25
+
26
+ extra do
27
+ {
28
+ #:current_sign_in_at => raw_info['extra']['current_sign_in_at'],
29
+ #:name => raw_info['extra']['name']
30
+ #:first_name => raw_info['extra']['first_name'],
31
+ #:last_name => raw_info['extra']['last_name']
32
+ }
33
+ end
34
+
35
+ def raw_info
36
+ @raw_info ||= access_token.get(CUSTOM_PROVIDER_ME_URL).parsed
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,4 @@
1
+ module SoAuth
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module SoAuth
2
+ VERSION = "0.0.1"
3
+ end
data/lib/so_auth.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "so_auth/engine"
2
+ require 'omniauth-oauth2'
3
+ require 'omniauth/strategies/so'
4
+
5
+ module SoAuth
6
+ end
@@ -0,0 +1,65 @@
1
+ # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2
+ # It is recommended to regenerate this file in the future when you upgrade to a
3
+ # newer version of cucumber-rails. Consider adding your own code to a new file
4
+ # instead of editing this one. Cucumber will automatically load all features/**/*.rb
5
+ # files.
6
+
7
+
8
+ unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks
9
+
10
+ vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
11
+ $LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil?
12
+
13
+ begin
14
+ require 'cucumber/rake/task'
15
+
16
+ namespace :cucumber do
17
+ Cucumber::Rake::Task.new({:ok => 'test:prepare'}, 'Run features that should pass') do |t|
18
+ t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
19
+ t.fork = true # You may get faster startup if you set this to false
20
+ t.profile = 'default'
21
+ end
22
+
23
+ Cucumber::Rake::Task.new({:wip => 'test:prepare'}, 'Run features that are being worked on') do |t|
24
+ t.binary = vendored_cucumber_bin
25
+ t.fork = true # You may get faster startup if you set this to false
26
+ t.profile = 'wip'
27
+ end
28
+
29
+ Cucumber::Rake::Task.new({:rerun => 'test:prepare'}, 'Record failing features and run only them if any exist') do |t|
30
+ t.binary = vendored_cucumber_bin
31
+ t.fork = true # You may get faster startup if you set this to false
32
+ t.profile = 'rerun'
33
+ end
34
+
35
+ desc 'Run all features'
36
+ task :all => [:ok, :wip]
37
+
38
+ task :statsetup do
39
+ require 'rails/code_statistics'
40
+ ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
41
+ ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
42
+ end
43
+ end
44
+ desc 'Alias for cucumber:ok'
45
+ task :cucumber => 'cucumber:ok'
46
+
47
+ task :default => :cucumber
48
+
49
+ task :features => :cucumber do
50
+ STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
51
+ end
52
+
53
+ # In case we don't have the generic Rails test:prepare hook, append a no-op task that we can depend upon.
54
+ task 'test:prepare' do
55
+ end
56
+
57
+ task :stats => 'cucumber:statsetup'
58
+ rescue LoadError
59
+ desc 'cucumber rake task not available (cucumber not installed)'
60
+ task :cucumber do
61
+ abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :so_auth do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: so_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Green
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.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.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: omniauth-oauth2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: cucumber-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.4.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.4.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.2.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.2.0
111
+ description: A gem that allows a Rails app to be an client of SoAuthProvider.
112
+ email:
113
+ - jeremy@octolabs.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - app/controllers/so_auth/application_controller.rb
122
+ - app/controllers/so_auth/user_sessions_controller.rb
123
+ - config/cucumber.yml
124
+ - config/routes.rb
125
+ - lib/generators/so_auth/install/USAGE
126
+ - lib/generators/so_auth/install/install_generator.rb
127
+ - lib/generators/so_auth/install/templates/omniauth.rb
128
+ - lib/omniauth/strategies/so.rb
129
+ - lib/so_auth.rb
130
+ - lib/so_auth/engine.rb
131
+ - lib/so_auth/version.rb
132
+ - lib/tasks/cucumber.rake
133
+ - lib/tasks/so_auth_tasks.rake
134
+ homepage: https://github.com/jagthedrummer/so_auth
135
+ licenses: []
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.0.rc.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: A gem that allows a Rails app to be an client of SoAuthProvider.
157
+ test_files: []