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
use crate::helix::Client;
use crate::helix::User;

use anyhow::Result;
use async_trait::async_trait;
use chrono::DateTime;
use chrono::Utc;
use serde::Deserialize;
use serde::Serialize;

#[async_trait]
pub trait TokenStorage {
    async fn save(&mut self, token: &Token) -> Result<()>;
}

#[derive(Debug, Default, Clone, PartialEq)]
pub enum TokenType {
    #[default]
    UserAccessToken,
    AppAccessToken,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Token {
    #[serde(skip)]
    pub token_type: TokenType,
    #[serde(default)]
    pub refresh_token: String,
    pub access_token: String,
    pub expires_in: i64,
    #[serde(default = "Utc::now")]
    pub created_at: DateTime<Utc>,
    #[serde(skip)]
    pub user: Option<User>,
}

#[derive(Debug)]
pub struct VoidStorage {}
#[async_trait]
impl TokenStorage for VoidStorage {
    async fn save(&mut self, _token: &Token) -> Result<()> {
        Ok(())
    }
}

#[derive(Deserialize)]
struct ValidateToken {
    pub expires_in: i64,
}

impl<T: TokenStorage> Client<T> {
    pub async fn validate_token(&mut self) -> Result<()> {
        let token = match self
            .get::<ValidateToken>("https://id.twitch.tv/oauth2/validate".to_string())
            .await
        {
            Ok(r) => r,
            Err(..) => {
                self.refresh_token().await?;
                return Ok(());
            }
        };

        if token.expires_in < 3600 {
            self.refresh_token().await?;
        }

        Ok(())
    }

    pub async fn refresh_token(&mut self) -> Result<()> {
        let res = self
            .http_request::<()>(
                reqwest::Method::POST,
                "https://id.twitch.tv/oauth2/token".to_string(),
                None,
                Some(format!(
                    "client_id={0}&client_secret={1}&grant_type=refresh_token&refresh_token={2}",
                    self.client_id, self.client_secret, self.token.refresh_token
                )),
            )
            .await?;

        self.token = res.json::<Token>().await?;
        self.token_storage.save(&self.token).await?;

        Ok(())
    }

    pub fn from_token_no_validation(
        client_id: String,
        client_secret: String,
        token_storage: T,
        token: Token,
    ) -> Client<T> {
        Client {
            client_id: client_id,
            client_secret: client_secret,
            token: token,
            http_client: reqwest::Client::new(),
            token_storage: token_storage,
        }
    }

    pub async fn from_token(
        client_id: String,
        client_secret: String,
        token_storage: T,
        token: Token,
    ) -> Result<Client<T>> {
        let mut client =
            Self::from_token_no_validation(client_id, client_secret, token_storage, token);
        client.token.user = Some(client.get_user().await?);
        Ok(client)
    }

    pub async fn from_get_app_token(
        client_id: String,
        client_secret: String,
        token_storage: T,
    ) -> Result<Client<T>> {
        let http_client = reqwest::Client::new();
        let token = http_client
            .post("https://id.twitch.tv/oauth2/token")
            .body(format!(
                "client_id={client_id}&client_secret={client_secret}&grant_type=client_credentials"
            ))
            .send()
            .await?
            .json::<Token>()
            .await?;
        let mut client = Client {
            client_id: client_id,
            client_secret: client_secret,
            token: token,
            http_client: http_client,
            token_storage: token_storage,
        };
        client.token.token_type = TokenType::AppAccessToken;
        client.token_storage.save(&client.token).await?;
        Ok(client)
    }

    pub async fn from_authorization(
        client_id: String,
        client_secret: String,
        token_storage: T,
        code: String,
        redirect_uri: String,
    ) -> Result<Client<T>> {
        let http_client = reqwest::Client::new();
        let token = http_client.post("https://id.twitch.tv/oauth2/token")
                .body(format!("client_id={client_id}&client_secret={client_secret}&code={code}&grant_type=authorization_code&redirect_uri={redirect_uri}"))
                .send()
                .await?
                .json::<Token>()
                .await?;
        let mut client = Client {
            client_id: client_id,
            client_secret: client_secret,
            token: token,
            http_client: http_client,
            token_storage: token_storage,
        };
        client.token.user = Some(client.get_user().await?);
        client.token_storage.save(&client.token).await?;
        Ok(client)
    }
}