thincloud-auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in thincloud-test.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 New Leaders
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Thincloud::Auth
2
+
3
+ ## Description
4
+
5
+ Authentication system generator for new Thincloud apps.
6
+
7
+ ## Requirements
8
+
9
+ This gem requires Rails 3.2+ and has been tested on the following versions:
10
+
11
+ * 3.2
12
+
13
+ This gem has been tested against the following Ruby versions:
14
+
15
+ * 1.9.3
16
+
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ``` ruby
23
+ gem "thincloud-auth"
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ ```
29
+ $ bundle
30
+ ```
31
+
32
+ Or install it yourself as:
33
+
34
+ ```
35
+ $ gem install thincloud-auth
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ This gem adds a generator to Rails, `thincloud:auth`. Running the generator will install auth-related gems, warden initializer and application configuration:
41
+
42
+ * Invoke the generator:
43
+
44
+ ```
45
+ $ rails generate thincloud:auth
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. [Fork it](https://github.com/newleaders/thincloud-auth/fork_select)
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. [Create a Pull Request](https://github.com/newleaders/thincloud-auth/pull/new)
55
+
56
+
57
+ ## License
58
+
59
+ * Freely distributable and licensed under the MIT-style license. See LICENSE file for details.
60
+ * Copyright (c) 2012 New Leaders
61
+ * https://newleaders.com
62
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,44 @@
1
+ require "rails"
2
+
3
+ module Thincloud
4
+ module Generators
5
+ class AuthGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Generates an authentication system for Rails3 Thincloud app."
9
+
10
+ def auth
11
+ copy_file "sessions_controller.rb", "app/controllers/sessions_controller.rb"
12
+ copy_file "sessions_controller_spec.rb", "spec/controllers/sessions_controller_spec.rb"
13
+
14
+ copy_file "users_controller.rb", "app/controllers/users_controller.rb"
15
+ copy_file "users_controller_spec.rb", "spec/controllers/users_controller_spec.rb"
16
+
17
+ empty_directory "app/views/sessions"
18
+ copy_file "login.html.erb", "app/views/sessions/new.html.erb"
19
+
20
+ empty_directory "app/views/users"
21
+ copy_file "signup.html.erb", "app/views/users/new.html.erb"
22
+
23
+ user_spec_path = Rails.root.join("spec/models/user_spec.rb")
24
+ spec_existed = File.exist?(user_spec_path)
25
+ generate "model", "user email password_digest --skip-test"
26
+ File.unlink user_spec_path unless spec_existed
27
+ inject_into_class("app/models/user.rb", User) do
28
+ " attr_accessible :password, :password_confirmation\n"
29
+ end
30
+
31
+ copy_file "user_authentication_spec.rb", "spec/models/user_authentication_spec.rb"
32
+
33
+ route 'get "signup", to: "users#new", as: "signup"'
34
+ route 'get "login", to: "sessions#new", as: "login"'
35
+ route 'get "logout", to: "sessions#destroy", as: "logout"'
36
+ route 'resources :sessions'
37
+ route 'resources :users'
38
+
39
+ copy_file "initializer.rb", "config/initializers/thincloud_auth.rb"
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ Rails.application.config.middleware.use Warden::Manager do |manager|
2
+ manager.failure_app = SessionsController.action(:new)
3
+ manager.default_scope = :user
4
+ manager.default_strategies :password
5
+ end
6
+
7
+ Warden::Manager.serialize_into_session do |user|
8
+ user.id
9
+ end
10
+
11
+ Warden::Manager.serialize_from_session do |id|
12
+ User.find(id)
13
+ end
14
+
15
+ Warden::Strategies.add(:password) do
16
+ def authenticate!
17
+ user = User.find_by_email(params["email"])
18
+ if user && user.authenticate(params["password"])
19
+ success! user
20
+ else
21
+ fail "Invalid email or password"
22
+ end
23
+ end
24
+ end
25
+
26
+ ApplicationController.send(:include, Thincloud::Auth::Controller)
27
+ User.send(:include, Thincloud::Auth::Model)
@@ -0,0 +1,15 @@
1
+ <h1>Log In</h1>
2
+
3
+ <%= flash.alert if flash.alert.present? %>
4
+
5
+ <%= form_tag sessions_path do %>
6
+ <div class="field">
7
+ <%= label_tag :email %><br />
8
+ <%= text_field_tag :email, params[:email] %>
9
+ </div>
10
+ <div class="field">
11
+ <%= label_tag :password %><br />
12
+ <%= password_field_tag :password %>
13
+ </div>
14
+ <div class="actions"><%= submit_tag "Log In" %></div>
15
+ <% end %>
@@ -0,0 +1,22 @@
1
+ class SessionsController < ApplicationController
2
+
3
+ def new
4
+ flash.now.alert = warden.message if warden.message.present?
5
+ end
6
+
7
+ def create
8
+ user = warden.authenticate!
9
+ if user
10
+ redirect_to after_login_path, notice: "Logged In"
11
+ else
12
+ flash.now.alert = warden.message
13
+ render :new
14
+ end
15
+ end
16
+
17
+ def destroy
18
+ warden.logout
19
+ redirect_to after_logout_path, notice: "Logged out"
20
+ end
21
+
22
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe SessionsController do
4
+
5
+ describe "GET new" do
6
+ before do
7
+ SessionsController.any_instance.stubs(:warden).returns(stub(message: nil))
8
+ get :new
9
+ end
10
+
11
+ it { response.status.must_equal 200 }
12
+ it { response.body.must_include "Log In" }
13
+ end
14
+
15
+ describe "POST create" do
16
+
17
+ describe "with invalid params" do
18
+ before do
19
+ warden_manager = stub(authenticate!: nil, message: "Not Logged In")
20
+ SessionsController.any_instance.stubs(:warden).returns(warden_manager)
21
+ post :create, email: "notfound@blah.com", password: "xxx123"
22
+ end
23
+
24
+ it { response.status.must_equal 200 }
25
+ end
26
+
27
+ describe "with valid params" do
28
+ let(:user) do
29
+ User.create(
30
+ email: "info@newleaders.com",
31
+ password: "123456",
32
+ password_confirmation: "1234546"
33
+ )
34
+ end
35
+
36
+ before do
37
+ warden_manager = stub(authenticate!: user)
38
+ SessionsController.any_instance.stubs(:warden).returns(warden_manager)
39
+ post :create, email: "info@newleaders.com", password: "123456"
40
+ end
41
+
42
+ it { response.status.must_equal 302 }
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,28 @@
1
+ <h1>Sign Up</h1>
2
+
3
+ <%= form_for @user do |f| %>
4
+ <% if @user.errors.any? %>
5
+ <div class="error_messages">
6
+ <h2>Form is invalid</h2>
7
+ <ul>
8
+ <% @user.errors.full_messages.each do |message| %>
9
+ <li><%= message %></li>
10
+ <% end %>
11
+ </ul>
12
+ </div>
13
+ <% end %>
14
+
15
+ <div class="field">
16
+ <%= f.label :email %><br>
17
+ <%= f.text_field :email %>
18
+ </div>
19
+ <div class="field">
20
+ <%= f.label :password %><br>
21
+ <%= f.password_field :password %>
22
+ </div>
23
+ <div class="field">
24
+ <%= f.label :password_confirmation %><br>
25
+ <%= f.password_field :password_confirmation %>
26
+ </div>
27
+ <div class="submit"><%= f.submit "Sign Up" %></div>
28
+ <% end %>
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe User do
4
+ describe "password authentication" do
5
+ let(:user) { User.new }
6
+
7
+ it { user.must validate_presence_of(:email) }
8
+ it { user.must_respond_to(:password) }
9
+ it { user.must_respond_to(:password_confirmation) }
10
+ it { user.must_respond_to(:authenticate) }
11
+
12
+ describe "uniqueness" do
13
+ before do
14
+ existing_user = User.new(email: "blah@blah.com")
15
+ existing_user.stubs(:valid?).returns(true)
16
+ existing_user.save
17
+ end
18
+
19
+ it { user.must validate_presence_of(:email) }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ class UsersController < ApplicationController
2
+
3
+ def new
4
+ @user = User.new
5
+ end
6
+
7
+ def create
8
+ @user = User.new(params[:user])
9
+ if @user.save
10
+ warden.set_user(@user)
11
+ redirect_to after_login_path, notice: "Thank you for signing up!"
12
+ else
13
+ render :new
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe UsersController do
4
+
5
+ describe "GET new" do
6
+ before do
7
+ get :new
8
+ end
9
+
10
+ it { response.status.must_equal 200 }
11
+ it { response.body.must_include "Sign Up" }
12
+ it { assigns[:user].new_record?.must_equal(true) }
13
+ end
14
+
15
+ describe "POST create" do
16
+ describe "with valid params" do
17
+ before do
18
+ UsersController.any_instance.stubs(:warden).returns(stub(set_user: nil))
19
+ post :create, user: { email: "valid@valid.com", password: "123456", password_confirmation: "123456" }
20
+ end
21
+
22
+ it { response.status.must_equal 302 }
23
+ it { assigns[:user].new_record?.must_equal(false) }
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,37 @@
1
+ module Thincloud
2
+ module Auth
3
+ module Controller
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ helper_method :current_user
8
+ end
9
+
10
+
11
+ private
12
+
13
+ def warden
14
+ env["warden"]
15
+ end
16
+
17
+ def current_user
18
+ warden.user
19
+ end
20
+
21
+ def authorize
22
+ return true if current_user.present?
23
+ session[:return_to] ||= request.url
24
+ redirect_to login_url, alert: "Not authorized"
25
+ end
26
+
27
+ def after_login_path
28
+ session.delete(:return_to) || root_path
29
+ end
30
+
31
+ def after_logout_path
32
+ login_path
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module Thincloud
2
+ module Auth
3
+ module Model
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ has_secure_password
8
+ validates :email, presence: true, uniqueness: true
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Thincloud
2
+ module Auth
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "warden"
2
+ require "thincloud/auth/controller"
3
+ require "thincloud/auth/model"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/thincloud/auth/version", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Robert Bousquet"]
6
+ gem.email = ["rbousquet@newleaders.com"]
7
+ gem.description = "Authentication generator for new Thincloud apps."
8
+ gem.summary = "Authentication generator for new Thincloud apps."
9
+ gem.homepage = "http://newleaders.github.com/thincloud-auth"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "thincloud-auth"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Thincloud::Auth::VERSION
17
+
18
+ gem.add_runtime_dependency "warden", "~> 1.2.1"
19
+ gem.add_runtime_dependency "bcrypt-ruby", "~> 3.0.0"
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thincloud-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Bousquet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: warden
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.1
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: 1.2.1
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: 3.0.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: 3.0.0
46
+ description: Authentication generator for new Thincloud apps.
47
+ email:
48
+ - rbousquet@newleaders.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/generators/thincloud/auth/auth_generator.rb
59
+ - lib/generators/thincloud/auth/templates/initializer.rb
60
+ - lib/generators/thincloud/auth/templates/login.html.erb
61
+ - lib/generators/thincloud/auth/templates/sessions_controller.rb
62
+ - lib/generators/thincloud/auth/templates/sessions_controller_spec.rb
63
+ - lib/generators/thincloud/auth/templates/signup.html.erb
64
+ - lib/generators/thincloud/auth/templates/user_authentication_spec.rb
65
+ - lib/generators/thincloud/auth/templates/users_controller.rb
66
+ - lib/generators/thincloud/auth/templates/users_controller_spec.rb
67
+ - lib/thincloud-auth.rb
68
+ - lib/thincloud/auth/controller.rb
69
+ - lib/thincloud/auth/model.rb
70
+ - lib/thincloud/auth/version.rb
71
+ - thincloud-auth.gemspec
72
+ homepage: http://newleaders.github.com/thincloud-auth
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.24
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Authentication generator for new Thincloud apps.
96
+ test_files: []