clients.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //! `oauth2`-crate client builder for the E2E suites (baseline flows only:
  2. //! authorization-code + PKCE, client-credentials, device).
  3. //!
  4. //! The v5 typestate requires each endpoint to be set before the matching flow
  5. //! method exists, so the setters chain in one expression and the return type
  6. //! names the set state (`EndpointSet` for auth + token). Extension flows
  7. //! (DPoP, mTLS, PAR, JAR, token exchange, DCR) stay hand-rolled `reqwest`.
  8. //!
  9. //! Included by the grant and conformance suites, which both use it.
  10. use super::harness::{TestError, fail};
  11. /// Build an `oauth2`-crate client pointed at this world's base URL.
  12. pub fn oauth2_client(
  13. base: &str,
  14. client_id: &str,
  15. secret: Option<&str>,
  16. ) -> Result<
  17. oauth2::basic::BasicClient<
  18. oauth2::EndpointSet,
  19. oauth2::EndpointNotSet,
  20. oauth2::EndpointNotSet,
  21. oauth2::EndpointNotSet,
  22. oauth2::EndpointSet,
  23. >,
  24. TestError,
  25. > {
  26. use oauth2::{AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl, basic::BasicClient};
  27. let auth_uri = AuthUrl::new(format!("{base}/authorize"))
  28. .map_err(|error| fail(format!("auth url: {error}")))?;
  29. let token_uri =
  30. TokenUrl::new(format!("{base}/token")).map_err(|error| fail(format!("token url: {error}")))?;
  31. let redirect_uri = RedirectUrl::new("http://127.0.0.1:9/cb".to_owned())
  32. .map_err(|error| fail(format!("redirect url: {error}")))?;
  33. let client = BasicClient::new(ClientId::new(client_id.to_owned()));
  34. let client = match secret {
  35. Some(secret) => client.set_client_secret(ClientSecret::new(secret.to_owned())),
  36. None => client,
  37. };
  38. Ok(
  39. client
  40. .set_auth_uri(auth_uri)
  41. .set_redirect_uri(redirect_uri)
  42. .set_token_uri(token_uri),
  43. )
  44. }