captcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +210 -0
  4. data/Rakefile +22 -0
  5. data/app/controllers/captcher/captchas_controller.rb +22 -0
  6. data/app/controllers/concerns/captcher/captcha_aware.rb +30 -0
  7. data/app/helpers/captcher/application_helper.rb +13 -0
  8. data/app/views/layouts/captcher/application.html.erb +16 -0
  9. data/config/routes.rb +10 -0
  10. data/lib/captcher.rb +57 -0
  11. data/lib/captcher/base_captcha.rb +46 -0
  12. data/lib/captcher/captchas/awesome_captcha.rb +7 -0
  13. data/lib/captcher/captchas/code_captcha.rb +40 -0
  14. data/lib/captcher/captchas/math_captcha.rb +7 -0
  15. data/lib/captcher/config.rb +41 -0
  16. data/lib/captcher/engine.rb +5 -0
  17. data/lib/captcher/text_image.rb +48 -0
  18. data/lib/captcher/version.rb +3 -0
  19. data/lib/fonts/Bangers-Regular.ttf +0 -0
  20. data/lib/fonts/CarterOne.ttf +0 -0
  21. data/lib/fonts/FrederickatheGreat-Regular.ttf +0 -0
  22. data/lib/fonts/IndieFlower-Regular.ttf +0 -0
  23. data/lib/fonts/LobsterTwo-BoldItalic.ttf +0 -0
  24. data/lib/fonts/SigmarOne-Regular.ttf +0 -0
  25. data/lib/tasks/captcher_tasks.rake +4 -0
  26. data/spec/dummy/Rakefile +6 -0
  27. data/spec/dummy/app/assets/config/manifest.js +4 -0
  28. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  29. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  30. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  31. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  32. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  33. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  34. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  35. data/spec/dummy/app/jobs/application_job.rb +2 -0
  36. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  37. data/spec/dummy/app/models/application_record.rb +3 -0
  38. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  39. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  40. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  41. data/spec/dummy/bin/bundle +3 -0
  42. data/spec/dummy/bin/rails +4 -0
  43. data/spec/dummy/bin/rake +4 -0
  44. data/spec/dummy/bin/setup +36 -0
  45. data/spec/dummy/bin/update +31 -0
  46. data/spec/dummy/bin/yarn +11 -0
  47. data/spec/dummy/config.ru +5 -0
  48. data/spec/dummy/config/application.rb +30 -0
  49. data/spec/dummy/config/boot.rb +5 -0
  50. data/spec/dummy/config/cable.yml +10 -0
  51. data/spec/dummy/config/database.yml +25 -0
  52. data/spec/dummy/config/environment.rb +5 -0
  53. data/spec/dummy/config/environments/development.rb +61 -0
  54. data/spec/dummy/config/environments/production.rb +94 -0
  55. data/spec/dummy/config/environments/test.rb +46 -0
  56. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  57. data/spec/dummy/config/initializers/assets.rb +14 -0
  58. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  59. data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
  60. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  61. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  62. data/spec/dummy/config/initializers/inflections.rb +16 -0
  63. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  64. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  65. data/spec/dummy/config/locales/en.yml +33 -0
  66. data/spec/dummy/config/puma.rb +34 -0
  67. data/spec/dummy/config/routes.rb +3 -0
  68. data/spec/dummy/config/spring.rb +6 -0
  69. data/spec/dummy/config/storage.yml +34 -0
  70. data/spec/dummy/db/development.sqlite3 +0 -0
  71. data/spec/dummy/db/test.sqlite3 +0 -0
  72. data/spec/dummy/log/development.log +6608 -0
  73. data/spec/dummy/log/test.log +6492 -0
  74. data/spec/dummy/package.json +5 -0
  75. data/spec/dummy/public/404.html +67 -0
  76. data/spec/dummy/public/422.html +67 -0
  77. data/spec/dummy/public/500.html +66 -0
  78. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  79. data/spec/dummy/public/apple-touch-icon.png +0 -0
  80. data/spec/dummy/public/favicon.ico +0 -0
  81. data/spec/dummy/tmp/development_secret.txt +1 -0
  82. data/spec/helpers.rb +23 -0
  83. data/spec/lib/captcher/config_spec.rb +41 -0
  84. data/spec/models/captchas/code_captcha_spec.rb +45 -0
  85. data/spec/rails_helper.rb +72 -0
  86. data/spec/requests/captcha_management_spec.rb +48 -0
  87. data/spec/spec_helper.rb +96 -0
  88. data/spec/tmp/test.png +0 -0
  89. metadata +291 -0
@@ -0,0 +1,7 @@
1
+ module Captcher
2
+ module Captchas
3
+ class AwesomeCaptcha
4
+ # TODO: implement
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,40 @@
1
+ module Captcher
2
+ module Captchas
3
+ class CodeCaptcha < BaseCaptcha
4
+ SPECIAL_CHAR_CODES = (91..96).freeze
5
+
6
+ self.name = :code_captcha
7
+
8
+ # rubocop:disable Naming/MemoizedInstanceVariableName
9
+ def after_initialize
10
+ @payload ||= random_text
11
+ end
12
+ # rubocop:enable Naming/MemoizedInstanceVariableName
13
+
14
+ # rubocop:disable Lint/UnusedMethodArgument
15
+ def represent(format = :html, options = {})
16
+ Captcher::TextImage.new(@payload, own_config).generate
17
+ end
18
+ # rubocop:enable Lint/UnusedMethodArgument
19
+
20
+ def validate(confirmation)
21
+ confirmation.to_s.strip.casecmp(@payload).zero?
22
+ end
23
+
24
+ private
25
+
26
+ def random_text
27
+ @random_text ||= Array.new(own_config[:count]).map { random_char }.join("")
28
+ end
29
+
30
+ def random_char
31
+ random_char_code.chr
32
+ end
33
+
34
+ def random_char_code
35
+ char_code = ("A".ord + (rand * ("z".ord - "A".ord)).floor)
36
+ char_code.in?(SPECIAL_CHAR_CODES) ? random_char_code : char_code
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ module Captcher
2
+ module Captchas
3
+ class MathCaptcha
4
+ # TODO: implement
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ module Captcher
2
+ class Config < OpenStruct
3
+ def initialize
4
+ super
5
+ yield(self) if block_given?
6
+ end
7
+
8
+ def merge(other)
9
+ to_h.merge(other.to_h)
10
+ end
11
+
12
+ def respond_to_missing?
13
+ true
14
+ end
15
+
16
+ def method_missing(name, *args)
17
+ if block_given?
18
+ branch = self.class.new
19
+ yield(branch)
20
+ args = [branch]
21
+ end
22
+ name = to_writer(name, args)
23
+ super
24
+ end
25
+
26
+ def to_h
27
+ attributes = super
28
+ attributes.map do |key, value|
29
+ if value.class == self.class
30
+ [key, value.to_h]
31
+ else
32
+ [key, value]
33
+ end
34
+ end.to_h
35
+ end
36
+
37
+ def to_writer(name, args)
38
+ args && (name !~ /=$/) ? "#{name}=" : name
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Captcher
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Captcher
4
+ end
5
+ end
@@ -0,0 +1,48 @@
1
+ module Captcher
2
+ class TextImage
3
+ attr_accessor :text, :config
4
+
5
+ def initialize(text, config)
6
+ @text = text
7
+ @config = config
8
+ end
9
+
10
+ # rubocop:disable Metrics/AbcSize
11
+ # rubocop:disable Metrics/MethodLength
12
+ # rubocop:disable Lint/Void
13
+ def generate
14
+ MiniMagick::Tool::Convert.new do |i|
15
+ i.font random_font
16
+ i.size image_size
17
+ i.pointsize config[:font_size]
18
+ i.fill config[:font_color]
19
+ i.gravity "center"
20
+ i.canvas config[:background]
21
+ i.draw "text 0,0 '#{text}'"
22
+ i.noise + "Gaussian"
23
+ i << "#{config[:format]}:-"
24
+ end
25
+ end
26
+ # rubocop:enable Lint/Void
27
+ # rubocop:enable Metrics/MethodLength
28
+ # rubocop:enable Metrics/AbcSize
29
+
30
+ private
31
+
32
+ def image_size
33
+ "#{image_width}x#{image_height}"
34
+ end
35
+
36
+ def image_height
37
+ config[:font_size] + 10
38
+ end
39
+
40
+ def image_width
41
+ config[:font_size] * config[:count] + 10
42
+ end
43
+
44
+ def random_font
45
+ config[:fonts].sample
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Captcher
2
+ VERSION = '0.1.0'
3
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :captcher do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,4 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
4
+ //= link captcher_manifest.js
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the `rails generate channel` command.
3
+ //
4
+ //= require action_cable
5
+ //= require_self
6
+ //= require_tree ./channels
7
+
8
+ (function() {
9
+ this.App || (this.App = {});
10
+
11
+ App.cable = ActionCable.createConsumer();
12
+
13
+ }).call(this);
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag 'application', media: 'all' %>
9
+ <%= javascript_include_tag 'application' %>
10
+ </head>
11
+
12
+ <body>
13
+ <%= yield %>
14
+ </body>
15
+ </html>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a starting point to setup your application.
14
+ # Add necessary setup steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ # Install JavaScript dependencies if using Yarn
21
+ # system('bin/yarn')
22
+
23
+ # puts "\n== Copying sample files =="
24
+ # unless File.exist?('config/database.yml')
25
+ # cp 'config/database.yml.sample', 'config/database.yml'
26
+ # end
27
+
28
+ puts "\n== Preparing database =="
29
+ system! 'bin/rails db:setup'
30
+
31
+ puts "\n== Removing old logs and tempfiles =="
32
+ system! 'bin/rails log:clear tmp:clear'
33
+
34
+ puts "\n== Restarting application server =="
35
+ system! 'bin/rails restart'
36
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a way to update your development environment automatically.
14
+ # Add necessary update steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ # Install JavaScript dependencies if using Yarn
21
+ # system('bin/yarn')
22
+
23
+ puts "\n== Updating database =="
24
+ system! 'bin/rails db:migrate'
25
+
26
+ puts "\n== Removing old logs and tempfiles =="
27
+ system! 'bin/rails log:clear tmp:clear'
28
+
29
+ puts "\n== Restarting application server =="
30
+ system! 'bin/rails restart'
31
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path('..', __dir__)
3
+ Dir.chdir(APP_ROOT) do
4
+ begin
5
+ exec "yarnpkg", *ARGV
6
+ rescue Errno::ENOENT
7
+ $stderr.puts "Yarn executable was not detected in the system."
8
+ $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
9
+ exit 1
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
@@ -0,0 +1,30 @@
1
+ require_relative 'boot'
2
+
3
+ require "rails"
4
+ # Pick the frameworks you want:
5
+ require "active_model/railtie"
6
+ require "active_job/railtie"
7
+ require "active_record/railtie"
8
+ require "active_storage/engine"
9
+ require "action_controller/railtie"
10
+ require "action_mailer/railtie"
11
+ require "action_view/railtie"
12
+ require "action_cable/engine"
13
+ require "sprockets/railtie"
14
+ # require "rails/test_unit/railtie"
15
+
16
+ Bundler.require(*Rails.groups)
17
+ require "captcher"
18
+
19
+ module Dummy
20
+ class Application < Rails::Application
21
+ # Initialize configuration defaults for originally generated Rails version.
22
+ config.load_defaults 5.2
23
+
24
+ # Settings in config/environments/* take precedence over those specified here.
25
+ # Application configuration can go into files in config/initializers
26
+ # -- all .rb files in that directory are automatically loaded after loading
27
+ # the framework and any gems in your application.
28
+ end
29
+ end
30
+