twitch_oauth2/
types.rs

1#![allow(clippy::extra_unused_lifetimes)]
2//! Types used in OAUTH2 flow.
3
4use std::fmt;
5
6use base64::Engine;
7
8/// A Client Id
9#[aliri_braid::braid(serde)]
10pub struct ClientId;
11
12/// A Client Secret
13#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
14pub struct ClientSecret;
15
16impl fmt::Debug for ClientSecretRef {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        f.write_str("[redacted client secret]")
19    }
20}
21impl fmt::Display for ClientSecretRef {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        f.write_str("[redacted client secret]")
24    }
25}
26
27/// An Access Token
28#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
29pub struct AccessToken;
30
31impl fmt::Debug for AccessTokenRef {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.write_str("[redacted access token]")
34    }
35}
36impl fmt::Display for AccessTokenRef {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.write_str("[redacted access token]")
39    }
40}
41
42/// A Refresh Token
43#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
44pub struct RefreshToken;
45
46impl fmt::Debug for RefreshTokenRef {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str("[redacted refresh token]")
49    }
50}
51impl fmt::Display for RefreshTokenRef {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        f.write_str("[redacted refresh token]")
54    }
55}
56
57/// A Csrf Token
58#[aliri_braid::braid(display = "owned", debug = "owned", serde)]
59pub struct CsrfToken;
60
61impl fmt::Debug for CsrfTokenRef {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.write_str("[redacted csrf token]")
64    }
65}
66impl fmt::Display for CsrfTokenRef {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        f.write_str("[redacted csrf token]")
69    }
70}
71
72impl CsrfToken {
73    /// Make a new random CSRF token.
74    pub fn new_random() -> CsrfToken { Self::new_random_len(16) }
75
76    /// Make a new random CSRF token with given amount of bytes
77    pub fn new_random_len(len: u32) -> CsrfToken {
78        use rand::Rng as _;
79        let random_bytes: Vec<u8> = (0..len).map(|_| rand::thread_rng().gen::<u8>()).collect();
80        CsrfToken::new(base64::engine::general_purpose::STANDARD.encode(random_bytes))
81    }
82}
83
84impl ClientSecretRef {
85    /// Get the secret from this string.
86    ///
87    /// This function is the same as [`ClientSecret::as_str`](ClientSecretRef::as_str), but has another name for searchability, prefer to use this function.
88    pub fn secret(&self) -> &str { self.as_str() }
89}
90
91impl AccessTokenRef {
92    /// Get the secret from this string.
93    ///
94    /// This function is the same as [`AccessToken::as_str`](AccessTokenRef::as_str), but has another name for searchability, prefer to use this function.
95    pub fn secret(&self) -> &str { self.as_str() }
96}
97impl RefreshTokenRef {
98    /// Get the secret from this string.
99    ///
100    /// This function is the same as [`RefreshToken::as_str`](RefreshTokenRef::as_str), but has another name for searchability, prefer to use this function.
101    pub fn secret(&self) -> &str { self.as_str() }
102}
103impl CsrfTokenRef {
104    /// Get the secret from this string.
105    ///
106    /// This function is the same as [`CsrfToken::as_str`](CsrfTokenRef::as_str), but has another name for searchability, prefer to use this function.
107    pub fn secret(&self) -> &str { self.as_str() }
108}