Merge pull request #11 from syng-im/protocol-integration

encrypt stored identity with password, beginning of msg store
This commit is contained in:
michaelr524 2016-03-18 17:20:08 +02:00
commit 7fe3c09563
8 changed files with 55 additions and 18 deletions

View File

@ -33,14 +33,14 @@
;;; server ;;; server
(defn sign-up [phone-number whisper-identity handler] (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 :whisper-identity whisper-identity
:handler handler}])) :handler handler}]))
(defn sign-up-confirm [confirmation-code handler] (defn sign-up-confirm [confirmation-code handler]
(publish! :service [:server :server/sign-up-confirm (publish! :service [:server :server/sign-up-confirm
{:confirmation-code confirmation-code {:confirmation-code confirmation-code
:handler handler}])) :handler handler}]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; contacts ;; contacts
@ -56,3 +56,7 @@
(defn protocol-initialized [identity] (defn protocol-initialized [identity]
(publish! :service [:protocol :protocol/initialized {:identity 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 (ns messenger.models.protocol
(:require [messenger.state :as state] (: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?] (defn set-initialized [initialized?]
(swap! state/app-state assoc-in state/protocol-initialized-path 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] (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 [] (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 {:name :kv-store
:primaryKey :key :primaryKey :key
:properties {:key "string" :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))) (def realm (js/Realm. (clj->js opts)))
@ -65,5 +70,9 @@
(write (fn [] (write (fn []
(.delete realm obj)))) (.delete realm obj))))
(defn exists? [schema-name field value]
(> (.-length (get-by-field schema-name field value))
0))
(comment (comment
) )

View File

@ -8,14 +8,13 @@
(r/write (r/write
(fn [] (fn []
(r/create :kv-store {:key key (r/create :kv-store {:key key
:value (str value)} true)))) :value (with-out-str (pr value))} true))))
(get [_ key] (get [_ key]
(some-> (r/get-by-field :kv-store :key key) (some-> (r/get-by-field :kv-store :key key)
(r/single-cljs) (r/single-cljs)
(r/decode-value))) (r/decode-value)))
(contains-key? [_ key] (contains-key? [_ key]
(= 0 (r/exists? :kv-store :key key))
(.-length (r/get-by-field :kv-store :key key))))
(delete [_ key] (delete [_ key]
(-> (r/get-by-field :kv-store :key key) (-> (r/get-by-field :kv-store :key key)
(r/single) (r/single)

View File

@ -1,10 +1,12 @@
(ns messenger.protocol.protocol-handler (ns messenger.protocol.protocol-handler
(:require [syng-im.utils.logging :as log] (:require [syng-im.utils.logging :as log]
[messenger.constants :refer [ethereum-rpc-url]] [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.models.protocol :refer [current-identity]]
[messenger.state :refer [kv-store]])) [messenger.state :refer [kv-store]]))
(defn make-handler [] (defn make-handler []
{:ethereum-rpc-url ethereum-rpc-url {:ethereum-rpc-url ethereum-rpc-url
:identity (current-identity) :identity (current-identity)
@ -14,9 +16,8 @@
(case event-type (case event-type
:initialized (let [{:keys [identity]} event] :initialized (let [{:keys [identity]} event]
(protocol-initialized identity)) (protocol-initialized identity))
;:new-msg (let [{from :from :new-msg (let [{:keys [from payload]} event]
; {content :content} :payload} event] (save-new-msg from payload))
; (add-to-chat "chat" from content))
;:msg-acked (let [{:keys [msg-id]} event] ;:msg-acked (let [{:keys [msg-id]} event]
; (add-to-chat "chat" ":" (str "Message " msg-id " was acked"))) ; (add-to-chat "chat" ":" (str "Message " msg-id " was acked")))
;:delivery-failed (let [{:keys [msg-id]} event] ;:delivery-failed (let [{:keys [msg-id]} event]

View File

@ -1,17 +1,23 @@
(ns messenger.services.protocol (ns messenger.services.protocol
(:require [messenger.models.protocol :refer [set-initialized (:require [messenger.models.protocol :refer [set-initialized
update-identity]] update-identity]]
[messenger.models.messages :refer [save-message]]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
(defmulti protocol (fn [state id args] (defmulti protocol (fn [state id args]
id)) id))
(defmethod protocol :protocol/initialized (defmethod protocol :protocol/initialized
[state id {:keys [identity] :as args}] [state id {:keys [identity] :as args}]
(log/info "handling " id "args = " args) (log/debug "handling " id "args = " args)
(update-identity identity) (update-identity identity)
(set-initialized true)) (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]] (defn protocol-handler [state [id args]]
(log/debug "protocol-handler: " args) (log/debug "protocol-handler: " args)
(protocol state id args)) (protocol state id args))

View File

@ -14,6 +14,7 @@
:user-phone-number nil :user-phone-number nil
:user-identity nil :user-identity nil
:confirmation-code nil :confirmation-code nil
:identity-password "replace-me-with-user-entered-password"
:channels {:pub-sub-publisher (chan) :channels {:pub-sub-publisher (chan)
:pub-sub-publication nil}})) :pub-sub-publication nil}}))
@ -40,6 +41,7 @@
(def user-notification-path [:user-notification]) (def user-notification-path [:user-notification])
(def protocol-initialized-path [:protocol-initialized]) (def protocol-initialized-path [:protocol-initialized])
(def simple-store-path [:simple-store]) (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 pub-sub-publisher [app] (get-in app pub-sub-bus-path))
(defn kv-store [] (defn kv-store []