cz_auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 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.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ == CzAuth
2
+
3
+ Just a simple authentication implementation using has_secure_password for convenience. This gem is not supported for public consumption. It's just something I threw together to simplify my daily life.
4
+
5
+ === Usage
6
+
7
+ rails g cz_auth:install <model_name>
8
+ rake db:migrate
9
+
10
+ === Details
11
+
12
+ The generator will generate a model with the following attributes:
13
+
14
+ * username
15
+ * email
16
+ * password_digest
17
+ * auth_token
18
+
19
+ It will then place a 'requires_authentication' method at the top of the model. This will trigger the has_secure_password, and before filter used to generate an auth_token.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
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 = 'CzAuth'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,26 @@
1
+ module CzAuth
2
+ class ApplicationController < ActionController::Base
3
+ before_filter :fetch_resource
4
+
5
+ protected
6
+
7
+ # Specify when a controller requires authentication
8
+ def require_authentication(options={})
9
+ unless current_user
10
+ flash[:error] = options[:error]
11
+ redirect_to options[:redirect]
12
+ end
13
+ end
14
+
15
+ # Load the resource, via params[:resource]
16
+ def fetch_resource
17
+ begin
18
+ klass = params[:resource].classify
19
+ @klass = klass.constantize
20
+ rescue
21
+ raise AbstractController::ActionNotFound
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ module CzAuth
2
+ class RegistrationsController < CzAuth::ApplicationController
3
+
4
+ def new
5
+ @resource = @klass.new
6
+ end
7
+
8
+ def create
9
+ @resource = @klass.new(params[:resource])
10
+ if @resource.save
11
+ cookies[:auth_token] = @resource.auth_token
12
+ redirect_to root_url
13
+ else
14
+ render :new
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module CzAuth
2
+ class SessionsController < CzAuth::ApplicationController
3
+
4
+ def new
5
+ @resource = @klass.new
6
+ end
7
+
8
+ def create
9
+ @resource = @klass.where(:resourcename => params[:resourcename]).first
10
+ if @resource && @resource.authenticate(params[:password])
11
+ cookies.permanent[:auth_token] = @resource.auth_token
12
+ end
13
+ redirect_to root_url
14
+ end
15
+
16
+ def destroy
17
+ cookies.delete(:auth_token)
18
+ redirect_to root_url
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module CzAuth
2
+ module ApplicationHelper
3
+ def current_user
4
+ @current_user ||= User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
5
+ end
6
+
7
+ def user_signed_in?
8
+ !current_user.nil?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ <h1>Registrations#New</h1>
@@ -0,0 +1 @@
1
+ <h1>Sessions#New</h1>
data/config/routes.rb ADDED
@@ -0,0 +1,12 @@
1
+ Rails.application.routes.draw do
2
+
3
+ # Resources
4
+ match '/:resource/sessions(/:action)', controller: 'cz_auth/sessions'
5
+ match '/:resource/registrations(/:action)', controller: 'cz_auth/registrations'
6
+
7
+ # Helpers
8
+ match '/:resource/login', to: 'cz_auth/sessions#new', as: 'login'
9
+ match '/:resource/logout', to: 'cz_auth/sessions#destroy', as: 'logout'
10
+ match '/:resource/signup', to: 'cz_auth/registrations#new', as: 'signup'
11
+
12
+ end
data/lib/cz_auth.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "cz_auth/engine"
2
+ require "cz_auth/requires_authentication"
3
+
4
+ module CzAuth
5
+ end
@@ -0,0 +1,4 @@
1
+ module CzAuth
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ module CzAuth
2
+ module RequiresAuthentication
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def self.requires_authentication
7
+ has_secure_password
8
+ before_create { generate_token(:auth_token) }
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def generate_token(column)
15
+ begin
16
+ self[column] = SecureRandom.urlsafe_base64
17
+ end while User.exists?(column => self[column])
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ ActiveRecord::Base.send :include, CzAuth::RequiresAuthentication
@@ -0,0 +1,3 @@
1
+ module CzAuth
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class CzAuth::InstallGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ desc "Installs the CzAuth Models, Migrations."
7
+
8
+ argument :user_model, :type => :string, :required => false, :default => "User", :desc => "Your user model name."
9
+
10
+ def create_models
11
+ model = user_model.singularize
12
+ generate("model", "#{model} username email password_digest auth_token")
13
+ if File.exists?("app/models/#{user_model}.rb")
14
+ inject_into_file "app/models/#{user_model}.rb", :after => "ActiveRecord::Base" do
15
+ "\n\trequires_authentication"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :cz_auth do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cz_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Kelley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.12
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.12
30
+ - !ruby/object:Gem::Dependency
31
+ name: bcrypt-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A Simple has_secure_password implementation
47
+ email:
48
+ - mike@codezombie.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - app/controllers/cz_auth/application_controller.rb
54
+ - app/controllers/cz_auth/registrations_controller.rb
55
+ - app/controllers/cz_auth/sessions_controller.rb
56
+ - app/helpers/cz_auth/application_helper.rb
57
+ - app/views/cz_auth/registrations/new.html.erb
58
+ - app/views/cz_auth/sessions/new.html.erb
59
+ - config/routes.rb
60
+ - lib/cz_auth/engine.rb
61
+ - lib/cz_auth/requires_authentication.rb
62
+ - lib/cz_auth/version.rb
63
+ - lib/cz_auth.rb
64
+ - lib/generators/cz_auth/install_generator.rb
65
+ - lib/tasks/cz_auth_tasks.rake
66
+ - MIT-LICENSE
67
+ - Rakefile
68
+ - README.rdoc
69
+ homepage:
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.25
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: A Simple has_secure_password implementation
93
+ test_files: []