From a079f00186e546de3f8220805552867c2e8a365b Mon Sep 17 00:00:00 2001 From: michaelr Date: Fri, 18 Mar 2016 17:11:43 +0200 Subject: [PATCH] encrypt stored identity with password, beginning of msg store --- src/messenger/comm/intercom.cljs | 10 +++++++--- src/messenger/models/messages.cljs | 10 ++++++++++ src/messenger/models/protocol.cljs | 16 +++++++++++----- src/messenger/persistence/realm.cljs | 11 ++++++++++- src/messenger/persistence/simple_kv_store.cljs | 5 ++--- src/messenger/protocol/protocol_handler.cljs | 9 +++++---- src/messenger/services/protocol.cljs | 10 ++++++++-- src/messenger/state.cljs | 2 ++ 8 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 src/messenger/models/messages.cljs diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index 5dccdf7cd6..aa033c28a8 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -33,14 +33,14 @@ ;;; server (defn sign-up [phone-number whisper-identity handler] - (publish! :service [:server :server/sign-up {:phone-number phone-number + (publish! :service [:server :server/sign-up {:phone-number phone-number :whisper-identity whisper-identity - :handler handler}])) + :handler handler}])) (defn sign-up-confirm [confirmation-code handler] (publish! :service [:server :server/sign-up-confirm {:confirmation-code confirmation-code - :handler handler}])) + :handler handler}])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; contacts @@ -56,3 +56,7 @@ (defn protocol-initialized [identity] (publish! :service [:protocol :protocol/initialized {:identity identity}])) + +(defn save-new-msg [from payload] + (publish! :service [:protocol :protocol/save-new-msg {:from from + :payload payload}])) diff --git a/src/messenger/models/messages.cljs b/src/messenger/models/messages.cljs new file mode 100644 index 0000000000..913893ea79 --- /dev/null +++ b/src/messenger/models/messages.cljs @@ -0,0 +1,10 @@ +(ns messenger.models.messages + (:require [messenger.persistence.realm :as r])) + +(defn save-message [from {:keys [msg-id] :as msg}] + (when-not (r/exists? :msgs :msg-id msg-id) + (r/write + (fn [] + (r/create :msgs {:msg-id msg-id + :chat-id from + :msg (with-out-str (pr msg))} true))))) \ No newline at end of file diff --git a/src/messenger/models/protocol.cljs b/src/messenger/models/protocol.cljs index 07f82e3921..17f4432d30 100644 --- a/src/messenger/models/protocol.cljs +++ b/src/messenger/models/protocol.cljs @@ -1,14 +1,20 @@ (ns messenger.models.protocol (:require [messenger.state :as state] - [syng-im.protocol.state.storage :as s])) + [syng-im.protocol.state.storage :as s] + [syng-im.utils.encryption :refer [password-encrypt + password-decrypt]])) (defn set-initialized [initialized?] (swap! state/app-state assoc-in state/protocol-initialized-path initialized?)) -;; TODO at least the private key has to be encrypted with user's password - (defn update-identity [identity] - (s/put (state/kv-store) :identity identity)) + (let [password (get-in @state/app-state state/identity-password-path) + encrypted (->> (str identity) + (password-encrypt password))] + (s/put (state/kv-store) :identity encrypted))) (defn current-identity [] - (s/get (state/kv-store) :identity)) + (let [encrypted (s/get (state/kv-store) :identity) + password (get-in @state/app-state state/identity-password-path)] + (when encrypted + (password-decrypt password encrypted)))) diff --git a/src/messenger/persistence/realm.cljs b/src/messenger/persistence/realm.cljs index ea72c9b561..76dcac277a 100644 --- a/src/messenger/persistence/realm.cljs +++ b/src/messenger/persistence/realm.cljs @@ -13,7 +13,12 @@ {:name :kv-store :primaryKey :key :properties {:key "string" - :value "string"}}]}) + :value "string"}} + {:name :msgs + :primaryKey :msg-id + :properties {:msg-id "string" + :chat-id "string" + :msg "string"}}]}) (def realm (js/Realm. (clj->js opts))) @@ -65,5 +70,9 @@ (write (fn [] (.delete realm obj)))) +(defn exists? [schema-name field value] + (> (.-length (get-by-field schema-name field value)) + 0)) + (comment ) \ No newline at end of file diff --git a/src/messenger/persistence/simple_kv_store.cljs b/src/messenger/persistence/simple_kv_store.cljs index fcddc6d3d5..073b915599 100644 --- a/src/messenger/persistence/simple_kv_store.cljs +++ b/src/messenger/persistence/simple_kv_store.cljs @@ -8,14 +8,13 @@ (r/write (fn [] (r/create :kv-store {:key key - :value (str value)} true)))) + :value (with-out-str (pr value))} true)))) (get [_ key] (some-> (r/get-by-field :kv-store :key key) (r/single-cljs) (r/decode-value))) (contains-key? [_ key] - (= 0 - (.-length (r/get-by-field :kv-store :key key)))) + (r/exists? :kv-store :key key)) (delete [_ key] (-> (r/get-by-field :kv-store :key key) (r/single) diff --git a/src/messenger/protocol/protocol_handler.cljs b/src/messenger/protocol/protocol_handler.cljs index 33ec6d55f4..543597a8f2 100644 --- a/src/messenger/protocol/protocol_handler.cljs +++ b/src/messenger/protocol/protocol_handler.cljs @@ -1,10 +1,12 @@ (ns messenger.protocol.protocol-handler (:require [syng-im.utils.logging :as log] [messenger.constants :refer [ethereum-rpc-url]] - [messenger.comm.intercom :refer [protocol-initialized]] + [messenger.comm.intercom :refer [protocol-initialized + save-new-msg]] [messenger.models.protocol :refer [current-identity]] [messenger.state :refer [kv-store]])) + (defn make-handler [] {:ethereum-rpc-url ethereum-rpc-url :identity (current-identity) @@ -14,9 +16,8 @@ (case event-type :initialized (let [{:keys [identity]} event] (protocol-initialized identity)) - ;:new-msg (let [{from :from - ; {content :content} :payload} event] - ; (add-to-chat "chat" from content)) + :new-msg (let [{:keys [from payload]} event] + (save-new-msg from payload)) ;:msg-acked (let [{:keys [msg-id]} event] ; (add-to-chat "chat" ":" (str "Message " msg-id " was acked"))) ;:delivery-failed (let [{:keys [msg-id]} event] diff --git a/src/messenger/services/protocol.cljs b/src/messenger/services/protocol.cljs index b48d38a5fe..6f9705afd3 100644 --- a/src/messenger/services/protocol.cljs +++ b/src/messenger/services/protocol.cljs @@ -1,17 +1,23 @@ (ns messenger.services.protocol (:require [messenger.models.protocol :refer [set-initialized update-identity]] + [messenger.models.messages :refer [save-message]] [syng-im.utils.logging :as log])) (defmulti protocol (fn [state id args] - id)) + id)) (defmethod protocol :protocol/initialized [state id {:keys [identity] :as args}] - (log/info "handling " id "args = " args) + (log/debug "handling " id "args = " args) (update-identity identity) (set-initialized true)) +(defmethod protocol :protocol/save-new-msg + [state id {:keys [from payload] :as args}] + (log/debug "handling " id "args = " args) + (save-message from payload)) + (defn protocol-handler [state [id args]] (log/debug "protocol-handler: " args) (protocol state id args)) diff --git a/src/messenger/state.cljs b/src/messenger/state.cljs index 2df8513ac6..6d412b4293 100644 --- a/src/messenger/state.cljs +++ b/src/messenger/state.cljs @@ -14,6 +14,7 @@ :user-phone-number nil :user-identity nil :confirmation-code nil + :identity-password "replace-me-with-user-entered-password" :channels {:pub-sub-publisher (chan) :pub-sub-publication nil}})) @@ -40,6 +41,7 @@ (def user-notification-path [:user-notification]) (def protocol-initialized-path [:protocol-initialized]) (def simple-store-path [:simple-store]) +(def identity-password-path [:identity-password]) (defn pub-sub-publisher [app] (get-in app pub-sub-bus-path)) (defn kv-store []