Skip to content

Commit 9e31a45

Browse files
authored
Rename set_*_url() to set_*_uri() (#128)
1 parent d92f4e1 commit 9e31a45

File tree

7 files changed

+88
-51
lines changed

7 files changed

+88
-51
lines changed

examples/github.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848
)
4949
// This example will be running its own server at localhost:8080.
5050
// See below for the server implementation.
51-
.set_redirect_url(
51+
.set_redirect_uri(
5252
RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"),
5353
);
5454

examples/github_async.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async fn main() {
4949
)
5050
// This example will be running its own server at localhost:8080.
5151
// See below for the server implementation.
52-
.set_redirect_url(
52+
.set_redirect_uri(
5353
RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"),
5454
);
5555

examples/google.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ fn main() {
4747
)
4848
// This example will be running its own server at localhost:8080.
4949
// See below for the server implementation.
50-
.set_redirect_url(
50+
.set_redirect_uri(
5151
RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"),
5252
)
5353
// Google supports OAuth 2.0 Token Revocation (RFC-7009)
54-
.set_revocation_url(
54+
.set_revocation_uri(
5555
RevocationUrl::new("https://oauth2.googleapis.com/revoke".to_string())
5656
.expect("Invalid revocation endpoint URL"),
5757
);

examples/msgraph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() {
6161
.set_auth_type(AuthType::RequestBody)
6262
// This example will be running its own server at localhost:3003.
6363
// See below for the server implementation.
64-
.set_redirect_url(
64+
.set_redirect_uri(
6565
RedirectUrl::new("http://localhost:3003/redirect".to_string())
6666
.expect("Invalid redirect URL"),
6767
);

examples/wunderlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ fn main() {
160160
)
161161
// This example will be running its own server at localhost:8080.
162162
// See below for the server implementation.
163-
.set_redirect_url(
163+
.set_redirect_uri(
164164
RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"),
165165
);
166166

src/lib.rs

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
//! Some(TokenUrl::new("http://token".to_string())?)
115115
//! )
116116
//! // Set the URL the user will be redirected to after the authorization process.
117-
//! .set_redirect_url(RedirectUrl::new("http://redirect".to_string())?);
117+
//! .set_redirect_uri(RedirectUrl::new("http://redirect".to_string())?);
118118
//!
119119
//! // Generate a PKCE challenge.
120120
//! let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
@@ -185,7 +185,7 @@
185185
//! Some(TokenUrl::new("http://token".to_string())?)
186186
//! )
187187
//! // Set the URL the user will be redirected to after the authorization process.
188-
//! .set_redirect_url(RedirectUrl::new("http://redirect".to_string())?);
188+
//! .set_redirect_uri(RedirectUrl::new("http://redirect".to_string())?);
189189
//!
190190
//! // Generate a PKCE challenge.
191191
//! let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
@@ -508,7 +508,7 @@ pub use types::{
508508
UserCode,
509509
};
510510

511-
pub use crate::revocation::RevocableToken;
511+
pub use revocation::{RevocableToken, RevocationErrorResponseType, StandardRevocableToken};
512512

513513
const CONTENT_TYPE_JSON: &str = "application/json";
514514
const CONTENT_TYPE_FORMENCODED: &str = "application/x-www-form-urlencoded";
@@ -561,20 +561,49 @@ pub enum AuthType {
561561
/// - Generic type `TRE` (aka Token Revocation Error) for errors defined by [RFC 7009 OAuth 2.0 Token Revocation](https://tools.ietf.org/html/rfc7009).
562562
///
563563
/// For example when revoking a token, error code `unsupported_token_type` (from RFC 7009) may be returned:
564-
/// ```ignore
565-
/// let revocation_response = client
564+
/// ```rust
565+
/// # use thiserror::Error;
566+
/// # use http::status::StatusCode;
567+
/// # use http::header::{HeaderValue, CONTENT_TYPE};
568+
/// # use oauth2::{*, basic::*};
569+
/// # let client = BasicClient::new(
570+
/// # ClientId::new("aaa".to_string()),
571+
/// # Some(ClientSecret::new("bbb".to_string())),
572+
/// # AuthUrl::new("https://example.com/auth".to_string()).unwrap(),
573+
/// # Some(TokenUrl::new("https://example.com/token".to_string()).unwrap()),
574+
/// # )
575+
/// # .set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
576+
/// #
577+
/// # #[derive(Debug, Error)]
578+
/// # enum FakeError {
579+
/// # #[error("error")]
580+
/// # Err,
581+
/// # }
582+
/// #
583+
/// # let http_client = |_| -> Result<HttpResponse, FakeError> {
584+
/// # Ok(HttpResponse {
585+
/// # status_code: StatusCode::BAD_REQUEST,
586+
/// # headers: vec![(
587+
/// # CONTENT_TYPE,
588+
/// # HeaderValue::from_str("application/json").unwrap(),
589+
/// # )]
590+
/// # .into_iter()
591+
/// # .collect(),
592+
/// # body: "{\"error\": \"unsupported_token_type\", \"error_description\": \"stuff happened\", \
593+
/// # \"error_uri\": \"https://errors\"}"
594+
/// # .to_string()
595+
/// # .into_bytes(),
596+
/// # })
597+
/// # };
598+
/// #
599+
/// let res = client
566600
/// .revoke_token(AccessToken::new("some token".to_string()).into())
567-
/// .request(...)
568-
/// .unwrap();
569-
///
570-
/// assert!(matches!(revocation_response, Err(
571-
/// RequestTokenError::ServerResponse(
572-
/// BasicRevocationErrorResponse{
573-
/// error: RevocationErrorResponseType::UnsupportedTokenType,
574-
/// ..
575-
/// })
576-
/// )
577-
/// ));
601+
/// .unwrap()
602+
/// .request(http_client);
603+
///
604+
/// assert!(matches!(res, Err(
605+
/// RequestTokenError::ServerResponse(err)) if matches!(err.error(),
606+
/// RevocationErrorResponseType::UnsupportedTokenType)));
578607
/// ```
579608
///
580609
#[derive(Clone, Debug)]
@@ -664,7 +693,7 @@ where
664693
///
665694
/// Sets the the redirect URL used by the authorization endpoint.
666695
///
667-
pub fn set_redirect_url(mut self, redirect_url: RedirectUrl) -> Self {
696+
pub fn set_redirect_uri(mut self, redirect_url: RedirectUrl) -> Self {
668697
self.redirect_url = Some(redirect_url);
669698

670699
self
@@ -674,7 +703,7 @@ where
674703
/// Sets the introspection URL for contacting the ([RFC 7662](https://tools.ietf.org/html/rfc7662))
675704
/// introspection endpoint.
676705
///
677-
pub fn set_introspection_url(mut self, introspection_url: IntrospectionUrl) -> Self {
706+
pub fn set_introspection_uri(mut self, introspection_url: IntrospectionUrl) -> Self {
678707
self.introspection_url = Some(introspection_url);
679708

680709
self
@@ -685,7 +714,7 @@ where
685714
///
686715
/// See: [`revoke_token()`](Self::revoke_token())
687716
///
688-
pub fn set_revocation_url(mut self, revocation_url: RevocationUrl) -> Self {
717+
pub fn set_revocation_uri(mut self, revocation_url: RevocationUrl) -> Self {
689718
self.revocation_url = Some(revocation_url);
690719

691720
self
@@ -876,6 +905,12 @@ where
876905
/// Query the authorization server [`RFC 7662 compatible`](https://tools.ietf.org/html/rfc7662) introspection
877906
/// endpoint to determine the set of metadata for a previously received token.
878907
///
908+
/// Requires that [`set_introspection_uri()`](Self::set_introspection_uri()) have already been called to set the
909+
/// introspection endpoint URL.
910+
///
911+
/// Attempting to submit the generated request without calling [`set_introspection_uri()`](Self::set_introspection_uri())
912+
/// first will result in an error.
913+
///
879914
pub fn introspect<'a>(
880915
&'a self,
881916
token: &'a AccessToken,
@@ -899,9 +934,10 @@ where
899934
/// Attempts to revoke the given previously received token using an [RFC 7009 OAuth 2.0 Token Revocation](https://tools.ietf.org/html/rfc7009)
900935
/// compatible endpoint.
901936
///
902-
/// Requires that [`set_revocation_url()`](Self::set_revocation_url()) have already been called to set the revocation endpoint URL.
937+
/// Requires that [`set_revocation_uri()`](Self::set_revocation_uri()) have already been called to set the
938+
/// revocation endpoint URL.
903939
///
904-
/// Attempting to submit the generated request without calling [`set_revocation_url()`](Self::set_revocation_url())
940+
/// Attempting to submit the generated request without calling [`set_revocation_uri()`](Self::set_revocation_uri())
905941
/// first will result in an error.
906942
///
907943
pub fn revoke_token(
@@ -1010,7 +1046,7 @@ impl<'a> AuthorizationRequest<'a> {
10101046
///
10111047
/// Overrides the `redirect_url` to the one specified.
10121048
///
1013-
pub fn set_redirect_url(mut self, redirect_url: Cow<'a, RedirectUrl>) -> Self {
1049+
pub fn set_redirect_uri(mut self, redirect_url: Cow<'a, RedirectUrl>) -> Self {
10141050
self.redirect_url = Some(redirect_url);
10151051
self
10161052
}

src/tests.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn test_authorize_url_with_extension_response_type() {
219219
#[test]
220220
fn test_authorize_url_with_redirect_url() {
221221
let client = new_client()
222-
.set_redirect_url(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
222+
.set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
223223

224224
let (url, _) = client
225225
.authorize_url(|| CsrfToken::new("csrf_token".to_string()))
@@ -240,11 +240,11 @@ fn test_authorize_url_with_redirect_url() {
240240
#[test]
241241
fn test_authorize_url_with_redirect_url_override() {
242242
let client = new_client()
243-
.set_redirect_url(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
243+
.set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
244244

245245
let (url, _) = client
246246
.authorize_url(|| CsrfToken::new("csrf_token".to_string()))
247-
.set_redirect_url(Cow::Owned(
247+
.set_redirect_uri(Cow::Owned(
248248
RedirectUrl::new("https://localhost/alternative".to_string()).unwrap(),
249249
))
250250
.url();
@@ -611,7 +611,7 @@ fn test_exchange_password_with_json_response() {
611611
fn test_exchange_code_successful_with_redirect_url() {
612612
let client = new_client()
613613
.set_auth_type(AuthType::RequestBody)
614-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
614+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
615615

616616
let token = client
617617
.exchange_code(AuthorizationCode::new("ccc".to_string()))
@@ -659,7 +659,7 @@ fn test_exchange_code_successful_with_redirect_url() {
659659
fn test_exchange_code_successful_with_basic_auth() {
660660
let client = new_client()
661661
.set_auth_type(AuthType::BasicAuth)
662-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
662+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
663663

664664
let token = client
665665
.exchange_code(AuthorizationCode::new("ccc".to_string()))
@@ -707,7 +707,7 @@ fn test_exchange_code_successful_with_basic_auth() {
707707
fn test_exchange_code_successful_with_pkce_and_extension() {
708708
let client = new_client()
709709
.set_auth_type(AuthType::BasicAuth)
710-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
710+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
711711

712712
let token = client
713713
.exchange_code(AuthorizationCode::new("ccc".to_string()))
@@ -763,7 +763,7 @@ fn test_exchange_code_successful_with_pkce_and_extension() {
763763
fn test_exchange_refresh_token_successful_with_extension() {
764764
let client = new_client()
765765
.set_auth_type(AuthType::BasicAuth)
766-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
766+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap());
767767

768768
let token = client
769769
.exchange_refresh_token(&RefreshToken::new("ccc".to_string()))
@@ -1535,8 +1535,8 @@ fn test_deserialize_optional_string_or_vec_string_vec() {
15351535
fn test_token_introspection_successful_with_basic_auth_minimal_response() {
15361536
let client = new_client()
15371537
.set_auth_type(AuthType::BasicAuth)
1538-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap())
1539-
.set_introspection_url(
1538+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap())
1539+
.set_introspection_uri(
15401540
IntrospectionUrl::new("https://introspection/url".to_string()).unwrap(),
15411541
);
15421542

@@ -1586,8 +1586,8 @@ fn test_token_introspection_successful_with_basic_auth_minimal_response() {
15861586
fn test_token_introspection_successful_with_basic_auth_full_response() {
15871587
let client = new_client()
15881588
.set_auth_type(AuthType::BasicAuth)
1589-
.set_redirect_url(RedirectUrl::new("https://redirect/here".to_string()).unwrap())
1590-
.set_introspection_url(
1589+
.set_redirect_uri(RedirectUrl::new("https://redirect/here".to_string()).unwrap())
1590+
.set_introspection_uri(
15911591
IntrospectionUrl::new("https://introspection/url".to_string()).unwrap(),
15921592
);
15931593

@@ -1691,7 +1691,7 @@ fn test_token_revocation_with_non_https_url() {
16911691
let client = new_client();
16921692

16931693
let result = client
1694-
.set_revocation_url(RevocationUrl::new("http://revocation/url".to_string()).unwrap())
1694+
.set_revocation_uri(RevocationUrl::new("http://revocation/url".to_string()).unwrap())
16951695
.revoke_token(AccessToken::new("access_token_123".to_string()).into())
16961696
.unwrap_err();
16971697

@@ -1704,7 +1704,7 @@ fn test_token_revocation_with_non_https_url() {
17041704
#[test]
17051705
fn test_token_revocation_with_unsupported_token_type() {
17061706
let client = new_client()
1707-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1707+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
17081708

17091709
let revocation_response = client
17101710
.revoke_token(AccessToken::new("access_token_123".to_string()).into()).unwrap()
@@ -1731,20 +1731,21 @@ fn test_token_revocation_with_unsupported_token_type() {
17311731
},
17321732
));
17331733

1734-
assert!(matches!(revocation_response, Err(
1735-
RequestTokenError::ServerResponse(
1736-
BasicRevocationErrorResponse{
1734+
assert!(matches!(
1735+
revocation_response,
1736+
Err(RequestTokenError::ServerResponse(
1737+
BasicRevocationErrorResponse {
17371738
error: RevocationErrorResponseType::UnsupportedTokenType,
17381739
..
1739-
})
1740-
)
1740+
}
1741+
))
17411742
));
17421743
}
17431744

17441745
#[test]
17451746
fn test_token_revocation_with_access_token_and_empty_json_response() {
17461747
let client = new_client()
1747-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1748+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
17481749

17491750
client
17501751
.revoke_token(AccessToken::new("access_token_123".to_string()).into())
@@ -1774,7 +1775,7 @@ fn test_token_revocation_with_access_token_and_empty_json_response() {
17741775
#[test]
17751776
fn test_token_revocation_with_access_token_and_empty_response() {
17761777
let client = new_client()
1777-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1778+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
17781779

17791780
client
17801781
.revoke_token(AccessToken::new("access_token_123".to_string()).into())
@@ -1799,7 +1800,7 @@ fn test_token_revocation_with_access_token_and_empty_response() {
17991800
#[test]
18001801
fn test_token_revocation_with_access_token_and_non_json_response() {
18011802
let client = new_client()
1802-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1803+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
18031804

18041805
client
18051806
.revoke_token(AccessToken::new("access_token_123".to_string()).into())
@@ -1829,7 +1830,7 @@ fn test_token_revocation_with_access_token_and_non_json_response() {
18291830
#[test]
18301831
fn test_token_revocation_with_refresh_token() {
18311832
let client = new_client()
1832-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1833+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
18331834

18341835
client
18351836
.revoke_token(RefreshToken::new("refresh_token_123".to_string()).into())
@@ -1865,7 +1866,7 @@ fn test_extension_token_revocation_successful() {
18651866
AuthUrl::new("https://example.com/auth".to_string()).unwrap(),
18661867
Some(TokenUrl::new("https://example.com/token".to_string()).unwrap()),
18671868
)
1868-
.set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
1869+
.set_revocation_uri(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
18691870

18701871
client
18711872
.revoke_token(ColorfulRevocableToken::Red(

0 commit comments

Comments
 (0)