cadinsor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +109 -0
  4. data/README.rdoc +3 -0
  5. data/Rakefile +34 -0
  6. data/app/controllers/cadinsor/api_keys_controller.rb +29 -0
  7. data/app/controllers/cadinsor/application_controller.rb +4 -0
  8. data/app/helpers/cadinsor/api_keys_helper.rb +4 -0
  9. data/app/helpers/cadinsor/application_helper.rb +4 -0
  10. data/app/models/cadinsor/api_key.rb +19 -0
  11. data/app/models/cadinsor/client_app.rb +13 -0
  12. data/app/views/cadinsor/api_keys/create.rabl +3 -0
  13. data/app/views/cadinsor/api_keys/show.rabl +10 -0
  14. data/app/views/cadinsor/application/cadinsor_error_response.rabl +3 -0
  15. data/app/views/layouts/cadinsor/application.html.erb +14 -0
  16. data/config/initializers/rabl_config.rb +4 -0
  17. data/config/routes.rb +4 -0
  18. data/db/migrate/20140111160304_create_cadinsor_client_apps.rb +9 -0
  19. data/db/migrate/20140111160653_create_cadinsor_api_keys.rb +10 -0
  20. data/lib/cadinsor.rb +6 -0
  21. data/lib/cadinsor/engine.rb +10 -0
  22. data/lib/cadinsor/extensions.rb +61 -0
  23. data/lib/cadinsor/extensions/request_error.rb +6 -0
  24. data/lib/cadinsor/version.rb +3 -0
  25. data/lib/generators/cadinsor/install/USAGE +7 -0
  26. data/lib/generators/cadinsor/install/install_generator.rb +30 -0
  27. data/lib/generators/cadinsor/install/templates/cadinsor_initializer.erb +29 -0
  28. data/lib/tasks/api_key.rake +27 -0
  29. data/lib/tasks/cadinsor_tasks.rake +4 -0
  30. data/lib/tasks/client_app.rake +85 -0
  31. data/test/cadinsor_test.rb +7 -0
  32. data/test/controllers/cadinsor/api_keys_controller_test.rb +9 -0
  33. data/test/dummy/Gemfile.lock +25 -0
  34. data/test/dummy/README.rdoc +28 -0
  35. data/test/dummy/Rakefile +6 -0
  36. data/test/dummy/app/assets/javascripts/application.js +13 -0
  37. data/test/dummy/app/assets/javascripts/cadinsor_tests.js +2 -0
  38. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  39. data/test/dummy/app/assets/stylesheets/cadinsor_tests.css +4 -0
  40. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  41. data/test/dummy/app/controllers/application_controller.rb +5 -0
  42. data/test/dummy/app/controllers/cadinsor_tests_controller.rb +26 -0
  43. data/test/dummy/app/helpers/application_helper.rb +2 -0
  44. data/test/dummy/app/helpers/cadinsor_tests_helper.rb +2 -0
  45. data/test/dummy/app/views/cadinsor_tests/do_not_check.rabl +3 -0
  46. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  47. data/test/dummy/bin/bundle +3 -0
  48. data/test/dummy/bin/rails +4 -0
  49. data/test/dummy/bin/rake +4 -0
  50. data/test/dummy/config.ru +4 -0
  51. data/test/dummy/config/application.rb +23 -0
  52. data/test/dummy/config/boot.rb +5 -0
  53. data/test/dummy/config/database.yml +25 -0
  54. data/test/dummy/config/environment.rb +5 -0
  55. data/test/dummy/config/environments/development.rb +29 -0
  56. data/test/dummy/config/environments/production.rb +80 -0
  57. data/test/dummy/config/environments/test.rb +36 -0
  58. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  59. data/test/dummy/config/initializers/cadinsor.rb +29 -0
  60. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  61. data/test/dummy/config/initializers/inflections.rb +16 -0
  62. data/test/dummy/config/initializers/mime_types.rb +5 -0
  63. data/test/dummy/config/initializers/secret_token.rb +12 -0
  64. data/test/dummy/config/initializers/session_store.rb +3 -0
  65. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  66. data/test/dummy/config/locales/en.yml +23 -0
  67. data/test/dummy/config/routes.rb +4 -0
  68. data/test/dummy/db/development.sqlite3 +0 -0
  69. data/test/dummy/db/migrate/20140115125610_create_cadinsor_client_apps.cadinsor.rb +11 -0
  70. data/test/dummy/db/migrate/20140115125611_create_cadinsor_api_keys.cadinsor.rb +11 -0
  71. data/test/dummy/db/schema.rb +33 -0
  72. data/test/dummy/db/test.sqlite3 +0 -0
  73. data/test/dummy/log/development.log +2737 -0
  74. data/test/dummy/public/404.html +58 -0
  75. data/test/dummy/public/422.html +58 -0
  76. data/test/dummy/public/500.html +57 -0
  77. data/test/dummy/public/favicon.ico +0 -0
  78. data/test/dummy/test/controllers/cadinsor_tests_controller_test.rb +9 -0
  79. data/test/dummy/test/helpers/cadinsor_tests_helper_test.rb +4 -0
  80. data/test/fixtures/cadinsor/api_keys.yml +11 -0
  81. data/test/fixtures/cadinsor/client_apps.yml +11 -0
  82. data/test/helpers/cadinsor/api_keys_helper_test.rb +6 -0
  83. data/test/integration/navigation_test.rb +10 -0
  84. data/test/models/cadinsor/api_key_test.rb +9 -0
  85. data/test/models/cadinsor/client_app_test.rb +9 -0
  86. data/test/test_helper.rb +15 -0
  87. metadata +228 -0
@@ -0,0 +1,30 @@
1
+ class Cadinsor::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ def install
4
+ puts "I need rabl to work. Adding rabl to your gem file"
5
+ gsub_file "Gemfile", /gem "rabl.*$/, ""
6
+ append_to_file "Gemfile", "\ngem \"rabl\", \"~> 0.9\""
7
+ Bundler.with_clean_env do
8
+ run "bundle install"
9
+ end
10
+ puts "Creating an initializer for Cadinsor..."
11
+ template "cadinsor_initializer.erb", "config/initializers/cadinsor.rb"
12
+ puts "Your initializer has been successfully created at config/initializers/cadinsor.rb."
13
+ puts "Installing Migrations..."
14
+ system("rake cadinsor:install:migrations")
15
+ puts "That is done."
16
+ puts "Adding route to mount the engine..."
17
+ print "Where would you like the engine to be mounted at (default: /cadinsor)? /"
18
+ namespace = gets.chomp
19
+ namespace = "cadinsor" if namespace.to_s == ""
20
+ gsub_file "config/routes.rb", /mount Cadinsor::Engine(.*)/, ''
21
+ insert_into_file "config/routes.rb", "\tmount Cadinsor::Engine => '/#{namespace}'", :after => ".routes.draw do\n"
22
+ puts "I have tried to add an entry into routes.rb to mount the engine. Printing contents of config/routes.rb"
23
+ system("cat config/routes.rb | more")
24
+ puts "Please verify that an entry exists. If not, create one like shown below."
25
+ puts 'Ex: To mount it at /cadinsor, your route should read: mount Cadinsor::Engine => "/cadinsor"'
26
+ puts "Once that is done, run rake db:migrate to complete the setup."
27
+ puts "\n"
28
+ puts "Cheers!"
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # Cadinsor Initializer - Generated on <%= DateTime.now.to_s(:long) %>
2
+ # Please visit github.com/lewstherin/cadinsor for more information on this initializer.
3
+
4
+ require 'cadinsor'
5
+ # Cadinsor Configuration Options
6
+ # # Set the api key expiry time.
7
+ # # If this value is set, any key that is generated will stop being valid after 5 minutes
8
+ # Cadinsor::Engine.config.key_expiry_time_in_mins = 5
9
+ # # Set the value of the client app id field name in params.
10
+ # # If set to :app_id (defaults to :client_app_id), Cadinsor will look for the client application id in params[:app_id]
11
+ # Cadinsor::Engine.config.client_app_id_param_name = :client_app_id
12
+ # # Set the value of the field name of the api key in params.
13
+ # # If set to :key, Cadinsor will look for the api key in params[:key]
14
+ # Cadinsor::Engine.config.api_key_param_name = :api_key
15
+ # # Set the value of the field name of the request signature in params.
16
+ # # If set to :signature, Cadinsor will look for the request signature in params[:signature]
17
+ # Cadinsor::Engine.config.request_signature_param_name = :signature
18
+
19
+ # Include Cadinsor methods in the application controller
20
+
21
+ class ApplicationController < ActionController::Base
22
+ include Cadinsor::Extensions
23
+ # When a request fails the cadinsor checks, it raises the Request Error exception
24
+ # The default behaviour redirects the request to a json/xml error page, with the reason for failure
25
+ # Edit this block if you would like the exception to be handled differently.
26
+ rescue_from Cadinsor::Extensions::RequestError do |exception_object|
27
+ cadinsor_rescue(exception_object.message)
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ namespace :cadinsor do
2
+ namespace :api_key do
3
+
4
+ desc " Interactive task to remove expired keys from the db, please use clean_background if you want to run this in a non-interactive mode."
5
+ task clean_manual: :environment do
6
+ puts
7
+ print "This option cannot be undone. Are you sure (Yes to continue)? "
8
+ response = STDIN.gets.chomp
9
+ if response == "Yes"
10
+ Cadinsor::ApiKey.all.each do |key|
11
+ key.destroy if key.expired?
12
+ end
13
+ else
14
+ puts "Quitting!"
15
+ end
16
+ end
17
+
18
+ desc "Remove all expired keys from the db without any confirmation, for background tasks like cron jobs only."
19
+ task clean_background: :environment do
20
+ Cadinsor::ApiKey.all.each do |key|
21
+ key.destroy if key.expired?
22
+ end
23
+ end
24
+
25
+
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :cadinsor do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,85 @@
1
+ namespace :cadinsor do
2
+ namespace :client_app do
3
+ desc "Add a new client app"
4
+ task create: :environment do
5
+ app = Cadinsor::ClientApp.new
6
+ print "Enter the client app name: "
7
+ app.name = STDIN.gets.chomp
8
+ app.generate_secret!
9
+ puts
10
+ print "Enter the app secret(blank: #{app.secret}): "
11
+ secret = STDIN.gets.chomp
12
+ app.secret = secret unless secret == ""
13
+ if app.save
14
+ puts "App saved successfully with id: #{app.id}."
15
+ puts app.inspect
16
+ else
17
+ puts "App could not be created."
18
+ puts app.errors.full_messages
19
+ end
20
+ end
21
+
22
+ desc "List all client apps with their secrets"
23
+ task list: :environment do
24
+ Cadinsor::ClientApp.all.each do |app|
25
+ puts "*************"
26
+ puts "App id: " + app.id.to_s
27
+ puts "Name: " + app.name
28
+ puts "App secret: " + app.secret
29
+ puts "*************"
30
+ puts
31
+ end
32
+ end
33
+
34
+ desc "Edit App Secret"
35
+ task edit_secret: :list do
36
+ print "Please enter the id of the app you would like to edit: "
37
+ id = STDIN.gets.chomp.to_i
38
+ app = Cadinsor::ClientApp.find_by_id(id)
39
+ if app
40
+ puts
41
+ app.generate_secret!
42
+ print "Enter the new secret (blank: #{app.secret}): "
43
+ secret = STDIN.gets.chomp
44
+ puts
45
+ app.secret = secret unless secret == ""
46
+ if app.save
47
+ puts "App saved successfully with secret: #{app.secret}."
48
+ puts app.inspect
49
+ else
50
+ puts "App could not be saved."
51
+ puts app.errors.full_messages
52
+ end
53
+ else
54
+ puts "Invalid id. Quitting!"
55
+ end
56
+ end
57
+
58
+ desc "Delete App"
59
+ task delete: :list do
60
+ print "Please enter the id of the app you would like to delete: "
61
+ id = STDIN.gets.chomp.to_i
62
+ app = Cadinsor::ClientApp.find_by_id(id)
63
+ if app
64
+ puts
65
+ print "This option cannot be undone. Hit Yes if you are sure, anything else to quit. "
66
+ response = STDIN.gets.chomp
67
+ puts
68
+ if response == "Yes"
69
+ if app.destroy
70
+ puts "App deleted successfully."
71
+ else
72
+ puts "App could not be deleted."
73
+ puts app.errors.full_messages
74
+ end
75
+ else
76
+ puts "Quitting without deleting app"
77
+ end
78
+ else
79
+ puts "Invalid id. Quitting!"
80
+ end
81
+ end
82
+
83
+
84
+ end
85
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CadinsorTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Cadinsor
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ module Cadinsor
4
+ class ApiKeysControllerTest < ActionController::TestCase
5
+ # test "the truth" do
6
+ # assert true
7
+ # end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ GEM
2
+ specs:
3
+ activesupport (4.0.2)
4
+ i18n (~> 0.6, >= 0.6.4)
5
+ minitest (~> 4.2)
6
+ multi_json (~> 1.3)
7
+ thread_safe (~> 0.1)
8
+ tzinfo (~> 0.3.37)
9
+ atomic (1.1.14)
10
+ i18n (0.6.9)
11
+ minitest (4.7.5)
12
+ multi_json (1.8.4)
13
+ rabl (0.9.3)
14
+ activesupport (>= 2.3.14)
15
+ sqlite3 (1.3.8)
16
+ thread_safe (0.1.3)
17
+ atomic
18
+ tzinfo (0.3.38)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ rabl
25
+ sqlite3
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -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 File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
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 vendor/assets/javascripts of plugins, if any, 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.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,26 @@
1
+ class CadinsorTestsController < ApplicationController
2
+
3
+ before_filter :check_request_with_cadinsor, except: [:inside_method_check, :do_not_check]
4
+
5
+ def default_check
6
+ respond_to do |format|
7
+ format.json {render :action => 'do_not_check', :format => 'json'}
8
+ format.xml {render :action => 'do_not_check', :format => 'xml'}
9
+ end
10
+ end
11
+
12
+ def inside_method_check
13
+ check_request_with_cadinsor
14
+ respond_to do |format|
15
+ format.json {render :action => 'do_not_check', :format => 'json'}
16
+ format.xml {render :action => 'do_not_check', :format => 'xml'}
17
+ end
18
+ end
19
+
20
+ def do_not_check
21
+ respond_to do |format|
22
+ format.json {render :action => 'do_not_check', :format => 'json'}
23
+ format.xml {render :action => 'do_not_check', :format => 'xml'}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module CadinsorTestsHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ object :false
2
+ node(:status) {"Success."}
3
+ node(:message) {"Cadinsor Tests Cleared."}
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
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