Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/fix-people-chat-scope-mapping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Map People service to `contacts` and `directory` scope prefixes so `gws auth login -s people` includes the required OAuth scopes
60 changes: 51 additions & 9 deletions src/auth_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,23 @@ fn scope_matches_service(scope_url: &str, services: &HashSet<String>) -> bool {
let prefix = short.split('.').next().unwrap_or(short);

services.iter().any(|svc| {
let mapped_svc = map_service_to_scope_prefix(svc);
prefix == mapped_svc || short.starts_with(&format!("{mapped_svc}."))
let prefixes = map_service_to_scope_prefixes(svc);
prefixes
.iter()
.any(|mapped| prefix == *mapped || short.starts_with(&format!("{mapped}.")))
})
}

/// Map user-friendly service names to their OAuth scope prefixes.
fn map_service_to_scope_prefix(service: &str) -> &str {
/// Some services map to multiple scope prefixes (e.g. People API uses
/// both `contacts` and `directory` scopes).
fn map_service_to_scope_prefixes(service: &str) -> Vec<&str> {
match service {
"sheets" => "spreadsheets",
"slides" => "presentations",
"docs" => "documents",
s => s,
"sheets" => vec!["spreadsheets"],
"slides" => vec!["presentations"],
"docs" => vec!["documents"],
"people" => vec!["contacts", "directory"],
s => vec![s],
}
}

Expand Down Expand Up @@ -1344,8 +1349,11 @@ fn find_unmatched_services(scopes: &[String], services: &HashSet<String>) -> Has
if matched_services.contains(service) {
continue;
}
let mapped_svc = map_service_to_scope_prefix(service);
if prefix == mapped_svc || short.starts_with(&format!("{mapped_svc}.")) {
let prefixes = map_service_to_scope_prefixes(service);
if prefixes
.iter()
.any(|mapped| prefix == *mapped || short.starts_with(&format!("{mapped}.")))
{
matched_services.insert(service.clone());
}
}
Expand Down Expand Up @@ -1926,6 +1934,40 @@ mod tests {
));
}

#[test]
fn scope_matches_service_people_contacts() {
let services: HashSet<String> = ["people"].iter().map(|s| s.to_string()).collect();
assert!(scope_matches_service(
"https://www.googleapis.com/auth/contacts",
&services
));
assert!(scope_matches_service(
"https://www.googleapis.com/auth/contacts.readonly",
&services
));
assert!(scope_matches_service(
"https://www.googleapis.com/auth/contacts.other.readonly",
&services
));
assert!(scope_matches_service(
"https://www.googleapis.com/auth/directory.readonly",
&services
));
}

#[test]
fn scope_matches_service_chat() {
let services: HashSet<String> = ["chat"].iter().map(|s| s.to_string()).collect();
assert!(scope_matches_service(
"https://www.googleapis.com/auth/chat.spaces",
&services
));
assert!(scope_matches_service(
"https://www.googleapis.com/auth/chat.messages",
&services
));
}

// ── services filter integration tests ────────────────────────────────

#[test]
Expand Down
Loading