zetto 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +21 -0
  5. data/lib/generators/active_record/templates/migration.erb +11 -0
  6. data/lib/generators/active_record/templates/migration_existing.erb +7 -0
  7. data/lib/generators/active_record/zetto_generator.rb +59 -0
  8. data/lib/generators/zetto/install_generator.rb +15 -0
  9. data/lib/generators/zetto/orm_helpers.rb +30 -0
  10. data/lib/generators/zetto/templates/zetto.rb +37 -0
  11. data/lib/generators/zetto/zetto_generator.rb +17 -0
  12. data/lib/zetto.rb +11 -0
  13. data/lib/zetto/config/params.rb +80 -0
  14. data/lib/zetto/engine.rb +16 -0
  15. data/lib/zetto/extension/action_controller_base.rb +54 -0
  16. data/lib/zetto/extension/active_record.rb +60 -0
  17. data/lib/zetto/load.rb +23 -0
  18. data/lib/zetto/locales/en.yml +21 -0
  19. data/lib/zetto/modules/crypto.rb +86 -0
  20. data/lib/zetto/modules/load.rb +4 -0
  21. data/lib/zetto/services/authentication/find_user.rb +29 -0
  22. data/lib/zetto/services/authentication/load.rb +11 -0
  23. data/lib/zetto/services/cookie/find_session.rb +50 -0
  24. data/lib/zetto/services/cookie/load.rb +12 -0
  25. data/lib/zetto/services/cookie/save_session.rb +43 -0
  26. data/lib/zetto/services/encryption/load.rb +9 -0
  27. data/lib/zetto/services/encryption/password_hashing.rb +19 -0
  28. data/lib/zetto/services/info.rb +27 -0
  29. data/lib/zetto/services/session/get_user.rb +46 -0
  30. data/lib/zetto/services/session/load.rb +16 -0
  31. data/lib/zetto/services/session/registration.rb +36 -0
  32. data/lib/zetto/services/zetto_logger.rb +26 -0
  33. data/lib/zetto/storage/common/load.rb +7 -0
  34. data/lib/zetto/storage/common/response.rb +21 -0
  35. data/lib/zetto/storage/connect/load.rb +11 -0
  36. data/lib/zetto/storage/connect/redis_singelton.rb +17 -0
  37. data/lib/zetto/storage/impurety_data/data/response.rb +21 -0
  38. data/lib/zetto/storage/impurety_data/generate.rb +55 -0
  39. data/lib/zetto/storage/impurety_data/load.rb +18 -0
  40. data/lib/zetto/storage/impurety_data/restore.rb +22 -0
  41. data/lib/zetto/storage/impurety_data/save.rb +25 -0
  42. data/lib/zetto/storage/session/create.rb +73 -0
  43. data/lib/zetto/storage/session/data/response.rb +42 -0
  44. data/lib/zetto/storage/session/find_by_session.rb +27 -0
  45. data/lib/zetto/storage/session/load.rb +17 -0
  46. data/lib/zetto/version.rb +3 -0
  47. data/spec/controllers/application_controller_spec.rb +80 -0
  48. data/spec/dummy/README.rdoc +28 -0
  49. data/spec/dummy/Rakefile +6 -0
  50. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  51. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  52. data/spec/dummy/app/controllers/application_controller.rb +22 -0
  53. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  54. data/spec/dummy/app/models/application_record.rb +3 -0
  55. data/spec/dummy/app/models/cat.rb +3 -0
  56. data/spec/dummy/app/models/user.rb +3 -0
  57. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  58. data/spec/dummy/config.ru +4 -0
  59. data/spec/dummy/config/application.rb +33 -0
  60. data/spec/dummy/config/boot.rb +5 -0
  61. data/spec/dummy/config/database.yml +25 -0
  62. data/spec/dummy/config/environment.rb +5 -0
  63. data/spec/dummy/config/environments/development.rb +41 -0
  64. data/spec/dummy/config/environments/production.rb +79 -0
  65. data/spec/dummy/config/environments/test.rb +42 -0
  66. data/spec/dummy/config/initializers/assets.rb +11 -0
  67. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  68. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  69. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  70. data/spec/dummy/config/initializers/inflections.rb +16 -0
  71. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  72. data/spec/dummy/config/initializers/session_store.rb +3 -0
  73. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  74. data/spec/dummy/config/initializers/zetto.rb +36 -0
  75. data/spec/dummy/config/locales/en.yml +23 -0
  76. data/spec/dummy/config/routes.rb +4 -0
  77. data/spec/dummy/config/secrets.yml +22 -0
  78. data/spec/dummy/db/schema.rb +33 -0
  79. data/spec/dummy/db/test.sqlite3 +0 -0
  80. data/spec/dummy/lib/test/emulators/cookie.rb +22 -0
  81. data/spec/dummy/lib/test/emulators/request.rb +19 -0
  82. data/spec/dummy/log/development.log +42 -0
  83. data/spec/dummy/log/test.log +210 -0
  84. data/spec/dummy/public/404.html +67 -0
  85. data/spec/dummy/public/422.html +67 -0
  86. data/spec/dummy/public/500.html +66 -0
  87. data/spec/dummy/public/favicon.ico +0 -0
  88. data/spec/factories/users.rb +25 -0
  89. data/spec/models/sessions_spec.rb +78 -0
  90. data/spec/rails_helper.rb +18 -0
  91. metadata +293 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f900c8afe4f24f4d55616172a6325e6fe4234b7
4
+ data.tar.gz: e11642fa9488e6caf62ce590417e80689dcfbb6c
5
+ SHA512:
6
+ metadata.gz: 4a29b9afa0b0459f1f5f156fbd7069b84eed8e58616ffa4ba460e437a2c82567c93ba62c1592af7693775481c00f0af63b794ba0d54bf8def059bd4bdd721fba
7
+ data.tar.gz: c5b85f4470ddd779fe970c22f56206301554c1a06c728d175271ff931adfdd6a6776638ff11a46de1f072d111beac8b6e968ed4cba3aaad5cfc5733c44e61431
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Ivan
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.
@@ -0,0 +1,3 @@
1
+ = Zetto
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,21 @@
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
+
8
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
14
+
15
+ require 'rspec/core'
16
+ require 'rspec/core/rake_task'
17
+
18
+ desc "Run all specs in spec directory (excluding plugin specs)"
19
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
20
+
21
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ class ZettoCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= table_name %> do |t|
4
+ t.string :name
5
+ t.text :description
6
+ end
7
+ <% migration_data.each do |name, properties| %>
8
+ add_column :<%= table_name %>, :<%= name %>, <%= properties %> unless column_exists? :<%= table_name %>, :<%= name %>
9
+ <% end %>
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ class AddZettoTo<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ <% migration_data.each do |name, properties| %>
4
+ add_column :<%= table_name %>, :<%= name %>, <%= properties %> unless column_exists? :<%= table_name %>, :<%= name %>
5
+ <% end %>
6
+ end
7
+ end
@@ -0,0 +1,59 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/zetto/orm_helpers'
3
+ require 'zetto/config/params'
4
+
5
+ module ActiveRecord
6
+ module Generators
7
+
8
+ class ZettoGenerator < ActiveRecord::Generators::Base
9
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
10
+
11
+ include Zetto::Generators::OrmHelpers
12
+ source_root File.expand_path("../templates", __FILE__)
13
+
14
+ def add_zetto_migration
15
+ if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
16
+ migration_template "migration_existing.erb", "db/migrate/add_zetto_to_#{table_name}.rb", migration_version: migration_version
17
+ else
18
+ migration_template "migration.erb", "db/migrate/zetto_create_#{table_name}.rb", migration_version: migration_version
19
+ end
20
+ end
21
+
22
+ def generate_model
23
+ invoke "active_record:model", [name], migration: false unless model_exists? && behavior == :invoke
24
+ end
25
+
26
+ def inject_zetto_model_content
27
+ content = model_contents
28
+
29
+ class_path = namespaced? ? class_name.to_s.split("::") : [class_name]
30
+
31
+ indent_depth = class_path.size - 1
32
+ content = content.split("\n").map { |line| " " * indent_depth + line }.join("\n") << "\n"
33
+
34
+ inject_into_class(model_path, class_path.last, content) if model_exists?
35
+ end
36
+
37
+ def migration_data
38
+ fields = {}
39
+ key = Zetto::Config::Params.user_class_name
40
+ fields[key] = ":string, :null => false, default: ''"
41
+ key = Zetto::Config::Params.user_class_password
42
+ fields[key] = ":string, :null => false, default: ''"
43
+ fields
44
+ end
45
+
46
+ def rails5?
47
+ Rails.version.start_with? '5'
48
+ end
49
+
50
+ def migration_version
51
+ if rails5?
52
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+ module Zetto
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Some description of my generator here"
5
+
6
+ def self.source_root
7
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
8
+ end
9
+
10
+ def copy_initializer
11
+ template "zetto.rb", "config/initializers/zetto.rb"
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module Zetto
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ buffer = <<-CONTENT
6
+ zetto :authentication
7
+ CONTENT
8
+ buffer
9
+ end
10
+
11
+ private
12
+
13
+ def model_exists?
14
+ File.exist?(File.join(destination_root, model_path))
15
+ end
16
+
17
+ def migration_exists?(table_name)
18
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_devise_to_#{table_name}.rb$/).first
19
+ end
20
+
21
+ def migration_path
22
+ @migration_path ||= File.join("db", "migrate")
23
+ end
24
+
25
+ def model_path
26
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ # Add extension
2
+ require "zetto/load"
3
+ # Simple generator for rails
4
+ Zetto.setup do |config|
5
+
6
+ # Список пользовательских классов
7
+ config.user_classes = ['User']
8
+ # Поиск сесиии по этим полям
9
+ config.user_class_name = 'email'
10
+ config.user_class_password = 'password'
11
+
12
+ # Длинна пароля, больше чем
13
+ config.user_class_password_length_larger = 6
14
+
15
+ # Хешировать пароль с помощью
16
+ # ['MD5', 'SHA1', 'RMD160', 'SHA256', 'SHA384', 'SHA512']
17
+ config.user_class_password_crypto = 'SHA1'
18
+
19
+ # Настройки для подключения к бд redis, не используем 0 бд, так-как она предназначается для тестовых нужд
20
+ config.redis_connect = {:password => "", "db" => 1}
21
+
22
+ # Чем больше тем надежнее, но будет медленее поиск
23
+ config.session_length = 9
24
+
25
+ # Время жизни сесии
26
+ config.session_time_min = 30
27
+
28
+ # За сколько минут перегенирировать
29
+ config.session_time_restart_min = 5
30
+
31
+ # Ведет лог посещений пользователей
32
+ config.log = false
33
+
34
+ # Можно настроить сессию на ип, что повысит стойкость, но имеем проблемы для людей с динамическим ип, в некоторых случаях
35
+ config.check_ip = false
36
+
37
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Zetto
4
+ module Generators
5
+ class ZettoGenerator < Rails::Generators::NamedBase
6
+ include Rails::Generators::ResourceHelpers
7
+
8
+ namespace "zetto"
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ desc "Create table or add need field if table exist"
12
+
13
+ hook_for :orm
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require "zetto/engine"
2
+ require "zetto/config/params"
3
+
4
+ module Zetto
5
+
6
+ def self.setup(&block)
7
+ I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'zetto/locales', '*.yml').to_s]
8
+ Zetto::Config::Params.set_params(&block)
9
+ end
10
+
11
+ end
@@ -0,0 +1,80 @@
1
+ module Zetto
2
+ module Config
3
+
4
+ module Params
5
+
6
+ CRYPTO_ALGORITHMS = ['MD5', 'SHA1', 'RMD160', 'SHA256', 'SHA384', 'SHA512']
7
+
8
+ @user_classes = ['User']
9
+
10
+ @user_class_name = 'email'
11
+ @user_class_password = 'password'
12
+ @user_class_password_length_larger = 6
13
+ @user_class_password_crypto = 'SHA1'
14
+ @check_ip = false
15
+ @log = false
16
+
17
+ @redis_connect = {:password => "", "db" => 1}
18
+
19
+ @session_length = 9
20
+ @session_time_min = 30
21
+ @session_time_restart_min = 5
22
+
23
+ class << self
24
+
25
+ def self.attr_writer_with_type(type, *args)
26
+ args.each do |arg|
27
+ self.send(:define_method, "#{arg}=".intern) do |value|
28
+ unless value.class.to_s == type
29
+ raise ArgumentError.new(I18n.t('exseptions.not_specified_type', arg: arg, type: type, class_name: value.class.to_s))
30
+ end
31
+ instance_variable_set("@#{arg}", value)
32
+ end
33
+ end
34
+ end
35
+
36
+ attr_writer :check_ip, :log
37
+ attr_writer_with_type 'Fixnum', :session_time_restart_min, :session_time_min, :session_length, :user_class_password_length_larger
38
+ attr_writer_with_type 'Hash', :redis_connect
39
+ attr_writer_with_type 'String', :user_class_name, :user_class_password
40
+
41
+ attr_reader :user_class_password_crypto, :check_ip, :log, :session_time_restart_min, :session_time_min, :session_length, :user_class_password_length_larger, :redis_connect,
42
+ :user_class_name, :user_class_password
43
+
44
+ def set_params
45
+ yield self
46
+ end
47
+
48
+ def user_class_password_crypto=(value)
49
+ value = value.to_s.upcase
50
+ unless self::CRYPTO_ALGORITHMS.include?(value)
51
+ raise ArgumentError.new(I18n.t('exseptions.unknown_algorithm', algorithm: value.to_s ))
52
+ end
53
+ @user_class_password_crypto = value
54
+ end
55
+
56
+ def user_classes=(arr)
57
+ @user_classes = arr.map{ |class_name| class_name.to_s.capitalize }
58
+ end
59
+
60
+ def user_class(class_str)
61
+ begin
62
+ unless @user_classes.include?(class_str)
63
+ raise ArgumentError.new(I18n.t('exseptions.unknown_target_class', class_name: class_str.to_s ))
64
+ end
65
+ class_str.constantize
66
+ rescue Exception => e
67
+ puts e.message
68
+ nil
69
+ end
70
+ end
71
+
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+
79
+
80
+
@@ -0,0 +1,16 @@
1
+ module Zetto
2
+
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Zetto
5
+
6
+ config.generators do |g|
7
+ g.test_framework :rspec, :fixture => false
8
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
9
+ g.assets false
10
+ g.helper false
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+
@@ -0,0 +1,54 @@
1
+ module Zetto::Extension::ActionControllerBase
2
+ extend ActiveSupport::Concern
3
+ require "zetto/config/params"
4
+
5
+ require "zetto/storage/common/load"
6
+ require "zetto/storage/connect/load"
7
+ require "zetto/storage/impurety_data/load"
8
+ require "zetto/storage/session/load"
9
+
10
+ require "zetto/modules/load"
11
+
12
+ require "zetto/services/encryption/load"
13
+ require "zetto/services/cookie/load"
14
+ require "zetto/services/session/load"
15
+ require "zetto/services/authentication/load"
16
+
17
+
18
+ included do
19
+
20
+ def current_user
21
+ begin
22
+ Zetto::Services::Session::GetUser.new(cookies, request.user_agent, request.remote_ip).execute
23
+ rescue ArgumentError => e
24
+ Zetto::Services::Info.error_message I18n.t('exseptions.invalid_arguments', argument: 'Zetto::ControllerMethods', current_method: __method__), e
25
+ nil
26
+ rescue Exception => e
27
+ Zetto::Services::Info.error_message I18n.t('exseptions.unknown_error', argument: 'Zetto::ControllerMethods', current_method: __method__), e
28
+ nil
29
+ end
30
+ end
31
+
32
+ def authorization(class_name, name, password)
33
+ begin
34
+ hashed_password = Zetto::Services::Encryption::PasswordHashing.new(password).execute
35
+ user = Zetto::Services::Authentication::FindUser.new(class_name, name, hashed_password).execute
36
+ return nil if user.nil?
37
+ return nil if user.new_record?
38
+ Zetto::Services::Session::Registration.new(user, cookies, request.user_agent, request.remote_ip).execute
39
+ rescue ArgumentError => e
40
+ Zetto::Services::Info.error_message I18n.t('exseptions.invalid_arguments', argument: 'Zetto::ControllerMethods', current_method: __method__), e
41
+ nil
42
+ rescue Exception => e
43
+ Zetto::Services::Info.error_message I18n.t('exseptions.unknown_error', argument: 'Zetto::ControllerMethods', current_method: __method__), e
44
+ nil
45
+ end
46
+ end
47
+
48
+ def logout
49
+ @cookies[:rembo] = nil
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,60 @@
1
+
2
+
3
+ module Zetto::Extension::ActiveRecord
4
+ extend ActiveSupport::Concern
5
+ require "zetto/modules/load"
6
+ require "zetto/services/encryption/load"
7
+
8
+ included do
9
+
10
+ protected
11
+
12
+ def password_confirmed
13
+ begin
14
+ password_field = Zetto::Config::Params.user_class_password
15
+ password_value = send(password_field).to_s
16
+ errors.add(password_field.intern, I18n.t('validate.password_confirm')) unless password_value == password_confirmation
17
+
18
+ password_confirmation
19
+ rescue Exception => e
20
+ Zetto::Services::Info.error_message I18n.t('exseptions.undefined_field', field: password_field), e
21
+ end
22
+ end
23
+
24
+ def password_encryption
25
+ begin
26
+ password_field = Zetto::Config::Params.user_class_password
27
+ password_value = send(password_field)
28
+ hashed_password = Zetto::Services::Encryption::PasswordHashing.new(password_value).execute
29
+ send(password_field+'=', hashed_password)
30
+ rescue Exception => e
31
+ Zetto::Services::Info.error_message I18n.t('exseptions.undefined_field', field: password_field), e
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ class_methods do
38
+
39
+ def zetto(method)
40
+ method_name = 'zetto_' + method.to_s
41
+ self.send(method_name) if 'zetto_authentication' == method_name
42
+ end
43
+
44
+ protected
45
+
46
+ def zetto_authentication
47
+ attr_accessor :password_confirmation
48
+ validates Zetto::Config::Params.user_class_name.intern, uniqueness:true
49
+ validates Zetto::Config::Params.user_class_password.intern, presence: true,
50
+ length: { minimum: Zetto::Config::Params.user_class_password_length_larger }
51
+
52
+ validate :password_confirmed
53
+ before_save :password_encryption
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+