Skip to content

Commit d6b3e6c

Browse files
committed
Pass an input object to the policy evaluation instead of multiple arguments
1 parent 353b290 commit d6b3e6c

File tree

10 files changed

+112
-151
lines changed

10 files changed

+112
-151
lines changed

crates/handlers/src/graphql/mutations/user_email.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,11 @@ impl UserEmailMutations {
424424

425425
if !skip_policy_check {
426426
let mut policy = state.policy().await?;
427-
let res = policy.evaluate_email(&input.email).await?;
427+
let res = policy
428+
.evaluate_email(mas_policy::EmailInput {
429+
email: &input.email,
430+
})
431+
.await?;
428432
if !res.valid() {
429433
return Ok(AddEmailPayload::Denied {
430434
violations: res.violations,
@@ -610,7 +614,11 @@ impl UserEmailMutations {
610614

611615
// Check if the email address is allowed by the policy
612616
let mut policy = state.policy().await?;
613-
let res = policy.evaluate_email(&input.email).await?;
617+
let res = policy
618+
.evaluate_email(mas_policy::EmailInput {
619+
email: &input.email,
620+
})
621+
.await?;
614622
if !res.valid() {
615623
return Ok(StartEmailAuthenticationPayload::Denied {
616624
violations: res.violations,

crates/handlers/src/oauth2/authorization/complete.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ pub(crate) async fn complete(
226226

227227
// Run through the policy
228228
let res = policy
229-
.evaluate_authorization_grant(&grant, client, &browser_session.user)
229+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
230+
user: Some(&browser_session.user),
231+
client,
232+
scope: &grant.scope,
233+
grant_type: mas_policy::GrantType::AuthorizationCode,
234+
})
230235
.await?;
231236

232237
if !res.valid() {

crates/handlers/src/oauth2/consent.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ pub(crate) async fn get(
111111
let (csrf_token, cookie_jar) = cookie_jar.csrf_token(&clock, &mut rng);
112112

113113
let res = policy
114-
.evaluate_authorization_grant(&grant, &client, &session.user)
114+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
115+
user: Some(&session.user),
116+
client: &client,
117+
scope: &grant.scope,
118+
grant_type: mas_policy::GrantType::AuthorizationCode,
119+
})
115120
.await?;
116121

117122
if res.valid() {
@@ -185,7 +190,12 @@ pub(crate) async fn post(
185190
.ok_or(RouteError::NoSuchClient)?;
186191

187192
let res = policy
188-
.evaluate_authorization_grant(&grant, &client, &session.user)
193+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
194+
user: Some(&session.user),
195+
client: &client,
196+
scope: &grant.scope,
197+
grant_type: mas_policy::GrantType::AuthorizationCode,
198+
})
189199
.await?;
190200

191201
if !res.valid() {

crates/handlers/src/oauth2/device/consent.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ pub(crate) async fn get(
8282

8383
// Evaluate the policy
8484
let res = policy
85-
.evaluate_device_code_grant(&grant, &client, &session.user)
85+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
86+
grant_type: mas_policy::GrantType::DeviceCode,
87+
client: &client,
88+
scope: &grant.scope,
89+
user: Some(&session.user),
90+
})
8691
.await?;
8792
if !res.valid() {
8893
warn!(violation = ?res, "Device code grant for client {} denied by policy", client.id);
@@ -157,7 +162,12 @@ pub(crate) async fn post(
157162

158163
// Evaluate the policy
159164
let res = policy
160-
.evaluate_device_code_grant(&grant, &client, &session.user)
165+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
166+
grant_type: mas_policy::GrantType::DeviceCode,
167+
client: &client,
168+
scope: &grant.scope,
169+
user: Some(&session.user),
170+
})
161171
.await?;
162172
if !res.valid() {
163173
warn!(violation = ?res, "Device code grant for client {} denied by policy", client.id);

crates/handlers/src/oauth2/registration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ pub(crate) async fn post(
244244
}
245245
}
246246

247-
let res = policy.evaluate_client_registration(&metadata).await?;
247+
let res = policy
248+
.evaluate_client_registration(mas_policy::ClientRegistrationInput {
249+
client_metadata: &metadata,
250+
})
251+
.await?;
248252
if !res.valid() {
249253
return Err(RouteError::PolicyDenied(res.violations));
250254
}

crates/handlers/src/oauth2/token.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,12 @@ async fn client_credentials_grant(
676676

677677
// Make the request go through the policy engine
678678
let res = policy
679-
.evaluate_client_credentials_grant(&scope, client)
679+
.evaluate_authorization_grant(mas_policy::AuthorizationGrantInput {
680+
user: None,
681+
client,
682+
scope: &scope,
683+
grant_type: mas_policy::GrantType::ClientCredentials,
684+
})
680685
.await?;
681686
if !res.valid() {
682687
return Err(RouteError::DeniedByPolicy(res.violations));

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ pub(crate) async fn get(
441441
}
442442

443443
let res = policy
444-
.evaluate_upstream_oauth_register(&localpart, None)
444+
.evaluate_register(mas_policy::RegisterInput {
445+
registration_method: mas_policy::RegistrationMethod::UpstreamOAuth2,
446+
username: &localpart,
447+
email: None,
448+
})
445449
.await?;
446450

447451
if res.valid() {
@@ -752,8 +756,13 @@ pub(crate) async fn post(
752756

753757
// Policy check
754758
let res = policy
755-
.evaluate_upstream_oauth_register(&username, email.as_deref())
759+
.evaluate_register(mas_policy::RegisterInput {
760+
registration_method: mas_policy::RegistrationMethod::UpstreamOAuth2,
761+
username: &username,
762+
email: email.as_deref(),
763+
})
756764
.await?;
765+
757766
if !res.valid() {
758767
let form_state =
759768
res.violations

crates/handlers/src/views/register/password.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ pub(crate) async fn post(
233233
}
234234

235235
let res = policy
236-
.evaluate_register(&form.username, &form.email)
236+
.evaluate_register(mas_policy::RegisterInput {
237+
registration_method: mas_policy::RegistrationMethod::Password,
238+
username: &form.username,
239+
email: Some(&form.email),
240+
})
237241
.await?;
238242

239243
for violation in res.violations {

0 commit comments

Comments
 (0)