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
//! A collection of types used throughout all four standard PASETO protocols.

use crate::{
    encoding::base64::{decode_no_padding as b64_decode, encode_no_padding as b64_encode},
    token::paseto::collapse_to_vec,
};
use std::{
    convert::TryFrom,
    ops::{Bound, Deref},
    str,
};

/// Maximum number of sections.
const MINIMUM_SECTION_COUNT: usize = 3;
/// Minimum number of sections.
const MAXIMUM_SECTION_COUNT: usize = 4;
/// Maximum number of sections.
const MINIMUM_PERIOD_COUNT: usize = MINIMUM_SECTION_COUNT - 1;
/// Minimum number of sections.
const MAXIMUM_PERIOD_COUNT: usize = MAXIMUM_SECTION_COUNT - 1;
/// Stores the location of either three or four periods.
enum MinToMaxPeriods {
    MinPeriods([usize; MINIMUM_PERIOD_COUNT]),
    MaxPeriods([usize; MAXIMUM_PERIOD_COUNT]),
}
impl MinToMaxPeriods {
    // TODO maybe in the future?
    // fn copy_to_array<T: Copy + Default, const N: usize>(slice: &[T]) -> [T; N] {
    //     let mut arr = [T::default(); N]; // TODO eliminate default value, use std::mem::uninitialized()
    //     arr.copy_from_slice(slice);
    //     arr
    // }
    /// Copies the first two elements of the slice into an array.
    // TODO remove when copy_to_array can work
    fn copy_to_min_array<T: Copy + Default>(slice: &[T]) -> [T; MINIMUM_PERIOD_COUNT] {
        let mut arr = [T::default(); MINIMUM_PERIOD_COUNT]; // TODO eliminate default value, use std::mem::uninitialized()
        arr.copy_from_slice(slice);
        arr
    }
    // TODO remove when copy_to_array can work
    /// Copies the first three elements of the slice into an array.
    fn copy_to_max_array<T: Copy + Default>(slice: &[T]) -> [T; MAXIMUM_PERIOD_COUNT] {
        let mut arr = [T::default(); MAXIMUM_PERIOD_COUNT]; // TODO eliminate default value, use std::mem::uninitialized()
        arr.copy_from_slice(slice);
        arr
    }
    /// Converts a slice of indices into the enum.
    fn from_slice(period_indices: &[usize]) -> Result<Self, UnexpectedNumberOfPeriods> {
        match period_indices.len() {
            MINIMUM_PERIOD_COUNT => Ok(Self::MinPeriods(Self::copy_to_min_array(period_indices))),
            MAXIMUM_PERIOD_COUNT => Ok(Self::MaxPeriods(Self::copy_to_max_array(period_indices))),
            _ => Err(UnexpectedNumberOfPeriods::new(period_indices.len())),
        }
    }
    /// Returns the number of periods represented.
    fn period_cnt(&self) -> usize {
        // TODO generic size
        match self {
            Self::MinPeriods(_) => MINIMUM_PERIOD_COUNT,
            Self::MaxPeriods(_) => MAXIMUM_PERIOD_COUNT,
        }
    }
    /// Get, optionally, the index of the nth period where n is less than 4.
    // TODO enforce const range on idx from 0..2
    // TODO replace with const generics when available
    fn opt_val_at(&self, i: usize) -> Option<usize> {
        if i < self.period_cnt() {
            let a = match self {
                Self::MinPeriods(a) => &a[..],
                Self::MaxPeriods(a) => &a[..],
            };
            Some(a[i])
        } else {
            None
        }
    }
    /// Get the index of the nth period where n is less than 2.
    // TODO enforce const range on idx from 0..1
    // TODO replace with const generics when available
    fn val_at(&self, i: usize) -> usize {
        match self {
            Self::MinPeriods(a) => a[i],
            Self::MaxPeriods(a) => a[i],
        }
    }
    /// The range of the protocol version header.
    fn version_range(&self) -> (Bound<usize>, Bound<usize>) {
        let start = 0;
        let end = self.val_at(0);
        (Bound::Included(start), Bound::Excluded(end))
    }
    /// The range of the protocol version header.
    fn purpose_range(&self) -> (Bound<usize>, Bound<usize>) {
        let start = self.val_at(0);
        let end = self.val_at(1);
        (Bound::Excluded(start), Bound::Excluded(end))
    }
    /// The range of the body, should it exist.
    fn body_range(&self) -> (Bound<usize>, Bound<usize>) {
        let start = self.val_at(1);
        let end = self.opt_val_at(2);
        (
            Bound::Excluded(start),
            end.map_or(Bound::Unbounded, |e| Bound::Excluded(e)),
        )
    }
    /// The range of the footer, should it exist.
    fn footer_range(&self) -> Option<(Bound<usize>, Bound<usize>)> {
        let start = self.opt_val_at(2)?;
        Some((Bound::Excluded(start), Bound::Unbounded))
    }
}
/// Stores the illegal quantity of periods found within the payload.
struct UnexpectedNumberOfPeriods(
    /// The number of periods.
    usize,
);
impl UnexpectedNumberOfPeriods {
    fn new(periods_cnt: usize) -> Self {
        if periods_cnt < MINIMUM_PERIOD_COUNT || periods_cnt > MAXIMUM_PERIOD_COUNT {
            panic!(
                "Expected illegal number of periods, instead was provided {}.",
                periods_cnt
            );
        }
        Self(periods_cnt)
    }
}

/// Errors that can occur during unpacking.
#[derive(Debug)]
pub enum UnpackingError {
    /// Incorrect number of sections.
    IncorrectNumberOfSections,
    /// Incorrect encoding.
    MalformedEncoding,
}
impl From<UnexpectedNumberOfPeriods> for UnpackingError {
    fn from(_: UnexpectedNumberOfPeriods) -> Self {
        UnpackingError::IncorrectNumberOfSections
    }
}
impl From<base64::DecodeError> for UnpackingError {
    fn from(_: base64::DecodeError) -> Self {
        UnpackingError::MalformedEncoding
    }
}

/// Errors that can occur during deserialization.
#[derive(Debug)]
pub enum DeserializeError {
    /// Payload was not JSON encoded.
    Json,
    /// Payload was not UTF8.
    Utf8,
}
impl From<serde_json::Error> for DeserializeError {
    fn from(_: serde_json::Error) -> Self {
        DeserializeError::Json
    }
}
impl From<str::Utf8Error> for DeserializeError {
    fn from(_: str::Utf8Error) -> Self {
        DeserializeError::Utf8
    }
}

/// Header structure.
#[derive(Debug, PartialEq, Eq)]
pub struct Header<'a> {
    /// Version of the protocol used.
    version: &'a [u8],
    /// Purpose of the protocol used.
    purpose: &'a [u8],
}
impl<'a> Header<'a> {
    /// Constructs a [`Header`] with listed `version` and `purpose`.
    pub const fn new(version: &'a [u8], purpose: &'a [u8]) -> Self {
        Self {
            version: version,
            purpose: purpose,
        }
    }
    /// Created the [`Header`] with periods inserted as such: "`version`.`purpose`."
    pub fn to_combined(&self) -> Vec<u8> {
        // TODO cache + custom PartialEq if needed
        collapse_to_vec(&[self.version, b".", self.purpose, b"."])
    }
}

/// Hold the pre-protocol data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Data<M, F> {
    /// The message to be serialized and sent.
    pub msg: M,
    /// The footer to be serialized and sent.
    pub footer: Option<F>,
}
impl<M: serde::Serialize, F: serde::Serialize> Data<M, F> {
    /// Serialize [`Data`] into the [`SerializedData`] form.
    pub fn serialize(self) -> Result<SerializedData, serde_json::Error> {
        SerializedData::try_from(self)
    }
}
impl<M: serde::de::DeserializeOwned, F: serde::de::DeserializeOwned> Data<M, F> {
    /// Deserialize a [`Vec`] of bytes into the a struct.
    fn deserialize_component<T: serde::de::DeserializeOwned>(
        target: &Vec<u8>,
    ) -> Result<T, DeserializeError> {
        Ok(serde_json::from_str(str::from_utf8(target.as_slice())?)?)
    }
    /// Deserialize an optional [`Vec`] of bytes into the a struct.
    fn opt_deserialize_component<T: serde::de::DeserializeOwned>(
        target: &Option<Vec<u8>>,
    ) -> Option<Result<T, DeserializeError>> {
        let target = target.as_ref()?;
        Some(Self::deserialize_component(target))
    }
    /// Deserialize a [`Vec`] of bytes into the a [`Data`] struct.
    fn deserialize(tok: SerializedData) -> Result<Self, DeserializeError> {
        Ok(Self {
            msg: Self::deserialize_component(&tok.msg)?,
            footer: Self::opt_deserialize_component(&tok.footer).transpose()?,
        })
    }
}
impl<M: serde::de::DeserializeOwned, F: serde::de::DeserializeOwned> TryFrom<SerializedData>
    for Data<M, F>
{
    type Error = DeserializeError;
    fn try_from(tok: SerializedData) -> Result<Self, Self::Error> {
        Self::deserialize(tok)
    }
}

/// Represents the [`Data`] struct, but serialized.
pub struct SerializedData {
    /// The message to be sent.
    pub msg: Vec<u8>,
    /// The footer to be sent.
    pub footer: Option<Vec<u8>>,
}
impl SerializedData {
    /// Serialize part of the struct.
    fn serialize_component<T: serde::Serialize>(target: &T) -> Result<Vec<u8>, serde_json::Error> {
        Ok(serde_json::to_string(target)?.as_bytes().to_vec())
    }
    /// Serialize an optional part of the struct.
    fn opt_serialize_component<T: serde::Serialize>(
        target: &Option<T>,
    ) -> Option<Result<Vec<u8>, serde_json::Error>> {
        let target = target.as_ref()?;
        Some(Self::serialize_component(target))
    }
    /// Serialize the [`Data`] struct into the [`SerializedData`] struct.
    fn serialize<M: serde::Serialize, F: serde::Serialize>(
        tok: Data<M, F>,
    ) -> Result<Self, serde_json::Error> {
        Ok(Self {
            msg: Self::serialize_component(&tok.msg)?,
            footer: Self::opt_serialize_component(&tok.footer).transpose()?,
        })
    }
    /// Deserialize the [`SerializedData`] into the [`Data`] struct.
    pub fn deserialize<M: serde::de::DeserializeOwned, F: serde::de::DeserializeOwned>(
        self,
    ) -> Result<Data<M, F>, DeserializeError> {
        Data::try_from(self)
    }
}
impl<M: serde::Serialize, F: serde::Serialize> TryFrom<Data<M, F>> for SerializedData {
    type Error = serde_json::Error;
    fn try_from(tok: Data<M, F>) -> Result<Self, Self::Error> {
        Self::serialize(tok)
    }
}

/// The packed PASETO token.
pub struct Packed(Vec<u8>);
impl Packed {
    /// Creates a new token from a payload.
    pub fn new(buffer: Vec<u8>) -> Self {
        Self(buffer)
    }
    /// Attempts to unpack a [`Packed`] token into an [`Unpacked`] one. Errors if the payload is
    /// malformed.
    pub fn unpack(self) -> Result<Unpacked, UnpackingError> {
        Unpacked::unpack(self)
    }
    /// Packs an [`Unpacked`] token into a [`Packed`] one. This follows the
    /// "`version`.`protocol`.`body`(.`footer`)" structure.
    fn pack(tok: Unpacked) -> Packed {
        let possible_footer = tok
            .footer
            .as_ref()
            .map_or(b"".to_vec(), |f| b64_encode(f.as_slice()));
        Packed(collapse_to_vec(&[
            tok.version.as_slice(),
            b".",
            tok.purpose.as_slice(),
            b".",
            b64_encode(tok.body.as_slice()).as_slice(),
            tok.footer.as_ref().map_or(b"", |_| b"."),
            possible_footer.as_slice(),
        ]))
    }
}
impl Deref for Packed {
    type Target = [u8];
    fn deref<'a>(&'a self) -> &'a Self::Target {
        &self.0
    }
}
impl From<Unpacked> for Packed {
    fn from(token: Unpacked) -> Self {
        Self::pack(token)
    }
}

/// The unpacked, but encrypted/signed PASETO token.
pub struct Unpacked {
    /// The binary version of the version.
    pub version: Vec<u8>,
    /// The binary version of the purpose.
    pub purpose: Vec<u8>,
    /// The binary version of the body of the message.
    pub body: Vec<u8>,
    /// The binary version of the optional footer.
    pub footer: Option<Vec<u8>>,
}
impl Unpacked {
    /// Creates a new token from the [`Header`], body, and optional footer. The [`Header`] is
    /// effectively cloned.
    pub(super) fn new(header: Header, body: Vec<u8>, footer: Option<Vec<u8>>) -> Self {
        Self {
            version: header.version.to_vec(),
            purpose: header.purpose.to_vec(),
            body: body,
            footer: footer,
        }
    }

    /// Locate and return the positions of the periods in the payload.
    fn find_section_dividers(
        buf: &[u8],
    ) -> Result<MinToMaxPeriods, UnexpectedNumberOfPeriods> {
        let mut indices = Vec::with_capacity(5);
        for (idx, c) in buf.iter().enumerate() {
            if *c == b'.' {
                indices.push(idx);
            }
        }
        Ok(MinToMaxPeriods::from_slice(indices.as_slice())?)
    }
    /// Slice a slice with a bound two-tuple.
    fn extract_bounds<'a>(slice: &'a [u8], (start, end): (Bound<usize>, Bound<usize>)) -> &'a [u8] {
        let start = match start {
            Bound::Included(i) => i,
            Bound::Excluded(i) => i + 1,
            Bound::Unbounded => 0,
        };
        let end = match end {
            Bound::Included(i) => i + 1,
            Bound::Excluded(i) => i,
            Bound::Unbounded => slice.len(),
        };
        &slice[start..end]
    }
    /// Attempt to unpack a [`Packed`] token.
    fn unpack(packed: Packed) -> Result<Self, UnpackingError> {
        let packed = &*packed;
        let period_indices = Self::find_section_dividers(packed)?;
        Ok(Self {
            version: Self::extract_bounds(packed, period_indices.version_range()).to_vec(),
            purpose: Self::extract_bounds(packed, period_indices.purpose_range()).to_vec(),
            body: b64_decode(Self::extract_bounds(packed, period_indices.body_range()))?,
            footer: period_indices
                .footer_range()
                .map(|r| b64_decode(Self::extract_bounds(packed, r)))
                .transpose()?,
        })
    }
    /// Pack token.
    pub fn pack(self) -> Packed {
        Packed::pack(self)
    }
    /// Verify header of the token.
    pub fn verify_header(&self, header: Header<'_>) -> bool {
        Header {
            version: self.version.as_slice(),
            purpose: self.purpose.as_slice(),
        } == header
    }
}
impl TryFrom<Packed> for Unpacked {
    type Error = UnpackingError;
    fn try_from(token: Packed) -> Result<Self, UnpackingError> {
        Self::unpack(token)
    }
}