Skip to content

Commit 7a7127e

Browse files
Merge #36
36: provide csrf and a way to set it r=Emilgardis a=Emilgardis Co-authored-by: Emil Gardström <emil.gardstrom@gmail.com>
2 parents 2d217a8 + f84f5b8 commit 7a7127e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/tokens/user_token.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl UserTokenBuilder {
163163
/// Generate the URL to request a code.
164164
///
165165
/// Step 1. in the [guide](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-authorization-code-flow)
166-
pub fn generate_url(&mut self) -> oauth2::url::Url {
166+
pub fn generate_url(&mut self) -> (oauth2::url::Url, oauth2::CsrfToken) {
167167
let mut auth = self.client.authorize_url(oauth2::CsrfToken::new_random);
168168

169169
for scope in self.scopes.iter() {
@@ -176,10 +176,16 @@ impl UserTokenBuilder {
176176
);
177177

178178
let (url, csrf) = auth.url();
179-
self.csrf = Some(csrf);
180-
url
179+
self.csrf = Some(csrf.clone());
180+
(url, csrf)
181181
}
182182

183+
/// Set the CSRF token.
184+
///
185+
/// Hidden because you should preferably not use this.
186+
#[doc(hidden)]
187+
pub fn set_csrf(&mut self, csrf: oauth2::CsrfToken) { self.csrf = Some(csrf); }
188+
183189
/// Generate the code with the help of the authorization code
184190
///
185191
/// Step. 3 and 4 in the [guide](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-authorization-code-flow)
@@ -198,6 +204,8 @@ impl UserTokenBuilder {
198204
if state.is_none() || csrf.secret() != state.expect("should not fail") {
199205
return Err(UserTokenExchangeError::StateMismatch);
200206
}
207+
} else {
208+
return Err(UserTokenExchangeError::StateMismatch);
201209
}
202210

203211
// FIXME: self.client.exchange_code(code) does not work as oauth2 currently only sends it in body as per spec, but twitch uses query params.
@@ -260,6 +268,7 @@ mod tests {
260268
.unwrap()
261269
.force_verify(true)
262270
.generate_url()
271+
.0
263272
.to_string());
264273
}
265274

0 commit comments

Comments
 (0)