2023-12-19 17:41:30 +00:00
|
|
|
(ns legacy.status-im.browser.permissions
|
2023-10-16 22:03:18 +00:00
|
|
|
(:require
|
2023-12-19 17:41:30 +00:00
|
|
|
[legacy.status-im.qr-scanner.core :as qr-scanner]
|
2023-12-19 19:59:07 +00:00
|
|
|
[status-im.constants :as constants]
|
|
|
|
[status-im.navigation.events :as navigation]
|
2023-10-16 22:03:18 +00:00
|
|
|
[utils.i18n :as i18n]
|
|
|
|
[utils.re-frame :as rf]))
|
2018-12-06 14:57:23 +00:00
|
|
|
|
|
|
|
(declare process-next-permission)
|
|
|
|
(declare send-response-to-bridge)
|
2018-09-21 13:41:40 +00:00
|
|
|
|
|
|
|
(def supported-permissions
|
2022-12-20 14:45:37 +00:00
|
|
|
{constants/dapp-permission-qr-code {:yield-control? true
|
|
|
|
:allowed? true}
|
|
|
|
constants/dapp-permission-contact-code {:type :profile
|
|
|
|
:title (i18n/label :t/wants-to-access-profile)
|
|
|
|
:description (i18n/label :t/your-contact-code)
|
|
|
|
:icon :main-icons/profile}
|
|
|
|
constants/dapp-permission-web3 {:type :wallet
|
|
|
|
:title (i18n/label :t/dapp-would-like-to-connect-wallet)
|
|
|
|
:description (i18n/label :t/allowing-authorizes-this-dapp)
|
|
|
|
:icon :main-icons/wallet}})
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn permission-yield-control
|
2018-12-06 14:57:23 +00:00
|
|
|
[{:keys [db] :as cofx} dapp-name permission message-id params]
|
2018-10-02 14:14:35 +00:00
|
|
|
(cond
|
|
|
|
(= permission constants/dapp-permission-qr-code)
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge (assoc-in cofx [:db :browser/options :yielding-control?] true)
|
2019-11-16 09:56:09 +00:00
|
|
|
(qr-scanner/scan-qr-code {:handler :browser.bridge.callback/qr-code-scanned
|
2018-10-02 14:14:35 +00:00
|
|
|
:cancel-handler :browser.bridge.callback/qr-code-canceled
|
|
|
|
:data {:dapp-name dapp-name
|
|
|
|
:permission permission
|
|
|
|
:message-id message-id}}))))
|
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn permission-show-permission
|
2018-10-02 14:14:35 +00:00
|
|
|
[{:keys [db] :as cofx} dapp-name permission message-id yield-control?]
|
2022-12-20 14:45:37 +00:00
|
|
|
{:db (assoc-in db
|
2022-12-28 07:47:55 +00:00
|
|
|
[:browser/options :show-permission]
|
|
|
|
{:requested-permission permission
|
|
|
|
:message-id message-id
|
|
|
|
:dapp-name dapp-name
|
|
|
|
:yield-control? yield-control?})})
|
2022-12-20 14:45:37 +00:00
|
|
|
|
|
|
|
(defn get-permission-data
|
|
|
|
[cofx allowed-permission]
|
2023-06-28 11:48:34 +00:00
|
|
|
(let [multiaccount (get-in cofx [:db :profile/profile])]
|
2019-07-03 14:29:01 +00:00
|
|
|
(get {constants/dapp-permission-contact-code (:public-key multiaccount)
|
2019-10-04 11:52:33 +00:00
|
|
|
constants/dapp-permission-web3 [(:dapps-address multiaccount)]}
|
2018-09-28 13:40:19 +00:00
|
|
|
allowed-permission)))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn send-response-to-bridge
|
2018-10-01 10:53:24 +00:00
|
|
|
"Send response to the bridge. If the permission is allowed, send data associated
|
|
|
|
with the permission"
|
2018-10-02 14:14:35 +00:00
|
|
|
[{:keys [db] :as cofx} permission message-id allowed? data]
|
2020-03-23 15:52:42 +00:00
|
|
|
{:browser/send-to-bridge (cond-> {:type constants/api-response
|
|
|
|
:isAllowed allowed?
|
|
|
|
:permission permission
|
|
|
|
:messageId message-id}
|
|
|
|
allowed?
|
|
|
|
(assoc :data data))})
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/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)}]
|
2022-12-22 06:03:55 +00:00
|
|
|
{:db (assoc-in db [:dapps/permissions dapp-name] allowed-permissions)
|
|
|
|
:json-rpc/call [{:method "permissions_addDappPermissions"
|
|
|
|
:params [allowed-permissions]
|
|
|
|
:on-success #()}]}))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn revoke-permissions
|
2020-12-09 15:08:21 +00:00
|
|
|
{:events [:browser/revoke-dapp-permissions]}
|
2019-02-04 09:07:41 +00:00
|
|
|
[{:keys [db] :as cofx} dapp]
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge cofx
|
2022-12-22 06:03:55 +00:00
|
|
|
{:db (update-in db [:dapps/permissions] dissoc dapp)
|
|
|
|
:json-rpc/call [{:method "permissions_deleteDappPermissions"
|
|
|
|
:params [dapp]
|
|
|
|
:on-success #()}]}))
|
2020-12-09 15:08:21 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn revoke-dapp-permissions
|
2021-02-12 14:58:43 +00:00
|
|
|
{:events [:dapps/revoke-access]}
|
2020-12-09 15:08:21 +00:00
|
|
|
[cofx dapp]
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge cofx
|
2020-12-09 15:08:21 +00:00
|
|
|
(revoke-permissions dapp)
|
2019-02-04 09:07:41 +00:00
|
|
|
(navigation/navigate-back)))
|
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn clear-dapps-permissions
|
2019-10-04 11:52:33 +00:00
|
|
|
[{:keys [db]}]
|
|
|
|
(let [dapp-permissions (keys (:dapps/permissions db))]
|
2022-12-22 06:03:55 +00:00
|
|
|
{:db (dissoc db :dapps/permissions)
|
|
|
|
:json-rpc/call (for [dapp dapp-permissions]
|
|
|
|
{:method "permissions_deleteDappPermissions"
|
|
|
|
:params [dapp]
|
|
|
|
:on-success #()})}))
|
2019-10-04 11:52:33 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/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-10-02 14:14:35 +00:00
|
|
|
(let [{:keys [show-permission yielding-control?]} (get db :browser/options)]
|
|
|
|
(if (or show-permission yielding-control?)
|
|
|
|
{:db db}
|
|
|
|
(let [pending-permissions (get-in db [:browser/options :pending-permissions])
|
|
|
|
next-permission (last pending-permissions)
|
2022-12-20 14:45:37 +00:00
|
|
|
new-cofx (update-in cofx [:db :browser/options :pending-permissions] butlast)]
|
2018-12-06 14:57:23 +00:00
|
|
|
(when-let [{:keys [yield-control? permission message-id allowed? params]} next-permission]
|
2018-10-02 14:14:35 +00:00
|
|
|
(if (and yield-control? allowed?)
|
2018-12-06 14:57:23 +00:00
|
|
|
(permission-yield-control new-cofx dapp-name permission message-id params)
|
2018-10-02 14:14:35 +00:00
|
|
|
(permission-show-permission new-cofx dapp-name permission message-id yield-control?)))))))
|
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn send-response-and-process-next-permission
|
2018-10-02 14:14:35 +00:00
|
|
|
[{:keys [db] :as cofx} dapp-name requested-permission message-id]
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge cofx
|
2018-10-02 14:14:35 +00:00
|
|
|
(send-response-to-bridge requested-permission
|
|
|
|
message-id
|
|
|
|
true
|
|
|
|
(get-permission-data cofx requested-permission))
|
|
|
|
(process-next-permission dapp-name)))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn allow-permission
|
2018-09-21 13:41:40 +00:00
|
|
|
"Add permission to set of allowed permission and process next permission"
|
2021-02-12 14:58:43 +00:00
|
|
|
{:events [:browser.permissions.ui/dapp-permission-allowed]}
|
2018-09-28 13:40:19 +00:00
|
|
|
[{:keys [db] :as cofx}]
|
2018-12-06 14:57:23 +00:00
|
|
|
(let [{:keys [requested-permission message-id dapp-name yield-control? params]}
|
2018-10-02 14:14:35 +00:00
|
|
|
(get-in db [:browser/options :show-permission])]
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
|
2018-09-28 13:40:19 +00:00
|
|
|
(update-dapp-permissions dapp-name requested-permission true)
|
2018-10-02 14:14:35 +00:00
|
|
|
(if yield-control?
|
2018-12-06 14:57:23 +00:00
|
|
|
(permission-yield-control dapp-name requested-permission message-id params)
|
2018-10-02 14:14:35 +00:00
|
|
|
(send-response-and-process-next-permission dapp-name requested-permission message-id)))))
|
2018-09-28 13:40:19 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn deny-permission
|
2018-09-28 13:40:19 +00:00
|
|
|
"Add permission to set of allowed permission and process next permission"
|
2021-02-12 14:58:43 +00:00
|
|
|
{:events [:browser.permissions.ui/dapp-permission-denied]}
|
2018-09-28 13:40:19 +00:00
|
|
|
[{:keys [db] :as cofx}]
|
2022-12-20 14:45:37 +00:00
|
|
|
(let [{:keys [requested-permission message-id dapp-name]} (get-in db
|
|
|
|
[:browser/options :show-permission])]
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
|
2018-10-02 14:14:35 +00:00
|
|
|
(send-response-to-bridge requested-permission
|
|
|
|
message-id
|
|
|
|
false
|
|
|
|
(get-permission-data cofx requested-permission))
|
2018-10-01 10:53:24 +00:00
|
|
|
(process-next-permission dapp-name))))
|
2018-09-21 13:41:40 +00:00
|
|
|
|
2022-12-26 15:00:17 +00:00
|
|
|
(rf/defn process-permission
|
2018-09-28 13:40:19 +00:00
|
|
|
"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-12-06 14:57:23 +00:00
|
|
|
[cofx dapp-name permission message-id params]
|
2018-10-02 14:14:35 +00:00
|
|
|
(let [allowed-permissions (set (get-in cofx [:db :dapps/permissions dapp-name :permissions]))
|
|
|
|
permission-allowed? (boolean (allowed-permissions permission))
|
|
|
|
supported-permission (get supported-permissions permission)]
|
|
|
|
(cond
|
|
|
|
(not supported-permission)
|
|
|
|
(send-response-to-bridge cofx permission message-id false nil)
|
|
|
|
|
2022-12-20 14:45:37 +00:00
|
|
|
(and (or permission-allowed? (:allowed? supported-permission))
|
|
|
|
(not (:yield-control? supported-permission)))
|
2018-10-02 14:14:35 +00:00
|
|
|
(send-response-to-bridge cofx permission message-id true (get-permission-data cofx permission))
|
|
|
|
|
|
|
|
:else
|
2022-12-20 14:45:37 +00:00
|
|
|
(process-next-permission (update-in cofx
|
|
|
|
[:db :browser/options :pending-permissions]
|
|
|
|
conj
|
|
|
|
{:permission permission
|
|
|
|
:allowed? (or permission-allowed?
|
|
|
|
(:allowed? supported-permission))
|
|
|
|
:yield-control? (:yield-control? supported-permission)
|
|
|
|
:params params
|
|
|
|
:message-id message-id})
|
2018-10-01 10:53:24 +00:00
|
|
|
dapp-name))))
|