toll 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b41bcc7073e05f9526843c304438b7bb81d558e
4
+ data.tar.gz: 175b6d736cc1d7353182fcd0450197ad76fce2b5
5
+ SHA512:
6
+ metadata.gz: 1a5f1675bf327e628a767611f5058a298079a87d06fdabd2b876d49fe1cf101e6272c4df637d06a1630377d2cd6e7ba66aaa90ae8d47ab07609d543f402ece6d
7
+ data.tar.gz: 097286e579359a2a70adeaa41568be6c6a0311e877debd0d27a6a8dc17e2da05a015a46aceebbc965bbdd4c78bf81e15e2dde661da0254dc96f155b1f62d5d40
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in toll.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Abraham Kuri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Toll
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/toll`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'toll'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install toll
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/toll/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "toll"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ class AddTollTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ change_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+ <% attributes.each do |attribute| -%>
6
+ t.<%= attribute.type %> :<%= attribute.name %>
7
+ <% end -%>
8
+
9
+ # Uncomment below if timestamps were not included in your original model.
10
+ # t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ # By default, we don't want to make any assumption about how to roll back a migration when your
16
+ # model already existed. Please edit below which fields you would like to remove in this migration.
17
+ raise ActiveRecord::IrreversibleMigration
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ class TollCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+ <% attributes.each do |attribute| -%>
6
+ t.<%= attribute.type %> :<%= attribute.name %>
7
+ <% end -%>
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/toll/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class TollGenerator < ActiveRecord::Generators::Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ include Toll::Generators::OrmHelpers
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ def copy_toll_migration
14
+ if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
15
+ migration_template "existing_migration.rb", "db/migrate/add_toll_to_#{table_name}.rb"
16
+ else
17
+ migration_template "migration.rb", "db/migrate/toll_create_#{table_name}.rb"
18
+ end
19
+ end
20
+
21
+ def generate_model
22
+ invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
23
+ end
24
+
25
+ def inject_toll_content
26
+ content = %q{ tollify}
27
+
28
+ class_path = if namespaced?
29
+ class_name.to_s.split("::")
30
+ else
31
+ [class_name]
32
+ end
33
+
34
+ indent_depth = class_path.size - 1
35
+ content = content.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n"
36
+ inject_into_class(model_path, class_path.last, content) if model_exists?
37
+ end
38
+
39
+ def migration_data
40
+ %Q{
41
+ ## Toll model fields
42
+ t.string :authentication_token}
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ module Toll
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ desc "Creates a Toll initializer in your application"
7
+
8
+ def copy_initializer
9
+ template "toll.rb", "config/initializers/toll.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Toll
2
+ module Generators
3
+ module OrmHelpers
4
+
5
+ def model_exists?
6
+ File.exists?(File.join(destination_root, model_path))
7
+ end
8
+
9
+ def migration_exists?(table_name)
10
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_toll_to_#{table_name}.rb$/).first
11
+ end
12
+
13
+ def migration_path
14
+ @migration_path ||= File.join("db", "migrate")
15
+ end
16
+
17
+ def model_path
18
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # Use this module to configure pager the available options
2
+ #
3
+ # Made with love by @icalialabs
4
+
5
+ Toll.setup do |config|
6
+
7
+ # The keys to use for user authentication
8
+ # config.authentication_keys = [:email]
9
+ #
10
+
11
+ # Name of the authentication token attribute for the User
12
+ # config.authentication_token_attribute_name = :authentication_token
13
+
14
+ # Authentication Token Length
15
+ # config.authentication_token_length = 64
16
+ end
@@ -0,0 +1,13 @@
1
+ module Toll
2
+ module Generators
3
+ class TollGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::ResourceHelpers
5
+
6
+ namespace "toll"
7
+ desc "Creates a model with the given name with toll" <<
8
+ "includes migration files"
9
+
10
+ hook_for :orm
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module Toll
2
+
3
+ # The keys to use for user authentication
4
+ mattr_accessor :authentication_keys
5
+ @@authentication_keys = [:email]
6
+
7
+ # Name of the authentication token attribute for the User
8
+ mattr_accessor :authentication_token_attribute_name
9
+ @@authentication_token_attribute_name = :authentication_token
10
+
11
+ # Authentication Token Length
12
+ mattr_accessor :authentication_token_length
13
+ @@authentication_token_length = 64
14
+
15
+ # Method to configure Toll
16
+ def self.setup
17
+ yield self
18
+ end
19
+
20
+ # Method to generate a Token
21
+ #
22
+ def self.token(length = Toll.authentication_token_length)
23
+ SecureRandom.urlsafe_base64(length)
24
+ end
25
+ end
26
+
27
+ require "toll/version"
28
+ require "toll/controllers/authenticable"
29
+ require "toll/hooks"
@@ -0,0 +1,59 @@
1
+ module Toll
2
+ module Controllers
3
+ module Authenticable
4
+
5
+ protected
6
+
7
+ def authenticate!
8
+ authenticate_with_token || render_unauthorized
9
+ end
10
+
11
+ def render_unauthorized
12
+ self.headers['WWW-Authenticate'] = 'Token realm="Application"'
13
+ render json: { errors: "Invalid session data" },
14
+ status: :unauthorized
15
+ end
16
+
17
+ def authenticate_with_token
18
+ authenticate_with_http_token do |token, options|
19
+
20
+ user = User.find_by(authentication_keys(options))
21
+
22
+ if user && secure_token_compare(user.send(Toll.authentication_token_attribute_name), token)
23
+ @current_user = user
24
+ end
25
+ end
26
+ end
27
+
28
+ def current_user
29
+ @current_user
30
+ end
31
+
32
+ def authenticated?
33
+ current_user.present?
34
+ end
35
+
36
+ private
37
+
38
+ # constant-time comparison algorithm to prevent timing attacks
39
+ # Thanks Devise
40
+ def secure_token_compare(a, b)
41
+ return false if a.blank? || b.blank? || a.bytesize != b.bytesize
42
+ l = a.unpack "C#{a.bytesize}"
43
+
44
+ res = 0
45
+ b.each_byte { |byte| res |= byte ^ l.shift }
46
+ res == 0
47
+ end
48
+
49
+ def authentication_keys(options = {})
50
+ {}.tap do |authentication_keys|
51
+ Toll.authentication_keys.each do |key|
52
+ authentication_keys[key] = options[key]
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,5 @@
1
+ require 'toll/interface'
2
+
3
+ ActiveSupport.on_load :active_record do
4
+ include Toll::Interface
5
+ end
@@ -0,0 +1,22 @@
1
+ require "toll/models/authenticable"
2
+
3
+ module Toll
4
+ module Interface
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def tollify
13
+ before_validation :ensure_authentication_token!
14
+
15
+ validates Toll.authentication_token_attribute_name, presence: true,
16
+ uniqueness: true
17
+
18
+ include Toll::Models::Authenticable
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ module Toll
2
+ module Models
3
+ module Authenticable
4
+
5
+ # Updates the record authentication but:
6
+ #
7
+ # - Validation is skipped.
8
+ # - Callbacks are invoked.
9
+ # - updated_at/updated_on column is updated if that column is available.
10
+ # - Updates all the attributes that are dirty in this object.
11
+ #
12
+ def update_authentication_token_without_validations
13
+ generate_authentication_token!
14
+ update_attribute(Toll.authentication_token_attribute_name, self.send(Toll.authentication_token_attribute_name))
15
+ end
16
+
17
+ # Updates the record but:
18
+ #
19
+ # - Validations are skipped.
20
+ # - Callbacks are skipped.
21
+ # - updated_at/updated_on are not updated.
22
+ def update_authentication_token!
23
+ generate_authentication_token!
24
+ update_column(Toll.authentication_token_attribute_name, self.send(Toll.authentication_token_attribute_name))
25
+ end
26
+
27
+ # Method to authenticate the user
28
+ # It only updates the token
29
+ #
30
+ # We are making sure the user is a valid record, that's why
31
+ # the `save` call
32
+ def authenticate_with_token
33
+ generate_authentication_token!
34
+ save
35
+ self
36
+ end
37
+
38
+ # Not really convinced about the sign_out method name
39
+ alias_method :sign_out, :authenticate_with_token
40
+
41
+ private
42
+
43
+ def generate_authentication_token!
44
+ begin
45
+ self.send("#{Toll.authentication_token_attribute_name}=", Toll.token)
46
+ end while self.class.exists?(Toll.authentication_token_attribute_name => self.send(Toll.authentication_token_attribute_name))
47
+ end
48
+
49
+ def ensure_authentication_token!
50
+ generate_authentication_token!
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Toll
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toll/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "toll"
8
+ spec.version = Toll::VERSION
9
+ spec.authors = ["Abraham Kuri"]
10
+ spec.email = ["kurenn@icalialabs.com"]
11
+
12
+ spec.summary = %q{Super simple yet powerful authentication for Rails APIs}
13
+ spec.description = %q{It is a super simple solution to authenticate users on an API using Rails based authentication_with_token}
14
+ spec.homepage = "https://github.com/IcaliaLabs/toll"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Abraham Kuri
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: It is a super simple solution to authenticate users on an API using Rails
42
+ based authentication_with_token
43
+ email:
44
+ - kurenn@icalialabs.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".travis.yml"
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/generators/active_record/templates/existing_migration.rb
60
+ - lib/generators/active_record/templates/migration.rb
61
+ - lib/generators/active_record/toll_generator.rb
62
+ - lib/generators/toll/install_generator.rb
63
+ - lib/generators/toll/orm_helpers.rb
64
+ - lib/generators/toll/templates/toll.rb
65
+ - lib/generators/toll/toll_generator.rb
66
+ - lib/toll.rb
67
+ - lib/toll/controllers/authenticable.rb
68
+ - lib/toll/hooks.rb
69
+ - lib/toll/interface.rb
70
+ - lib/toll/models/authenticable.rb
71
+ - lib/toll/version.rb
72
+ - toll.gemspec
73
+ homepage: https://github.com/IcaliaLabs/toll
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Super simple yet powerful authentication for Rails APIs
97
+ test_files: []
98
+ has_rdoc: