mini_auth 0.1.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/Gemfile +5 -0
- data/README.md +58 -0
- data/Rakefile +9 -0
- data/lib/mini_auth/version.rb +3 -0
- data/lib/mini_auth.rb +26 -0
- data/mini_auth.gemspec +24 -0
- data/spec/fake_app.rb +25 -0
- data/spec/mini_auth/authenticate_spec.rb +24 -0
- data/spec/mini_auth/password_spec.rb +15 -0
- data/spec/spec_helper.rb +19 -0
- metadata +100 -0
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,58 @@ | |
| 1 | 
            +
            mini_auth
         | 
| 2 | 
            +
            =========
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            A minimal authentication module for Rails
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Install
         | 
| 7 | 
            +
            -------
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Add to your Gemfile:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                gem "mini_auth"
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            or install as a plugin
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                $ cd RAILS_ROOT
         | 
| 16 | 
            +
                $ rails plugin install git://github.com/kuroda/mini_auth.git
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            Usage
         | 
| 19 | 
            +
            -----
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                class CreateUsers < ActiveRecord::Migration
         | 
| 22 | 
            +
                  def change
         | 
| 23 | 
            +
                    create_table :users do |t|
         | 
| 24 | 
            +
                      t.string :name, :null => false
         | 
| 25 | 
            +
                      t.string :password_digest, :null => true
         | 
| 26 | 
            +
                
         | 
| 27 | 
            +
                      t.timestamps
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
                
         | 
| 32 | 
            +
                class User < ActiveRecord::Base
         | 
| 33 | 
            +
                  include MiniAuth
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
                
         | 
| 36 | 
            +
                a = User.new(:name => "alice", :password => "hotyoga")
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                a.save                              # => true
         | 
| 39 | 
            +
                a.password_digest                   # => "$2a$10$F5YbEd..."
         | 
| 40 | 
            +
                a.authenticate("hotyoga)            # => true
         | 
| 41 | 
            +
                
         | 
| 42 | 
            +
                a.update_attributes :name => "Alice"
         | 
| 43 | 
            +
                a.authenticate("hotyoga")           # => true
         | 
| 44 | 
            +
                
         | 
| 45 | 
            +
                b = User.new(:name => "bob")
         | 
| 46 | 
            +
                
         | 
| 47 | 
            +
                b.password = ""
         | 
| 48 | 
            +
                b.valid?                            # => false
         | 
| 49 | 
            +
                b.errors[:password]                 # => "can't be blank"
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                b.password = nil
         | 
| 52 | 
            +
                b.valid?                            # => true
         | 
| 53 | 
            +
                b.save!
         | 
| 54 | 
            +
                b.password_digest                   # => nil
         | 
| 55 | 
            +
                b.authenticate(nil)                 # => false
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                b.update_attributes :password_digest => 'dummy'
         | 
| 58 | 
            +
                b.password_digest                   # => nil (unchanged)
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/lib/mini_auth.rb
    ADDED
    
    | @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            require "mini_auth/version"
         | 
| 2 | 
            +
            require "bcrypt"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module MiniAuth
         | 
| 5 | 
            +
              extend ActiveSupport::Concern
         | 
| 6 | 
            +
              
         | 
| 7 | 
            +
              included do
         | 
| 8 | 
            +
                attr_accessor :password
         | 
| 9 | 
            +
                
         | 
| 10 | 
            +
                validate do
         | 
| 11 | 
            +
                  if password && password.blank?
         | 
| 12 | 
            +
                    errors.add(:password, :blank)
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
                
         | 
| 16 | 
            +
                before_save do
         | 
| 17 | 
            +
                  if password
         | 
| 18 | 
            +
                    self.password_digest = BCrypt::Password.create(password)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
              
         | 
| 23 | 
            +
              def authenticate(password)
         | 
| 24 | 
            +
                password_digest && BCrypt::Password.new(password_digest) == password
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
    
        data/mini_auth.gemspec
    ADDED
    
    | @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 2 | 
            +
            require "mini_auth/version"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            Gem::Specification.new do |s|
         | 
| 5 | 
            +
              s.name        = "mini_auth"
         | 
| 6 | 
            +
              s.version     = MiniAuth::VERSION
         | 
| 7 | 
            +
              s.authors     = ["Tsutomu Kuroda"]
         | 
| 8 | 
            +
              s.email       = ["t-kuroda@oiax.jp"]
         | 
| 9 | 
            +
              s.homepage    = ""
         | 
| 10 | 
            +
              s.summary     = %q{A minimal authentication module for Rails}
         | 
| 11 | 
            +
              s.description = %q{A minimal authentication module for Rails}
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              s.rubyforge_project = "mini_auth"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 16 | 
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 17 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 18 | 
            +
              s.require_paths = ["lib"]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              s.add_development_dependency "sqlite3"
         | 
| 21 | 
            +
              s.add_development_dependency "rspec"
         | 
| 22 | 
            +
              s.add_development_dependency "rspec-rails"
         | 
| 23 | 
            +
              s.add_runtime_dependency "bcrypt-ruby"
         | 
| 24 | 
            +
            end
         | 
    
        data/spec/fake_app.rb
    ADDED
    
    | @@ -0,0 +1,25 @@ | |
| 1 | 
            +
            # Establish connection to the database running on memory
         | 
| 2 | 
            +
            ActiveRecord::Base.establish_connection(
         | 
| 3 | 
            +
              :adapter  => "sqlite3",
         | 
| 4 | 
            +
              :database => ":memory:"
         | 
| 5 | 
            +
            )
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # Discard ActiveRecord's log
         | 
| 8 | 
            +
            ActiveRecord::Base.logger = Logger.new('/dev/null')
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            # Define migration class
         | 
| 11 | 
            +
            class CreateAllTables < ActiveRecord::Migration
         | 
| 12 | 
            +
              def change
         | 
| 13 | 
            +
                create_table(:users) { |t| t.string :name; t.string :password_digest }
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            # Run migrations
         | 
| 18 | 
            +
            migration = CreateAllTables.new
         | 
| 19 | 
            +
            migration.verbose = false
         | 
| 20 | 
            +
            migration.change
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            # Models
         | 
| 23 | 
            +
            class User < ActiveRecord::Base
         | 
| 24 | 
            +
              include MiniAuth
         | 
| 25 | 
            +
            end
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe "authenticate" do
         | 
| 4 | 
            +
              it "should authenticate with a valid password" do
         | 
| 5 | 
            +
                u = User.new(:name => 'alice', :password => 'hotyoga')
         | 
| 6 | 
            +
                u.save!
         | 
| 7 | 
            +
                
         | 
| 8 | 
            +
                u.authenticate('hotyoga').should be_true
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
              
         | 
| 11 | 
            +
              it "should not authenticate with a wrong password" do
         | 
| 12 | 
            +
                u = User.new(:name => 'alice', :password => 'hotyoga')
         | 
| 13 | 
            +
                u.save!
         | 
| 14 | 
            +
                
         | 
| 15 | 
            +
                u.authenticate('wrong').should be_false
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
              
         | 
| 18 | 
            +
              it "should not authenticate with ni; password" do
         | 
| 19 | 
            +
                u = User.new(:name => 'alice', :password => nil)
         | 
| 20 | 
            +
                u.save!
         | 
| 21 | 
            +
                
         | 
| 22 | 
            +
                u.authenticate(nil).should be_false
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe "password" do
         | 
| 4 | 
            +
              it "should accept nil password" do
         | 
| 5 | 
            +
                u = User.new(:name => 'alice', :password => nil)
         | 
| 6 | 
            +
                u.should be_valid
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
              it "should reject blank password" do
         | 
| 10 | 
            +
                u = User.new(:name => 'alice', :password => '')
         | 
| 11 | 
            +
                u.should_not be_valid
         | 
| 12 | 
            +
                u.should have(1).error_on(:password)
         | 
| 13 | 
            +
                u.errors[:password].first.should == "can't be blank"
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            # Configure Rails envinronment
         | 
| 2 | 
            +
            ENV["RAILS_ENV"] = "test"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require "rails/all"
         | 
| 5 | 
            +
            require "rspec/rails"
         | 
| 6 | 
            +
            require "mini_auth"
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # Pull in the fake rails app
         | 
| 9 | 
            +
            require 'fake_app'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            # Load support files
         | 
| 12 | 
            +
            Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # Configure RSpec
         | 
| 15 | 
            +
            RSpec.configure do |config|
         | 
| 16 | 
            +
              require 'rspec/expectations'
         | 
| 17 | 
            +
              config.include RSpec::Matchers
         | 
| 18 | 
            +
              config.mock_with :rspec
         | 
| 19 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,100 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: mini_auth
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0.pre
         | 
| 5 | 
            +
              prerelease: 6
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Tsutomu Kuroda
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2011-12-13 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: sqlite3
         | 
| 16 | 
            +
              requirement: &11513420 !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                none: false
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - ! '>='
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: '0'
         | 
| 22 | 
            +
              type: :development
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              version_requirements: *11513420
         | 
| 25 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 26 | 
            +
              name: rspec
         | 
| 27 | 
            +
              requirement: &11512560 !ruby/object:Gem::Requirement
         | 
| 28 | 
            +
                none: false
         | 
| 29 | 
            +
                requirements:
         | 
| 30 | 
            +
                - - ! '>='
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: '0'
         | 
| 33 | 
            +
              type: :development
         | 
| 34 | 
            +
              prerelease: false
         | 
| 35 | 
            +
              version_requirements: *11512560
         | 
| 36 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 37 | 
            +
              name: rspec-rails
         | 
| 38 | 
            +
              requirement: &11510640 !ruby/object:Gem::Requirement
         | 
| 39 | 
            +
                none: false
         | 
| 40 | 
            +
                requirements:
         | 
| 41 | 
            +
                - - ! '>='
         | 
| 42 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 43 | 
            +
                    version: '0'
         | 
| 44 | 
            +
              type: :development
         | 
| 45 | 
            +
              prerelease: false
         | 
| 46 | 
            +
              version_requirements: *11510640
         | 
| 47 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 48 | 
            +
              name: bcrypt-ruby
         | 
| 49 | 
            +
              requirement: &11509260 !ruby/object:Gem::Requirement
         | 
| 50 | 
            +
                none: false
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ! '>='
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
              type: :runtime
         | 
| 56 | 
            +
              prerelease: false
         | 
| 57 | 
            +
              version_requirements: *11509260
         | 
| 58 | 
            +
            description: A minimal authentication module for Rails
         | 
| 59 | 
            +
            email:
         | 
| 60 | 
            +
            - t-kuroda@oiax.jp
         | 
| 61 | 
            +
            executables: []
         | 
| 62 | 
            +
            extensions: []
         | 
| 63 | 
            +
            extra_rdoc_files: []
         | 
| 64 | 
            +
            files:
         | 
| 65 | 
            +
            - .gitignore
         | 
| 66 | 
            +
            - Gemfile
         | 
| 67 | 
            +
            - README.md
         | 
| 68 | 
            +
            - Rakefile
         | 
| 69 | 
            +
            - lib/mini_auth.rb
         | 
| 70 | 
            +
            - lib/mini_auth/version.rb
         | 
| 71 | 
            +
            - mini_auth.gemspec
         | 
| 72 | 
            +
            - spec/fake_app.rb
         | 
| 73 | 
            +
            - spec/mini_auth/authenticate_spec.rb
         | 
| 74 | 
            +
            - spec/mini_auth/password_spec.rb
         | 
| 75 | 
            +
            - spec/spec_helper.rb
         | 
| 76 | 
            +
            homepage: ''
         | 
| 77 | 
            +
            licenses: []
         | 
| 78 | 
            +
            post_install_message: 
         | 
| 79 | 
            +
            rdoc_options: []
         | 
| 80 | 
            +
            require_paths:
         | 
| 81 | 
            +
            - lib
         | 
| 82 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 83 | 
            +
              none: false
         | 
| 84 | 
            +
              requirements:
         | 
| 85 | 
            +
              - - ! '>='
         | 
| 86 | 
            +
                - !ruby/object:Gem::Version
         | 
| 87 | 
            +
                  version: '0'
         | 
| 88 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 89 | 
            +
              none: false
         | 
| 90 | 
            +
              requirements:
         | 
| 91 | 
            +
              - - ! '>'
         | 
| 92 | 
            +
                - !ruby/object:Gem::Version
         | 
| 93 | 
            +
                  version: 1.3.1
         | 
| 94 | 
            +
            requirements: []
         | 
| 95 | 
            +
            rubyforge_project: mini_auth
         | 
| 96 | 
            +
            rubygems_version: 1.8.10
         | 
| 97 | 
            +
            signing_key: 
         | 
| 98 | 
            +
            specification_version: 3
         | 
| 99 | 
            +
            summary: A minimal authentication module for Rails
         | 
| 100 | 
            +
            test_files: []
         |