Skip to content

Commit 62196c7

Browse files
Merge #39
39: small improvements r=Emilgardis a=Emilgardis - expose oauth2 and RedirectUrl - minor changes to get_user_token, making it more accessible. This could be considered weaker security wise. But I don't see the point personally. This makes the interface easier to use, as we only compare these fields, not consume like with client secrets etc Co-authored-by: Emil Gardström <[email protected]>
2 parents afa0bf1 + 0b6e75b commit 62196c7

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub mod tokens;
3434
#[doc(hidden)]
3535
pub use oauth2;
3636
#[doc(no_inline)]
37-
pub use oauth2::{AccessToken, ClientId, ClientSecret, RedirectUrl, RefreshToken};
37+
pub use oauth2::{
38+
AccessToken, AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, RefreshToken,
39+
};
3840

3941
use id::{TwitchClient, TwitchTokenErrorResponse};
4042
use oauth2::{url::Url, AuthUrl, HttpRequest, HttpResponse, TokenResponse};

src/tokens/user_token.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,17 @@ impl UserTokenBuilder {
192192
pub async fn get_user_token<RE, C, F>(
193193
self,
194194
http_client: C,
195-
state: Option<&str>,
196-
code: oauth2::AuthorizationCode,
195+
state: &str,
196+
// TODO: Should be either str or AuthorizationCode
197+
code: &str,
197198
) -> Result<UserToken, UserTokenExchangeError<RE>>
198199
where
199200
RE: std::error::Error + Send + Sync + 'static,
200201
C: Copy + FnOnce(HttpRequest) -> F,
201202
F: Future<Output = Result<HttpResponse, RE>>,
202203
{
203204
if let Some(csrf) = self.csrf {
204-
if state.is_none() || csrf.secret() != state.expect("should not fail") {
205+
if csrf.secret() != state {
205206
return Err(UserTokenExchangeError::StateMismatch);
206207
}
207208
} else {
@@ -214,7 +215,7 @@ impl UserTokenBuilder {
214215
let mut params = HashMap::new();
215216
params.insert("client_id", self.client_id.as_str());
216217
params.insert("client_secret", self.client_secret.secret().as_str());
217-
params.insert("code", code.secret().as_str());
218+
params.insert("code", code);
218219
params.insert("grant_type", "authorization_code");
219220
params.insert("redirect_uri", self.redirect_url.as_str());
220221
let req = HttpRequest {
@@ -278,17 +279,13 @@ mod tests {
278279
let mut t = UserTokenBuilder::new(
279280
ClientId::new("clientid".to_string()),
280281
ClientSecret::new("secret".to_string()),
281-
oauth2::RedirectUrl::new(r#"https://localhost"#.to_string()).unwrap(),
282+
crate::RedirectUrl::new(r#"https://localhost"#.to_string()).unwrap(),
282283
)
283284
.unwrap()
284285
.force_verify(true);
285286
t.csrf = Some(oauth2::CsrfToken::new("random".to_string()));
286287
let token = t
287-
.get_user_token(
288-
crate::client::surf_http_client,
289-
Some("random"),
290-
oauth2::AuthorizationCode::new("authcode".to_string()),
291-
)
288+
.get_user_token(crate::client::surf_http_client, "random", "authcode")
292289
.await
293290
.unwrap();
294291
println!("token: {:?} - {}", token, token.access_token.secret());

0 commit comments

Comments
 (0)