Skip to content

Commit e6bef71

Browse files
committed
Add configuration for session limiting
1 parent abc9532 commit e6bef71

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

crates/cli/src/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use mas_config::{
1313
PolicyConfig, TemplatesConfig,
1414
};
1515
use mas_context::LogContext;
16-
use mas_data_model::{SessionExpirationConfig, SiteConfig};
16+
use mas_data_model::{SessionExpirationConfig, SessionLimitConfig, SiteConfig};
1717
use mas_email::{MailTransport, Mailer};
1818
use mas_handlers::passwords::PasswordManager;
1919
use mas_matrix::{HomeserverConnection, ReadOnlyHomeserverConnection};
@@ -225,6 +225,13 @@ pub fn site_config_from_config(
225225
session_expiration,
226226
login_with_email_allowed: account_config.login_with_email_allowed,
227227
plan_management_iframe_uri: experimental_config.plan_management_iframe_uri.clone(),
228+
session_limit: experimental_config
229+
.session_limit
230+
.as_ref()
231+
.map(|c| SessionLimitConfig {
232+
soft_limit: c.soft_limit,
233+
hard_limit: c.hard_limit,
234+
}),
228235
})
229236
}
230237

crates/config/src/sections/experimental.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pub struct ExperimentalConfig {
8181
/// validation.
8282
#[serde(skip_serializing_if = "Option::is_none")]
8383
pub plan_management_iframe_uri: Option<String>,
84+
85+
/// Experimental feature to limit the number of application sessions per
86+
/// user.
87+
///
88+
/// Disabled by default.
89+
#[serde(skip_serializing_if = "Option::is_none")]
90+
pub session_limit: Option<SessionLimitConfig>,
8491
}
8592

8693
impl Default for ExperimentalConfig {
@@ -90,6 +97,7 @@ impl Default for ExperimentalConfig {
9097
compat_token_ttl: default_token_ttl(),
9198
inactive_session_expiration: None,
9299
plan_management_iframe_uri: None,
100+
session_limit: None,
93101
}
94102
}
95103
}
@@ -106,3 +114,10 @@ impl ExperimentalConfig {
106114
impl ConfigurationSection for ExperimentalConfig {
107115
const PATH: Option<&'static str> = Some("experimental");
108116
}
117+
118+
/// Configuration options for the inactive session expiration feature
119+
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
120+
pub struct SessionLimitConfig {
121+
pub soft_limit: u64,
122+
pub hard_limit: u64,
123+
}

crates/data-model/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ pub use self::{
3939
DeviceCodeGrantState, InvalidRedirectUriError, JwksOrJwksUri, Pkce, Session, SessionState,
4040
},
4141
policy_data::PolicyData,
42-
site_config::{CaptchaConfig, CaptchaService, SessionExpirationConfig, SiteConfig},
42+
site_config::{
43+
CaptchaConfig, CaptchaService, SessionLimitConfig, SessionExpirationConfig, SiteConfig,
44+
},
4345
tokens::{
4446
AccessToken, AccessTokenState, RefreshToken, RefreshTokenState, TokenFormatError, TokenType,
4547
},

crates/data-model/src/site_config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Please see LICENSE files in the repository root for full details.
66

77
use chrono::Duration;
8+
use serde::Serialize;
89
use url::Url;
910

1011
/// Which Captcha service is being used
@@ -36,6 +37,12 @@ pub struct SessionExpirationConfig {
3637
pub compat_session_inactivity_ttl: Option<Duration>,
3738
}
3839

40+
#[derive(Serialize, Debug, Clone)]
41+
pub struct SessionLimitConfig {
42+
pub soft_limit: u64,
43+
pub hard_limit: u64,
44+
}
45+
3946
/// Random site configuration we want accessible in various places.
4047
#[allow(clippy::struct_excessive_bools)]
4148
#[derive(Debug, Clone)]
@@ -99,4 +106,7 @@ pub struct SiteConfig {
99106

100107
/// The iframe URL to show in the plan tab of the UI
101108
pub plan_management_iframe_uri: Option<String>,
109+
110+
/// Limits on the number of application sessions that each user can have
111+
pub session_limit: Option<SessionLimitConfig>,
102112
}

docs/config.schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,14 @@
26592659
"plan_management_iframe_uri": {
26602660
"description": "Experimental feature to show a plan management tab and iframe. This value is passed through \"as is\" to the client without any validation.",
26612661
"type": "string"
2662+
},
2663+
"session_limit": {
2664+
"description": "Experimental feature to limit the number of application sessions per user.\n\nDisabled by default.",
2665+
"allOf": [
2666+
{
2667+
"$ref": "#/definitions/SessionLimitConfig"
2668+
}
2669+
]
26622670
}
26632671
}
26642672
},
@@ -2692,6 +2700,26 @@
26922700
"type": "boolean"
26932701
}
26942702
}
2703+
},
2704+
"SessionLimitConfig": {
2705+
"description": "Configuration options for the inactive session expiration feature",
2706+
"type": "object",
2707+
"required": [
2708+
"hard_limit",
2709+
"soft_limit"
2710+
],
2711+
"properties": {
2712+
"soft_limit": {
2713+
"type": "integer",
2714+
"format": "uint64",
2715+
"minimum": 0.0
2716+
},
2717+
"hard_limit": {
2718+
"type": "integer",
2719+
"format": "uint64",
2720+
"minimum": 0.0
2721+
}
2722+
}
26952723
}
26962724
}
26972725
}

0 commit comments

Comments
 (0)