Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: web request permissions in popup #133

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 61 additions & 30 deletions example-web/my-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function CircularIndeterminate() {
);
}

const VENDOR_URL = "https://api.npoint.io/d59bd3ab0b31de863a20";
const PROVENANT_URL = "https://api.npoint.io/d59bd3ab0b31de863a20";
const FACEBOOK_URL = "https://api.npoint.io/1d388b8942c4ec3ed763";
const GITHUB_URL = "https://api.npoint.io/a75a0383d2820a2153c1";

function App() {
const [signifyData, setSignifyData] = useState();
Expand All @@ -51,9 +53,6 @@ function App() {
useEffect(() => {
subscribeToSignature(handleSignifyData);
isExtensionInstalled((extensionId) => {
if (extensionId) {
trySettingVendorUrl(VENDOR_URL);
}
setExtensionInstalled(extensionId);
});
return () => {
Expand All @@ -67,6 +66,10 @@ function App() {
setParsedSignifyData(null);
};

const handleSettingVendorUrl = async (url) => {
trySettingVendorUrl(url);
};

const handleRequestAutoSignin = async () => {
console.log("canCallAsync()", canCallAsync());
if (canCallAsync()) {
Expand All @@ -84,33 +87,61 @@ function App() {

if (extensionInstalled)
return (
<div className="auth-btn-container">
<p className="auth-heading">Authenticate with</p>
<Button variant="contained" color="success" onClick={requestAid}>
AID
</Button>
<Button
variant="contained"
color="success"
onClick={requestCredential}
>
Credential
</Button>
<Button
variant="contained"
color="success"
onClick={requestAidORCred}
<>
<div
style={{ display: "flex", flexDirection: "column", rowGap: "8px" }}
>
AID or CRED
</Button>
<Button
variant="contained"
color="success"
onClick={handleRequestAutoSignin}
>
Auto Sign in
</Button>
</div>
<Button
variant="contained"
color="success"
onClick={() => handleSettingVendorUrl(PROVENANT_URL)}
>
Provenant Theme (has Agent)
</Button>
<Button
variant="contained"
color="success"
onClick={() => handleSettingVendorUrl(FACEBOOK_URL)}
>
Facebook Theme
</Button>
<Button
variant="contained"
color="success"
onClick={() => handleSettingVendorUrl(GITHUB_URL)}
>
Github Theme (has Agent)
</Button>
</div>

<div className="auth-btn-container">
<p className="auth-heading">Authenticate with</p>
<Button variant="contained" color="success" onClick={requestAid}>
AID
</Button>
<Button
variant="contained"
color="success"
onClick={requestCredential}
>
Credential
</Button>
<Button
variant="contained"
color="success"
onClick={requestAidORCred}
>
AID or CRED
</Button>
<Button
variant="contained"
color="success"
onClick={handleRequestAutoSignin}
>
Auto Sign in
</Button>
</div>
</>
);

return (
Expand Down
8 changes: 8 additions & 0 deletions src/_locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"account.onboard.cta": "Don't have a KERIA agent?",
"account.settings": "Settings",
"account.support": "support",
"action.allow": "Allow",
"action.back": "Back",
"action.cancel": "Cancel",
"action.click": "click",
"action.clickToRemove": "click to remove",
"action.connect": "Connect",
"action.create": "Create",
"action.createNew": "Create New",
"action.delete": "Delete",
"action.deny": "Deny",
"action.disconnect": "Disconnect",
"action.load": "Load",
"action.open": "Open",
Expand Down Expand Up @@ -43,6 +46,11 @@
"identifier.uniqueName": "Enter unique name for identifier",
"identifiers.title": "Identifiers",
"message.noItems": "No items to show",
"permissions.action.noProceedWithoutAgent": "No, Proceed without agent",
"permissions.action.yesProceed": "Yes, Proceed",
"permissions.desc.loadVendorUrl": "{origin} is requesting to load vendor url {url}",
"permissions.label.permissionRequired": "Permissions Required",
"permissions.warning.containsAgentUrl": "Response contains an agent Url ({url}), you will get logged out in order to connect with it.",
"signin.aidOrCredential": "AID or Credential",
"signin.autoSignin": "Auto Sign in",
"signin.identifierAlias": "Identifier Alias",
Expand Down
8 changes: 8 additions & 0 deletions src/_locales/es-419.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"account.onboard.cta": "¿No tienes un agente KERIA?",
"account.settings": "Ajustes",
"account.support": "soporte",
"action.allow": "Permitir",
"action.back": "Atrás",
"action.cancel": "Cancelar",
"action.click": "hacer clic",
"action.clickToRemove": "haga clic para eliminar",
"action.connect": "Conectar",
"action.create": "Crear",
"action.createNew": "Crear nuevo",
"action.delete": "Borrar",
"action.deny": "Denegar",
"action.disconnect": "Desconectar",
"action.load": "Carga",
"action.open": "Abrir",
Expand Down Expand Up @@ -43,6 +46,11 @@
"identifier.uniqueName": "Introduzca un nombre único para el identificador",
"identifiers.title": "Identificadores",
"message.noItems": "No hay elementos para mostrar",
"permissions.action.noProceedWithoutAgent": "No, proceder sin agente",
"permissions.action.yesProceed": "Si, proceda",
"permissions.desc.loadVendorUrl": "{origin} solicita cargar la URL del proveedor {url}",
"permissions.label.permissionRequired": "Permisos requeridos",
"permissions.warning.containsAgentUrl": "La respuesta contiene una URL de agente ({url}), cerrará su sesión para poder conectarse con ella.",
"signin.aidOrCredential": "AID o Credencial",
"signin.autoSignin": "Acceso automático",
"signin.identifierAlias": "Alias ​​del identificador",
Expand Down
8 changes: 8 additions & 0 deletions src/_locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"account.onboard.cta": "¿No tienes un agente KERIA?",
"account.settings": "Ajustes",
"account.support": "soporte",
"action.allow": "Permitir",
"action.back": "Atrás",
"action.cancel": "Cancelar",
"action.click": "hacer clic",
"action.clickToRemove": "haga clic para eliminar",
"action.connect": "Conectar",
"action.create": "Crear",
"action.createNew": "Crear nuevo",
"action.delete": "Borrar",
"action.deny": "Denegar",
"action.disconnect": "Desconectar",
"action.load": "Carga",
"action.open": "Abrir",
Expand Down Expand Up @@ -43,6 +46,11 @@
"identifier.uniqueName": "Introduzca un nombre único para el identificador",
"identifiers.title": "Identificadores",
"message.noItems": "No hay elementos para mostrar",
"permissions.action.noProceedWithoutAgent": "No, proceder sin agente",
"permissions.action.yesProceed": "Si, proceda",
"permissions.desc.loadVendorUrl": "{origin} solicita cargar la URL del proveedor {url}",
"permissions.label.permissionRequired": "Permisos requeridos",
"permissions.warning.containsAgentUrl": "La respuesta contiene una URL de agente ({url}), cerrará su sesión para poder conectarse con ella.",
"signin.aidOrCredential": "AID o Credencial",
"signin.autoSignin": "Acceso automático",
"signin.identifierAlias": "Alias ​​del identificador",
Expand Down
33 changes: 11 additions & 22 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { browserStorageService } from "@pages/background/services/browser-storage";
import { configService } from "@pages/background/services/config";
import {
WEB_APP_PERMS,
configService,
} from "@pages/background/services/config";
import { userService } from "@pages/background/services/user";
import { signifyService } from "@pages/background/services/signify";
import { IMessage, IIdentifier, ICredential } from "@config/types";
Expand Down Expand Up @@ -82,28 +85,14 @@ chrome.runtime.onMessage.addListener(function (
message.type === "vendor-info" &&
message.subtype === "attempt-set-vendor-url"
) {
const currentUrl = await getCurrentUrl();
const { vendorUrl } = message?.data ?? {};
if (!vendorUrl) {
return;
}

const _vendorUrl = await configService.getUrl();
if (!_vendorUrl) {
try {
const resp = await (await fetch(vendorUrl)).json();
if (resp?.agentUrl) {
await configService.setAgentUrl(resp?.agentUrl);
await configService.setHasOnboarded(true);
}
await configService.setData(resp);
if (resp?.icon) {
await setActionIcon(resp?.icon);
}
await configService.setUrl(vendorUrl);
} catch (error) {}
}

sendResponse({ data: { _vendorUrl } });

await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
{ origin: currentUrl?.origin, vendorUrl }
);
sendResponse({ data: { success: true } });
}

if (
Expand Down
37 changes: 32 additions & 5 deletions src/pages/background/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const CONFIG_ENUMS = {
VENDOR_LANG: "vendor-lang",
AGENT_URL: "agent-url",
HAS_ONBOARDED: "has-onboarded",
WEB_APP_PERMISSION: "WEB_APP_PERMISSION",
};

export const WEB_APP_PERMS = {
SET_VENDOR_URL: `${CONFIG_ENUMS.WEB_APP_PERMISSION}_set_vendor-url`,
};

const Config = () => {
Expand Down Expand Up @@ -65,20 +70,40 @@ const Config = () => {
};

const getAgentAndVendorInfo = async (): Promise<any> => {
const resp = await browserStorageService.getValues([
const resp = (await browserStorageService.getValues([
CONFIG_ENUMS.AGENT_URL,
CONFIG_ENUMS.VENDOR_URL,
CONFIG_ENUMS.VENDOR_DATA,
CONFIG_ENUMS.HAS_ONBOARDED
]) as any;
CONFIG_ENUMS.HAS_ONBOARDED,
])) as any;
return {
vendorUrl: resp[CONFIG_ENUMS.VENDOR_URL],
agentUrl: resp[CONFIG_ENUMS.AGENT_URL],
vendorData: resp[CONFIG_ENUMS.VENDOR_DATA],
hasOnboarded: resp[CONFIG_ENUMS.HAS_ONBOARDED]
hasOnboarded: resp[CONFIG_ENUMS.HAS_ONBOARDED],
};
};

const getWebRequestedPermissions = async (): Promise<any> => {
const resp = (await browserStorageService.getValue(
CONFIG_ENUMS.WEB_APP_PERMISSION
)) as any;
return resp ?? {};
};

const setWebRequestedPermission = async (
permissionKey: string,
value: any
): Promise<void> => {
const resp = await getWebRequestedPermissions();
if (resp[permissionKey] && value === "delete") {
delete resp[permissionKey];
} else {
resp[permissionKey] = value;
}
await browserStorageService.setValue(CONFIG_ENUMS.WEB_APP_PERMISSION, resp);
};

return {
setUrl,
removeUrl,
Expand All @@ -91,7 +116,9 @@ const Config = () => {
getAgentUrl,
setAgentUrl,
setHasOnboarded,
getAgentAndVendorInfo
getAgentAndVendorInfo,
setWebRequestedPermission,
getWebRequestedPermissions,
};
};

Expand Down
Loading