Skip to content

Commit

Permalink
Add ability to override the authorization url (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
mettke authored Jun 8, 2020
1 parent f3c4d99 commit ed23547
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ where
client_id: &self.client_id,
extra_params: Vec::new(),
pkce_challenge: None,
redirect_url: self.redirect_url.as_ref(),
redirect_url: self.redirect_url.as_ref().map(Cow::Borrowed),
response_type: "code".into(),
scopes: Vec::new(),
state: state_fn(),
Expand Down Expand Up @@ -785,7 +785,7 @@ pub struct AuthorizationRequest<'a> {
client_id: &'a ClientId,
extra_params: Vec<(Cow<'a, str>, Cow<'a, str>)>,
pkce_challenge: Option<PkceCodeChallenge>,
redirect_url: Option<&'a RedirectUrl>,
redirect_url: Option<Cow<'a, RedirectUrl>>,
response_type: Cow<'a, str>,
scopes: Vec<Cow<'a, Scope>>,
state: CsrfToken,
Expand Down Expand Up @@ -852,6 +852,14 @@ impl<'a> AuthorizationRequest<'a> {
self
}

///
/// Overrides the `redirect_url` to the one specified.
///
pub fn set_redirect_url(mut self, redirect_url: Cow<'a, RedirectUrl>) -> Self {
self.redirect_url = Some(redirect_url);
self
}

///
/// Returns the full authorization URL and CSRF state for this authorization
/// request.
Expand Down
22 changes: 22 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ fn test_authorize_url_with_redirect_url() {
);
}

#[test]
fn test_authorize_url_with_redirect_url_override() {
let client = new_client()
.set_redirect_url(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());

let (url, _) = client
.authorize_url(|| CsrfToken::new("csrf_token".to_string()))
.set_redirect_url(Cow::Owned(RedirectUrl::new("https://localhost/alternative".to_string()).unwrap()))
.url();

assert_eq!(
Url::parse(
"https://example.com/auth?response_type=code\
&client_id=aaa\
&state=csrf_token\
&redirect_uri=https%3A%2F%2Flocalhost%2Falternative"
)
.unwrap(),
url
);
}

#[derive(Debug, Fail)]
enum FakeError {
#[fail(display = "error")]
Expand Down

0 comments on commit ed23547

Please sign in to comment.