Skip to content

Commit 5e323e3

Browse files
atergasea-snake
andauthored
feat: Sync backend config with II frontend canister (dfinity#3634)
<!-- Make sure you talk to us before submitting changes. See CONTRIBUTING.md. --> # Motivation This is the next step towards the II canister split. # Changes - Add `backend_origin` to II init args/types (Rust + Candid + generated JS/TS bindings). - Update the II frontend canister HTML fixup to require `backend_canister_id`/`backend_origin` and preload `/.config.did.bin`. - Update the web frontend bootstrap to asynchronously fetch and merge backend-provided OpenID config into the runtime config. # Tests Existing OpenID tests should suffice. --------- Co-authored-by: sea-snake <sea-snake@outlook.com>
1 parent c3bb8d3 commit 5e323e3

18 files changed

Lines changed: 124 additions & 95 deletions

File tree

.github/workflows/frontend-checks.yml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,3 @@ jobs:
4545
demos/vc_issuer/app/generated
4646
default_author: github_actions
4747
message: "🤖 npm run generate auto-update"
48-
49-
# Deploy the showcase to GitHub Pages
50-
showcase:
51-
runs-on: ubuntu-latest
52-
steps:
53-
- uses: actions/checkout@v4
54-
- uses: ./.github/actions/setup-node
55-
- run: npm ci
56-
57-
# Build the showcase with a special base URL (used in links and esp. routing)
58-
# to match the deploy URL: https://dfinity.github.io/internet-identity/
59-
- run: npm run build:showcase -- --base '/internet-identity/'
60-
- name: Setup Pages
61-
uses: actions/configure-pages@v4
62-
- name: Upload Pages artifact
63-
uses: actions/upload-pages-artifact@v3
64-
with:
65-
path: "dist-showcase"
66-
67-
# Deploy the showcase to GitHub Pages
68-
showcase-deploy:
69-
runs-on: ubuntu-latest
70-
needs: showcase
71-
if: github.ref == 'refs/heads/main'
72-
environment:
73-
name: github-pages
74-
url: ${{ steps.deployment.outputs.page_url }}
75-
76-
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
77-
permissions:
78-
pages: write
79-
id-token: write
80-
81-
steps:
82-
- name: Deploy to GitHub Pages
83-
uses: actions/deploy-pages@v4
84-
id: deployment

src/frontend/src/hooks.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const overrideFeatureFlags = () => {
4242
};
4343

4444
export const init: ClientInit = async () => {
45-
initGlobals();
45+
await initGlobals();
4646
// Initialize them after globals so canister config can be used for defaults
4747
Object.values(featureFlags).forEach((flag) => flag.initialize());
4848
overrideFeatureFlags();

src/frontend/src/lib/generated/internet_identity_idl.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const idlFactory = ({ IDL }) => {
4747
const InternetIdentityInit = IDL.Record({
4848
'fetch_root_key' : IDL.Opt(IDL.Bool),
4949
'is_production' : IDL.Opt(IDL.Bool),
50+
'backend_canister_id' : IDL.Opt(IDL.Principal),
5051
'enable_dapps_explorer' : IDL.Opt(IDL.Bool),
5152
'assigned_user_number_range' : IDL.Opt(IDL.Tuple(IDL.Nat64, IDL.Nat64)),
5253
'new_flow_origins' : IDL.Opt(IDL.Vec(IDL.Text)),
@@ -55,6 +56,7 @@ export const idlFactory = ({ IDL }) => {
5556
'analytics_config' : IDL.Opt(IDL.Opt(AnalyticsConfig)),
5657
'related_origins' : IDL.Opt(IDL.Vec(IDL.Text)),
5758
'openid_configs' : IDL.Opt(IDL.Vec(OpenIdConfig)),
59+
'backend_origin' : IDL.Opt(IDL.Text),
5860
'captcha_config' : IDL.Opt(CaptchaConfig),
5961
'dummy_auth' : IDL.Opt(IDL.Opt(DummyAuthConfig)),
6062
'register_rate_limit' : IDL.Opt(RateLimitConfig),
@@ -370,26 +372,10 @@ export const idlFactory = ({ IDL }) => {
370372
'headers' : IDL.Vec(HeaderField),
371373
'certificate_version' : IDL.Opt(IDL.Nat16),
372374
});
373-
const Token = IDL.Record({});
374-
const StreamingCallbackHttpResponse = IDL.Record({
375-
'token' : IDL.Opt(Token),
376-
'body' : IDL.Vec(IDL.Nat8),
377-
});
378-
const StreamingStrategy = IDL.Variant({
379-
'Callback' : IDL.Record({
380-
'token' : Token,
381-
'callback' : IDL.Func(
382-
[Token],
383-
[StreamingCallbackHttpResponse],
384-
['query'],
385-
),
386-
}),
387-
});
388375
const HttpResponse = IDL.Record({
389376
'body' : IDL.Vec(IDL.Nat8),
390377
'headers' : IDL.Vec(HeaderField),
391378
'upgrade' : IDL.Opt(IDL.Bool),
392-
'streaming_strategy' : IDL.Opt(StreamingStrategy),
393379
'status_code' : IDL.Nat16,
394380
});
395381
const IdentityAuthnInfo = IDL.Record({
@@ -945,6 +931,7 @@ export const init = ({ IDL }) => {
945931
const InternetIdentityInit = IDL.Record({
946932
'fetch_root_key' : IDL.Opt(IDL.Bool),
947933
'is_production' : IDL.Opt(IDL.Bool),
934+
'backend_canister_id' : IDL.Opt(IDL.Principal),
948935
'enable_dapps_explorer' : IDL.Opt(IDL.Bool),
949936
'assigned_user_number_range' : IDL.Opt(IDL.Tuple(IDL.Nat64, IDL.Nat64)),
950937
'new_flow_origins' : IDL.Opt(IDL.Vec(IDL.Text)),
@@ -953,6 +940,7 @@ export const init = ({ IDL }) => {
953940
'analytics_config' : IDL.Opt(IDL.Opt(AnalyticsConfig)),
954941
'related_origins' : IDL.Opt(IDL.Vec(IDL.Text)),
955942
'openid_configs' : IDL.Opt(IDL.Vec(OpenIdConfig)),
943+
'backend_origin' : IDL.Opt(IDL.Text),
956944
'captcha_config' : IDL.Opt(CaptchaConfig),
957945
'dummy_auth' : IDL.Opt(IDL.Opt(DummyAuthConfig)),
958946
'register_rate_limit' : IDL.Opt(RateLimitConfig),

src/frontend/src/lib/generated/internet_identity_types.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ export interface HttpResponse {
520520
'body' : Uint8Array | number[],
521521
'headers' : Array<HeaderField>,
522522
'upgrade' : [] | [boolean],
523-
'streaming_strategy' : [] | [StreamingStrategy],
524523
'status_code' : number,
525524
}
526525
/**
@@ -695,6 +694,10 @@ export interface InternetIdentityInit {
695694
* For now, this is used only to show or hide the banner.
696695
*/
697696
'is_production' : [] | [boolean],
697+
/**
698+
* Backend canister ID, needed for backward compatibility.
699+
*/
700+
'backend_canister_id' : [] | [Principal],
698701
/**
699702
* Configuration to show dapps explorer or not
700703
*/
@@ -734,6 +737,10 @@ export interface InternetIdentityInit {
734737
* Configurations for OpenID clients
735738
*/
736739
'openid_configs' : [] | [Array<OpenIdConfig>],
740+
/**
741+
* Backend origin, needed to sync configuration with frontend.
742+
*/
743+
'backend_origin' : [] | [string],
737744
/**
738745
* Configuration of the captcha in the registration flow.
739746
*/

src/frontend/src/lib/globals.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Principal } from "@icp-sdk/core/principal";
2-
import type {
3-
_SERVICE,
4-
InternetIdentityInit,
2+
import {
3+
type _SERVICE,
4+
type InternetIdentityInit,
55
} from "$lib/generated/internet_identity_types";
66
import { readCanisterConfig, readCanisterId } from "$lib/utils/init";
77
import {
@@ -11,8 +11,12 @@ import {
1111
HttpAgentOptions,
1212
} from "@icp-sdk/core/agent";
1313
import { inferHost } from "$lib/utils/iiConnection";
14-
import { idlFactory as internet_identity_idl } from "$lib/generated/internet_identity_idl";
14+
import {
15+
idlFactory as internet_identity_idl,
16+
init as internet_identity_init,
17+
} from "$lib/generated/internet_identity_idl";
1518
import { features } from "$lib/legacy/features";
19+
import { IDL } from "@icp-sdk/core/candid";
1620

1721
export let canisterId: Principal;
1822
export let canisterConfig: InternetIdentityInit;
@@ -24,9 +28,31 @@ export let parentIFrameOrigin: string | undefined;
2428
// Search param passed by parent window to indicate its origin to child window
2529
export const IFRAME_PARENT_PARAM = "parent_origin";
2630

27-
export const initGlobals = () => {
31+
export const initGlobals = async () => {
2832
canisterId = Principal.fromText(readCanisterId());
29-
canisterConfig = readCanisterConfig();
33+
const frontendConfig = readCanisterConfig();
34+
35+
const backendOrigin = frontendConfig.backend_origin[0];
36+
37+
if (backendOrigin !== undefined) {
38+
// Patch the canister config with the `openid_configs` field from the backend HTTPS response
39+
const response = await fetch(`${backendOrigin}/.config.did.bin`);
40+
const openidConfigCandid = await response.arrayBuffer();
41+
42+
const [{ openid_configs }] = IDL.decode(
43+
[internet_identity_init({ IDL })[0]._type],
44+
new Uint8Array(openidConfigCandid),
45+
) as unknown as [InternetIdentityInit];
46+
47+
canisterConfig = {
48+
...frontendConfig,
49+
openid_configs,
50+
};
51+
} else {
52+
// Legacy flow, when the frontend assets are from the one and only II canister
53+
canisterConfig = frontendConfig;
54+
}
55+
3056
agentOptions = {
3157
host: inferHost(),
3258
shouldFetchRootKey:

src/frontend/src/lib/utils/domainUtils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe("isOfficialOrigin", () => {
2020
is_production: [],
2121
new_flow_origins: [],
2222
dummy_auth: [],
23+
backend_canister_id: [],
24+
backend_origin: [],
2325
});
2426

2527
// Default related origins for most tests

src/frontend/src/lib/utils/iiConnection.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const DEFAULT_INIT: InternetIdentityInit = {
8080
is_production: [],
8181
new_flow_origins: [],
8282
dummy_auth: [],
83+
backend_canister_id: [],
84+
backend_origin: [],
8385
};
8486

8587
vi.mock("$lib/globals", () => ({

src/frontend/src/lib/utils/isRegistrationAllowed.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe("isRegistrationAllowed", () => {
2020
is_production: [],
2121
new_flow_origins: [],
2222
dummy_auth: [],
23+
backend_canister_id: [],
24+
backend_origin: [],
2325
};
2426
};
2527

src/frontend/src/lib/utils/lastUsedIdentity.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const mockInternetIdentityInit = (
1919
captcha_config: [],
2020
dummy_auth: [],
2121
register_rate_limit: [],
22+
backend_canister_id: [],
23+
backend_origin: [],
2224
...overrides,
2325
});
2426

src/frontend/src/routes/legacy/authorize/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts">
22
import Flow from "$lib/components/utils/Flow.svelte";
33
import { Connection } from "$lib/utils/iiConnection";
4-
import { readCanisterConfig, readCanisterId } from "$lib/utils/init";
54
import { authFlowAuthorize } from "$lib/legacy/flows/authorize";
65
import { analytics } from "$lib/utils/analytics/analytics";
76
import { canisterConfig, canisterId } from "$lib/globals";

0 commit comments

Comments
 (0)