techlahoma_auth 0.0.2

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: 0bd244067505003b209416f560249e723beac813
4
+ data.tar.gz: 679703eeda49d72355f91de45caced5b273c9319
5
+ SHA512:
6
+ metadata.gz: 948c572dfded567b9ba05abbcd431c956345d3611aeb6e9a6396e2380d72a6799169651ea2b2823494acdb6d43a62af9f06869896c76daa3da60a29357ae4249
7
+ data.tar.gz: 172702a568d7844212b4449a7c44794387ab6995f3235f5f12b398ab36cf2d7d13e2b9e8a60120ced61c8f938b9184cfce8a5c46a1f042f819836a906bcfa83c
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
+ # TechlahomaAuth
2
+
3
+ A Rails engine that makes it easy to delegate authentication for a Rails site to
4
+ [Techlahoma](https://github.com/techlahoma/techlahomar).
5
+ See the [Techlahoma Donations](https://github.com/techlahoma_donations)
6
+ project for an example of using this gem.
7
+
8
+ This is based on the SoAuth projects. See [http://www.octolabs.com/so-auth](http://www.octolabs.com/so-auth)
9
+ for more details.
10
+
11
+ Usage
12
+ ==============
13
+
14
+ ## Add `techlahoma_auth` to the `Gemfile`
15
+
16
+ ```ruby
17
+ gem 'techlahoma_auth'
18
+ ```
19
+
20
+ ## Generate an initializer
21
+
22
+ Run this command
23
+
24
+ ```bash
25
+ rails generate techlahoma_auth:install
26
+ ```
27
+
28
+ This will create the following files
29
+
30
+ ```
31
+ config/initializers/omniauth.rb
32
+ ```
33
+
34
+ ## Create a new application in your `TechlahomaAuthProvider` instance
35
+
36
+ Go to the `/oauth/applications` endpoint on the `Techlahoma`
37
+ installation that you want to integrate with. For development this will
38
+ probably be `http://localhost:3000/oauth/applications`.
39
+
40
+ Create a new application, and set the callback URL to
41
+ `http://localhost:3001/auth/so/callback`. Change the port if you
42
+ plan to run your client app on a different port. (See the optional
43
+ section below.)
44
+
45
+ After creating the app make note of the Application Id and the
46
+ Secret.
47
+
48
+ ## Set some environment variables for your client
49
+
50
+ In your new client project (where you installed this gem), you should
51
+ set some environment variables. Using something like `foreman` is
52
+ probably the best so that you can just set them in a `.env` file.
53
+
54
+ ```
55
+ AUTH_PROVIDER_URL=http://localhost:3000
56
+ AUTH_PROVIDER_APPLICATION_ID=1234
57
+ AUTH_PROVIDER_SECRET=5678
58
+ AUTH_PROVIDER_ME_URL=/oauth/me.json
59
+ ```
60
+
61
+ Be sure to use the Application Id you got in the last step as
62
+ `AUTH_PROVIDER_APPLICATION_ID` and the Secret as `AUTH_PROVIDER_SECRET`.
63
+
64
+ ## Create a `User` model
65
+
66
+ If you haven't already done it, you should create a `User` model
67
+
68
+ ```bash
69
+ rails generate model user email:string
70
+ ```
71
+
72
+ Then be sure to run migrations.
73
+
74
+ ```bash
75
+ rake db:migrate; rake db:test:prepare
76
+ ```
77
+
78
+ ## Update `ApplicationController`
79
+
80
+ Change your `ApplicationController` to inherit from
81
+ `TechlahomaAuth::ApplicationController`. The first line should look like this.
82
+
83
+ ```ruby
84
+ class ApplicationController < TechlahomaAuth::ApplicationController
85
+ ```
86
+
87
+ ## Protect some stuff in a controller
88
+
89
+ Use a `before_filter` to protect some controller actions.
90
+
91
+ ```ruby
92
+ before_filter :login_required
93
+ ```
94
+
95
+ ## OPTIONAL : Change the default port of your new project
96
+
97
+ Since we're relying on `techlahoma_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 = 'TechlahomaAuth'
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 TechlahomaAuth
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 TechlahomaAuth::UserSessionsController < TechlahomaAuth::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 => 'techlahoma_auth/user_sessions#create'
4
+ get '/auth/failure', :to => 'techlahoma_auth/user_sessions#failure'
5
+
6
+ # Custom logout
7
+ post '/logout', :to => 'techlahoma_auth/user_sessions#destroy'
8
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate config and initializer for a client of TechlahomaAuthProvider.
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 TechlahomaAuth::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,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 :techlahoma_auth do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,4 @@
1
+ module TechlahomaAuth
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module TechlahomaAuth
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "techlahoma_auth/engine"
2
+ require 'omniauth-oauth2'
3
+ require 'omniauth/strategies/so'
4
+
5
+ module TechlahomaAuth
6
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: techlahoma_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Green
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-11 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 TechlahomaAuthProvider.
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/techlahoma_auth/application_controller.rb
122
+ - app/controllers/techlahoma_auth/user_sessions_controller.rb
123
+ - config/cucumber.yml
124
+ - config/routes.rb
125
+ - lib/generators/techlahoma_auth/install/USAGE
126
+ - lib/generators/techlahoma_auth/install/install_generator.rb
127
+ - lib/generators/techlahoma_auth/install/templates/omniauth.rb
128
+ - lib/omniauth/strategies/so.rb
129
+ - lib/tasks/cucumber.rake
130
+ - lib/tasks/techlahoma_auth_tasks.rake
131
+ - lib/techlahoma_auth.rb
132
+ - lib/techlahoma_auth/engine.rb
133
+ - lib/techlahoma_auth/version.rb
134
+ homepage: https://github.com/jagthedrummer/techlahoma_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.4.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: A gem that allows a Rails app to be an client of TechlahomaAuthProvider.
157
+ test_files: []