| 228 | } |
| 229 | |
| 230 | fn expand(args: &AuthzArgs, item: &mut ItemImpl) -> Result<proc_macro2::TokenStream> { |
| 231 | // `trait_ident` is still called for its validation side effect: the |
| 232 | // macro must be applied to a trait impl (`impl Trait for Type`). |
| 233 | let trait_ident = trait_ident(item)?; |
| 234 | let const_name = Ident::new(AUTH_METADATA_CONST, trait_ident.span()); |
| 235 | let service = args.service.value(); |
| 236 | |
| 237 | let mut entries: Vec<proc_macro2::TokenStream> = Vec::new(); |
| 238 | let mut seen_paths: Vec<String> = Vec::new(); |
| 239 | |
| 240 | for impl_item in &mut item.items { |
| 241 | let ImplItem::Fn(method) = impl_item else { |
| 242 | continue; |
| 243 | }; |
| 244 | |
| 245 | let mut found: Option<Meta> = None; |
| 246 | let mut kept = Vec::with_capacity(method.attrs.len()); |
| 247 | for attr in method.attrs.drain(..) { |
| 248 | if attr.path().is_ident("rpc_auth") { |
| 249 | if found.is_some() { |
| 250 | return Err(Error::new( |
| 251 | attr.span(), |
| 252 | "duplicate `#[rpc_auth]` on the same method", |
| 253 | )); |
| 254 | } |
| 255 | found = Some(attr.meta); |
| 256 | } else { |
| 257 | kept.push(attr); |
| 258 | } |
| 259 | } |
| 260 | method.attrs = kept; |
| 261 | |
| 262 | let Some(meta) = found else { |
| 263 | return Err(Error::new( |
| 264 | method.sig.ident.span(), |
| 265 | "method is missing `#[rpc_auth(...)]`; every RPC method must declare its auth metadata", |
| 266 | )); |
| 267 | }; |
| 268 | |
| 269 | let auth = RpcAuth::parse(&meta)?; |
| 270 | let method_path = format!( |
| 271 | "/{}/{}", |
| 272 | service, |
| 273 | snake_to_pascal(&method.sig.ident.to_string()) |
| 274 | ); |
| 275 | |
| 276 | if seen_paths.contains(&method_path) { |
| 277 | return Err(Error::new( |
| 278 | method.sig.ident.span(), |
| 279 | format!("duplicate gRPC method path `{method_path}`"), |
| 280 | )); |
| 281 | } |
| 282 | seen_paths.push(method_path.clone()); |
| 283 | |
| 284 | let mode_tokens = match auth.mode { |
| 285 | AuthMode::Unauthenticated => { |
| 286 | quote! { crate::auth::method_authz::AuthMode::Unauthenticated } |
| 287 | } |