| ... | ... |
@@ -43,13 +43,13 @@ class ApplicationController < ActionController::Base |
| 43 | 43 |
session[:ip] = request.remote_ip |
| 44 | 44 |
|
| 45 | 45 |
if user_credentials |
| 46 |
- session[:users] = {} unless session[:users]
|
|
| 47 |
- |
|
| 48 | 46 |
encrypted_pass = Security::encrypt(user_credentials[:password]) |
| 49 | 47 |
cookies[:key] = Security::cipher_key |
| 50 | 48 |
cookies[:iv] = Security::cipher_iv |
| 51 | 49 |
|
| 52 |
- session[:users][user_credentials[:jid]] = encrypted_pass |
|
| 50 |
+ user = create_new_user(user_credentials) |
|
| 51 |
+ user.update_pass(user_credentials[:jid], encrypted_pass) |
|
| 52 |
+ user_id = user.id |
|
| 53 | 53 |
end |
| 54 | 54 |
|
| 55 | 55 |
@token.save_session(session, user_id) |
| ... | ... |
@@ -58,7 +58,5 @@ class ApplicationController < ActionController::Base |
| 58 | 58 |
def create_new_user(user_credentials) |
| 59 | 59 |
jid = user_credentials[:jid] |
| 60 | 60 |
user = User.existing_jid(jid) || User.create_jid(jid) |
| 61 |
- |
|
| 62 |
- user.id |
|
| 63 | 61 |
end |
| 64 | 62 |
end |
| ... | ... |
@@ -24,17 +24,15 @@ class WsRosterController < WsController |
| 24 | 24 |
def connect |
| 25 | 25 |
initialize_storage() |
| 26 | 26 |
|
| 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 |
- # TODO: skusit zrychlit |
|
| 31 | 27 |
cookies = env['rack.request.cookie_hash'] # TODO: nahlasit bug na websocket-rails, lebo sa neda pristupit ku `cookies' |
| 32 | 28 |
cipher_key = cookies['key'] |
| 33 | 29 |
cipher_iv = cookies['iv'] |
| 34 | 30 |
|
| 35 |
- clients = session[:users].map do |jid, encrypted_pass| |
|
| 36 |
- decrypted_pass = Security::decrypt(encrypted_pass, cipher_key, cipher_iv) |
|
| 37 |
- {jid: jid, pass: decrypted_pass}
|
|
| 31 |
+ credentials = User.crendentials_for_token(session[:token]) |
|
| 32 |
+ |
|
| 33 |
+ clients = credentials.map do |tuple| |
|
| 34 |
+ decrypted_pass = Security::decrypt(tuple['pass'], cipher_key, cipher_iv) |
|
| 35 |
+ {jid: tuple['jid'], pass: decrypted_pass}
|
|
| 38 | 36 |
end |
| 39 | 37 |
|
| 40 | 38 |
clients.each do |client| |
| ... | ... |
@@ -13,6 +13,8 @@ module Security |
| 13 | 13 |
|
| 14 | 14 |
encrypted = cipher.update(unencrypted_message) |
| 15 | 15 |
encrypted << cipher.final |
| 16 |
+ |
|
| 17 |
+ Base64.encode64(encrypted).encode('utf-8')
|
|
| 16 | 18 |
end |
| 17 | 19 |
|
| 18 | 20 |
def self.decrypt(encrypted_message, key, iv) |
| ... | ... |
@@ -23,7 +25,7 @@ module Security |
| 23 | 23 |
cipher.iv = @@cipher_iv = iv |
| 24 | 24 |
|
| 25 | 25 |
begin |
| 26 |
- decrypted = cipher.update(encrypted_message) |
|
| 26 |
+ decrypted = cipher.update(Base64.decode64(encrypted_message.encode('ascii-8bit')))
|
|
| 27 | 27 |
decrypted << cipher.final |
| 28 | 28 |
rescue |
| 29 | 29 |
decrypted = '' |
| ... | ... |
@@ -5,7 +5,7 @@ class User |
| 5 | 5 |
|
| 6 | 6 |
def update_pass(jid, pass) |
| 7 | 7 |
account_credentials = jids.detect do |f| |
| 8 |
- f[:jid] == jid || f["jid"] == jid |
|
| 8 |
+ f[:jid] == jid || f['jid'] == jid |
|
| 9 | 9 |
end |
| 10 | 10 |
|
| 11 | 11 |
account_credentials[:pass] = pass |
| ... | ... |
@@ -13,7 +13,7 @@ class User |
| 13 | 13 |
end |
| 14 | 14 |
|
| 15 | 15 |
def self.existing_jid(jid) |
| 16 |
- where("jids.jid" => jid).only(:jids).first
|
|
| 16 |
+ where('jids.jid' => jid).only(:jids).first
|
|
| 17 | 17 |
end |
| 18 | 18 |
|
| 19 | 19 |
def self.create_jid(jid) |
| ... | ... |
@@ -26,4 +26,16 @@ class User |
| 26 | 26 |
jids << {jid: another_jid, pass: password}
|
| 27 | 27 |
save |
| 28 | 28 |
end |
| 29 |
+ |
|
| 30 |
+ def self.crendentials_for_token(token) |
|
| 31 |
+ found = Token.where(token: token).only(:user_id).limit(1).first |
|
| 32 |
+ if found |
|
| 33 |
+ user_id = found.user_id |
|
| 34 |
+ else |
|
| 35 |
+ return [] |
|
| 36 |
+ end |
|
| 37 |
+ |
|
| 38 |
+ user = find(user_id) |
|
| 39 |
+ return user ? user.jids : [] |
|
| 40 |
+ end |
|
| 29 | 41 |
end |
| 30 | 42 |
\ No newline at end of file |