Skip to content

Commit 5cb0814

Browse files
Sh099078Khoyo
andcommitted
editoast: merge middleware authorization extensions
Remove several middleware extensions that were used for authorization. Replace them on call sites by the updated `State` middleware extension that should contain all the information required. Add some quality of life methods for `State` to make its usage simpler. Co-authored-by: Younes Khoudli <younes.khoudli@epita.fr> Signed-off-by: Younes Khoudli <younes.khoudli@epita.fr> Signed-off-by: Loup Federico <16464925+Sh099078@users.noreply.github.com>
1 parent 2e09376 commit 5cb0814

6 files changed

Lines changed: 184 additions & 282 deletions

File tree

editoast/src/authentication.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ pub enum Mode {
2323
impersonator_name: String,
2424
impersonated_identity: String,
2525
},
26-
Skip {
27-
identity: Option<String>,
28-
name: Option<String>,
29-
},
26+
Skip,
3027
}
3128

3229
#[derive(Debug, Default)]
@@ -43,10 +40,11 @@ impl Mode {
4340
let authn = match params {
4441
AuthenticationParameters {
4542
skip: true,
46-
identity,
47-
name,
48-
..
49-
} => Self::Skip { identity, name },
43+
identity: None,
44+
name: None,
45+
impersonate: None,
46+
} => Self::Skip,
47+
AuthenticationParameters { skip: true, .. } => return Err(params),
5048
AuthenticationParameters {
5149
impersonate: Some(impersonated_identity),
5250
identity: Some(impersonator_identity),
@@ -76,19 +74,26 @@ impl Mode {
7674

7775
#[derive(Debug, Clone)]
7876
pub enum State {
79-
Skip {
80-
#[expect(unused)]
81-
user: Option<authz::User>,
82-
#[expect(unused)]
83-
roles: Vec<Role>,
84-
},
85-
Authenticated {
86-
user: authz::User,
87-
roles: Vec<Role>,
88-
},
77+
Skip,
78+
Authenticated { user: authz::User, roles: Vec<Role> },
8979
}
9080

9181
impl State {
82+
pub fn user(&self) -> Option<authz::User> {
83+
match self {
84+
State::Skip => None,
85+
State::Authenticated { user, .. } => Some(*user),
86+
}
87+
}
88+
89+
pub fn roles(&self) -> &[Role] {
90+
static EMPTY_ROLES: [Role; 0] = [];
91+
match self {
92+
State::Skip => &EMPTY_ROLES,
93+
State::Authenticated { roles, .. } => roles,
94+
}
95+
}
96+
9297
pub fn authorizer<'a>(
9398
&self,
9499
openfga: &'a fga::Client,
@@ -98,7 +103,7 @@ impl State {
98103
State::Authenticated { user, roles } => {
99104
itertools::Either::Left(UserAuthorizer::new(*user, roles.clone(), openfga, conn))
100105
}
101-
State::Skip { .. } => itertools::Either::Right(SystemAuthorizer {
106+
State::Skip => itertools::Either::Right(SystemAuthorizer {
102107
openfga,
103108
conn: conn.clone(),
104109
}),
@@ -121,7 +126,7 @@ impl State {
121126
let user = user.expect("providing the request user is required when Mode::Authenticated or Mode::Impersonating");
122127
Ok(State::Authenticated { user, roles })
123128
}
124-
Mode::Skip { .. } => Ok(State::Skip { user, roles }),
129+
Mode::Skip => Ok(State::Skip),
125130
}
126131
}
127132
}

0 commit comments

Comments
 (0)