yup_oauth2/
lib.rs

1//! This library can be used to acquire oauth2.0 authentication for services.
2//!
3//! For your application to use this library, you will have to obtain an application
4//! id and secret by
5//! [following this guide](https://developers.google.com/youtube/registering_an_application) (for
6//! Google services) respectively the documentation of the API provider you want to connect to.
7//!
8//! # Device Flow Usage
9//! With an application secret you can get started right away, building a `DeviceFlowAuthenticator`
10//! and obtaining tokens from it.
11//!
12//! # Service account "flow"
13//! When using service account credentials, no user interaction is required. The access token
14//! can be obtained automatically using the private key of the client (which you can download
15//! from the API provider). See `examples/service_account/` for an example on how to use service
16//! account credentials. See
17//! [developers.google.com](https://developers.google.com/identity/protocols/OAuth2ServiceAccount)
18//! for a detailed description of the protocol. This crate implements OAuth for Service Accounts
19//! based on the Google APIs; it may or may not work with other providers.
20//!
21//! # Installed Flow Usage
22//! The installed flow involves showing a URL to the user (or opening it in a browser)
23//! and then either prompting the user to enter a displayed code, or make the authorizing
24//! website redirect to a web server spun up by this library and running on localhost.
25//!
26//! In order to use the interactive method, use the `Interactive` `InstalledFlowReturnMethod`;
27//! for the redirect method, use `HTTPRedirect`.
28//!
29//! You can implement your own authenticator delegate in order to customize the flow;
30//! the installed flow uses the `present_user_url` method. See the `crate::authenticator_delegate`
31//! module.
32//!
33//! The returned `Token` will be stored in memory in order to authorize future
34//! API requests to the same scopes. The tokens can optionally be persisted to
35//! disk by using `persist_tokens_to_disk` when creating the authenticator.
36//!
37//! The following example, which is derived from the (actual and runnable) example in
38//! `examples/test-installed/`, shows the basics of using this crate:
39//!
40//! ```test_harness,no_run
41//! use yup_oauth2::{InstalledFlowAuthenticator, InstalledFlowReturnMethod};
42//!
43//! # #[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
44//! #[tokio::main]
45//! async fn main() {
46//!     // Read application secret from a file. Sometimes it's easier to compile it directly into
47//!     // the binary. The clientsecret file contains JSON like `{"installed":{"client_id": ... }}`
48//!     let secret = yup_oauth2::read_application_secret("clientsecret.json")
49//!         .await
50//!         .expect("clientsecret.json");
51//!
52//!     // Create an authenticator that uses an InstalledFlow to authenticate. The
53//!     // authentication tokens are persisted to a file named tokencache.json. The
54//!     // authenticator takes care of caching tokens to disk and refreshing tokens once
55//!     // they've expired.
56//!     let mut auth = InstalledFlowAuthenticator::builder(secret, InstalledFlowReturnMethod::HTTPRedirect)
57//!     .persist_tokens_to_disk("tokencache.json")
58//!     .build()
59//!     .await
60//!     .unwrap();
61//!
62//!     let scopes = &["https://www.googleapis.com/auth/drive.file"];
63//!
64//!     // token(<scopes>) is the one important function of this crate; it does everything to
65//!     // obtain a token that can be sent e.g. as Bearer token.
66//!     match auth.token(scopes).await {
67//!         Ok(token) => println!("The token is {:?}", token),
68//!         Err(e) => println!("error: {:?}", e),
69//!     }
70//! }
71//! ```
72//!
73#![deny(missing_docs)]
74#![cfg_attr(docsrs, feature(doc_cfg))]
75
76pub mod access_token;
77mod application_default_credentials;
78pub mod authenticator;
79pub mod authenticator_delegate;
80pub mod authorized_user;
81pub mod client;
82mod device;
83pub mod error;
84pub mod external_account;
85mod helper;
86mod installed;
87mod refresh;
88pub mod service_account_impersonator;
89
90#[cfg(feature = "service-account")]
91mod service_account;
92
93/// Interface for storing tokens so that they can be re-used. There are built-in memory and
94/// file-based storage providers. You can implement your own by implementing the TokenStorage trait.
95pub mod storage;
96
97mod types;
98
99pub use hyper;
100
101#[cfg(feature = "hyper-rustls")]
102pub use hyper_rustls;
103
104#[cfg(feature = "service-account")]
105#[doc(inline)]
106pub use crate::authenticator::ServiceAccountAuthenticator;
107
108#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
109pub use crate::authenticator::AccessTokenAuthenticator;
110
111#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
112pub use crate::client::DefaultHyperClientBuilder;
113pub use crate::client::{CustomHyperClientBuilder, HttpClient, HyperClientBuilder};
114
115#[doc(inline)]
116pub use crate::authenticator::{
117    ApplicationDefaultCredentialsAuthenticator, AuthorizedUserAuthenticator,
118    DeviceFlowAuthenticator, ExternalAccountAuthenticator, InstalledFlowAuthenticator,
119    ServiceAccountImpersonationAuthenticator,
120};
121
122pub use crate::helper::*;
123pub use crate::installed::InstalledFlowReturnMethod;
124
125pub use crate::application_default_credentials::ApplicationDefaultCredentialsFlowOpts;
126#[cfg(feature = "service-account")]
127pub use crate::service_account::ServiceAccountKey;
128
129#[doc(inline)]
130pub use crate::error::Error;
131pub use crate::types::{AccessToken, ApplicationSecret, ConsoleApplicationSecret};