Struct Client

Source
pub struct Client<P> {
    pub provider: P,
    pub client_id: String,
    pub client_secret: String,
    pub redirect_uri: Option<String>,
}
Expand description

OAuth 2.0 client.

Fields§

§provider: P

OAuth provider.

§client_id: String

Client ID.

§client_secret: String

Client secret.

§redirect_uri: Option<String>

Redirect URI.

Implementations§

Source§

impl<P: Provider> Client<P>

Source

pub fn new( provider: P, client_id: String, client_secret: String, redirect_uri: Option<String>, ) -> Self

Creates a client.

§Examples
use inth_oauth2::Client;
use inth_oauth2::provider::google::Installed;

let client = Client::new(
    Installed,
    String::from("CLIENT_ID"),
    String::from("CLIENT_SECRET"),
    Some(String::from("urn:ietf:wg:oauth:2.0:oob")),
);
Examples found in repository?
examples/github.rs (lines 12-17)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        GitHub,
14        String::from("01774654cd9a6051e478"),
15        String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("user"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
More examples
Hide additional examples
examples/google-web.rs (lines 12-17)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Web,
14        String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15        String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
examples/imgur.rs (lines 12-17)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Imgur,
14        String::from("505c8ca804230e0"),
15        String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(None, None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
examples/google-installed.rs (lines 12-17)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Installed,
14        String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15        String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16        Some(String::from(REDIRECT_URI_OOB)),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
Source

pub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> Url

Returns an authorization endpoint URI to direct the user to.

See RFC 6749, section 3.1.

§Examples
use inth_oauth2::Client;
use inth_oauth2::provider::google::Installed;

let client = Client::new(
    Installed,
    String::from("CLIENT_ID"),
    String::from("CLIENT_SECRET"),
    Some(String::from("urn:ietf:wg:oauth:2.0:oob")),
);

let auth_uri = client.auth_uri(
    Some("https://www.googleapis.com/auth/userinfo.email"),
    None,
);
Examples found in repository?
examples/github.rs (line 19)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        GitHub,
14        String::from("01774654cd9a6051e478"),
15        String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("user"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
More examples
Hide additional examples
examples/google-web.rs (line 19)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Web,
14        String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15        String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
examples/imgur.rs (line 19)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Imgur,
14        String::from("505c8ca804230e0"),
15        String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(None, None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
examples/google-installed.rs (line 19)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Installed,
14        String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15        String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16        Some(String::from(REDIRECT_URI_OOB)),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
Source

pub fn request_token( &self, http_client: &Client, code: &str, ) -> Result<P::Token, ClientError>

Requests an access token using an authorization code.

See RFC 6749, section 4.1.3.

Examples found in repository?
examples/github.rs (line 25)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        GitHub,
14        String::from("01774654cd9a6051e478"),
15        String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("user"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
More examples
Hide additional examples
examples/google-web.rs (line 25)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Web,
14        String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15        String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27}
examples/imgur.rs (line 25)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Imgur,
14        String::from("505c8ca804230e0"),
15        String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(None, None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
examples/google-installed.rs (line 25)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Installed,
14        String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15        String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16        Some(String::from(REDIRECT_URI_OOB)),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
Source§

impl<P> Client<P>
where P: Provider, P::Token: Token<Refresh>,

Source

pub fn refresh_token( &self, http_client: &Client, token: P::Token, scope: Option<&str>, ) -> Result<P::Token, ClientError>

Refreshes an access token.

See RFC 6749, section 6.

Examples found in repository?
examples/imgur.rs (line 28)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Imgur,
14        String::from("505c8ca804230e0"),
15        String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16        Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17    );
18
19    let auth_uri = client.auth_uri(None, None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
More examples
Hide additional examples
examples/google-installed.rs (line 28)
9fn main() {
10    let http_client = reqwest::Client::new();
11
12    let client = Client::new(
13        Installed,
14        String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15        String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16        Some(String::from(REDIRECT_URI_OOB)),
17    );
18
19    let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20    println!("{}", auth_uri);
21
22    let mut code = String::new();
23    io::stdin().read_line(&mut code).unwrap();
24
25    let token = client.request_token(&http_client, code.trim()).unwrap();
26    println!("{:?}", token);
27
28    let token = client.refresh_token(&http_client, token, None).unwrap();
29    println!("{:?}", token);
30}
Source

pub fn ensure_token( &self, http_client: &Client, token: P::Token, ) -> Result<P::Token, ClientError>

Ensures an access token is valid by refreshing it if necessary.

Trait Implementations§

Source§

impl<P: Clone> Clone for Client<P>

Source§

fn clone(&self) -> Client<P>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug> Debug for Client<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: PartialEq> PartialEq for Client<P>

Source§

fn eq(&self, other: &Client<P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<P: Eq> Eq for Client<P>

Source§

impl<P> StructuralPartialEq for Client<P>

Auto Trait Implementations§

§

impl<P> Freeze for Client<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for Client<P>
where P: RefUnwindSafe,

§

impl<P> Send for Client<P>
where P: Send,

§

impl<P> Sync for Client<P>
where P: Sync,

§

impl<P> Unpin for Client<P>
where P: Unpin,

§

impl<P> UnwindSafe for Client<P>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>

Source§

impl<T> ErasedDestructor for T
where T: 'static,