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
//! Types and functions for auth flows
//!
//! See [`Twitch Auth Documentation`]
//!
//! [`Twitch Auth Documentation`]: https://dev.twitch.tv/docs/authentication

pub mod scopes;

/// Represents a authorization token of some type that can be sent as a header to a
/// twitch endpoint.
pub trait AuthToken: crate::requests::Headers + Clone {
    /// Get the set of scopes that this token has associated with it
    fn scopes(&self) -> &scopes::ScopeSet;
}

use reqwest::RequestBuilder;
use std::rc::Rc;
use std::sync::Arc;

impl<H> crate::requests::Headers for Arc<H>
where
    H: crate::requests::Headers,
{
    fn write_headers(&self, req: RequestBuilder) -> RequestBuilder {
        self.as_ref().write_headers(req)
    }
}

impl<A> AuthToken for Arc<A>
where
    A: AuthToken,
{
    fn scopes(&self) -> &scopes::ScopeSet {
        self.as_ref().scopes()
    }
}

impl<H> crate::requests::Headers for Rc<H>
where
    H: crate::requests::Headers,
{
    fn write_headers(&self, req: RequestBuilder) -> RequestBuilder {
        self.as_ref().write_headers(req)
    }
}

impl<A> AuthToken for Rc<A>
where
    A: AuthToken,
{
    fn scopes(&self) -> &scopes::ScopeSet {
        self.as_ref().scopes()
    }
}

use crate::values::FieldValue;
use crate::{field_wrapper_name, from_inner, quick_deref_into};
use serde::{Deserialize, Serialize};

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a [`crate::auth::client_credentials`] id.  
/// See [`Twitch Auth Guide`] for more
///
/// [`Twitch Auth Guide`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow
pub struct ClientId(String);

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a [`crate::auth::client_credentials`] secret.  
/// See [`Twitch Auth Guide`] for more
///
/// [`Twitch Auth Guide`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow
pub struct ClientSecret(String);

quick_deref_into![(ClientId, String), (ClientSecret, String)];
from_inner![(ClientId, String), (ClientSecret, String)];
field_wrapper_name![ClientId => "client_id", ClientSecret => "client_secret"];

/// [`Implicit Code`] Flow
///
/// [`Implicit Code`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-implicit-code-flow
pub mod implicit_code {}

/// [`Authorization Code`] Flow
///
/// [`Authorization Code`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-implicit-code-flow
pub mod authorization_code {}

/// [`Client Credentials`] Flow
///
/// [`Client Credentials`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-client-credentials-flow
///
/// ```ignore
/// # use twitch_api_rs::requests::*;
/// # let (client_id, client_secret) = (
/// #     String::from("uo6dggojyb8d6soh92zknwmi5ej1q2"),
/// #     String::from("nyo51xcdrerl8z9m56w9w6wg"),
/// # );
///
/// let client_resp = ClientAuthRequestBuilder::builder()
///     .set_client_id(client_id)
///     .set_client_secret(client_secret)
///     .make_request()
///     .await;
///
/// match client_resp {
///
///     Ok(resp) => {
///         let (token, expiration) = resp.into();
///         eprintln!("Got Token {}. (Expires in {} seconds)", token, expiration);
///     }
///
///     Err(RequestError::MalformedRequest(msg)) =>
///         unreachable!("We set all the parameters but the struct said {}", msg),
///
///     Err(RequestError::ErrorCodes(code)) =>
///         eprintln!("Server rejected request for reason {}", code),
///
///     Err(e) =>
///         eprintln!("Failed to make request for reason {}", e),
///
/// }
/// ```
pub mod client_credentials {

    use super::*;
    use crate::requests::*; // TODO: Replace with internal prelude
    use reqwest::RequestBuilder;
    use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};

    #[derive(Debug)]
    #[doc(hidden)]
    /// Do not use directly, instead use [`ClientAuthRequest`]
    pub struct ClientAuthRequestParams {
        client_id: Option<ClientId>,
        client_secret: Option<ClientSecret>,
        scopes: Vec<String>, // TODO change to list of Scope Enum items or maybe bitset that has display trait and named bits
    }

    impl ParametersExt for ClientAuthRequestParams {}

    #[derive(Debug)]
    /// Request for the [`client authentication`] flow.  
    /// See module level documentation for usage.
    ///
    /// implemnts [`Request`], see documentation for more information.
    ///
    /// [`client authentication`]: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-client-credentials-flow
    pub struct ClientAuthRequest {
        params: ClientAuthRequestParams,
    }

    impl Serialize for ClientAuthRequestParams {
        fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let mut map = ser.serialize_map(Some(if self.scopes.len() > 0 { 4 } else { 3 }))?;
            map.serialize_entry("client_id", self.client_id.as_ref().unwrap())?;
            map.serialize_entry("client_secret", self.client_secret.as_ref().unwrap())?;
            map.serialize_entry("grant_type", "client_credentials")?;

            // TODO Serialize vec as space separated list

            map.end()
        }
    }

    #[cfg_attr(feature = "nightly", doc(spotlight))]
    impl Request for ClientAuthRequest {
        const ENDPOINT: &'static str = "https://id.twitch.tv/oauth2/token";

        type Headers = ();
        type Parameters = ClientAuthRequestParams;
        type Body = ();

        type Response = ClientAuthResponse;

        type ErrorCodes = CommonResponseCodes;

        const METHOD: reqwest::Method = reqwest::Method::POST;

        fn builder() -> Self {
            Self {
                params: ClientAuthRequestParams {
                    client_id: None,
                    client_secret: None,
                    scopes: Vec::new(),
                },
            }
        }

        fn headers(&self) -> &Self::Headers {
            &()
        }
        fn parameters(&self) -> &Self::Parameters {
            &self.params
        }
        fn body(&self) -> &Self::Body {
            &()
        }

        fn ready(&self) -> Result<(), RequestError<Self::ErrorCodes>> {
            if self.params.client_id.is_none() {
                Err(RequestError::MalformedRequest(String::from(
                    "field client_id must be set",
                )))
            } else if self.params.client_secret.is_none() {
                Err(RequestError::MalformedRequest(String::from(
                    "field client_secret must be set",
                )))
            } else {
                Ok(())
            }
        }
    }

    impl ClientAuthRequest {
        /// Set the client_id
        pub fn set_client_id<I: Into<ClientId>>(&mut self, client_id: I) -> &mut Self {
            self.params.client_id.replace(client_id.into());
            self
        }

        /// Set the client_secret
        pub fn set_client_secret<S: Into<ClientSecret>>(&mut self, client_secret: S) -> &mut Self {
            self.params.client_secret.replace(client_secret.into());
            self
        }
    }

    /// Build a complete request from `(client_id, client_secret)`
    impl<I, S> From<(I, S)> for ClientAuthRequest
    where
        I: Into<ClientId>,
        S: Into<ClientSecret>,
    {
        fn from((client_id, client_secret): (I, S)) -> Self {
            Self {
                params: ClientAuthRequestParams {
                    client_id: Some(client_id.into()),
                    client_secret: Some(client_secret.into()),
                    scopes: vec![],
                },
            }
        }
    }

    #[derive(Debug, Deserialize)]
    /// Response from a successful [`ClientAuthRequest`]
    ///
    /// See module level docuemntation to see how to get
    pub struct ClientAuthResponse {
        /// The access_token returned by twitch
        pub access_token: String,
        // refresh_token:
        /// The amount of seconds until the token expires
        pub expires_in: u32,
        // token_type: String // Always bearer
    }

    impl Into<(String, u32)> for ClientAuthResponse {
        fn into(self) -> (String, u32) {
            (self.access_token, self.expires_in)
        }
    }

    use super::scopes::ScopeSet;

    /// Represents an authorization token header for requests
    #[derive(Debug, Clone)]
    #[allow(missing_docs)]
    pub struct ClientAuthToken {
        scopes: ScopeSet,
        pub token: String,
        pub client_id: ClientId,
    }

    impl ClientAuthToken {
        /// Create the auth token from a sucessful auth response and a client_id
        pub fn from_client<C>(auth_response: ClientAuthResponse, client_id: C) -> Self
        where
            C: Into<ClientId>,
        {
            Self {
                // Fill with empty scopes item as scopes only apply to OAuth tokens
                scopes: ScopeSet::new(),
                token: auth_response.access_token,
                client_id: client_id.into(),
            }
        }
    }

    impl Headers for ClientAuthToken {
        fn write_headers(&self, req: RequestBuilder) -> RequestBuilder {
            req.header("Authorization", format!("Bearer {}", self.token))
                .header("Client-Id", std::ops::Deref::deref(&self.client_id))
        }
    }

    impl super::AuthToken for ClientAuthToken {
        fn scopes(&self) -> &ScopeSet {
            &self.scopes
        }
    }
}