Skip to content

Commit

Permalink
feat: implement multiple permission options
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanjoshi914 committed Feb 21, 2024
1 parent b4f3a58 commit 594778c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
11 changes: 4 additions & 7 deletions src/app/components/SitePreferences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,10 @@ function SitePreferences({ launcherType, allowance, onEdit, onDelete }: Props) {
.slice(-1)
.toString()}`,
{
defaultValue: tNostr("kinds.unknown", {
kind: permission.method
.toLowerCase()
.split("/")
.slice(-1)
.toString(),
}),
defaultValue: permission.method
.split("/")
.slice(-1)
.toString(),
}
)
: permission.method.split("/").slice(0, 2).join("/")
Expand Down
30 changes: 14 additions & 16 deletions src/app/screens/Nostr/ConfirmSignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Container from "@components/Container";
import ContentMessage from "@components/ContentMessage";
import PublisherCard from "@components/PublisherCard";
import SuccessMessage from "@components/SuccessMessage";
import Checkbox from "@components/form/Checkbox";
import { useState } from "react";
import { Trans, useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
Expand All @@ -28,7 +27,7 @@ function ConfirmSignMessage() {
const origin = navState.origin as OriginData;
const [loading, setLoading] = useState(false);
const [successMessage, setSuccessMessage] = useState("");
const [rememberPermission, setRememberPermission] = useState(false);
const [permissionOption, setPermissionOption] = useState("ask_every_time");
const [showJSON, setShowJSON] = useState(false);

// TODO: refactor: the success message and loading will not be displayed because after the reply the prompt is closed.
Expand All @@ -37,7 +36,7 @@ function ConfirmSignMessage() {
setLoading(true);
msg.reply({
blocked: false,
enabled: rememberPermission,
permissionOption: permissionOption,
});
setSuccessMessage(tCommon("success"));
} catch (e) {
Expand Down Expand Up @@ -115,20 +114,19 @@ function ConfirmSignMessage() {
</div>
<div>
<div className="flex items-center mb-4">
<Checkbox
id="remember_permission"
name="remember_permission"
checked={rememberPermission}
onChange={(event) => {
setRememberPermission(event.target.checked);
}}
/>
<label
htmlFor="remember_permission"
className="cursor-pointer ml-2 block text-sm text-gray-900 font-medium dark:text-white"
<select
value={permissionOption}
onChange={(event) => setPermissionOption(event.target.value)}
className="block w-full mt-1 rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
>
{tCommon("actions.remember")}
</label>
<option value="ask_every_time">actions.ask_every_time</option>
<option value="dont_ask_again_current">
actions.dont_ask_again_current
</option>
<option value="dont_ask_again_all">
actions.dont_ask_again_all
</option>
</select>
</div>
<ConfirmOrCancel
disabled={loading}
Expand Down
24 changes: 18 additions & 6 deletions src/extension/background-script/actions/nostr/signEventOrPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,38 @@ const signEventOrPrompt = async (message: MessageSignEvent, sender: Sender) => {
};
}

const hasPermission = await hasPermissionFor(
PermissionMethodNostr["NOSTR_SIGNMESSAGE"] + "/" + event.kind,
host
);
const hasPermission =
(await hasPermissionFor(
PermissionMethodNostr["NOSTR_SIGNMESSAGE"],
host
)) ||
(await hasPermissionFor(
PermissionMethodNostr["NOSTR_SIGNMESSAGE"] + "/" + event.kind,
host
));
if (!hasPermission) {
const promptResponse = await utils.openPrompt<{
enabled: boolean;
blocked: boolean;
permissionOption: string;
}>({
...message,
action: "public/nostr/confirmSignMessage",
});

// add permission to db only if user decided to always allow this request
if (promptResponse.data.enabled) {
if (promptResponse.data.permissionOption == "dont_ask_again_current") {
await addPermissionFor(
PermissionMethodNostr["NOSTR_SIGNMESSAGE"] + "/" + event.kind,
host
);
}

if (promptResponse.data.permissionOption == "dont_ask_again_all") {
await addPermissionFor(
PermissionMethodNostr["NOSTR_SIGNMESSAGE"],
host
);
}
}

if (!event.pubkey) event.pubkey = nostr.getPublicKey();
Expand Down

0 comments on commit 594778c

Please sign in to comment.