1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*!
# `jwt-authorization` Request Guard for Rocket Framework

This crate provides a procedural macro to create request guards used for authorization.

See `examples`.
*/

mod panic;

use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    DeriveInput, Expr, Lit, Meta, Path, Token,
};

const CORRECT_USAGE_FOR_JWT_ATTRIBUTE: &[&str] = &[
    "#[jwt(\"key\")]",
    "#[jwt(PATH)]",
    "#[jwt(\"key\", sha2::Sha512)]",
    "#[jwt(PATH, sha2::Sha512)]",
    "#[jwt(PATH, sha2::Sha512, Header)]",
    "#[jwt(PATH, sha2::Sha512, Cookie(\"access_token\"), Header, Query(PATH))]",
];

enum Source {
    Header,
    Cookie(Expr),
    Query(Expr),
    // TODO currently it's hard to be implemented, just ignore it
    #[allow(dead_code)]
    Body(Expr),
}

impl Source {
    #[inline]
    fn as_str(&self) -> &'static str {
        match self {
            Source::Header => "header",
            Source::Cookie(_) => "cookie",
            Source::Query(_) => "query",
            Source::Body(_) => "body",
        }
    }

    #[inline]
    fn from<S: AsRef<str>>(name: S, expr: Expr) -> Option<Source> {
        let name = name.as_ref();

        match name {
            "query" => Some(Source::Query(expr)),
            "cookie" => Some(Source::Cookie(expr)),
            "body" => unimplemented!(),
            _ => None,
        }
    }

    #[inline]
    fn search<S: AsRef<str>>(sources: &[Source], name: S) -> Option<&Source> {
        let name = name.as_ref();

        sources.iter().find(|source| source.as_str() == name)
    }

    #[inline]
    fn search_cookie_get_expr(sources: &[Source]) -> Option<&Expr> {
        for source in sources.iter() {
            if let Source::Cookie(expr) = source {
                return Some(expr);
            }
        }

        None
    }
}

struct Parser2 {
    expr: Expr,
}

impl Parse for Parser2 {
    #[inline]
    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
        match input.parse::<Expr>() {
            Ok(expr) => {
                let pass = match &expr {
                    Expr::Path(_) => true,
                    Expr::Lit(lit) => matches!(lit.lit, Lit::Str(_)),
                    _ => false,
                };

                if !pass {
                    panic::attribute_incorrect_format("jwt", CORRECT_USAGE_FOR_JWT_ATTRIBUTE);
                }

                Ok(Parser2 {
                    expr,
                })
            },
            _ => panic::attribute_incorrect_format("jwt", CORRECT_USAGE_FOR_JWT_ATTRIBUTE),
        }
    }
}

struct Parser {
    key:       Expr,
    algorithm: Path,
    sources:   Vec<Source>,
}

impl Parse for Parser {
    #[inline]
    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
        let key = input.parse::<Parser2>()?.expr;

        let (algorithm, sources): (Path, Vec<Source>) = {
            if input.is_empty() {
                (syn::parse2(quote!(::sha2::Sha256))?, vec![Source::Header])
            } else {
                input.parse::<Token!(,)>()?;

                match input.parse::<Path>() {
                    Ok(p) => {
                        let mut sources = Vec::new();

                        while !input.is_empty() {
                            input.parse::<Token!(,)>()?;

                            let m = input.parse::<Meta>()?;

                            let attr_name = match m.path().get_ident() {
                                Some(ident) => ident.to_string().to_ascii_lowercase(),
                                None => {
                                    panic::attribute_incorrect_format(
                                        "jwt",
                                        CORRECT_USAGE_FOR_JWT_ATTRIBUTE,
                                    );
                                },
                            };

                            if Source::search(&sources, attr_name.as_str()).is_some() {
                                panic::duplicated_source(attr_name.as_str());
                            }

                            match m {
                                Meta::Path(_) => {
                                    if attr_name.eq_ignore_ascii_case("header") {
                                        sources.push(Source::Header);
                                    } else {
                                        panic::attribute_incorrect_format(
                                            "jwt",
                                            CORRECT_USAGE_FOR_JWT_ATTRIBUTE,
                                        );
                                    }
                                },
                                Meta::NameValue(v) => {
                                    let expr = v.value;

                                    let pass = match &expr {
                                        Expr::Path(_) => true,
                                        Expr::Lit(lit) => matches!(lit.lit, Lit::Str(_)),
                                        _ => false,
                                    };

                                    if !pass {
                                        panic::attribute_incorrect_format(
                                            "jwt",
                                            CORRECT_USAGE_FOR_JWT_ATTRIBUTE,
                                        );
                                    }

                                    match Source::from(attr_name, expr) {
                                        Some(source) => sources.push(source),
                                        None => panic::attribute_incorrect_format(
                                            "jwt",
                                            CORRECT_USAGE_FOR_JWT_ATTRIBUTE,
                                        ),
                                    }
                                },
                                Meta::List(list) => {
                                    let parsed: Parser2 = list.parse_args()?;

                                    let expr = parsed.expr;

                                    match Source::from(attr_name, expr) {
                                        Some(source) => sources.push(source),
                                        None => panic::attribute_incorrect_format(
                                            "jwt",
                                            CORRECT_USAGE_FOR_JWT_ATTRIBUTE,
                                        ),
                                    }
                                },
                            }
                        }

                        if sources.is_empty() {
                            sources.push(Source::Header);
                        }

                        (p, sources)
                    },
                    Err(_) => {
                        panic::attribute_incorrect_format("jwt", CORRECT_USAGE_FOR_JWT_ATTRIBUTE)
                    },
                }
            }
        };

        Ok(Parser {
            key,
            algorithm,
            sources,
        })
    }
}

fn derive_input_handler(ast: DeriveInput) -> TokenStream {
    for attr in ast.attrs {
        if attr.path().is_ident("jwt") {
            match attr.meta {
                Meta::List(list) => {
                    let parsed: Parser = list.parse_args().unwrap();

                    let algorithm = parsed.algorithm;
                    let key = parsed.key;
                    let sources = parsed.sources;

                    let name = &ast.ident;
                    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

                    let get_jwt_hasher = quote! {
                        #[inline]
                        pub fn get_jwt_hasher() -> &'static hmac::Hmac<#algorithm> {
                            static START: ::std::sync::Once = ::std::sync::Once::new();
                            static mut HMAC: Option<hmac::Hmac<#algorithm>> = None;

                            unsafe {
                                START.call_once(|| {
                                    use ::hmac::Hmac;
                                    use ::hmac::digest::KeyInit;

                                    HMAC = Some(Hmac::new_from_slice(unsafe {#key}.as_ref()).unwrap())
                                });

                                HMAC.as_ref().unwrap()
                            }
                        }
                    };

                    let get_jwt_token = quote! {
                        #[inline]
                        pub fn get_jwt_token(&self) -> String {
                            use ::jwt::SignWithKey;

                            let hasher = Self::get_jwt_hasher();

                            self.sign_with_key(hasher).unwrap()
                        }
                    };

                    let verify_jwt_token = quote! {
                        #[inline]
                        pub fn verify_jwt_token<S: AsRef<str>>(token: S) -> Result<Self, ::jwt::Error> {
                            use ::jwt::VerifyWithKey;

                            let token = token.as_ref();

                            let hasher = Self::get_jwt_hasher();

                            token.verify_with_key(hasher)
                        }
                    };

                    let (set_cookie, set_cookie_insecure, remove_cookie) = if let Some(expr) =
                        Source::search_cookie_get_expr(&sources)
                    {
                        let set_cookie = quote! {
                            #[inline]
                            pub fn set_cookie(&self, cookies: &::rocket::http::CookieJar) {
                                let mut cookie = ::rocket::http::Cookie::new(unsafe {#expr}, self.get_jwt_token());

                                cookie.set_secure(true);

                                cookies.add(cookie);
                            }
                        };

                        let set_cookie_insecure = quote! {
                            #[inline]
                            pub fn set_cookie_insecure(&self, cookies: &::rocket::http::CookieJar) {
                                let mut cookie = ::rocket::http::Cookie::new(unsafe {#expr}, self.get_jwt_token());

                                cookie.set_same_site(::rocket::http::SameSite::Strict);

                                cookies.add(cookie);
                            }
                        };

                        let remove_cookie = quote! {
                            #[inline]
                            pub fn remove_cookie(cookies: &::rocket::http::CookieJar) {
                                cookies.remove(::rocket::http::Cookie::named(unsafe {#expr}));
                            }
                        };

                        (set_cookie, set_cookie_insecure, remove_cookie)
                    } else {
                        (quote!(), quote!(), quote!())
                    };

                    let (from_request, from_request_cache) = {
                        let mut source_streams = Vec::with_capacity(sources.len());

                        for source in sources.iter() {
                            let source_stream = match source {
                                Source::Header => {
                                    quote! {
                                        else if let Some(authorization) = request.headers().get("authorization").next() {
                                            if let Some(token) = authorization.strip_prefix("Bearer ") {
                                                match #name::verify_jwt_token(token) {
                                                    Ok(o) => Some(o),
                                                    Err(_) => None
                                                }
                                            } else {
                                                None
                                            }
                                        }
                                    }
                                },
                                Source::Cookie(expr) => {
                                    quote! {
                                        else if let Some(token) = request.cookies().get(unsafe {#expr}) {
                                            match #name::verify_jwt_token(token.value()) {
                                                Ok(o) => Some(o),
                                                Err(_) => {
                                                    #name::remove_cookie(&request.cookies());

                                                    None
                                                }
                                            }
                                        }
                                    }
                                },
                                Source::Query(expr) => {
                                    quote! {
                                        else if let Some(token) = request.query_value(unsafe {#expr}) {
                                            let token: &str = token.unwrap();

                                            match #name::verify_jwt_token(token) {
                                                Ok(o) => Some(o),
                                                Err(_) => None
                                            }
                                        }
                                    }
                                },
                                _ => unimplemented!(),
                            };

                            source_streams.push(source_stream);
                        }

                        let from_request_body = quote! {
                            if false {
                                None
                            }
                            #(
                                #source_streams
                            )*
                            else {
                                None
                            }
                        };

                        let from_request = quote! {
                            #[rocket::async_trait]
                            impl<'r> ::rocket::request::FromRequest<'r> for #name {
                                type Error = ();

                                async fn from_request(request: &'r ::rocket::request::Request<'_>) -> ::rocket::request::Outcome<Self, Self::Error> {
                                    match #from_request_body {
                                        Some(o) => ::rocket::outcome::Outcome::Success(o),
                                        None => ::rocket::outcome::Outcome::Forward(::rocket::http::Status::Unauthorized),
                                    }
                                }
                            }
                        };

                        let from_request_cache = quote! {
                            #[rocket::async_trait]
                            impl<'r> ::rocket::request::FromRequest<'r> for &'r #name {
                                type Error = ();

                                async fn from_request(request: &'r ::rocket::request::Request<'_>) -> ::rocket::request::Outcome<Self, Self::Error> {
                                    let cache = request.local_cache(|| {
                                        #from_request_body
                                    });

                                    match cache.as_ref() {
                                        Some(o) => ::rocket::outcome::Outcome::Success(o),
                                        None => ::rocket::outcome::Outcome::Forward(::rocket::http::Status::Unauthorized),
                                    }
                                }
                            }
                        };

                        (from_request, from_request_cache)
                    };

                    let jwt_impl = quote! {
                        impl #impl_generics #name #ty_generics #where_clause {
                            #get_jwt_hasher

                            #get_jwt_token

                            #verify_jwt_token

                            #set_cookie

                            #set_cookie_insecure

                            #remove_cookie
                        }

                        #from_request

                        #from_request_cache
                    };

                    return jwt_impl.into();
                },
                _ => panic::attribute_incorrect_format("jwt", CORRECT_USAGE_FOR_JWT_ATTRIBUTE),
            }
        }
    }

    panic::jwt_not_found();
}

#[proc_macro_derive(JWT, attributes(jwt))]
pub fn jwt_derive(input: TokenStream) -> TokenStream {
    derive_input_handler(syn::parse(input).unwrap())
}