Skip to content

Commit 3a7baf2

Browse files
authored
Fix Internet Identity integration in vetKD examples and make them work in Safari (#1007)
1 parent 16324e9 commit 3a7baf2

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

motoko/vetkd/src/app_backend/Main.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ actor {
2222

2323
let vetkd_system_api : VETKD_SYSTEM_API = actor ("s55qq-oqaaa-aaaaa-aaakq-cai");
2424

25-
public shared ({ caller }) func app_vetkd_public_key(derivation_path : [Blob]) : async Text {
25+
public shared ({ caller = _ }) func app_vetkd_public_key(derivation_path : [Blob]) : async Text {
2626
let { public_key } = await vetkd_system_api.vetkd_public_key({
2727
canister_id = null;
2828
derivation_path;
@@ -31,7 +31,7 @@ actor {
3131
Hex.encode(Blob.toArray(public_key));
3232
};
3333

34-
public shared ({ caller }) func symmetric_key_verification_key() : async Text {
34+
public shared ({ caller = _ }) func symmetric_key_verification_key() : async Text {
3535
let { public_key } = await vetkd_system_api.vetkd_public_key({
3636
canister_id = null;
3737
derivation_path = Array.make(Text.encodeUtf8("symmetric_key"));
@@ -52,7 +52,7 @@ actor {
5252
Hex.encode(Blob.toArray(encrypted_key));
5353
};
5454

55-
public shared ({ caller }) func ibe_encryption_key() : async Text {
55+
public shared ({ caller = _ }) func ibe_encryption_key() : async Text {
5656
let { public_key } = await vetkd_system_api.vetkd_public_key({
5757
canister_id = null;
5858
derivation_path = Array.make(Text.encodeUtf8("ibe_encryption"));

motoko/vetkd/src/app_frontend_js/assets/.ic-assets.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// See: https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md.
2525
// - We added img-src data: because data: images are used often.
2626
// - frame-ancestors: none mitigates clickjacking attacks. See https://owasp.org/www-community/attacks/Clickjacking.
27-
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';upgrade-insecure-requests;",
27+
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';",
2828

2929
// Security: The permissions policy disables all features for security reasons. If your site needs such permissions, activate them.
3030
// To configure permissions go here https://www.permissionspolicy.com/

motoko/vetkd/src/app_frontend_js/src/.ic-assets.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// See: https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md.
2525
// - We added img-src data: because data: images are used often.
2626
// - frame-ancestors: none mitigates clickjacking attacks. See https://owasp.org/www-community/attacks/Clickjacking.
27-
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';upgrade-insecure-requests;",
27+
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';",
2828

2929
// Security: The permissions policy disables all features for security reasons. If your site needs such permissions, activate them.
3030
// To configure permissions go here https://www.permissionspolicy.com/

motoko/vetkd/src/app_frontend_js/src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,22 @@ async function ibe_decrypt(ibe_ciphertext_hex) {
235235

236236
document.getElementById("login").onclick = async (e) => {
237237
e.preventDefault();
238+
239+
// According to https://github.com/dfinity/internet-identity?tab=readme-ov-file#local-replica,
240+
// for local deployments, the II URL must be different depending on the browser:
241+
// Chrome, Firefox: http://<canister_id>.localhost:4943
242+
// Safari: http://localhost:4943?canisterId=<canister_id>
243+
//
244+
// Safari detection rules are according to: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#browser_name_and_version
245+
let isSafari = /^(?!.*chrome\/\d+)(?!.*chromium\/\d+).*safari\/\d+/i.test(navigator.userAgent);
246+
let identityProvider = isSafari ?
247+
`http://localhost:4943/?canisterId=${process.env.CANISTER_ID_INTERNET_IDENTITY}` :
248+
`http://${process.env.CANISTER_ID_INTERNET_IDENTITY}.localhost:4943/`;
249+
238250
let authClient = await AuthClient.create();
239251
await new Promise((resolve) => {
240252
authClient.login({
241-
identityProvider: `http://127.0.0.1:4943/?canisterId=${process.env.INTERNET_IDENTITY_CANISTER_ID}`,
253+
identityProvider: identityProvider,
242254
onSuccess: resolve,
243255
});
244256
});
@@ -247,7 +259,7 @@ document.getElementById("login").onclick = async (e) => {
247259
// Using the identity obtained from the auth client, we can create an agent to interact with the IC.
248260
const agent = new HttpAgent({ identity });
249261
// Using the interface description of our webapp, we create an actor that we use to call the service methods. We override the global actor, such that the other button handler will automatically use the new actor with the Internet Identity provided delegation.
250-
app_backend_actor = createActor(process.env.APP_BACKEND_CANISTER_ID, {
262+
app_backend_actor = createActor(process.env.CANISTER_ID_APP_BACKEND, {
251263
agent,
252264
});
253265
app_backend_principal = identity.getPrincipal();

rust/vetkd/src/app_frontend_js/assets/.ic-assets.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// See: https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md.
2525
// - We added img-src data: because data: images are used often.
2626
// - frame-ancestors: none mitigates clickjacking attacks. See https://owasp.org/www-community/attacks/Clickjacking.
27-
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';upgrade-insecure-requests;",
27+
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';",
2828

2929
// Security: The permissions policy disables all features for security reasons. If your site needs such permissions, activate them.
3030
// To configure permissions go here https://www.permissionspolicy.com/

rust/vetkd/src/app_frontend_js/src/.ic-assets.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// See: https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md.
2525
// - We added img-src data: because data: images are used often.
2626
// - frame-ancestors: none mitigates clickjacking attacks. See https://owasp.org/www-community/attacks/Clickjacking.
27-
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';upgrade-insecure-requests;",
27+
"Content-Security-Policy": "default-src 'self';script-src 'self' 'unsafe-eval';connect-src 'self' https://icp0.io https://*.icp0.io;img-src 'self' data:;style-src * 'unsafe-inline';style-src-elem * 'unsafe-inline';font-src *;object-src 'none';base-uri 'self';frame-ancestors 'none';form-action 'self';",
2828

2929
// Security: The permissions policy disables all features for security reasons. If your site needs such permissions, activate them.
3030
// To configure permissions go here https://www.permissionspolicy.com/

rust/vetkd/src/app_frontend_js/src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,22 @@ async function ibe_decrypt(ibe_ciphertext_hex) {
235235

236236
document.getElementById("login").onclick = async (e) => {
237237
e.preventDefault();
238+
239+
// According to https://github.com/dfinity/internet-identity?tab=readme-ov-file#local-replica,
240+
// for local deployments, the II URL must be different depending on the browser:
241+
// Chrome, Firefox: http://<canister_id>.localhost:4943
242+
// Safari: http://localhost:4943?canisterId=<canister_id>
243+
//
244+
// Safari detection rules are according to: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#browser_name_and_version
245+
let isSafari = /^(?!.*chrome\/\d+)(?!.*chromium\/\d+).*safari\/\d+/i.test(navigator.userAgent);
246+
let identityProvider = isSafari ?
247+
`http://127.0.0.1:4943/?canisterId=${process.env.CANISTER_ID_INTERNET_IDENTITY}` :
248+
`http://${process.env.CANISTER_ID_INTERNET_IDENTITY}.localhost:4943/`;
249+
238250
let authClient = await AuthClient.create();
239251
await new Promise((resolve) => {
240252
authClient.login({
241-
identityProvider: `http://127.0.0.1:4943/?canisterId=${process.env.INTERNET_IDENTITY_CANISTER_ID}`,
253+
identityProvider: identityProvider,
242254
onSuccess: resolve,
243255
});
244256
});
@@ -247,7 +259,7 @@ document.getElementById("login").onclick = async (e) => {
247259
// Using the identity obtained from the auth client, we can create an agent to interact with the IC.
248260
const agent = new HttpAgent({ identity });
249261
// Using the interface description of our webapp, we create an actor that we use to call the service methods. We override the global actor, such that the other button handler will automatically use the new actor with the Internet Identity provided delegation.
250-
app_backend_actor = createActor(process.env.APP_BACKEND_CANISTER_ID, {
262+
app_backend_actor = createActor(process.env.CANISTER_ID_APP_BACKEND, {
251263
agent,
252264
});
253265
app_backend_principal = identity.getPrincipal();

0 commit comments

Comments
 (0)