Browse code

Merge branch 'authentication' into sending-messages

Cinan Rakosnik authored on 15/04/2013 at 12:44:43
Showing 9 changed files
... ...
@@ -13,3 +13,5 @@
13 13
 # Ignore all logfiles and tempfiles.
14 14
 /log/*.log
15 15
 /tmp
16
+
17
+/config/initializers/secret_token.rb
16 18
\ No newline at end of file
... ...
@@ -5,10 +5,11 @@ gem 'rails', '~> 3.2.11'
5 5
 # Bundle edge Rails instead:
6 6
 # gem 'rails', :git => 'git://github.com/rails/rails.git'
7 7
 
8
-gem "mongoid", "~> 3.0.20"
9
-gem "xmpp4r", "~> 0.5"
10
-gem "thin", "~> 1.5.0"
11
-gem "websocket-rails", "~> 0.4.3"
8
+gem 'mongoid', '~> 3.0.20'
9
+gem 'xmpp4r', '~> 0.5'
10
+gem 'thin', '~> 1.5.0'
11
+gem 'websocket-rails', '~> 0.4.3'
12
+gem 'bcrypt-ruby'
12 13
 
13 14
 # Gems used only for assets and not required
14 15
 # in production environments by default.
... ...
@@ -17,7 +18,7 @@ group :assets do
17 17
   gem 'coffee-rails', '~> 3.2.1'
18 18
   gem 'normalize-rails'
19 19
   gem 'compass-rails'
20
-  gem "rails-backbone"
20
+  gem 'rails-backbone'
21 21
   gem 'haml_coffee_assets'
22 22
   #gem "marionette-rails", "~> 0.10.2"
23 23
 
... ...
@@ -37,28 +38,25 @@ group :development do
37 37
 end
38 38
 
39 39
 group :test do
40
-	gem "minitest"
40
+	gem 'minitest'
41 41
 	gem 'minitest-reporters', '>= 0.5.0'
42 42
 	gem 'test-unit'
43 43
 	gem 'timecop'
44
-	gem "rspec-rails"
45
-	gem "capybara"
46
-	gem "launchy"
47
-	gem "factory_girl_rails"
44
+	gem 'rspec-rails'
45
+	gem 'capybara'
46
+	gem 'launchy'
47
+	gem 'factory_girl_rails'
48 48
     gem 'guard-spork'
49 49
     gem 'spork'
50
-    gem "phantomjs-binaries"
51
-    gem "casperjs"
50
+    gem 'phantomjs-binaries'
51
+    gem 'casperjs'
52 52
 end
53 53
 
54 54
 gem 'jquery-rails'
55
-gem "haml", "~> 3.1.7"
56
-gem "haml-rails", "~> 0.3.5"
55
+gem 'haml', '~> 3.1.7'
56
+gem 'haml-rails', '~> 0.3.5'
57 57
 gem 'i18n-js'
58 58
 
59
-# To use ActiveModel has_secure_password
60
-# gem 'bcrypt-ruby', '~> 3.0.0'
61
-
62 59
 # To use Jbuilder templates for JSON
63 60
 # gem 'jbuilder'
64 61
 
... ...
@@ -31,6 +31,7 @@ GEM
31 31
     addressable (2.3.3)
32 32
     ansi (1.4.3)
33 33
     arel (3.0.2)
34
+    bcrypt-ruby (3.0.1)
34 35
     better_errors (0.7.2)
35 36
       coderay (>= 1.0.0)
36 37
       erubis (>= 2.6.6)
... ...
@@ -262,6 +263,7 @@ PLATFORMS
262 262
   ruby
263 263
 
264 264
 DEPENDENCIES
265
+  bcrypt-ruby
265 266
   better_errors
266 267
   binding_of_caller
267 268
   capistrano
... ...
@@ -27,28 +27,37 @@ class ApplicationController < ActionController::Base
27 27
             user_id = @token.user_id
28 28
             @token.delete
29 29
         else
30
-            user_id = !user_credentials ? nil : create_new_user(user_credentials)
30
+            user_id = nil
31 31
         end
32 32
 
33 33
         @token = Token.new
34 34
 
35
-        save_session(user_id)
35
+        save_session(user_id, user_credentials)
36 36
     end
37 37
 
38
-    def save_session(user_id)
39
-        session[:token] = Token.generate_token()
38
+    private
39
+
40
+    def save_session(user_id, user_credentials)
41
+        session[:token]      = Token.generate_token()
40 42
         session[:created_at] = Time.now
41
-        session[:ip] = request.remote_ip
43
+        session[:ip]         = request.remote_ip
44
+
45
+        if user_credentials
46
+            session[:users] = {} unless session[:users]
47
+
48
+            encrypted_pass = Security::encrypt(user_credentials[:password])
49
+            cookies[:key] = Security::cipher_key
50
+            cookies[:iv]  = Security::cipher_iv
51
+
52
+            session[:users][user_credentials[:jid]] = encrypted_pass
53
+        end
42 54
 
43 55
         @token.save_session(session, user_id)
44 56
     end
45 57
 
46 58
     def create_new_user(user_credentials)
47 59
         jid  = user_credentials[:jid]
48
-        pass = user_credentials[:password]
49
-
50 60
         user = User.existing_jid(jid) || User.create_jid(jid)
51
-        user.update_pass(jid, pass)
52 61
 
53 62
         user.id
54 63
     end
... ...
@@ -4,8 +4,6 @@ class SessionsController < ApplicationController
4 4
 	end
5 5
 
6 6
 	def create
7
-		#require_dependency "signin.rb"
8
-
9 7
 		begin
10 8
 			Signin.try_login(params[:jid].downcase, params[:password])
11 9
 		rescue Signin::LoginError
... ...
@@ -24,11 +24,21 @@ class WsRosterController < WsController
24 24
     def connect
25 25
         initialize_storage()
26 26
 
27
-        clients = Token.fing_user_accounts_having_to_token(session[:token])
27
+        # TODO: Pouzit najprv:
28
+        # clients = Token.fing_user_accounts_having_to_token(session[:token])
29
+        # ale toto, az ked budem mat dokonceny multiaccount (settings a popup)
30
+        cookies = env['rack.request.cookie_hash'] # TODO: nahlasit bug na websocket-rails, lebo sa neda pristupit ku `cookies'
31
+        cipher_key = cookies['key']
32
+        cipher_iv = cookies['iv']
33
+
34
+        clients = session[:users].map do |jid, encrypted_pass|
35
+            decrypted_pass = Security::decrypt(encrypted_pass, cipher_key, cipher_iv)
36
+            {jid: jid, pass: decrypted_pass}
37
+        end
28 38
 
29
-        clients.each do |credentials|
39
+        clients.each do |client|
30 40
             begin
31
-                client = Signin.try_login(credentials['jid'], credentials['pass'])
41
+                client = Signin.try_login(client[:jid], client[:pass])
32 42
                 connection_store[:clients] << client
33 43
             rescue Signin::LoginError
34 44
                 send_message 'app.client.cannot_connect', true
35 45
new file mode 100644
... ...
@@ -0,0 +1,43 @@
0
+require 'bcrypt'
1
+
2
+module Security
3
+    mattr_reader :cipher_iv, :cipher_key
4
+
5
+    def self.encrypt(unencrypted_message, password = nil)
6
+        cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
7
+        cipher.encrypt()
8
+
9
+        pass = password ? password : generate_token()
10
+        cipher.key = @@cipher_key = Digest::SHA1.hexdigest(pass)
11
+        cipher.iv  = @@cipher_iv  = cipher.random_iv
12
+
13
+        encrypted = cipher.update(unencrypted_message)
14
+        encrypted << cipher.final
15
+    end
16
+
17
+    def self.decrypt(encrypted_message, key, iv)
18
+        cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
19
+        cipher.decrypt()
20
+
21
+        cipher.key = @@cipher_key = key
22
+        cipher.iv  = @@cipher_iv = iv
23
+
24
+        begin
25
+            decrypted = cipher.update(encrypted_message)
26
+            decrypted << cipher.final
27
+        rescue
28
+            decrypted = ''
29
+        end
30
+
31
+        decrypted
32
+    end
33
+
34
+    def self.generate_token()
35
+        BCrypt::Engine.generate_salt()
36
+    end
37
+
38
+    def self.save_cookies
39
+        cookies[:key] = @@cipher_key
40
+        cookies[:iv]  = @@cipher_iv
41
+    end
42
+end
0 43
\ No newline at end of file
... ...
@@ -43,7 +43,7 @@ module Xmpp
43 43
     config.encoding = "utf-8"
44 44
 
45 45
     # Configure sensitive parameters which will be filtered from the log file.
46
-    config.filter_parameters += [:password]
46
+    config.filter_parameters += [:password, :users]
47 47
 
48 48
     # Enable escaping HTML in JSON.
49 49
     config.active_support.escape_html_entities_in_json = true
... ...
@@ -4,4 +4,11 @@
4 4
 # If you change this key, all old signed cookies will become invalid!
5 5
 # Make sure the secret is at least 30 characters and all random,
6 6
 # no regular words or you'll be exposed to dictionary attacks.
7
-Xmpp::Application.config.secret_token = '48c49768f9ec3134de5f76e352f13bf8fd66252d67f6d285c1c6de8457f0499075975aaaef1c32be9d596120302e8e3f7b6200c207835463ecdbb6c1610705c2'
7
+
8
+if Rails.env == 'development'
9
+    Xmpp::Application.config.secret_token = '48c49768f9ec3134de5f76e352f13bf8fd66258941sa5d489g4fhj8k4uk8499075975aaaef1c32be9d596120302e8e3f7b6200c207835463ecdbb6c1610705c2'
10
+elsif Rails.env == 'test'
11
+    Xmpp::Application.config.secret_token = '48c49768f9ec3134de5f76e352f13bf8fd66252d67f6d285c1c6de8457f0499075975aaaesdjfdsaf84dasf4das89f47d44hgf7yuuy35463ecdbb6c1610705c2'
12
+else
13
+    Xmpp::Application.config.secret_token = '8as5dg8fd4s5f76e352f13bf8fd66289dsa41s4tyijk8uy494j4e8457f0499075975aaaef89asd4302e8e3f7b6200c207835463ecdbb6cugsadbfy31610705c2'
14
+end