omniauth-password 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 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.
@@ -0,0 +1,104 @@
1
+ # OmniauthPassword
2
+
3
+ The OmniAuth Password gem is designed to be a simple way to add local login/password authentication along side other strategies. It simply makes ActiveModel's `has_secure_password` work directly with OmniAuth like any other provider without the need for a separate Authentication or Identity model.
4
+
5
+ This gem will work well for you if you want to create your authentication system from scratch. Authenticating to a local User model is treated just like authenticating to an external provider like Twitter. OmniAuth Password stays out of your way leaving local user registration up to you. It does not provide controllers or views.
6
+
7
+ ## Installation
8
+
9
+ Add the strategy to your Gemfile
10
+
11
+ gem 'omniauth-password'
12
+
13
+ Add the following to your `config/initializers/omniauth.rb`:
14
+
15
+ Rails.application.config.middleware.use OmniAuth::Builder do
16
+ # provider :twitter ...
17
+ # provider :github ...
18
+ provider :password
19
+ end
20
+
21
+ Add some routes:
22
+
23
+ match "/auth/:provider/callback" => "sessions#create"
24
+ match "/auth/failure", to: "sessions#failure"
25
+ resource :session
26
+
27
+ OmniAuth Password expects a POST to `/auth/password/callback` with login params in the session hash. You might create a form something like this:
28
+
29
+ <h1>Sign in</h1>
30
+
31
+ <%= form_for(:sessions, url: "/auth/password/callback") do |f| %>
32
+ <div class="field">
33
+ <%= label_tag :email %><br />
34
+ <%= f.text_field :email %>
35
+ </div>
36
+ <div class="field">
37
+ <%= label_tag :password %><br />
38
+ <%= f.password_field :password %>
39
+ </div>
40
+ <div class="actions">
41
+ <%= submit_tag "Sign in" %>
42
+ </div>
43
+ <% end %>
44
+
45
+ Your Sessions controller's `create` method takes the omniauth hash from this and any other strategy you are using; it might look something like this:
46
+
47
+ class SessionsController < ApplicationController
48
+ def new
49
+ end
50
+
51
+ def create
52
+ user = User.from_omniauth(request.env["omniauth.auth"])
53
+ session[:user_id] = user.id
54
+ redirect_to root_url, notice: "Signed in"
55
+ end
56
+
57
+ def destroy
58
+ session[:user_id] = nil
59
+ redirect_to root_url, notice: "Signed out"
60
+ end
61
+
62
+ def failure
63
+ redirect_to new_session_path, notice: "Authentication failed"
64
+ end
65
+ end
66
+
67
+ The User model does not need a `uid` field for OmniAuth because it reuses the password_digest field when not doing local login/password authentication. ActiveModel's `has_secure_password` causes password_digest to be a required field.:
68
+
69
+ # == Schema Information
70
+ #
71
+ # Table name: users
72
+ #
73
+ # id :integer not null, primary key
74
+ # email :string(255)
75
+ # provider :string(255)
76
+ # password_digest :string(255)
77
+ #
78
+
79
+ class User < ActiveRecord::Base
80
+ has_secure_password
81
+
82
+ def self.from_omniauth(auth)
83
+ find_by_provider_and_password_digest(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
84
+ end
85
+
86
+ def self.create_with_omniauth(auth)
87
+ create! do |user|
88
+ user.provider = auth["provider"]
89
+ user.password_digest = auth["uid"]
90
+ user.name = auth["info"]["name"]
91
+ end
92
+ end
93
+ end
94
+
95
+ ## Customizing
96
+
97
+ By default, OmniAuth Password expects to authenticate to the email and password of the User model. You can override these in your `config/initializers/omniauth.rb`:
98
+
99
+ Rails.application.config.middleware.use OmniAuth::Builder do
100
+ # provider :twitter ...
101
+ # provider :github ...
102
+ provider :password, :login_field => :username, :user_model => Admin
103
+ end
104
+
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'OmniauthPassword'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ require "rspec/core/rake_task"
24
+
25
+ RSpec::Core::RakeTask.new
26
+
27
+ task :default => :spec
28
+ task :test => :spec
29
+
30
+
31
+ Bundler::GemHelper.install_tasks
32
+
@@ -0,0 +1,3 @@
1
+ require 'omniauth'
2
+ require 'omniauth-password/version'
3
+ require 'omniauth/strategies/password'
@@ -0,0 +1,3 @@
1
+ module OmniauthPassword
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ module OmniAuth
2
+ module Strategies
3
+ class Password
4
+ include OmniAuth::Strategy
5
+
6
+ option :user_model, nil # default set to 'User' below
7
+ option :login_field, :email
8
+
9
+ def request_phase
10
+ redirect "/session/new"
11
+ end
12
+
13
+ def callback_phase
14
+ return fail!(:invalid_credentials) unless user.try(:authenticate, password)
15
+ super
16
+ end
17
+
18
+ def user
19
+ @user ||= user_model.send("find_by_#{options[:login_field]}", login)
20
+ end
21
+
22
+ def user_model
23
+ options[:user_model] || ::User
24
+ end
25
+
26
+ def login
27
+ request[:sessions][options[:login_field].to_s]
28
+ end
29
+
30
+ def password
31
+ request[:sessions]['password']
32
+ end
33
+
34
+ uid do
35
+ user.password_digest
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :omniauth-password do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-password
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Amick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &73839200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *73839200
25
+ - !ruby/object:Gem::Dependency
26
+ name: omniauth
27
+ requirement: &73836760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *73836760
36
+ - !ruby/object:Gem::Dependency
37
+ name: bcrypt-ruby
38
+ requirement: &73835650 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *73835650
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &73834990 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *73834990
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec-rails
60
+ requirement: &73834330 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *73834330
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: &73833910 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *73833910
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard-rspec
82
+ requirement: &73833160 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *73833160
91
+ - !ruby/object:Gem::Dependency
92
+ name: guard-spork
93
+ requirement: &73832620 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *73832620
102
+ description: An OmniAuth strategy for use with ActiveModel's has_secure_password
103
+ email:
104
+ - github@nathanamick.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - lib/omniauth/strategies/password.rb
110
+ - lib/omniauth-password.rb
111
+ - lib/omniauth-password/version.rb
112
+ - lib/tasks/omniauth-password_tasks.rake
113
+ - MIT-LICENSE
114
+ - Rakefile
115
+ - README.md
116
+ homepage: https://github.com/namick/omniauth-password
117
+ licenses: []
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: -74827891
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ segments:
138
+ - 0
139
+ hash: -74827891
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.16
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: An OmniAuth strategy for use with ActiveModel's has_secure_password
146
+ test_files: []