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
pub mod scopes;
pub trait AuthToken: crate::requests::Headers + Clone {
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)]
pub struct ClientId(String);
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
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"];
pub mod implicit_code {}
pub mod authorization_code {}
pub mod client_credentials {
use super::*;
use crate::requests::*;
use reqwest::RequestBuilder;
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
#[derive(Debug)]
#[doc(hidden)]
pub struct ClientAuthRequestParams {
client_id: Option<ClientId>,
client_secret: Option<ClientSecret>,
scopes: Vec<String>,
}
impl ParametersExt for ClientAuthRequestParams {}
#[derive(Debug)]
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")?;
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 {
pub fn set_client_id<I: Into<ClientId>>(&mut self, client_id: I) -> &mut Self {
self.params.client_id.replace(client_id.into());
self
}
pub fn set_client_secret<S: Into<ClientSecret>>(&mut self, client_secret: S) -> &mut Self {
self.params.client_secret.replace(client_secret.into());
self
}
}
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)]
pub struct ClientAuthResponse {
pub access_token: String,
pub expires_in: u32,
}
impl Into<(String, u32)> for ClientAuthResponse {
fn into(self) -> (String, u32) {
(self.access_token, self.expires_in)
}
}
use super::scopes::ScopeSet;
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct ClientAuthToken {
scopes: ScopeSet,
pub token: String,
pub client_id: ClientId,
}
impl ClientAuthToken {
pub fn from_client<C>(auth_response: ClientAuthResponse, client_id: C) -> Self
where
C: Into<ClientId>,
{
Self {
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
}
}
}