Skip to main content

OAuthClient

Struct OAuthClient 

Source
pub struct OAuthClient { /* private fields */ }
Expand description

An application’s registration with one provider.

Debug redacts the secret. This type ends up in application state and in the development error page, and a client secret in either is a credential that has to be rotated.

Implementations§

Source§

impl OAuthClient

Source

pub fn new(provider: Provider) -> OAuthClient

Source

pub fn credentials( self, client_id: impl Into<String>, client_secret: impl Into<String>, ) -> OAuthClient

Source

pub fn redirect_uri(self, uri: impl Into<String>) -> OAuthClient

The URL the provider redirects back to.

It must match what is registered with the provider byte for byte, including the scheme and any trailing slash — a mismatch is rejected at the token endpoint, after the visitor has already consented, which is why the error is so often reported as “it worked yesterday”.

Source

pub fn scopes(self, scopes: impl Into<Scopes>) -> OAuthClient

Ask for these scopes instead of the provider’s defaults.

Source

pub fn scope(self, scope: impl Into<String>) -> OAuthClient

Add one scope to whatever is already being asked for.

Source

pub fn with( self, name: impl Into<String>, value: impl Into<String>, ) -> OAuthClient

A provider-specific authorisation parameter: prompt, access_type, login_hint, hd.

Setting a name the provider preset already sets replaces it rather than sending it twice — two prompt values in one URL is a request most providers reject and none interpret the way the caller meant.

Source

pub fn using(self, http: Client) -> OAuthClient

Use a particular HTTP client — a shared one, or Http::fake() in tests.

Source

pub fn provider(&self) -> &Provider

Source

pub fn http(&self) -> &Client

Source

pub fn client_id(&self) -> &str

Source

pub fn requested_scopes(&self) -> Scopes

What is actually being asked for: the application’s scopes, or the provider’s defaults when it did not say.

Source

pub fn authorize_url(&self) -> Authorization

Leg one: the URL to send a visitor to, with a fresh state and verifier.

The caller has to keep both — see OAuthClient::begin, which keeps them for you.

Source

pub fn begin(&self, guard: &StateGuard) -> Result<Authorization, OAuthError>

Leg one, with the state issued and remembered by a guard.

Source

pub fn authorize_url_for(&self, state: &str, pkce: &Pkce) -> String

The authorisation URL for a state and verifier chosen elsewhere.

PKCE is not a parameter here and there is no overload without it: OAuth 2.1 requires it for every authorisation code flow, and a client that could be talked into omitting it is a client that will be.

Source

pub async fn callback( &self, query: &str, guard: &StateGuard, ) -> Result<TokenResponse, OAuthError>

Leg two: everything the callback has to do, in one call.

query is the raw query string the provider redirected to — in a handler, req.target()’s query part.

The state is checked before the provider’s own error is read. An error response is still a response to a request somebody made, and answering an unsolicited one — even to say “access denied” — is a page an attacker can drive.

Source

pub async fn exchange( &self, code: &str, verifier: &str, ) -> Result<TokenResponse, OAuthError>

Trade an authorisation code for a token.

Source

pub async fn refresh( &self, refresh_token: &str, ) -> Result<TokenResponse, OAuthError>

Trade a refresh token for a new access token, RFC 6749 §6.

The response may or may not carry a new refresh token. Providers disagree, and one that rotates them means the old one stops working the moment this returns — so store whatever comes back rather than assuming the one you had is still good.

Source

pub async fn revoke(&self, token: &str, hint: &str) -> Result<(), OAuthError>

Revoke a token, RFC 7009.

hint is access_token or refresh_token; it is advisory, and a provider that ignores it still has to search both. Revoking a refresh token usually revokes the access tokens issued from it — usually, because RFC 7009 §2.1 only recommends it.

Source

pub async fn user( &self, token: &TokenResponse, ) -> Result<SocialUser, OAuthError>

Fetch the provider’s profile for a token and normalise it.

Trait Implementations§

Source§

impl Clone for OAuthClient

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for OAuthClient

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.