twitch_oauth2/
types.rs

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
#![allow(clippy::extra_unused_lifetimes)]
//! Types used in OAUTH2 flow.

use std::fmt;

use base64::Engine;

/// A Client Id
#[aliri_braid::braid(serde)]
pub struct ClientId;

/// A Client Secret
#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
pub struct ClientSecret;

impl fmt::Debug for ClientSecretRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted client secret]")
    }
}
impl fmt::Display for ClientSecretRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted client secret]")
    }
}

/// An Access Token
#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
pub struct AccessToken;

impl fmt::Debug for AccessTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted access token]")
    }
}
impl fmt::Display for AccessTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted access token]")
    }
}

/// A Refresh Token
#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
pub struct RefreshToken;

impl fmt::Debug for RefreshTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted refresh token]")
    }
}
impl fmt::Display for RefreshTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted refresh token]")
    }
}

/// A Csrf Token
#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
pub struct CsrfToken;

impl fmt::Debug for CsrfTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted csrf token]")
    }
}
impl fmt::Display for CsrfTokenRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[redacted csrf token]")
    }
}

impl CsrfToken {
    /// Make a new random CSRF token.
    pub fn new_random() -> CsrfToken { Self::new_random_len(16) }

    /// Make a new random CSRF token with given amount of bytes
    pub fn new_random_len(len: u32) -> CsrfToken {
        use rand::Rng as _;
        let random_bytes: Vec<u8> = (0..len).map(|_| rand::thread_rng().gen::<u8>()).collect();
        CsrfToken::new(base64::engine::general_purpose::STANDARD.encode(random_bytes))
    }
}

impl ClientSecretRef {
    /// Get the secret from this string.
    ///
    /// This function is the same as [`ClientSecret::as_str`](ClientSecretRef::as_str), but has another name for searchability, prefer to use this function.
    pub fn secret(&self) -> &str { self.as_str() }
}

impl AccessTokenRef {
    /// Get the secret from this string.
    ///
    /// This function is the same as [`AccessToken::as_str`](AccessTokenRef::as_str), but has another name for searchability, prefer to use this function.
    pub fn secret(&self) -> &str { self.as_str() }
}
impl RefreshTokenRef {
    /// Get the secret from this string.
    ///
    /// This function is the same as [`RefreshToken::as_str`](RefreshTokenRef::as_str), but has another name for searchability, prefer to use this function.
    pub fn secret(&self) -> &str { self.as_str() }
}
impl CsrfTokenRef {
    /// Get the secret from this string.
    ///
    /// This function is the same as [`CsrfToken::as_str`](CsrfTokenRef::as_str), but has another name for searchability, prefer to use this function.
    pub fn secret(&self) -> &str { self.as_str() }
}