Skip to main content

tongbal_oauth/
lib.rs

1//! OAuth 2.0 authorization client for the Chzzk API.
2//!
3//! Handles the authorization code flow.
4//!
5//! ## Quick Start
6//!
7//! ```no_run
8//! use std::str::FromStr;
9//! use tongbal_oauth::{ClientId, ClientSecret, RedirectUrl, TongbalOauth};
10//!
11//! #[tokio::main]
12//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
13//!     let oauth = TongbalOauth::new(
14//!         ClientId::from("client_id"),
15//!         ClientSecret::from("client_secret"),
16//!         RedirectUrl::from_str("http://localhost:3000/auth/callback")?,
17//!     );
18//!
19//!     let auth_url = oauth.auth_url();
20//!     println!("Visit: {}", auth_url);
21//!
22//!     Ok(())
23//! }
24//! ```
25//!
26//! ## Handling OAuth Callbacks
27//!
28//! ```no_run
29//! use tongbal_oauth::{AuthCallback, Error, TongbalOauth};
30//!
31//! async fn handle_callback(
32//!     oauth: &TongbalOauth,
33//!     callback: AuthCallback,
34//! ) -> Result<(), Error> {
35//!     let token = oauth
36//!         .exchange_code(callback.code, callback.state)
37//!         .await?;
38//!
39//!     Ok(())
40//! }
41//! ```
42//!
43//! ## Token Management
44//!
45//! ### Refresh
46//!
47//! ```rust
48//! # use tongbal_oauth::{Error, RefreshToken, TongbalOauth};
49//! # async fn run(oauth: TongbalOauth, refresh_token: RefreshToken) -> Result<(), Error> {
50//! let new_token = oauth.refresh_token(refresh_token).await?;
51//! # Ok(())
52//! # }
53//! ```
54//!
55//! ### Revocation
56//!
57//! Both [`AccessToken`] and [`RefreshToken`] can be revoked.
58//!
59//! ```rust
60//! # use tongbal_oauth::{AccessToken, Error, RefreshToken, TongbalOauth};
61//! # async fn run(oauth: TongbalOauth, access_token: AccessToken, refresh_token: RefreshToken) -> Result<(), Error> {
62//! oauth.revoke_token(access_token).await?;
63//! oauth.revoke_token(refresh_token).await?;
64//! # Ok(())
65//! # }
66//! ```
67//!
68//! ## HTTP Client
69//!
70//! A suitable HTTP client for authorization is used by default.
71//! If customization is needed, see [`client::setup`].
72#![cfg_attr(docsrs, feature(doc_cfg))]
73
74pub mod client;
75
76mod error;
77mod oauth;
78mod request;
79mod response;
80mod tokens;
81mod types;
82
83pub use error::Error;
84pub use oauth::TongbalOauth;
85pub use tokens::Token;
86pub use types::AuthCallback;
87
88pub use asknothingx2_util::oauth::{
89    AuthUrl, AuthorizationCode, RedirectUrl, RefreshToken, RevocationUrl, TokenUrl,
90};
91pub use tongbal_types::{AccessToken, ClientId, ClientSecret, Response};
92
93pub mod csrf {
94    pub use asknothingx2_util::oauth::signed_token::{
95        TokenConfig as CsrfConfig, TokenError, current_timestamp, extract_datetime,
96        extract_timestamp, generate, generate_at_time, generate_secret_key, is_expired, token_age,
97        verify, verify_at_time, verify_with_config,
98    };
99}