Skip to content

Commit

Permalink
check alternative origin
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Dec 5, 2023
1 parent 1fc9623 commit 7fd3d55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 27 additions & 6 deletions tools/ui/src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dfinityLogo, copyIcon } from "./icons"

export async function renderAuth() {
const is_logged = await authClient?.isAuthenticated();
is_logged ? insertLogout() : insertLoginForm();
is_logged ? await insertLogout() : insertLoginForm();
}

function is_valid_url(url: string): boolean {
Expand All @@ -18,7 +18,21 @@ function is_valid_url(url: string): boolean {
return obj.protocol === "http:" || obj.protocol === "https:";
}

function insertLoginForm() {
async function check_alternative_origin(): Promise<boolean> {
try {
const url = window.location.origin;
const response = await fetch(`${url}/.well-known/ii-alternative-origins`);
const data = await response.json();
if (data.hasProperty("alternativeOrigins") && Array.isArray(data["alternativeOrigins"])) {
return data["alternativeOrigins"].some((origin: string) => origin === url);
}
return false;
} catch (_) {
return false;
}
}

async function insertLoginForm() {
const auth = document.getElementById("authentication")!;
const buttonLogin = document.createElement("button");
buttonLogin.className = "btn btn-auth";
Expand All @@ -38,11 +52,17 @@ function insertLoginForm() {
if (provider && !is_valid_url(provider)) {
throw new Error("Please provide a valid internet identity url in ii parameter");
}
const origin = params.get("origin");
if (origin && !is_valid_url(origin)) {
throw new Error("Please provide a valid derivationOrigin url in origin parameter");
}
const cid = Principal.fromText(params.get("id")!);
let origin = params.get("origin");
if (!origin && is_mainnet && await check_alternative_origin()) {
origin = `https://${cid.toText()}.icp0.io`;
}
if (origin) {
if (!is_valid_url(origin)) {
throw new Error("Please provide a valid derivationOrigin url in origin parameter");
}
buttonLogin.title = "derivationOrigin is enabled. Remember to disable alternative origin in the production canister.";
}

buttonLogin.addEventListener("click", async () => {
let config: any = {
Expand All @@ -60,6 +80,7 @@ function insertLoginForm() {
});
} catch (err) {
buttonLogin.disabled = true;
buttonLogin.classList.add("disabled");
buttonLogin.title = (err as any).toString();
}
}
Expand Down
4 changes: 4 additions & 0 deletions tools/ui/src/candid.css
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ input[type='checkbox'] ~ .popup-form {
gap: 8px;
padding: 10px;
}
.disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-auth svg {
transition: transform ease-out .05s;
}
Expand Down

0 comments on commit 7fd3d55

Please sign in to comment.