@@ -163,7 +163,7 @@ impl UserTokenBuilder {
163
163
/// Generate the URL to request a code.
164
164
///
165
165
/// 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 ) {
167
167
let mut auth = self . client . authorize_url ( oauth2:: CsrfToken :: new_random) ;
168
168
169
169
for scope in self . scopes . iter ( ) {
@@ -176,10 +176,16 @@ impl UserTokenBuilder {
176
176
) ;
177
177
178
178
let ( url, csrf) = auth. url ( ) ;
179
- self . csrf = Some ( csrf) ;
180
- url
179
+ self . csrf = Some ( csrf. clone ( ) ) ;
180
+ ( url, csrf )
181
181
}
182
182
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
+
183
189
/// Generate the code with the help of the authorization code
184
190
///
185
191
/// 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 {
198
204
if state. is_none ( ) || csrf. secret ( ) != state. expect ( "should not fail" ) {
199
205
return Err ( UserTokenExchangeError :: StateMismatch ) ;
200
206
}
207
+ } else {
208
+ return Err ( UserTokenExchangeError :: StateMismatch ) ;
201
209
}
202
210
203
211
// 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 {
260
268
. unwrap( )
261
269
. force_verify( true )
262
270
. generate_url( )
271
+ . 0
263
272
. to_string( ) ) ;
264
273
}
265
274
0 commit comments