jaws 1.0.4

JSON Algorithms and Web Signing
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! JWT token formats.
//!
//! This module defines the token formats supported by this crate.

use std::fmt::Write;

use bytes::Bytes;
use serde::de::DeserializeOwned;
use serde::{ser, Deserialize, Serialize};
use signature::SignatureEncoding;

use super::{HasSignature, MaybeSigned, Unverified};
use super::{Payload, Token};
use crate::algorithms::SignatureBytes;
use crate::base64data::{Base64JSON, Base64Signature, DecodeError};
use crate::jose::{Header, HeaderState, RenderedHeader};

/// A token format that serializes the token as a compact string.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Compact;

impl Compact {
    /// Creates a new `Compact` token format.
    ///
    /// Since no extra information is needed, the header and payload
    /// are stored in the token itself.
    pub fn new() -> Compact {
        Compact
    }
}

/// A token format that serializes the token as a single JSON object,
/// with the unprotected header as a top-level field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FlatUnprotected<U> {
    unprotected: U,
}

impl<U> FlatUnprotected<U> {
    /// Creates a new `Flat` token format with the given unprotected header.
    pub fn new(unprotected: U) -> Self {
        Self { unprotected }
    }

    /// Returns a reference to the unprotected header.
    pub fn unprotected(&self) -> &U {
        &self.unprotected
    }

    /// Returns a mutable reference to the unprotected header.
    pub fn unprotected_mut(&mut self) -> &mut U {
        &mut self.unprotected
    }
}

/// A token format that serializes the token as a single JSON object.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Flat;

/// Error returned when a token cannot be formatted as a string.
///
/// This error can occur when serializing the header or payload, or
/// when writing to the writer.
#[derive(Debug, thiserror::Error)]
pub enum TokenFormattingError {
    /// An error occured while serailizing the header or payload.
    /// This indicates that something is probably wrong with your
    /// custom header types.
    #[error("serializing: {0}")]
    Serialization(#[from] serde_json::Error),

    /// An error occured while writing to the writer.
    #[error("io: {0}")]
    IO(#[from] std::fmt::Error),
}

/// Error returned when a token cannot be parsed from a string.
///
/// This error can occur when deserializing the header or payload
#[derive(Debug, thiserror::Error)]
pub enum TokenParseError {
    /// Unable to find the header in the raw data.
    #[error("missing header")]
    MissingHeader,

    /// Unable to find the payload in the raw data.
    #[error("missing payload")]
    MissingPayload,

    /// Unable to find the signature in the raw data.
    #[error("missing signature")]
    MissingSignature,

    /// An error occured while decoding the data as UTF-8.
    #[error(transparent)]
    Utf8(#[from] std::str::Utf8Error),

    /// An error occured while decoding the base64 data.
    #[error(transparent)]
    Base64(#[from] DecodeError),

    /// An error occured while deserializing the header.
    #[error(transparent)]
    Json(#[from] serde_json::Error),

    /// An error occured while deserializing the header.
    #[error("unexpected JSON value for {0}: {1}")]
    UnexpectedJSONValue(&'static str, serde_json::Value),
}

/// Trait for token formats, defining how they are serialized.
pub trait TokenFormat {
    /// Render the token to the given writer.
    fn render<S>(
        &self,
        writer: &mut impl Write,
        token: &Token<impl Serialize, S, Self>,
    ) -> Result<(), TokenFormattingError>
    where
        Self: Sized,
        S: HasSignature,
        <S as MaybeSigned>::Header: Serialize,
        <S as MaybeSigned>::HeaderState: HeaderState;

    /// Parse the token from a slice.
    fn parse<P, H>(data: Bytes) -> Result<Token<P, Unverified<H>, Self>, TokenParseError>
    where
        P: DeserializeOwned,
        H: DeserializeOwned,
        Self: Sized;
}

impl TokenFormat for Compact {
    fn render<S>(
        &self,
        writer: &mut impl Write,
        token: &Token<impl Serialize, S, Self>,
    ) -> Result<(), TokenFormattingError>
    where
        Self: Sized,
        S: HasSignature,
        <S as MaybeSigned>::Header: Serialize,
        <S as MaybeSigned>::HeaderState: HeaderState,
    {
        let header = Base64JSON(&token.state.header()).serialized_value()?;
        let payload = token.payload.serialized_value()?;
        let signature = Base64Signature(SignatureBytes::from(
            token.state.signature().to_bytes().as_ref(),
        ))
        .serialized_value()?;
        write!(writer, "{}.{}.{}", header, payload, signature)?;
        Ok(())
    }

    fn parse<P, H>(data: Bytes) -> Result<Token<P, Unverified<H>, Self>, TokenParseError>
    where
        P: DeserializeOwned,
        H: DeserializeOwned,
        Self: Sized,
    {
        let mut parts = data.splitn(3, |&b| b == b'.');
        let header = {
            let b64_header =
                std::str::from_utf8(parts.next().ok_or(TokenParseError::MissingHeader)?)?;

            let wrapped_header = Base64JSON::<Header<H, RenderedHeader>>::parse(b64_header)?;
            let mut header = wrapped_header.data;
            header.state.raw = wrapped_header.bytes;
            header
        };

        let (payload, raw_payload) = {
            let b64_payload: &str =
                std::str::from_utf8(parts.next().ok_or(TokenParseError::MissingPayload)?)?;
            let payload: Payload<P> = Payload::parse(b64_payload)?;
            let raw_payload: Vec<u8> = b64_payload.as_bytes().into();
            (payload, raw_payload)
        };

        let signature = {
            let signature = parts.next().ok_or(TokenParseError::MissingSignature)?;
            let signature: Base64Signature<SignatureBytes> =
                Base64Signature::parse(std::str::from_utf8(signature)?)?;
            signature
        };

        Ok(Token {
            payload,
            state: Unverified {
                header,
                signature,
                payload: raw_payload.into(),
            },
            fmt: Compact,
        })
    }
}

#[derive(Debug, Serialize)]
struct FlatToken<'t, P, U> {
    payload: &'t Payload<P>,
    protected: String,
    unprotected: &'t U,
    signature: String,
}

impl<U> TokenFormat for FlatUnprotected<U>
where
    U: Serialize + DeserializeOwned,
{
    fn render<S>(
        &self,
        writer: &mut impl Write,
        token: &Token<impl Serialize, S, Self>,
    ) -> Result<(), TokenFormattingError>
    where
        Self: Sized,
        S: HasSignature,
        <S as MaybeSigned>::Header: Serialize,
        <S as MaybeSigned>::HeaderState: HeaderState,
    {
        let header = Base64JSON(token.state.header()).serialized_value()?;
        let signature = Base64Signature(SignatureBytes::from(
            token.state.signature().to_bytes().as_ref(),
        ))
        .serialized_value()?;

        let flat = FlatToken {
            payload: &token.payload,
            protected: header,
            unprotected: &self.unprotected,
            signature,
        };

        let data = serde_json::to_string(&flat)?;
        write!(writer, "{}", data)?;

        Ok(())
    }

    fn parse<P, H>(data: Bytes) -> Result<Token<P, Unverified<H>, Self>, TokenParseError>
    where
        P: DeserializeOwned,
        H: DeserializeOwned,
        Self: Sized,
    {
        let value: serde_json::Value = serde_json::from_slice(&data)?;
        let serde_json::Value::Object(mut object): serde_json::Value = value else {
            return Err(TokenParseError::UnexpectedJSONValue("token", value));
        };

        let Token { payload, state, .. } = parse_flat_common_values(&mut object)?;

        let unprotected = {
            let unprotected = object
                .remove("unprotected")
                .ok_or(TokenParseError::MissingHeader)?;
            let unprotected: U = serde_json::from_value(unprotected)?;
            unprotected
        };

        Ok(Token {
            payload,
            state,
            fmt: FlatUnprotected { unprotected },
        })
    }
}

impl<P, S, U> Serialize for Token<P, S, FlatUnprotected<U>>
where
    S: HasSignature,
    <S as MaybeSigned>::Header: Serialize,
    <S as MaybeSigned>::HeaderState: HeaderState,
    U: Serialize + DeserializeOwned,
    P: Serialize,
{
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: ser::Serializer,
    {
        let header = Base64JSON(self.state.header())
            .serialized_value()
            .map_err(ser::Error::custom)?;
        let signature = Base64Signature(SignatureBytes::from(
            self.state.signature().to_bytes().as_ref(),
        ))
        .serialized_value()
        .map_err(ser::Error::custom)?;

        let flat = FlatToken {
            payload: &self.payload,
            protected: header,
            unprotected: &self.fmt.unprotected,
            signature,
        };

        flat.serialize(serializer)
    }
}

#[derive(Debug, Serialize)]
struct FlatSimpleToken<'t, P> {
    payload: &'t Payload<P>,
    protected: String,
    signature: String,
}

impl TokenFormat for Flat {
    fn render<S>(
        &self,
        writer: &mut impl Write,
        token: &Token<impl Serialize, S, Self>,
    ) -> Result<(), TokenFormattingError>
    where
        Self: Sized,
        S: HasSignature,
        <S as MaybeSigned>::Header: Serialize,
        <S as MaybeSigned>::HeaderState: HeaderState,
    {
        let header = Base64JSON(token.state.header()).serialized_value()?;
        let signature = Base64Signature(SignatureBytes::from(
            token.state.signature().to_bytes().as_ref(),
        ))
        .serialized_value()?;

        let flat = FlatSimpleToken {
            payload: &token.payload,
            protected: header,
            signature,
        };

        let data = serde_json::to_string(&flat)?;
        write!(writer, "{}", data)?;

        Ok(())
    }

    fn parse<P, H>(data: Bytes) -> Result<Token<P, Unverified<H>, Self>, TokenParseError>
    where
        P: DeserializeOwned,
        H: DeserializeOwned,
        Self: Sized,
    {
        let value: serde_json::Value = serde_json::from_slice(&data)?;
        let serde_json::Value::Object(mut object): serde_json::Value = value else {
            return Err(TokenParseError::UnexpectedJSONValue("token", value));
        };
        parse_flat_common_values(&mut object)
    }
}

fn parse_flat_common_values<P, H>(
    object: &mut serde_json::Map<String, serde_json::Value>,
) -> Result<Token<P, Unverified<H>, Flat>, TokenParseError>
where
    P: DeserializeOwned,
    H: DeserializeOwned,
{
    let header = {
        let protected = object
            .get("protected")
            .ok_or(TokenParseError::MissingHeader)?;
        let protected =
            Base64JSON::<Header<H, RenderedHeader>>::parse(protected.as_str().ok_or_else(
                || TokenParseError::UnexpectedJSONValue("header", protected.clone()),
            )?)?;
        let mut header = protected.data;
        header.state.raw = protected.bytes;
        header
    };

    let (payload, raw_payload) = {
        let value_payload = object
            .remove("payload")
            .ok_or(TokenParseError::MissingPayload)?;
        let b64_payload = value_payload.as_str().ok_or_else(|| {
            TokenParseError::UnexpectedJSONValue("payload", value_payload.clone())
        })?;

        let payload: Payload<P> = Payload::parse(b64_payload)?;
        let raw_payload: Vec<u8> = b64_payload.as_bytes().into();
        (payload, raw_payload)
    };

    let signature = {
        let signature = object
            .remove("signature")
            .ok_or(TokenParseError::MissingSignature)?;
        let signature: Base64Signature<SignatureBytes> =
            Base64Signature::parse(signature.as_str().ok_or_else(|| {
                TokenParseError::UnexpectedJSONValue("signature", signature.clone())
            })?)?;
        signature
    };

    Ok(Token {
        payload,
        state: Unverified {
            header,
            signature,
            payload: raw_payload.into(),
        },
        fmt: Flat,
    })
}

impl<P, S> Serialize for Token<P, S, Flat>
where
    S: HasSignature,
    <S as MaybeSigned>::Header: Serialize,
    <S as MaybeSigned>::HeaderState: HeaderState,
    P: Serialize,
{
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: ser::Serializer,
    {
        let header = Base64JSON(self.state.header())
            .serialized_value()
            .map_err(ser::Error::custom)?;
        let signature = Base64Signature(SignatureBytes::from(
            self.state.signature().to_bytes().as_ref(),
        ))
        .serialized_value()
        .map_err(ser::Error::custom)?;

        let flat = FlatSimpleToken {
            payload: &self.payload,
            protected: header,
            signature,
        };

        flat.serialize(serializer)
    }
}

impl From<Compact> for Flat {
    fn from(_: Compact) -> Self {
        Flat
    }
}

impl From<Compact> for FlatUnprotected<()> {
    fn from(_: Compact) -> Self {
        FlatUnprotected { unprotected: () }
    }
}

impl<U> From<FlatUnprotected<U>> for Flat {
    fn from(_: FlatUnprotected<U>) -> Self {
        Flat
    }
}

impl<U> From<FlatUnprotected<U>> for Compact {
    fn from(_: FlatUnprotected<U>) -> Self {
        Compact
    }
}

impl From<Flat> for Compact {
    fn from(_: Flat) -> Self {
        Compact
    }
}

impl From<Flat> for FlatUnprotected<()> {
    fn from(_: Flat) -> Self {
        FlatUnprotected { unprotected: () }
    }
}