|
| 1 | +//! This library provides convenient derive and attribute macros for custom Header and Claim |
| 2 | +//! structs for `jsonwebtoken`. |
| 3 | +//! |
| 4 | +//! Example |
| 5 | +//! ```rust |
| 6 | +//! use jsonwebtoken::Algorithm; |
| 7 | +//! use jsonwebtoken::macros::header; |
| 8 | +//! #[header] |
| 9 | +//! struct CustomJwtHeader { |
| 10 | +//! // `alg` is the only required struct field |
| 11 | +//! alg: Algorithm, |
| 12 | +//! custom_header: Option<String>, |
| 13 | +//! another_custom_header: String, |
| 14 | +//! } |
| 15 | +//! ```` |
| 16 | +extern crate proc_macro; |
| 17 | + |
| 18 | +use proc_macro::TokenStream; |
| 19 | +use quote::quote; |
| 20 | +use syn::{DeriveInput, Item, ItemStruct, parse_macro_input}; |
| 21 | + |
| 22 | +/// Convenience macro for JWT header structs |
| 23 | +/// |
| 24 | +/// Adds the following derive macros: |
| 25 | +/// ```rust |
| 26 | +/// #[derive( |
| 27 | +/// Debug, |
| 28 | +/// Clone, |
| 29 | +/// Default, |
| 30 | +/// serde::Serialize, |
| 31 | +/// serde::Deserialize |
| 32 | +/// )] |
| 33 | +/// ``` |
| 34 | +#[proc_macro_attribute] |
| 35 | +pub fn claims(_attr: TokenStream, input: TokenStream) -> TokenStream { |
| 36 | + let mut item = parse_macro_input!(input as Item); |
| 37 | + |
| 38 | + match &mut item { |
| 39 | + Item::Struct(ItemStruct { attrs, .. }) => { |
| 40 | + attrs.push( |
| 41 | + syn::parse_quote!(#[derive(Debug, Clone, Default, jsonwebtoken::serde::Serialize, jsonwebtoken::serde::Deserialize)]), |
| 42 | + ); |
| 43 | + quote!(#item).into() |
| 44 | + } |
| 45 | + _ => syn::Error::new_spanned(&item, "#[header] can only be used on structs") |
| 46 | + .to_compile_error() |
| 47 | + .into(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Convenience macro for JWT header structs |
| 52 | +/// |
| 53 | +/// Adds the following derive macros: |
| 54 | +/// ```rust |
| 55 | +/// #[derive( |
| 56 | +/// Debug, |
| 57 | +/// Clone, |
| 58 | +/// Default, |
| 59 | +/// jsonwebtoken::macros::Header, |
| 60 | +/// serde::Serialize, |
| 61 | +/// serde::Deserialize |
| 62 | +/// )] |
| 63 | +/// ``` |
| 64 | +#[proc_macro_attribute] |
| 65 | +pub fn header(_attr: TokenStream, input: TokenStream) -> TokenStream { |
| 66 | + let mut item = parse_macro_input!(input as Item); |
| 67 | + |
| 68 | + match &mut item { |
| 69 | + Item::Struct(ItemStruct { attrs, .. }) => { |
| 70 | + attrs.push(syn::parse_quote!(#[derive(Debug, Clone, Default, jsonwebtoken::serde::Serialize, jsonwebtoken::serde::Deserialize, jsonwebtoken::macros::Header)])); |
| 71 | + quote!(#item).into() |
| 72 | + } |
| 73 | + _ => syn::Error::new_spanned(&item, "#[header] can only be used on structs") |
| 74 | + .to_compile_error() |
| 75 | + .into(), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Derive macro required for custom JWT headers used with `jsonwebtoken` |
| 80 | +/// |
| 81 | +/// Requires an `alg: jsonwebtoken::Algorithm` field exists in the struct |
| 82 | +#[proc_macro_derive(Header)] |
| 83 | +pub fn derive_header(input: TokenStream) -> TokenStream { |
| 84 | + let input = parse_macro_input!(input as DeriveInput); |
| 85 | + |
| 86 | + let name = &input.ident; |
| 87 | + let generics = &input.generics; |
| 88 | + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 89 | + |
| 90 | + let expanded = quote! { |
| 91 | + impl #impl_generics ::jsonwebtoken::header::FromEncoded for #name #ty_generics #where_clause {} |
| 92 | + impl #impl_generics ::jsonwebtoken::header::Alg for #name #ty_generics #where_clause { |
| 93 | + fn alg(&self) -> &::jsonwebtoken::Algorithm { |
| 94 | + &self.alg |
| 95 | + } |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + TokenStream::from(expanded) |
| 100 | +} |
0 commit comments