Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/routes/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use axum::{Extension, Json};
use serde::Serialize;

use crate::{features::Features, self_host};
use crate::{features::Features, routes::team_objects, self_host};

#[derive(Serialize)]
pub struct MetaResponse {
Expand All @@ -17,6 +17,8 @@ pub struct MetaResponse {
pub team_invites_enabled: bool,
pub identity_picks: bool,
pub handles_from_email: bool,
/// A client must not upload a type missing from this list; a server that omits the field predates it.
pub team_secret_types: Vec<&'static str>,
}

pub async fn get_meta(Extension(features): Extension<Features>) -> Json<MetaResponse> {
Expand All @@ -28,6 +30,7 @@ pub async fn get_meta(Extension(features): Extension<Features>) -> Json<MetaResp
team_invites_enabled: features.team_invites,
identity_picks: true,
handles_from_email: features.handles_from_email,
team_secret_types: team_objects::secret_types(),
})
}

Expand All @@ -42,6 +45,15 @@ mod tests {
assert_eq!(body["identity_picks"], serde_json::json!(true));
}

#[tokio::test]
async fn meta_advertises_the_team_secret_types_it_accepts() {
let Json(meta) = get_meta(Extension(Features::open())).await;
let body = serde_json::to_value(&meta).unwrap();
let types = body["team_secret_types"].as_array().unwrap();
assert!(types.contains(&serde_json::json!("connection_knock_sequence")));
assert!(types.contains(&serde_json::json!("connection_password")));
}

#[tokio::test]
async fn meta_reports_the_handle_switch() {
let Json(meta) = get_meta(Extension(Features { handles_from_email: true, ..Features::open() })).await;
Expand Down
30 changes: 19 additions & 11 deletions src/routes/team_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,25 @@ fn edit_permission_for_secret_type(secret_type: &str) -> Option<i64> {
secret_owner_type(secret_type).and_then(edit_permission_for_str)
}

/// Each accepted secret type with the object type that owns it; `/v1/meta` advertises the first column.
const SECRET_TYPES: &[(&str, &str)] = &[
("connection_password", "connection"),
("connection_key", "connection"),
("connection_passphrase", "connection"),
("connection_proxy_password", "connection"),
("connection_knock_sequence", "connection"),
("identity_password", "identity"),
("key_private", "key"),
("key_public", "key"),
("key_passphrase", "key"),
];

pub fn secret_types() -> Vec<&'static str> {
SECRET_TYPES.iter().map(|(secret_type, _)| *secret_type).collect()
}

fn secret_owner_type(secret_type: &str) -> Option<&'static str> {
match secret_type {
"connection_password"
| "connection_key"
| "connection_passphrase"
| "connection_proxy_password"
| "connection_knock_sequence" => Some("connection"),
"identity_password" => Some("identity"),
"key_private" | "key_public" | "key_passphrase" => Some("key"),
_ => None,
}
SECRET_TYPES.iter().find(|(known, _)| *known == secret_type).map(|(_, owner)| *owner)
}

/// Inverse of the client's `teamSecretFromLocalKey`; a colon in a connection key id would alias `key:<id>:<part>`.
Expand Down Expand Up @@ -1227,8 +1235,8 @@ mod authz_tests {
];
for (secret_type, id) in cases {
assert_eq!(canonical_secret_id("o", secret_type).as_deref(), Some(id));
assert!(secret_owner_type(secret_type).is_some());
}
assert_eq!(secret_types(), cases.map(|(secret_type, _)| secret_type));
assert_eq!(canonical_secret_id("o", "bogus"), None);
assert_eq!(canonical_secret_id("k:passphrase", "connection_key"), None);
assert_eq!(canonical_secret_id("__global__", "connection_proxy_password"), None);
Expand Down
Loading