|
|
@ -22,6 +22,7 @@ pub struct Registration<'a, H: HttpSend = HttpSender> { |
|
|
|
base: String, |
|
|
|
base: String, |
|
|
|
client: Client, |
|
|
|
client: Client, |
|
|
|
app_builder: AppBuilder<'a>, |
|
|
|
app_builder: AppBuilder<'a>, |
|
|
|
|
|
|
|
force_login: bool, |
|
|
|
http_sender: H, |
|
|
|
http_sender: H, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -54,6 +55,7 @@ impl<'a> Registration<'a, HttpSender> { |
|
|
|
base: base.into(), |
|
|
|
base: base.into(), |
|
|
|
client: Client::new(), |
|
|
|
client: Client::new(), |
|
|
|
app_builder: AppBuilder::new(), |
|
|
|
app_builder: AppBuilder::new(), |
|
|
|
|
|
|
|
force_login: false, |
|
|
|
http_sender: HttpSender, |
|
|
|
http_sender: HttpSender, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -66,6 +68,7 @@ impl<'a, H: HttpSend> Registration<'a, H> { |
|
|
|
base: base.into(), |
|
|
|
base: base.into(), |
|
|
|
client: Client::new(), |
|
|
|
client: Client::new(), |
|
|
|
app_builder: AppBuilder::new(), |
|
|
|
app_builder: AppBuilder::new(), |
|
|
|
|
|
|
|
force_login: false, |
|
|
|
http_sender, |
|
|
|
http_sender, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -99,6 +102,13 @@ impl<'a, H: HttpSend> Registration<'a, H> { |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Forces the user to re-login (useful if you need to re-auth as a
|
|
|
|
|
|
|
|
/// different user on the same instance
|
|
|
|
|
|
|
|
pub fn force_login(&mut self, force_login: bool) -> &mut Self { |
|
|
|
|
|
|
|
self.force_login = force_login; |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn send(&self, req: RequestBuilder) -> Result<Response> { |
|
|
|
fn send(&self, req: RequestBuilder) -> Result<Response> { |
|
|
|
Ok(self.http_sender.send(&self.client, req)?) |
|
|
|
Ok(self.http_sender.send(&self.client, req)?) |
|
|
|
} |
|
|
|
} |
|
|
@ -138,6 +148,7 @@ impl<'a, H: HttpSend> Registration<'a, H> { |
|
|
|
client_secret: oauth.client_secret, |
|
|
|
client_secret: oauth.client_secret, |
|
|
|
redirect: oauth.redirect_uri, |
|
|
|
redirect: oauth.redirect_uri, |
|
|
|
scopes: app.scopes().clone(), |
|
|
|
scopes: app.scopes().clone(), |
|
|
|
|
|
|
|
force_login: self.force_login, |
|
|
|
http_sender: self.http_sender.clone(), |
|
|
|
http_sender: self.http_sender.clone(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -173,6 +184,7 @@ impl<'a, H: HttpSend> Registration<'a, H> { |
|
|
|
client_secret: oauth.client_secret, |
|
|
|
client_secret: oauth.client_secret, |
|
|
|
redirect: oauth.redirect_uri, |
|
|
|
redirect: oauth.redirect_uri, |
|
|
|
scopes: app.scopes().clone(), |
|
|
|
scopes: app.scopes().clone(), |
|
|
|
|
|
|
|
force_login: self.force_login, |
|
|
|
http_sender: self.http_sender.clone(), |
|
|
|
http_sender: self.http_sender.clone(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -200,6 +212,7 @@ impl Registered<HttpSender> { |
|
|
|
/// "the-client-secret",
|
|
|
|
/// "the-client-secret",
|
|
|
|
/// "https://example.com/redirect",
|
|
|
|
/// "https://example.com/redirect",
|
|
|
|
/// Scopes::read_all(),
|
|
|
|
/// Scopes::read_all(),
|
|
|
|
|
|
|
|
/// false,
|
|
|
|
/// );
|
|
|
|
/// );
|
|
|
|
/// let url = registration.authorize_url()?;
|
|
|
|
/// let url = registration.authorize_url()?;
|
|
|
|
/// // Here you now need to open the url in the browser
|
|
|
|
/// // Here you now need to open the url in the browser
|
|
|
@ -217,6 +230,7 @@ impl Registered<HttpSender> { |
|
|
|
client_secret: &str, |
|
|
|
client_secret: &str, |
|
|
|
redirect: &str, |
|
|
|
redirect: &str, |
|
|
|
scopes: Scopes, |
|
|
|
scopes: Scopes, |
|
|
|
|
|
|
|
force_login: bool, |
|
|
|
) -> Registered<HttpSender> { |
|
|
|
) -> Registered<HttpSender> { |
|
|
|
Registered { |
|
|
|
Registered { |
|
|
|
base: base.to_string(), |
|
|
|
base: base.to_string(), |
|
|
@ -225,6 +239,7 @@ impl Registered<HttpSender> { |
|
|
|
client_secret: client_secret.to_string(), |
|
|
|
client_secret: client_secret.to_string(), |
|
|
|
redirect: redirect.to_string(), |
|
|
|
redirect: redirect.to_string(), |
|
|
|
scopes, |
|
|
|
scopes, |
|
|
|
|
|
|
|
force_login, |
|
|
|
http_sender: HttpSender, |
|
|
|
http_sender: HttpSender, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -245,25 +260,41 @@ impl<H: HttpSend> Registered<H> { |
|
|
|
/// use elefren::{prelude::*, registration::Registered};
|
|
|
|
/// use elefren::{prelude::*, registration::Registered};
|
|
|
|
/// # fn main() -> Result<(), Box<std::error::Error>> {
|
|
|
|
/// # fn main() -> Result<(), Box<std::error::Error>> {
|
|
|
|
///
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let origbase = "https://example.social";
|
|
|
|
|
|
|
|
/// let origclient_id = "some-client_id";
|
|
|
|
|
|
|
|
/// let origclient_secret = "some-client-secret";
|
|
|
|
|
|
|
|
/// let origredirect = "https://example.social/redirect";
|
|
|
|
|
|
|
|
/// let origscopes = Scopes::all();
|
|
|
|
|
|
|
|
/// let origforce_login = false;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// let registered = Registered::from_parts(
|
|
|
|
/// let registered = Registered::from_parts(
|
|
|
|
/// "https://example.social",
|
|
|
|
/// origbase,
|
|
|
|
/// "some-client-id",
|
|
|
|
/// origclient_id,
|
|
|
|
/// "some-client-secret",
|
|
|
|
/// origclient_secret,
|
|
|
|
/// "https://example.social/redirect",
|
|
|
|
/// origredirect,
|
|
|
|
/// Scopes::all(),
|
|
|
|
/// origscopes.clone(),
|
|
|
|
|
|
|
|
/// origforce_login,
|
|
|
|
/// );
|
|
|
|
/// );
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let (base, client_id, client_secret, redirect, scopes) = registered.into_parts();
|
|
|
|
/// let (base, client_id, client_secret, redirect, scopes, force_login) = registered.into_parts();
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// assert_eq!(origbase, &base);
|
|
|
|
|
|
|
|
/// assert_eq!(origclient_id, &client_id);
|
|
|
|
|
|
|
|
/// assert_eq!(origclient_secret, &client_secret);
|
|
|
|
|
|
|
|
/// assert_eq!(origredirect, &redirect);
|
|
|
|
|
|
|
|
/// assert_eq!(origscopes, scopes);
|
|
|
|
|
|
|
|
/// assert_eq!(origforce_login, force_login);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub fn into_parts(self) -> (String, String, String, String, Scopes) { |
|
|
|
pub fn into_parts(self) -> (String, String, String, String, Scopes, bool) { |
|
|
|
( |
|
|
|
( |
|
|
|
self.base, |
|
|
|
self.base, |
|
|
|
self.client_id, |
|
|
|
self.client_id, |
|
|
|
self.client_secret, |
|
|
|
self.client_secret, |
|
|
|
self.redirect, |
|
|
|
self.redirect, |
|
|
|
self.scopes, |
|
|
|
self.scopes, |
|
|
|
|
|
|
|
self.force_login, |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -272,10 +303,18 @@ impl<H: HttpSend> Registered<H> { |
|
|
|
pub fn authorize_url(&self) -> Result<String> { |
|
|
|
pub fn authorize_url(&self) -> Result<String> { |
|
|
|
let scopes = format!("{}", self.scopes); |
|
|
|
let scopes = format!("{}", self.scopes); |
|
|
|
let scopes: String = utf8_percent_encode(&scopes, DEFAULT_ENCODE_SET).collect(); |
|
|
|
let scopes: String = utf8_percent_encode(&scopes, DEFAULT_ENCODE_SET).collect(); |
|
|
|
let url = format!( |
|
|
|
let url = if self.force_login { |
|
|
|
|
|
|
|
format!( |
|
|
|
|
|
|
|
"{}/oauth/authorize?client_id={}&redirect_uri={}&scope={}&force_login=true&\ |
|
|
|
|
|
|
|
response_type=code", |
|
|
|
|
|
|
|
self.base, self.client_id, self.redirect, scopes, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
format!( |
|
|
|
"{}/oauth/authorize?client_id={}&redirect_uri={}&scope={}&response_type=code", |
|
|
|
"{}/oauth/authorize?client_id={}&redirect_uri={}&scope={}&response_type=code", |
|
|
|
self.base, self.client_id, self.redirect, scopes, |
|
|
|
self.base, self.client_id, self.redirect, scopes, |
|
|
|
); |
|
|
|
) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Ok(url) |
|
|
|
Ok(url) |
|
|
|
} |
|
|
|
} |
|
|
@ -315,6 +354,7 @@ pub struct Registered<H: HttpSend> { |
|
|
|
client_secret: String, |
|
|
|
client_secret: String, |
|
|
|
redirect: String, |
|
|
|
redirect: String, |
|
|
|
scopes: Scopes, |
|
|
|
scopes: Scopes, |
|
|
|
|
|
|
|
force_login: bool, |
|
|
|
http_sender: H, |
|
|
|
http_sender: H, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|