komandir 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ # Usage
2
+
3
+ In your controller:
4
+
5
+ ```
6
+ if action_signature_valid?(current_user)
7
+ ..
8
+ else
9
+ ..
10
+ end
11
+ ```
@@ -0,0 +1,82 @@
1
+ class Protocol < ActiveRecord::Base
2
+
3
+ belongs_to :user
4
+
5
+ before_validation :set_client_time, :on => :create
6
+ before_validation :set_account_name, :on => :create
7
+ before_validation :set_certificate, :on => :create
8
+
9
+ validates :action_url, :presence => true
10
+ validates :client_time_epoch, :presence => true
11
+ validates :client_time, :presence => true
12
+ validates :client_ip, :presence => true
13
+ validates :body, :presence => true
14
+ validates :signature, :presence => true
15
+ validates :account_name, :presence => true
16
+ validate :validate_signature
17
+
18
+ attr_accessor :client_time_epoch
19
+
20
+ private
21
+
22
+ def set_client_time
23
+ self.client_time = Time.at(client_time_epoch.to_i)
24
+ end
25
+
26
+ def set_account_name
27
+ if user.respond_to?(:email)
28
+ self.account_name = user.email
29
+ end
30
+ end
31
+
32
+ def set_certificate
33
+ self.certificate_id = user.certificate.id
34
+ end
35
+
36
+ def validate_signature
37
+ verification_set = {
38
+ :message => digest,
39
+ :signature => signature,
40
+ :certificate => user.certificate.body
41
+ }
42
+ unless Cryptopro::Signature.verify(verification_set)
43
+ errors.add(:signature, "Signature not valid")
44
+ end
45
+ end
46
+
47
+ def digest
48
+ "#{system_params}:#{body}"
49
+ end
50
+
51
+ def system_params
52
+ check_client_time!
53
+ "#{action_url}:#{client_ip}:#{client_time_epoch}"
54
+ end
55
+
56
+ def check_client_time!
57
+ if client_time_epoch.present?
58
+ server_time_epoch = Time.now.to_i
59
+ self.client_time_epoch = client_time_epoch.to_i
60
+ raise Komandir::WrongTime if (client_time_epoch - server_time_epoch).abs > 60
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+
67
+ # == Schema Information
68
+ #
69
+ # Table name: protocols
70
+ #
71
+ # id :integer not null, primary key
72
+ # user_id :integer
73
+ # certificate_id :integer
74
+ # action_url :string(255)
75
+ # account_name :string(255)
76
+ # client_ip :string(255)
77
+ # client_time :datetime
78
+ # body :text
79
+ # signature :text
80
+ # created_at :datetime
81
+ # updated_at :datetime
82
+ #
@@ -1,5 +1,7 @@
1
1
  require "komandir/version"
2
2
  require "komandir/railtie"
3
+ require "komandir/engine"
4
+ require "komandir/exceptions"
3
5
 
4
6
  module Komandir
5
7
  end
@@ -1,6 +1,4 @@
1
1
  module Komandir
2
- class WrongTime < Exception; end
3
-
4
2
  module ControllerMethods
5
3
 
6
4
  module ClassMethods
@@ -13,20 +11,20 @@ module Komandir
13
11
  raise "Blank komandir_signature" if params[:komandir_signature].blank?
14
12
  raise "Blank certificate for user. Make sure user.certificate.body contains certificate" unless user.certificate.try(:body?)
15
13
 
16
- verification_set = {
17
- :message => digest,
18
- :signature => params[:komandir_signature],
19
- :certificate => user.certificate.body
20
- }
21
- Cryptopro::Signature.verify(verification_set)
14
+ protocol = Protocol.new(
15
+ :user => user,
16
+ :action_url => request.path,
17
+ :client_ip => request.remote_ip,
18
+ :client_time_epoch => params[:komandir_time],
19
+ :body => serialized_form,
20
+ :signature => params[:komandir_signature]
21
+ )
22
+
23
+ protocol.save
22
24
  end
23
25
 
24
26
  private
25
27
 
26
- def digest
27
- "#{system_params}:#{serialized_form}"
28
- end
29
-
30
28
  def serialized_form
31
29
  # TODO Сортировать по алфавиту
32
30
  pairs = request.raw_post.split("&")
@@ -42,19 +40,6 @@ module Komandir
42
40
  odd_param_names.include?(param_name) || filtered_param_names.any? { |filtered_param_name| param_name.include?(filtered_param_name) }
43
41
  end
44
42
  end
45
-
46
- def system_params
47
- check_client_time!
48
- "#{request.path}:#{request.remote_ip}:#{params[:komandir_time]}"
49
- end
50
-
51
- def check_client_time!
52
- if params[:komandir_time].present?
53
- server_time_epoch = Time.now.to_i
54
- client_time_epoch = params[:komandir_time].to_i
55
- raise Komandir::WrongTime if (client_time_epoch - server_time_epoch).abs > 60
56
- end
57
- end
58
43
  end
59
44
 
60
45
  def self.included(receiver) # :nodoc:
@@ -0,0 +1,4 @@
1
+ module Komandir
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Komandir
2
+ class WrongTime < Exception; end
3
+ end
@@ -1,3 +1,3 @@
1
1
  module Komandir
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: komandir
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - divineforest
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-28 00:00:00 +04:00
18
+ date: 2011-11-30 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -43,10 +43,14 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - .gitignore
45
45
  - Gemfile
46
+ - README.md
46
47
  - Rakefile
48
+ - app/models/protocol.rb
47
49
  - komandir.gemspec
48
50
  - lib/komandir.rb
49
51
  - lib/komandir/action_controller.rb
52
+ - lib/komandir/engine.rb
53
+ - lib/komandir/exceptions.rb
50
54
  - lib/komandir/railtie.rb
51
55
  - lib/komandir/version.rb
52
56
  has_rdoc: true