2018-09-21 13:41:40 +00:00
|
|
|
(ns status-im.browser.permissions
|
2018-09-28 13:40:19 +00:00
|
|
|
(:require [status-im.constants :as constants]
|
2018-09-21 13:41:40 +00:00
|
|
|
[status-im.data-store.dapp-permissions :as dapp-permissions]
|
|
|
|
[status-im.i18n :as i18n]
|
|
|
|
[status-im.utils.ethereum.core :as ethereum]
|
2018-09-24 15:59:02 +00:00
|
|
|
[status-im.utils.fx :as fx]))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
|
|
|
(def supported-permissions
|
|
|
|
{constants/dapp-permission-contact-code {:title (i18n/label :t/wants-to-access-profile)
|
|
|
|
:description (i18n/label :t/your-contact-code)
|
|
|
|
:icon :icons/profile-active}
|
|
|
|
constants/dapp-permission-web3 {:title (i18n/label :t/dapp-would-like-to-connect-wallet)
|
|
|
|
:description (i18n/label :t/allowing-authorizes-this-dapp)
|
|
|
|
:icon :icons/wallet-active}})
|
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
(defn get-permission-data [cofx allowed-permission]
|
2018-09-21 13:41:40 +00:00
|
|
|
(let [account (get-in cofx [:db :account/account])]
|
2018-09-28 13:40:19 +00:00
|
|
|
(get {constants/dapp-permission-contact-code (:public-key account)
|
|
|
|
constants/dapp-permission-web3 (ethereum/normalized-address (:address account))}
|
|
|
|
allowed-permission)))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
(fx/defn send-permission-data-to-bridge
|
|
|
|
"If there is granted permission, return the data to the bridge"
|
|
|
|
[{:keys [db] :as cofx} permission message-id allowed?]
|
|
|
|
{:browser/send-to-bridge {:message (cond-> {:type constants/api-response
|
|
|
|
:isAllowed allowed?
|
|
|
|
:permission permission
|
|
|
|
:messageId message-id}
|
|
|
|
allowed?
|
|
|
|
(assoc :data (get-permission-data cofx permission)))
|
|
|
|
:webview (:webview-bridge db)}})
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2018-09-24 15:59:02 +00:00
|
|
|
(fx/defn update-dapp-permissions
|
2018-09-28 13:40:19 +00:00
|
|
|
[{:keys [db]} dapp-name permission allowed?]
|
|
|
|
(let [dapp-permissions-set (set (get-in db [:dapps/permissions dapp-name :permissions]))
|
|
|
|
allowed-permissions-set (if allowed?
|
|
|
|
(conj dapp-permissions-set permission)
|
|
|
|
(disj dapp-permissions-set permission))
|
|
|
|
allowed-permissions {:dapp dapp-name
|
2018-09-21 13:41:40 +00:00
|
|
|
:permissions (vec allowed-permissions-set)}]
|
2018-09-28 13:40:19 +00:00
|
|
|
{:db (assoc-in db [:dapps/permissions dapp-name] allowed-permissions)
|
|
|
|
:data-store/tx [(dapp-permissions/save-dapp-permissions allowed-permissions)]}))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2018-09-24 15:59:02 +00:00
|
|
|
(fx/defn process-next-permission
|
2018-09-28 13:40:19 +00:00
|
|
|
"Process next permission by removing it from pending permissions and prompting user
|
2018-09-21 13:41:40 +00:00
|
|
|
if there is no pending permissions left, save all granted permissions
|
|
|
|
and return the result to the bridge"
|
2018-09-24 15:59:02 +00:00
|
|
|
[{:keys [db] :as cofx} dapp-name]
|
2018-09-28 13:40:19 +00:00
|
|
|
(if (get-in db [:browser/options :show-permission])
|
|
|
|
{:db db}
|
|
|
|
(let [pending-permissions (get-in db [:browser/options :pending-permissions])
|
|
|
|
next-permission (last pending-permissions)]
|
|
|
|
(when next-permission
|
|
|
|
{:db (-> db
|
|
|
|
(update-in [:browser/options :pending-permissions] butlast)
|
|
|
|
(assoc-in [:browser/options :show-permission]
|
|
|
|
{:requested-permission (:permission next-permission)
|
|
|
|
:message-id (:message-id next-permission)
|
|
|
|
:dapp-name dapp-name}))}))))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2018-09-24 15:59:02 +00:00
|
|
|
(fx/defn allow-permission
|
2018-09-21 13:41:40 +00:00
|
|
|
"Add permission to set of allowed permission and process next permission"
|
2018-09-28 13:40:19 +00:00
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(let [{:keys [requested-permission message-id dapp-name]} (get-in db [:browser/options :show-permission])]
|
|
|
|
(fx/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
|
|
|
|
(update-dapp-permissions dapp-name requested-permission true)
|
|
|
|
(send-permission-data-to-bridge requested-permission message-id true))))
|
|
|
|
|
|
|
|
(fx/defn deny-permission
|
|
|
|
"Add permission to set of allowed permission and process next permission"
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(let [{:keys [requested-permission message-id dapp-name]} (get-in db [:browser/options :show-permission])]
|
|
|
|
(fx/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
|
|
|
|
(send-permission-data-to-bridge requested-permission message-id false))))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2018-09-28 13:40:19 +00:00
|
|
|
(fx/defn process-permission
|
|
|
|
"Process the permission requested by a dapp
|
|
|
|
If supported permission is already granted, return the result immediatly to the bridge
|
2018-09-21 13:41:40 +00:00
|
|
|
Otherwise process the first permission which will prompt user"
|
2018-09-28 13:40:19 +00:00
|
|
|
[cofx dapp-name permission message-id]
|
|
|
|
(let [allowed-permissions (set (get-in cofx [:db :dapps/permissions dapp-name :permissions]))
|
|
|
|
permission-allowed? (boolean (allowed-permissions permission))
|
|
|
|
permission-supported? ((set (keys supported-permissions)) permission)]
|
|
|
|
(if (or permission-allowed? (not permission-supported?))
|
|
|
|
(send-permission-data-to-bridge cofx permission message-id permission-allowed?)
|
|
|
|
(process-next-permission (update-in cofx [:db :browser/options :pending-permissions]
|
|
|
|
conj {:permission permission
|
|
|
|
:message-id message-id})
|
|
|
|
dapp-name))))
|