Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Into<String> in new type constructors #169

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ pretty_assertions = "1.0"
reqwest = { version = "0.12", features = ["blocking", "rustls-tls"], default-features = false }
retry = "1.0"
anyhow = "1.0"

[patch.crates-io]
# TEMP: https://github.com/ramosbugs/oauth2-rs/pull/275
oauth2 = { git = "https://github.com/gibbz00/oauth2-rs", branch = "into_string" }
8 changes: 4 additions & 4 deletions examples/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() {
env::var("GITLAB_CLIENT_SECRET")
.expect("Missing the GITLAB_CLIENT_SECRET environment variable."),
);
let issuer_url = IssuerUrl::new("https://gitlab.com".to_string()).unwrap_or_else(|err| {
let issuer_url = IssuerUrl::new("https://gitlab.com").unwrap_or_else(|err| {
handle_error(&err, "Invalid issuer URL");
unreachable!();
});
Expand Down Expand Up @@ -93,7 +93,7 @@ fn main() {
// This example will be running its own server at localhost:8080.
// See below for the server implementation.
.set_redirect_uri(
RedirectUrl::new("http://localhost:8080".to_string()).unwrap_or_else(|err| {
RedirectUrl::new("http://localhost:8080").unwrap_or_else(|err| {
handle_error(&err, "Invalid redirect URL");
unreachable!();
}),
Expand All @@ -107,8 +107,8 @@ fn main() {
Nonce::new_random,
)
// This example is requesting access to the the user's profile including email.
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("email"))
.add_scope(Scope::new("profile"))
.url();

println!("Open this URL in your browser:\n{authorize_url}\n");
Expand Down
15 changes: 7 additions & 8 deletions examples/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ fn main() {
env::var("GOOGLE_CLIENT_SECRET")
.expect("Missing the GOOGLE_CLIENT_SECRET environment variable."),
);
let issuer_url =
IssuerUrl::new("https://accounts.google.com".to_string()).unwrap_or_else(|err| {
handle_error(&err, "Invalid issuer URL");
unreachable!();
});
let issuer_url = IssuerUrl::new("https://accounts.google.com").unwrap_or_else(|err| {
handle_error(&err, "Invalid issuer URL");
unreachable!();
});

let http_client = reqwest::blocking::ClientBuilder::new()
// Following redirects opens the client up to SSRF vulnerabilities.
Expand Down Expand Up @@ -128,7 +127,7 @@ fn main() {
// This example will be running its own server at localhost:8080.
// See below for the server implementation.
.set_redirect_uri(
RedirectUrl::new("http://localhost:8080".to_string()).unwrap_or_else(|err| {
RedirectUrl::new("http://localhost:8080").unwrap_or_else(|err| {
handle_error(&err, "Invalid redirect URL");
unreachable!();
}),
Expand All @@ -149,8 +148,8 @@ fn main() {
Nonce::new_random,
)
// This example is requesting access to the "calendar" features and the user's profile.
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("email"))
.add_scope(Scope::new("profile"))
.url();

println!("Open this URL in your browser:\n{}\n", authorize_url);
Expand Down
2 changes: 1 addition & 1 deletion examples/okta_device_grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn main() -> Result<(), anyhow::Error> {

let details: CoreDeviceAuthorizationResponse = client
.exchange_device_code()
.add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("profile"))
.request(&http_client)
.unwrap_or_else(|err| {
handle_error(&err, "Failed to get device code");
Expand Down
87 changes: 42 additions & 45 deletions src/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ mod tests {
> {
color_backtrace::install();
CoreClient::new(
ClientId::new("aaa".to_string()),
IssuerUrl::new("https://example".to_string()).unwrap(),
ClientId::new("aaa"),
IssuerUrl::new("https://example").unwrap(),
JsonWebKeySet::default(),
)
.set_client_secret(ClientSecret::new("bbb".to_string()))
.set_auth_uri(AuthUrl::new("https://example/authorize".to_string()).unwrap())
.set_token_uri(TokenUrl::new("https://example/token".to_string()).unwrap())
.set_client_secret(ClientSecret::new("bbb"))
.set_auth_uri(AuthUrl::new("https://example/authorize").unwrap())
.set_token_uri(TokenUrl::new("https://example/token").unwrap())
}

#[test]
Expand All @@ -301,8 +301,8 @@ mod tests {
let (authorize_url, _, _) = client
.authorize_url(
AuthenticationFlow::AuthorizationCode::<CoreResponseType>,
|| CsrfToken::new("CSRF123".to_string()),
|| Nonce::new("NONCE456".to_string()),
|| CsrfToken::new("CSRF123"),
|| Nonce::new("NONCE456"),
)
.url();

Expand All @@ -320,8 +320,8 @@ mod tests {
let (authorize_url, _, _) = client
.authorize_url(
AuthenticationFlow::<CoreResponseType>::Implicit(true),
|| CsrfToken::new("CSRF123".to_string()),
|| Nonce::new("NONCE456".to_string()),
|| CsrfToken::new("CSRF123"),
|| Nonce::new("NONCE456"),
)
.url();

Expand All @@ -342,8 +342,8 @@ mod tests {
CoreResponseType::Code,
CoreResponseType::Extension("other".to_string()),
]),
|| CsrfToken::new("CSRF123".to_string()),
|| Nonce::new("NONCE456".to_string()),
|| CsrfToken::new("CSRF123"),
|| Nonce::new("NONCE456"),
)
.url();

Expand All @@ -356,30 +356,30 @@ mod tests {

#[test]
fn test_authorize_url_full() {
let client = new_client()
.set_redirect_uri(RedirectUrl::new("http://localhost:8888/".to_string()).unwrap());
let client =
new_client().set_redirect_uri(RedirectUrl::new("http://localhost:8888/").unwrap());

let flow = CoreAuthenticationFlow::AuthorizationCode;

fn new_csrf() -> CsrfToken {
CsrfToken::new("CSRF123".to_string())
CsrfToken::new("CSRF123")
}
fn new_nonce() -> Nonce {
Nonce::new("NONCE456".to_string())
Nonce::new("NONCE456")
}

let (authorize_url, _, _) = client
.authorize_url(flow.clone(), new_csrf, new_nonce)
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("email"))
.set_display(CoreAuthDisplay::Touch)
.add_prompt(CoreAuthPrompt::Login)
.add_prompt(CoreAuthPrompt::Consent)
.set_max_age(Duration::from_secs(1800))
.add_ui_locale(LanguageTag::new("fr-CA".to_string()))
.add_ui_locale(LanguageTag::new("fr".to_string()))
.add_ui_locale(LanguageTag::new("en".to_string()))
.add_ui_locale(LanguageTag::new("fr-CA"))
.add_ui_locale(LanguageTag::new("fr"))
.add_ui_locale(LanguageTag::new("en"))
.add_auth_context_value(AuthenticationContextClass::new(
"urn:mace:incommon:iap:silver".to_string(),
"urn:mace:incommon:iap:silver",
))
.url();
assert_eq!(
Expand All @@ -405,18 +405,18 @@ mod tests {

let (authorize_url, _, _) = client
.authorize_url(flow.clone(), new_csrf, new_nonce)
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("email"))
.set_display(CoreAuthDisplay::Touch)
.set_id_token_hint(&id_token)
.set_login_hint(LoginHint::new("[email protected]".to_string()))
.set_login_hint(LoginHint::new("[email protected]"))
.add_prompt(CoreAuthPrompt::Login)
.add_prompt(CoreAuthPrompt::Consent)
.set_max_age(Duration::from_secs(1800))
.add_ui_locale(LanguageTag::new("fr-CA".to_string()))
.add_ui_locale(LanguageTag::new("fr".to_string()))
.add_ui_locale(LanguageTag::new("en".to_string()))
.add_ui_locale(LanguageTag::new("fr-CA"))
.add_ui_locale(LanguageTag::new("fr"))
.add_ui_locale(LanguageTag::new("en"))
.add_auth_context_value(AuthenticationContextClass::new(
"urn:mace:incommon:iap:silver".to_string(),
"urn:mace:incommon:iap:silver",
))
.add_extra_param("foo", "bar")
.url();
Expand All @@ -434,21 +434,18 @@ mod tests {

let (authorize_url, _, _) = client
.authorize_url(flow, new_csrf, new_nonce)
.add_scopes(vec![
Scope::new("email".to_string()),
Scope::new("profile".to_string()),
])
.add_scopes(vec![Scope::new("email"), Scope::new("profile")])
.set_display(CoreAuthDisplay::Touch)
.set_id_token_hint(&id_token)
.set_login_hint(LoginHint::new("[email protected]".to_string()))
.set_login_hint(LoginHint::new("[email protected]"))
.add_prompt(CoreAuthPrompt::Login)
.add_prompt(CoreAuthPrompt::Consent)
.set_max_age(Duration::from_secs(1800))
.add_ui_locale(LanguageTag::new("fr-CA".to_string()))
.add_ui_locale(LanguageTag::new("fr".to_string()))
.add_ui_locale(LanguageTag::new("en".to_string()))
.add_ui_locale(LanguageTag::new("fr-CA"))
.add_ui_locale(LanguageTag::new("fr"))
.add_ui_locale(LanguageTag::new("en"))
.add_auth_context_value(AuthenticationContextClass::new(
"urn:mace:incommon:iap:silver".to_string(),
"urn:mace:incommon:iap:silver",
))
.add_extra_param("foo", "bar")
.url();
Expand All @@ -467,33 +464,33 @@ mod tests {

#[test]
fn test_authorize_url_redirect_url_override() {
let client = new_client()
.set_redirect_uri(RedirectUrl::new("http://localhost:8888/".to_string()).unwrap());
let client =
new_client().set_redirect_uri(RedirectUrl::new("http://localhost:8888/").unwrap());

let flow = CoreAuthenticationFlow::AuthorizationCode;

fn new_csrf() -> CsrfToken {
CsrfToken::new("CSRF123".to_string())
CsrfToken::new("CSRF123")
}
fn new_nonce() -> Nonce {
Nonce::new("NONCE456".to_string())
Nonce::new("NONCE456")
}

let (authorize_url, _, _) = client
.authorize_url(flow, new_csrf, new_nonce)
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("email"))
.set_display(CoreAuthDisplay::Touch)
.add_prompt(CoreAuthPrompt::Login)
.add_prompt(CoreAuthPrompt::Consent)
.set_max_age(Duration::from_secs(1800))
.add_ui_locale(LanguageTag::new("fr-CA".to_string()))
.add_ui_locale(LanguageTag::new("fr".to_string()))
.add_ui_locale(LanguageTag::new("en".to_string()))
.add_ui_locale(LanguageTag::new("fr-CA"))
.add_ui_locale(LanguageTag::new("fr"))
.add_ui_locale(LanguageTag::new("en"))
.add_auth_context_value(AuthenticationContextClass::new(
"urn:mace:incommon:iap:silver".to_string(),
"urn:mace:incommon:iap:silver",
))
.set_redirect_uri(Cow::Owned(
RedirectUrl::new("http://localhost:8888/alternative".to_string()).unwrap(),
RedirectUrl::new("http://localhost:8888/alternative").unwrap(),
))
.url();
assert_eq!(
Expand Down
18 changes: 9 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ const OPENID_SCOPE: &str = "openid";
/// #
/// # let client =
/// # CoreClient::new(
/// # ClientId::new("aaa".to_string()),
/// # IssuerUrl::new("https://example".to_string()).unwrap(),
/// # ClientId::new("aaa"),
/// # IssuerUrl::new("https://example").unwrap(),
/// # JsonWebKeySet::default(),
/// # )
/// # .set_client_secret(ClientSecret::new("bbb".to_string()))
/// # .set_auth_uri(AuthUrl::new("https://example/authorize".to_string()).unwrap())
/// # .set_token_uri(TokenUrl::new("https://example/token".to_string()).unwrap())
/// # .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
/// # .set_client_secret(ClientSecret::new("bbb"))
/// # .set_auth_uri(AuthUrl::new("https://example/authorize").unwrap())
/// # .set_token_uri(TokenUrl::new("https://example/token").unwrap())
/// # .set_revocation_url(RevocationUrl::new("https://revocation/url").unwrap());
/// #
/// # #[derive(Debug, Error)]
/// # enum FakeError {
Expand All @@ -81,7 +81,7 @@ const OPENID_SCOPE: &str = "openid";
/// # };
/// #
/// let res = client
/// .revoke_token(AccessToken::new("some token".to_string()).into())
/// .revoke_token(AccessToken::new("some token").into())
/// .unwrap()
/// .request(&http_client);
///
Expand Down Expand Up @@ -782,7 +782,7 @@ where
ui_locales: Vec::new(),
};
if self.use_openid_scope {
request.add_scope(Scope::new(OPENID_SCOPE.to_string()))
request.add_scope(Scope::new(OPENID_SCOPE))
} else {
request
}
Expand Down Expand Up @@ -1124,7 +1124,7 @@ where
pub fn exchange_device_code(&self) -> DeviceAuthorizationRequest<TE> {
let request = self.oauth2_client.exchange_device_code();
if self.use_openid_scope {
request.add_scope(Scope::new(OPENID_SCOPE.to_string()))
request.add_scope(Scope::new(OPENID_SCOPE))
} else {
request
}
Expand Down
20 changes: 7 additions & 13 deletions src/core/jwk/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_core_jwk_deserialization_rsa() {
let key: CoreJsonWebKey = serde_json::from_str(json).expect("deserialization failed");
assert_eq!(key.kty, CoreJsonWebKeyType::RSA);
assert_eq!(key.use_, Some(CoreJsonWebKeyUse::Signature));
assert_eq!(key.kid, Some(JsonWebKeyId::new("2011-04-29".to_string())));
assert_eq!(key.kid, Some(JsonWebKeyId::new("2011-04-29")));
assert_eq!(
key.n,
Some(Base64UrlEncodedBytes::new(vec![
Expand Down Expand Up @@ -71,7 +71,7 @@ fn test_core_jwk_deserialization_ec() {
let key: CoreJsonWebKey = serde_json::from_str(json).expect("deserialization failed");
assert_eq!(key.kty, CoreJsonWebKeyType::EllipticCurve);
assert_eq!(key.use_, Some(CoreJsonWebKeyUse::Signature));
assert_eq!(key.kid, Some(JsonWebKeyId::new("2011-04-29".to_string())));
assert_eq!(key.kid, Some(JsonWebKeyId::new("2011-04-29")));
assert_eq!(key.crv, Some(CoreJsonCurveType::P256));
assert_eq!(
key.y,
Expand Down Expand Up @@ -970,17 +970,11 @@ fn test_jwks_unsupported_key() {

assert_eq!(jwks.keys()[0].kty, CoreJsonWebKeyType::RSA);
assert_eq!(jwks.keys()[0].use_, Some(CoreJsonWebKeyUse::Signature));
assert_eq!(
jwks.keys()[0].kid,
Some(JsonWebKeyId::new("2011-04-29".to_string()))
);
assert_eq!(jwks.keys()[0].kid, Some(JsonWebKeyId::new("2011-04-29")));

assert_eq!(jwks.keys()[1].kty, CoreJsonWebKeyType::EllipticCurve);
assert_eq!(jwks.keys()[1].use_, Some(CoreJsonWebKeyUse::Signature));
assert_eq!(
jwks.keys()[1].kid,
Some(JsonWebKeyId::new("2011-05-01".to_string()))
);
assert_eq!(jwks.keys()[1].kid, Some(JsonWebKeyId::new("2011-05-01")));
assert_eq!(jwks.keys()[1].crv, Some(CoreJsonCurveType::P256));
}

Expand Down Expand Up @@ -1010,7 +1004,7 @@ fn test_jwks_unsupported_alg() {
.expect("deserialization should succeed");
assert_eq!(jwks.keys().len(), 1);
let key = &jwks.keys()[0];
assert_eq!(&key.kid, &Some(JsonWebKeyId::new("2011-05-01".to_string())));
assert_eq!(&key.kid, &Some(JsonWebKeyId::new("2011-05-01")));
}

// Test filtering keys by algorithm
Expand Down Expand Up @@ -1050,7 +1044,7 @@ fn test_jwks_same_kid_different_alg() {

{
let keys = jwks.filter_keys(
Some(&JsonWebKeyId::new("2011-04-29".to_string())),
Some(&JsonWebKeyId::new("2011-04-29")),
&CoreJwsSigningAlgorithm::RsaSsaPssSha384,
);
assert_eq!(keys.len(), 1);
Expand All @@ -1064,7 +1058,7 @@ fn test_jwks_same_kid_different_alg() {

{
let keys = jwks.filter_keys(
Some(&JsonWebKeyId::new("2011-04-29".to_string())),
Some(&JsonWebKeyId::new("2011-04-29")),
&CoreJwsSigningAlgorithm::RsaSsaPssSha512,
);
assert_eq!(keys.len(), 0);
Expand Down
Loading