Skip to content

Commit 8455242

Browse files
feat: external alternative origins (#1200)
* feat: external alternative origins * feat: generate did * feat: adapt for did * feat: test external urls
1 parent 0d4822d commit 8455242

File tree

11 files changed

+336
-110
lines changed

11 files changed

+336
-110
lines changed

src/declarations/satellite/satellite.did.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface AuthenticationConfig {
2828
}
2929
export interface AuthenticationConfigInternetIdentity {
3030
derivation_origin: [] | [string];
31+
external_alternative_origins: [] | [Array<string>];
3132
}
3233
export type CollectionType = { Db: null } | { Storage: null };
3334
export interface CommitBatch {
@@ -158,7 +159,6 @@ export interface RateConfig {
158159
export interface Rule {
159160
max_capacity: [] | [number];
160161
memory: [] | [Memory];
161-
max_changes_per_user: [] | [number];
162162
updated_at: bigint;
163163
max_size: [] | [bigint];
164164
read: Permission;
@@ -167,6 +167,7 @@ export interface Rule {
167167
mutable_permissions: [] | [boolean];
168168
rate_config: [] | [RateConfig];
169169
write: Permission;
170+
max_changes_per_user: [] | [number];
170171
}
171172
export interface SetController {
172173
metadata: Array<[string, string]>;
@@ -185,13 +186,13 @@ export interface SetDoc {
185186
export interface SetRule {
186187
max_capacity: [] | [number];
187188
memory: [] | [Memory];
188-
max_changes_per_user: [] | [number];
189189
max_size: [] | [bigint];
190190
read: Permission;
191191
version: [] | [bigint];
192192
mutable_permissions: [] | [boolean];
193193
rate_config: [] | [RateConfig];
194194
write: Permission;
195+
max_changes_per_user: [] | [number];
195196
}
196197
export interface SetUserUsage {
197198
changes_count: number;

src/declarations/satellite/satellite.factory.certified.did.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const idlFactory = ({ IDL }) => {
7676
version: IDL.Opt(IDL.Nat64)
7777
});
7878
const AuthenticationConfigInternetIdentity = IDL.Record({
79-
derivation_origin: IDL.Opt(IDL.Text)
79+
derivation_origin: IDL.Opt(IDL.Text),
80+
external_alternative_origins: IDL.Opt(IDL.Vec(IDL.Text))
8081
});
8182
const AuthenticationConfig = IDL.Record({
8283
internet_identity: IDL.Opt(AuthenticationConfigInternetIdentity)
@@ -136,15 +137,15 @@ export const idlFactory = ({ IDL }) => {
136137
const Rule = IDL.Record({
137138
max_capacity: IDL.Opt(IDL.Nat32),
138139
memory: IDL.Opt(Memory),
139-
max_changes_per_user: IDL.Opt(IDL.Nat32),
140140
updated_at: IDL.Nat64,
141141
max_size: IDL.Opt(IDL.Nat),
142142
read: Permission,
143143
created_at: IDL.Nat64,
144144
version: IDL.Opt(IDL.Nat64),
145145
mutable_permissions: IDL.Opt(IDL.Bool),
146146
rate_config: IDL.Opt(RateConfig),
147-
write: Permission
147+
write: Permission,
148+
max_changes_per_user: IDL.Opt(IDL.Nat32)
148149
});
149150
const UserUsage = IDL.Record({
150151
updated_at: IDL.Nat64,
@@ -231,13 +232,13 @@ export const idlFactory = ({ IDL }) => {
231232
const SetRule = IDL.Record({
232233
max_capacity: IDL.Opt(IDL.Nat32),
233234
memory: IDL.Opt(Memory),
234-
max_changes_per_user: IDL.Opt(IDL.Nat32),
235235
max_size: IDL.Opt(IDL.Nat),
236236
read: Permission,
237237
version: IDL.Opt(IDL.Nat64),
238238
mutable_permissions: IDL.Opt(IDL.Bool),
239239
rate_config: IDL.Opt(RateConfig),
240-
write: Permission
240+
write: Permission,
241+
max_changes_per_user: IDL.Opt(IDL.Nat32)
241242
});
242243
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
243244
const UploadChunk = IDL.Record({

src/declarations/satellite/satellite.factory.did.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const idlFactory = ({ IDL }) => {
7676
version: IDL.Opt(IDL.Nat64)
7777
});
7878
const AuthenticationConfigInternetIdentity = IDL.Record({
79-
derivation_origin: IDL.Opt(IDL.Text)
79+
derivation_origin: IDL.Opt(IDL.Text),
80+
external_alternative_origins: IDL.Opt(IDL.Vec(IDL.Text))
8081
});
8182
const AuthenticationConfig = IDL.Record({
8283
internet_identity: IDL.Opt(AuthenticationConfigInternetIdentity)
@@ -136,15 +137,15 @@ export const idlFactory = ({ IDL }) => {
136137
const Rule = IDL.Record({
137138
max_capacity: IDL.Opt(IDL.Nat32),
138139
memory: IDL.Opt(Memory),
139-
max_changes_per_user: IDL.Opt(IDL.Nat32),
140140
updated_at: IDL.Nat64,
141141
max_size: IDL.Opt(IDL.Nat),
142142
read: Permission,
143143
created_at: IDL.Nat64,
144144
version: IDL.Opt(IDL.Nat64),
145145
mutable_permissions: IDL.Opt(IDL.Bool),
146146
rate_config: IDL.Opt(RateConfig),
147-
write: Permission
147+
write: Permission,
148+
max_changes_per_user: IDL.Opt(IDL.Nat32)
148149
});
149150
const UserUsage = IDL.Record({
150151
updated_at: IDL.Nat64,
@@ -231,13 +232,13 @@ export const idlFactory = ({ IDL }) => {
231232
const SetRule = IDL.Record({
232233
max_capacity: IDL.Opt(IDL.Nat32),
233234
memory: IDL.Opt(Memory),
234-
max_changes_per_user: IDL.Opt(IDL.Nat32),
235235
max_size: IDL.Opt(IDL.Nat),
236236
read: Permission,
237237
version: IDL.Opt(IDL.Nat64),
238238
mutable_permissions: IDL.Opt(IDL.Bool),
239239
rate_config: IDL.Opt(RateConfig),
240-
write: Permission
240+
write: Permission,
241+
max_changes_per_user: IDL.Opt(IDL.Nat32)
241242
});
242243
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
243244
const UploadChunk = IDL.Record({

src/declarations/satellite/satellite.factory.did.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const idlFactory = ({ IDL }) => {
7676
version: IDL.Opt(IDL.Nat64)
7777
});
7878
const AuthenticationConfigInternetIdentity = IDL.Record({
79-
derivation_origin: IDL.Opt(IDL.Text)
79+
derivation_origin: IDL.Opt(IDL.Text),
80+
external_alternative_origins: IDL.Opt(IDL.Vec(IDL.Text))
8081
});
8182
const AuthenticationConfig = IDL.Record({
8283
internet_identity: IDL.Opt(AuthenticationConfigInternetIdentity)
@@ -136,15 +137,15 @@ export const idlFactory = ({ IDL }) => {
136137
const Rule = IDL.Record({
137138
max_capacity: IDL.Opt(IDL.Nat32),
138139
memory: IDL.Opt(Memory),
139-
max_changes_per_user: IDL.Opt(IDL.Nat32),
140140
updated_at: IDL.Nat64,
141141
max_size: IDL.Opt(IDL.Nat),
142142
read: Permission,
143143
created_at: IDL.Nat64,
144144
version: IDL.Opt(IDL.Nat64),
145145
mutable_permissions: IDL.Opt(IDL.Bool),
146146
rate_config: IDL.Opt(RateConfig),
147-
write: Permission
147+
write: Permission,
148+
max_changes_per_user: IDL.Opt(IDL.Nat32)
148149
});
149150
const UserUsage = IDL.Record({
150151
updated_at: IDL.Nat64,
@@ -231,13 +232,13 @@ export const idlFactory = ({ IDL }) => {
231232
const SetRule = IDL.Record({
232233
max_capacity: IDL.Opt(IDL.Nat32),
233234
memory: IDL.Opt(Memory),
234-
max_changes_per_user: IDL.Opt(IDL.Nat32),
235235
max_size: IDL.Opt(IDL.Nat),
236236
read: Permission,
237237
version: IDL.Opt(IDL.Nat64),
238238
mutable_permissions: IDL.Opt(IDL.Bool),
239239
rate_config: IDL.Opt(RateConfig),
240-
write: Permission
240+
write: Permission,
241+
max_changes_per_user: IDL.Opt(IDL.Nat32)
241242
});
242243
const SetUserUsage = IDL.Record({ changes_count: IDL.Nat32 });
243244
const UploadChunk = IDL.Record({

src/frontend/src/lib/utils/auth.config.utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AuthenticationConfig } from '$declarations/satellite/satellite.did';
2-
import { fromNullable, isNullish, nonNullish } from '@dfinity/utils';
2+
import { fromNullable, isNullish, nonNullish, toNullable } from '@dfinity/utils';
33

44
export const buildSetAuthenticationConfig = ({
55
config,
@@ -12,7 +12,8 @@ export const buildSetAuthenticationConfig = ({
1212
? {
1313
internet_identity: [
1414
{
15-
derivation_origin: [domainName]
15+
derivation_origin: [domainName],
16+
external_alternative_origins: toNullable()
1617
}
1718
]
1819
}
@@ -22,7 +23,8 @@ export const buildSetAuthenticationConfig = ({
2223
internet_identity: [
2324
{
2425
...fromNullable(config.internet_identity),
25-
derivation_origin: [domainName]
26+
derivation_origin: [domainName],
27+
external_alternative_origins: toNullable()
2628
}
2729
]
2830
})
@@ -36,7 +38,8 @@ export const buildDeleteAuthenticationConfig = (
3638
internet_identity: [
3739
{
3840
...fromNullable(config.internet_identity),
39-
derivation_origin: []
41+
derivation_origin: [],
42+
external_alternative_origins: []
4043
}
4144
]
4245
})

src/libs/satellite/satellite.did

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type AuthenticationConfig = record {
2424
};
2525
type AuthenticationConfigInternetIdentity = record {
2626
derivation_origin : opt text;
27+
external_alternative_origins : opt vec text;
2728
};
2829
type CollectionType = variant { Db; Storage };
2930
type CommitBatch = record {
@@ -122,7 +123,6 @@ type RateConfig = record { max_tokens : nat64; time_per_token_ns : nat64 };
122123
type Rule = record {
123124
max_capacity : opt nat32;
124125
memory : opt Memory;
125-
max_changes_per_user : opt nat32;
126126
updated_at : nat64;
127127
max_size : opt nat;
128128
read : Permission;
@@ -131,6 +131,7 @@ type Rule = record {
131131
mutable_permissions : opt bool;
132132
rate_config : opt RateConfig;
133133
write : Permission;
134+
max_changes_per_user : opt nat32;
134135
};
135136
type SetController = record {
136137
metadata : vec record { text; text };
@@ -149,13 +150,13 @@ type SetDoc = record {
149150
type SetRule = record {
150151
max_capacity : opt nat32;
151152
memory : opt Memory;
152-
max_changes_per_user : opt nat32;
153153
max_size : opt nat;
154154
read : Permission;
155155
version : opt nat64;
156156
mutable_permissions : opt bool;
157157
rate_config : opt RateConfig;
158158
write : Permission;
159+
max_changes_per_user : opt nat32;
159160
};
160161
type SetUserUsage = record { changes_count : nat32 };
161162
type StorageConfig = record {

src/libs/satellite/src/auth/alternative_origins.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ struct AlternativeOrigins {
1717
}
1818

1919
pub fn update_alternative_origins(config: &AuthenticationConfig) -> Result<(), String> {
20-
config
21-
.internet_identity
22-
.as_ref()
23-
.and_then(|config| config.derivation_origin.as_ref())
24-
.map_or_else(
25-
|| delete_alternative_origins_asset(&StorageState),
26-
set_alternative_origins,
27-
)
20+
if let Some(internet_identity) = &config.internet_identity {
21+
if let Some(derivation_origin) = &internet_identity.derivation_origin {
22+
return set_alternative_origins(
23+
derivation_origin,
24+
&internet_identity.external_alternative_origins,
25+
);
26+
}
27+
}
28+
29+
delete_alternative_origins_asset(&StorageState)
2830
}
2931

30-
fn set_alternative_origins(derivation_origin: &DomainName) -> Result<(), String> {
32+
fn set_alternative_origins(
33+
derivation_origin: &DomainName,
34+
external_alternative_origins: &Option<Vec<DomainName>>,
35+
) -> Result<(), String> {
3136
let mut custom_domains: Vec<DomainName> = get_custom_domains_store()
3237
.keys()
3338
.filter(|domain| *domain != derivation_origin)
@@ -40,6 +45,21 @@ fn set_alternative_origins(derivation_origin: &DomainName) -> Result<(), String>
4045
custom_domains.push(canister_url);
4146
}
4247

48+
let external_domains: Vec<DomainName> =
49+
external_alternative_origins
50+
.as_ref()
51+
.map_or_else(Vec::new, |alternative_origins| {
52+
alternative_origins
53+
.iter()
54+
.filter(|domain| {
55+
*domain != derivation_origin && !custom_domains.contains(domain)
56+
})
57+
.cloned()
58+
.collect()
59+
});
60+
61+
custom_domains.extend(external_domains);
62+
4363
if custom_domains.is_empty() {
4464
return delete_alternative_origins_asset(&StorageState);
4565
}

src/libs/satellite/src/auth/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ pub mod config {
2222
#[derive(Default, CandidType, Serialize, Deserialize, Clone)]
2323
pub struct AuthenticationConfigInternetIdentity {
2424
pub derivation_origin: Option<DomainName>,
25+
pub external_alternative_origins: Option<Vec<DomainName>>,
2526
}
2627
}

src/libs/shared/src/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ where
1919
} else {
2020
version + 1
2121
}
22-
},
22+
}
2323
}
2424
}

src/satellite/satellite.did

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type AuthenticationConfig = record {
2626
};
2727
type AuthenticationConfigInternetIdentity = record {
2828
derivation_origin : opt text;
29+
external_alternative_origins : opt vec text;
2930
};
3031
type CollectionType = variant { Db; Storage };
3132
type CommitBatch = record {
@@ -124,7 +125,6 @@ type RateConfig = record { max_tokens : nat64; time_per_token_ns : nat64 };
124125
type Rule = record {
125126
max_capacity : opt nat32;
126127
memory : opt Memory;
127-
max_changes_per_user : opt nat32;
128128
updated_at : nat64;
129129
max_size : opt nat;
130130
read : Permission;
@@ -133,6 +133,7 @@ type Rule = record {
133133
mutable_permissions : opt bool;
134134
rate_config : opt RateConfig;
135135
write : Permission;
136+
max_changes_per_user : opt nat32;
136137
};
137138
type SetController = record {
138139
metadata : vec record { text; text };
@@ -151,13 +152,13 @@ type SetDoc = record {
151152
type SetRule = record {
152153
max_capacity : opt nat32;
153154
memory : opt Memory;
154-
max_changes_per_user : opt nat32;
155155
max_size : opt nat;
156156
read : Permission;
157157
version : opt nat64;
158158
mutable_permissions : opt bool;
159159
rate_config : opt RateConfig;
160160
write : Permission;
161+
max_changes_per_user : opt nat32;
161162
};
162163
type SetUserUsage = record { changes_count : nat32 };
163164
type StorageConfig = record {

0 commit comments

Comments
 (0)