Skip to content

Commit 6dcee88

Browse files
flomonsterKhoyo
authored andcommitted
editoast, gateway: fix x-remote-user usage
- Editoast: stop splitting the provider and the identity id - Gateway: provide the x-remote-name (match the design doc) Signed-off-by: Florian Amsallem <florian.amsallem@gmail.com>
1 parent 7993c50 commit 6dcee88

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

editoast/src/views/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ editoast_common::schemas! {
123123
pub enum Authentication {
124124
/// The issuer of the request did not provide any authentication information.
125125
Unauthenticated,
126-
/// The issuer of the request provided the 'x-remote-user' header, which contains the
127-
/// identity and name of the user.
126+
/// The issuer of the request provided the 'x-remote-user-identity' header.
128127
Authenticated(Authorizer<PgAuthDriver<BuiltinRole>>),
129128
/// The requests comes from a Core instance. All requests are considered safe.
130129
Core,
@@ -172,17 +171,23 @@ async fn authenticate(
172171
PgAuthDriver::<BuiltinRole>::new(db_pool),
173172
)));
174173
}
175-
let Some(header) = headers.get("x-remote-user") else {
174+
let Some(identity) = headers.get("x-remote-user-identity") else {
176175
if headers.contains_key("x-osrd-core") {
177176
return Ok(Authentication::Core);
178177
}
179178
return Ok(Authentication::Unauthenticated);
180179
};
181-
let (identity, name) = header
180+
let identity = identity
182181
.to_str()
183-
.expect("unexpected non-ascii characters in x-remote-user")
184-
.split_once('/') // FIXME: the gateway should inject two headers instead
185-
.expect("odd x-remote-user format");
182+
.expect("unexpected non-ascii characters in x-remote-user-identity");
183+
184+
let name = match headers.get("x-remote-user-name") {
185+
Some(name) => name
186+
.to_str()
187+
.expect("unexpected non-ascii characters in x-remote-user-name"),
188+
None => "",
189+
};
190+
186191
let authorizer = Authorizer::try_initialize(
187192
UserInfo {
188193
identity: identity.to_owned(),

gateway/src/request_modifier.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,34 @@ use actix_auth::{AuthStatus, RequestAuth, RequestAuthExt};
22
use actix_proxy::{ClientRequest, HeaderName, HeaderValue, WebsocketsRequest};
33
use actix_web::error::ErrorForbidden;
44

5-
fn check_auth(request_auth: &RequestAuth) -> Result<String, actix_web::Error> {
5+
struct UserInformation {
6+
identity: String,
7+
name: String,
8+
}
9+
10+
fn check_auth(request_auth: &RequestAuth) -> Result<UserInformation, actix_web::Error> {
611
match request_auth.status() {
712
AuthStatus::Unknown => Err(ErrorForbidden("authentication required")),
813
AuthStatus::Error(_) => Err(ErrorForbidden("authentication error")),
914
AuthStatus::Known {
1015
provider_handler,
1116
user_id,
12-
..
17+
username,
1318
} => {
1419
let provider_id = request_auth.context().get_provider_id(*provider_handler);
15-
let remote_user = format!("{provider_id}/{user_id}");
16-
Ok(remote_user)
20+
Ok(UserInformation {
21+
identity: format!("{provider_id}/{user_id}"),
22+
name: username.clone().unwrap_or_default(),
23+
})
1724
}
1825
}
1926
}
2027

2128
#[derive(Clone)]
2229
pub struct ProxyAuthAdapter;
2330

24-
static AUTH_USER_ID: HeaderName = HeaderName::from_static("x-remote-user");
31+
static AUTH_USER_ID: HeaderName = HeaderName::from_static("x-remote-user-identity");
32+
static AUTH_USER_NAME: HeaderName = HeaderName::from_static("x-remote-user-name");
2533

2634
impl actix_proxy::RequestModifier for ProxyAuthAdapter {
2735
fn modify_http_request(
@@ -34,7 +42,15 @@ impl actix_proxy::RequestModifier for ProxyAuthAdapter {
3442
};
3543
let remote_user = check_auth(&request_auth)?;
3644
let headers = back_request.headers_mut();
37-
headers.insert(AUTH_USER_ID.clone(), HeaderValue::from_str(&remote_user)?);
45+
headers.insert(
46+
AUTH_USER_ID.clone(),
47+
HeaderValue::from_str(&remote_user.identity)?,
48+
);
49+
headers.insert(
50+
AUTH_USER_NAME.clone(),
51+
HeaderValue::from_str(&remote_user.name)?,
52+
);
53+
3854
Ok(())
3955
}
4056

@@ -47,6 +63,8 @@ impl actix_proxy::RequestModifier for ProxyAuthAdapter {
4763
return Err(ErrorForbidden("missing authentication data"));
4864
};
4965
let remote_user = check_auth(&request_auth)?;
50-
Ok(back_request.set_header(AUTH_USER_ID.clone(), remote_user))
66+
Ok(back_request
67+
.set_header(AUTH_USER_ID.clone(), remote_user.identity)
68+
.set_header(AUTH_USER_NAME.clone(), remote_user.name))
5169
}
5270
}

0 commit comments

Comments
 (0)