tok 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +62 -0
  8. data/Rakefile +22 -0
  9. data/app/controllers/tok/base_controller.rb +11 -0
  10. data/app/controllers/tok/sessions_controller.rb +21 -0
  11. data/app/controllers/tok/users_controller.rb +14 -0
  12. data/config/routes.rb +5 -0
  13. data/db/migrate/20141011022222_create_users.rb +13 -0
  14. data/lib/generators/tok/install_generator.rb +55 -0
  15. data/lib/generators/tok/templates/migration/create_model.rb +13 -0
  16. data/lib/generators/tok/templates/model.rb +3 -0
  17. data/lib/generators/tok/templates/tok.rb +38 -0
  18. data/lib/tok.rb +19 -0
  19. data/lib/tok/authentication.rb +53 -0
  20. data/lib/tok/configuration.rb +41 -0
  21. data/lib/tok/controller.rb +64 -0
  22. data/lib/tok/engine.rb +26 -0
  23. data/lib/tok/version.rb +3 -0
  24. data/spec/controllers/sessions_controller_spec.rb +62 -0
  25. data/spec/controllers/users_controller_spec.rb +28 -0
  26. data/spec/dummy/README.rdoc +28 -0
  27. data/spec/dummy/Rakefile +6 -0
  28. data/spec/dummy/app/assets/images/.keep +0 -0
  29. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  30. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  31. data/spec/dummy/app/mailers/.keep +0 -0
  32. data/spec/dummy/app/models/.keep +0 -0
  33. data/spec/dummy/app/models/concerns/.keep +0 -0
  34. data/spec/dummy/bin/bundle +3 -0
  35. data/spec/dummy/bin/rails +4 -0
  36. data/spec/dummy/bin/rake +4 -0
  37. data/spec/dummy/config.ru +4 -0
  38. data/spec/dummy/config/application.rb +30 -0
  39. data/spec/dummy/config/boot.rb +4 -0
  40. data/spec/dummy/config/database.yml +85 -0
  41. data/spec/dummy/config/environment.rb +5 -0
  42. data/spec/dummy/config/environments/development.rb +28 -0
  43. data/spec/dummy/config/environments/production.rb +67 -0
  44. data/spec/dummy/config/environments/test.rb +39 -0
  45. data/spec/dummy/config/initializers/secret_token.rb +19 -0
  46. data/spec/dummy/config/initializers/wrap_parameters.rb +13 -0
  47. data/spec/dummy/config/locales/en.yml +23 -0
  48. data/spec/dummy/config/routes.rb +56 -0
  49. data/spec/dummy/config/secrets.yml +22 -0
  50. data/spec/dummy/db/schema.rb +29 -0
  51. data/spec/dummy/lib/assets/.keep +0 -0
  52. data/spec/dummy/lib/tasks/.keep +0 -0
  53. data/spec/dummy/log/.keep +0 -0
  54. data/spec/dummy/public/404.html +67 -0
  55. data/spec/dummy/public/422.html +67 -0
  56. data/spec/dummy/public/500.html +66 -0
  57. data/spec/dummy/public/favicon.ico +0 -0
  58. data/spec/dummy/public/robots.txt +5 -0
  59. data/spec/factories/users.rb +6 -0
  60. data/spec/generators/install_generator_spec.rb +30 -0
  61. data/spec/routing/routes_spec.rb +57 -0
  62. data/spec/spec_helper.rb +30 -0
  63. data/spec/support/generator_helpers.rb +21 -0
  64. data/spec/support/json_helpers.rb +7 -0
  65. data/spec/tok/configuration_spec.rb +103 -0
  66. data/spec/tok/controller_spec.rb +24 -0
  67. data/tok.gemspec +32 -0
  68. metadata +279 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f8d7c0073c47bf0076e05f6a2e5b0e8efc27969
4
+ data.tar.gz: 37421e1517b529672da047e6be6538916671b8e4
5
+ SHA512:
6
+ metadata.gz: 94a39870d25f1817e861bf137e56df9b77c96cb3857426f805c7cb51af474064ddd99fc21eec39f63b5f8faba146f805c62a6a998b4b77215dd4ea539f6ad8a8
7
+ data.tar.gz: cec2214f4f1dafb913ab2bc24bb4622c67f25a8ecc62bbb16a5ed9fb408689ebabae96d9b7c2af52fa36bad62f2c95d2b7dc6dd821ef0e863da178795454c58a
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/dummy/db/seeds.rb
10
+ /spec/dummy/db/migrate/*.rb
11
+ /spec/dummy/Gemfile
12
+ /spec/dummy/Gemfile.lock
13
+ /tmp/
14
+ *.bundle
15
+ *.so
16
+ *.o
17
+ *.a
18
+ mkmf.log
19
+ .ruby-version
20
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+
5
+ before_script:
6
+ - psql -c 'create database dummy_test;' -U postgres
7
+
8
+ script:
9
+ - RAILS_ENV=test bundle exec rake db:migrate
10
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ahmed Hazem
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.
@@ -0,0 +1,62 @@
1
+ # Tok
2
+
3
+ No-frills token-based authentication for modern Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Tok works with (and is tested against) Rails > 4.0. To install, add this line to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tok'
11
+ ```
12
+
13
+ Then run `bundle install`. You can always install the gem yourself by executing:
14
+
15
+ $ gem install tok
16
+
17
+ ## Usage
18
+
19
+ First, run the generator by executing:
20
+
21
+ $ rails generate tok:install
22
+
23
+ The generator by default does the following:
24
+
25
+ - Inserts `Tok::Authentication` into `User` model (or creates one if not available)
26
+ - Inserts `Tok::Controller` into your `ApplicationController`
27
+ - Creates a number of default routes `signup`, `login`, and `logout`
28
+ - Creates an initializer at `config/initializers/tok.rb` to allow further configuration
29
+
30
+ A custom model can be specified by passing `--model` (or just `-m`) as an option to the generator:
31
+
32
+ $ rails generate tok:install -m Account
33
+ $ rails generate tok:install --model=Account
34
+
35
+ In this case, the generator will also create a migration in order to create a table with the specified model pluralized.
36
+
37
+ Next, run migrations by executing:
38
+
39
+ $ rake db:migrate
40
+
41
+ ### Configure
42
+
43
+ To configure Tok, you can override defaults in your `config/initializers/tok.rb`. Be sure to check the initializer for further details into what values are used by default.
44
+
45
+ ```ruby
46
+ # Use the following configuration block to adjust Tok.
47
+ Tok.configure do |config|
48
+ config.model = Account
49
+ config.bcrypt_cost = 8
50
+ config.signup_route = "sign_up"
51
+ config.login_route = "sign_in"
52
+ config.logout_route = "sign_out"
53
+ end
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/[my-github-username]/tok/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
@@ -0,0 +1,22 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ namespace :db do
5
+ desc "Migrate /spec/dummy app's database"
6
+ task :migrate do
7
+ `cd spec/dummy && rake db:migrate`
8
+ end
9
+
10
+ desc "Rollback /spec/dummy app's database"
11
+ task :rollback do
12
+ `cd spec/dummy && rake db:rollback`
13
+ end
14
+
15
+ desc "Reset /spec/dummy app's development and test databases"
16
+ task :reset do
17
+ `cd spec/dummy && rake db:drop && rake db:create`
18
+ end
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new(:spec)
22
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ module Tok
2
+ class BaseController < ApplicationController
3
+ include Tok::Controller
4
+
5
+ private
6
+
7
+ def model_params
8
+ params.require(model_name.to_sym).permit(:email, :password)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module Tok
2
+ class SessionsController < BaseController
3
+ # POST /login
4
+ def create
5
+ @model = model_class.authenticate(model_params)
6
+
7
+ if @model
8
+ render json: {token: @model.authentication_token}.to_json, status: :created
9
+ else
10
+ render json: {"error" => "Invalid email or password!"}.to_json, status: :unprocessable_entity
11
+ end
12
+ end
13
+
14
+ # DELETE /logout
15
+ def destroy
16
+ current_user.reset_authentication_token if current_user
17
+
18
+ head :no_content
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module Tok
2
+ class UsersController < BaseController
3
+ # POST /signup
4
+ def create
5
+ @model = model_class.new(model_params)
6
+
7
+ if @model.save
8
+ render json: @model, status: :created
9
+ else
10
+ render json: @model.errors, status: :unprocessable_entity
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ post "#{Tok.configuration.signup_route}", to: "tok/users#create"
3
+ post "#{Tok.configuration.login_route}", to: "tok/sessions#create"
4
+ delete "#{Tok.configuration.logout_route}", to: "tok/sessions#destroy"
5
+ end
@@ -0,0 +1,13 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :email, null: false
5
+ t.string :encrypted_password, null: false
6
+ t.string :authentication_token
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :users, :email, unique: true
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Tok
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ class_option :model, type: :string, aliases: "-m", desc: "Specify a model different than User"
12
+
13
+ def copy_initializer
14
+ template "tok.rb", "config/initializers/tok.rb"
15
+ end
16
+
17
+ def include_tok_into_application_controller
18
+ inject_into_class "app/controllers/application_controller.rb", ApplicationController, " include Tok::Controller\n"
19
+ end
20
+
21
+ def create_or_include_tok_in_model
22
+ if File.exists?("app/models/#{model_file}")
23
+ inject_into_file "app/models/#{model_file}", " include Tok::Authentication\n",
24
+ after: "class #{model_name.classify} < ActiveRecord::Base"
25
+ else
26
+ template "model.rb", "app/models/#{model_file}"
27
+ end
28
+ end
29
+
30
+ def create_tok_migration
31
+ if options[:model]
32
+ migration_template "migration/create_model.rb", "db/migrate/create_#{options[:model].downcase.pluralize}.rb"
33
+ end
34
+ end
35
+
36
+ def self.next_migration_number(dir)
37
+ ActiveRecord::Generators::Base.next_migration_number(dir)
38
+ end
39
+
40
+ private
41
+
42
+ def model
43
+ options[:model].classify
44
+ end
45
+
46
+ def model_name
47
+ options[:model] || "user"
48
+ end
49
+
50
+ def model_file
51
+ "#{model_name.downcase}.rb"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ class Create<%= model.pluralize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= model.tableize %> do |t|
4
+ t.string :email, null: false
5
+ t.string :encrypted_password, null: false
6
+ t.string :authentication_token
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :<%= model.tableize %>, :email, unique: true
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ class <%= model_name.classify %> < ActiveRecord::Base
2
+ include Tok::Authentication
3
+ end
@@ -0,0 +1,38 @@
1
+ # Use the following configuration block to adjust Tok.
2
+ Tok.configure do |config|
3
+ # -- Model --
4
+ # If you used a model different than the already included `User` model, you
5
+ # have to specify the name of the model over here.
6
+ # Default: User
7
+ #
8
+ # config.model =
9
+
10
+
11
+ # -- Bcrypt Cost --
12
+ # The cost factor for Bcrypt, defines how many rounds a password should be
13
+ # hashed.
14
+ # Default: 10
15
+ #
16
+ # config.bcrypt_cost =
17
+
18
+
19
+ # -- Signup Route --
20
+ # The route to be used for handling creation of new users.
21
+ # Default: `signup`
22
+ #
23
+ # config.signup_route =
24
+
25
+
26
+ # -- Login Route --
27
+ # The route to be used for handling authentication.
28
+ # Default: `login`
29
+ #
30
+ # config.login_route =
31
+
32
+
33
+ # -- Logout Route --
34
+ # The route to be used for logging users out.
35
+ # Default: `logout`
36
+ #
37
+ # config.logout_route =
38
+ end
@@ -0,0 +1,19 @@
1
+ require 'tok/engine'
2
+ require 'tok/configuration'
3
+ require 'tok/controller'
4
+ require 'tok/authentication'
5
+ require 'tok/version'
6
+
7
+ module Tok
8
+ def self.configuration
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ def self.configuration=(config)
13
+ @configuration = config
14
+ end
15
+
16
+ def self.configure
17
+ yield configuration
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ module Tok
2
+ module Authentication
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_accessor :password
7
+
8
+ include Callbacks
9
+ end
10
+
11
+ module ClassMethods
12
+ def authenticate(params)
13
+ model = self.where(email: params[:email]).first
14
+ model if model && BCrypt::Password.new(model.encrypted_password) == params[:password]
15
+ end
16
+ end
17
+
18
+ module Callbacks
19
+ extend ActiveSupport::Concern
20
+
21
+ included do
22
+ before_save :encrypt_password
23
+ before_create :ensure_authentication_token
24
+ end
25
+ end
26
+
27
+ def reset_authentication_token
28
+ self.authentication_token = generate_authentication_token
29
+ self.save
30
+ end
31
+
32
+ private
33
+
34
+ def encrypt_password
35
+ self.encrypted_password = encrypt(password) if password.present?
36
+ end
37
+
38
+ def encrypt(password)
39
+ BCrypt::Password.create(password, cost: Tok.configuration.bcrypt_cost)
40
+ end
41
+
42
+ def ensure_authentication_token
43
+ self.authentication_token = generate_authentication_token if authentication_token.blank?
44
+ end
45
+
46
+ def generate_authentication_token
47
+ loop do
48
+ random = SecureRandom.urlsafe_base64(nil, false)
49
+ break random unless self.class.exists?(authentication_token: random)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ require 'bcrypt'
2
+
3
+ module Tok
4
+ class Configuration
5
+ attr_accessor :model,
6
+ :bcrypt_cost,
7
+ :signup_route,
8
+ :login_route,
9
+ :logout_route
10
+
11
+ def model
12
+ @model || ::User
13
+ end
14
+
15
+ def bcrypt_cost
16
+ if test_environment?
17
+ BCrypt::Engine::MIN_COST
18
+ else
19
+ @bcrypt_cost || BCrypt::Engine::DEFAULT_COST
20
+ end
21
+ end
22
+
23
+ def signup_route
24
+ @signup_route || "signup"
25
+ end
26
+
27
+ def login_route
28
+ @login_route || "login"
29
+ end
30
+
31
+ def logout_route
32
+ @logout_route || "logout"
33
+ end
34
+
35
+ private
36
+
37
+ def test_environment?
38
+ defined?(:Rails) && Rails.env.test?
39
+ end
40
+ end
41
+ end