encrypt stored identity with password, beginning of msg store

This commit is contained in:
michaelr 2016-03-18 17:11:43 +02:00
parent 0da2757450
commit a079f00186
8 changed files with 55 additions and 18 deletions

View File

@ -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}]))

View File

@ -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)))))

View File

@ -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))))

View File

@ -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
)

View File

@ -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)

View File

@ -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]

View File

@ -1,6 +1,7 @@
(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]
@ -8,10 +9,15 @@
(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))

View File

@ -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 []