api: Ignore SendAccessToken::Always on non-client-api endpoints
This commit is contained in:
parent
3bb4a3e31e
commit
d467eaf621
@ -1,5 +1,6 @@
|
|||||||
//! Details of the `metadata` section of the procedural macro.
|
//! Details of the `metadata` section of the procedural macro.
|
||||||
|
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{
|
use syn::{
|
||||||
braced,
|
braced,
|
||||||
@ -17,6 +18,11 @@ mod kw {
|
|||||||
syn::custom_keyword!(path);
|
syn::custom_keyword!(path);
|
||||||
syn::custom_keyword!(rate_limited);
|
syn::custom_keyword!(rate_limited);
|
||||||
syn::custom_keyword!(authentication);
|
syn::custom_keyword!(authentication);
|
||||||
|
|
||||||
|
syn::custom_keyword!(None);
|
||||||
|
syn::custom_keyword!(AccessToken);
|
||||||
|
syn::custom_keyword!(ServerSignatures);
|
||||||
|
syn::custom_keyword!(QueryOnlyAccessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A field of Metadata that contains attribute macros
|
/// A field of Metadata that contains attribute macros
|
||||||
@ -46,7 +52,7 @@ pub struct Metadata {
|
|||||||
pub rate_limited: Vec<MetadataField<LitBool>>,
|
pub rate_limited: Vec<MetadataField<LitBool>>,
|
||||||
|
|
||||||
/// The authentication field.
|
/// The authentication field.
|
||||||
pub authentication: Vec<MetadataField<Ident>>,
|
pub authentication: Vec<MetadataField<AuthScheme>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_field<T: ToTokens>(field: &mut Option<T>, value: T) -> syn::Result<()> {
|
fn set_field<T: ToTokens>(field: &mut Option<T>, value: T) -> syn::Result<()> {
|
||||||
@ -118,6 +124,43 @@ impl Parse for Metadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub enum AuthScheme {
|
||||||
|
None(kw::None),
|
||||||
|
AccessToken(kw::AccessToken),
|
||||||
|
ServerSignatures(kw::ServerSignatures),
|
||||||
|
QueryOnlyAccessToken(kw::QueryOnlyAccessToken),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse for AuthScheme {
|
||||||
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||||
|
let lookahead = input.lookahead1();
|
||||||
|
|
||||||
|
if lookahead.peek(kw::None) {
|
||||||
|
input.parse().map(Self::None)
|
||||||
|
} else if lookahead.peek(kw::AccessToken) {
|
||||||
|
input.parse().map(Self::AccessToken)
|
||||||
|
} else if lookahead.peek(kw::ServerSignatures) {
|
||||||
|
input.parse().map(Self::ServerSignatures)
|
||||||
|
} else if lookahead.peek(kw::QueryOnlyAccessToken) {
|
||||||
|
input.parse().map(Self::QueryOnlyAccessToken)
|
||||||
|
} else {
|
||||||
|
Err(lookahead.error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToTokens for AuthScheme {
|
||||||
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
|
match self {
|
||||||
|
AuthScheme::None(kw) => kw.to_tokens(tokens),
|
||||||
|
AuthScheme::AccessToken(kw) => kw.to_tokens(tokens),
|
||||||
|
AuthScheme::ServerSignatures(kw) => kw.to_tokens(tokens),
|
||||||
|
AuthScheme::QueryOnlyAccessToken(kw) => kw.to_tokens(tokens),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum Field {
|
enum Field {
|
||||||
Description,
|
Description,
|
||||||
Method,
|
Method,
|
||||||
@ -161,7 +204,7 @@ enum FieldValue {
|
|||||||
Name(LitStr),
|
Name(LitStr),
|
||||||
Path(LitStr),
|
Path(LitStr),
|
||||||
RateLimited(LitBool, Vec<Attribute>),
|
RateLimited(LitBool, Vec<Attribute>),
|
||||||
Authentication(Ident, Vec<Attribute>),
|
Authentication(AuthScheme, Vec<Attribute>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for FieldValue {
|
impl Parse for FieldValue {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use proc_macro2::{Ident, Span, TokenStream};
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
use super::{Metadata, Request, RequestField, RequestFieldKind};
|
use super::{Request, RequestField, RequestFieldKind};
|
||||||
|
use crate::api::metadata::{AuthScheme, Metadata};
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub fn expand_incoming(
|
pub fn expand_incoming(
|
||||||
@ -193,7 +194,7 @@ impl Request {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let non_auth_impls = metadata.authentication.iter().filter_map(|auth| {
|
let non_auth_impls = metadata.authentication.iter().filter_map(|auth| {
|
||||||
(auth.value == "None").then(|| {
|
matches!(auth.value, AuthScheme::None(_)).then(|| {
|
||||||
let attrs = &auth.attrs;
|
let attrs = &auth.attrs;
|
||||||
quote! {
|
quote! {
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use proc_macro2::{Ident, Span, TokenStream};
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
use super::{Metadata, Request, RequestField, RequestFieldKind};
|
use crate::api::metadata::{AuthScheme, Metadata};
|
||||||
|
|
||||||
|
use super::{Request, RequestField, RequestFieldKind};
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub fn expand_outgoing(
|
pub fn expand_outgoing(
|
||||||
@ -133,8 +135,8 @@ impl Request {
|
|||||||
for auth in &metadata.authentication {
|
for auth in &metadata.authentication {
|
||||||
let attrs = &auth.attrs;
|
let attrs = &auth.attrs;
|
||||||
|
|
||||||
let hdr_kv = if auth.value == "AccessToken" {
|
let hdr_kv = match auth.value {
|
||||||
quote! {
|
AuthScheme::AccessToken(_) => quote! {
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
req_headers.insert(
|
req_headers.insert(
|
||||||
#http::header::AUTHORIZATION,
|
#http::header::AUTHORIZATION,
|
||||||
@ -145,9 +147,8 @@ impl Request {
|
|||||||
.ok_or(#ruma_api::error::IntoHttpError::NeedsAuthentication)?,
|
.ok_or(#ruma_api::error::IntoHttpError::NeedsAuthentication)?,
|
||||||
))?,
|
))?,
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
} else {
|
AuthScheme::None(_) => quote! {
|
||||||
quote! {
|
|
||||||
if let Some(access_token) = access_token.get_not_required_for_endpoint() {
|
if let Some(access_token) = access_token.get_not_required_for_endpoint() {
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
req_headers.insert(
|
req_headers.insert(
|
||||||
@ -157,7 +158,8 @@ impl Request {
|
|||||||
)?
|
)?
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
AuthScheme::QueryOnlyAccessToken(_) | AuthScheme::ServerSignatures(_) => quote! {},
|
||||||
};
|
};
|
||||||
|
|
||||||
header_kvs.extend(hdr_kv);
|
header_kvs.extend(hdr_kv);
|
||||||
@ -184,7 +186,7 @@ impl Request {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let non_auth_impls = metadata.authentication.iter().filter_map(|auth| {
|
let non_auth_impls = metadata.authentication.iter().filter_map(|auth| {
|
||||||
(auth.value == "None").then(|| {
|
matches!(auth.value, AuthScheme::None(_)).then(|| {
|
||||||
let attrs = &auth.attrs;
|
let attrs = &auth.attrs;
|
||||||
quote! {
|
quote! {
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user