#oauth2 #scope #enums #google-oauth #actix #grouped-scopes

tlns-google-oauth2

An enshitificated Google OAuth2 Server Side support for Rust with built in Scopes enum

14 releases

new 0.5.4 May 7, 2025
0.5.3 May 4, 2025
0.4.1 Jul 13, 2024
0.3.1 Jul 1, 2024
0.1.1 Jun 29, 2024

#312 in Authentication

Download history 1/week @ 2025-01-24 2/week @ 2025-02-21 12/week @ 2025-02-28 8/week @ 2025-03-21 2/week @ 2025-04-04 23/week @ 2025-04-11 2/week @ 2025-04-18 31/week @ 2025-04-25 537/week @ 2025-05-02

594 downloads per month

MIT license

54KB
165 lines

tlns-google-oauth2

A server-side Google OAuth2 authentication with built in scopes for extra type safety.

Generating scopes enums (for proc macro library part only)

Go to Google's OAuth2 Scopes Listing and start copying from the first header to the final row of the table at the bottom of the document. A picture showing the first blue header A picture showing the last row of the table Then pasting all of that to the info.txt and build the library, it will give you all possible scopes!

Usage (crate::grouped_scopes)

fn main() -> Result<(), Box<dyn std::error::Error>> {
    use tlns_google_oauth2::GoogleOAuth2Client as Client;
    use tlns_google_oauth2::grouped_scopes;
    use pollster::FutureExt as _;

    let client = Client::new("CLIENT_ID", "CLIENT_SECRET", "http://localhost:8080/callback")?;
    let auth = client.build_authorize_url(None, &[&grouped_scopes::GoogleOAuth2APIv2::AuthUserinfoProfile]);
    let url = auth.redirect_url;
    let csrf_token = auth.csrf_token;
    let scopes = auth.scopes;

    // ... Callback codes here (You can't save states in oauth2 crate for some reasons :( )

    let code = "...";
    let token = client.get_token(code, None).block_on()?;
    // Do request stuff
    Ok(())
}

Usage (crate::scopes::Scopes)

fn main() -> Result<(), Box<dyn std::error::Error>> {
    use tlns_google_oauth2::GoogleOAuth2Client as Client;
    use tlns_google_oauth2::scopes;
    use pollster::FutureExt as _;

    let client = Client::new("CLIENT_ID", "CLIENT_SECRET", "http://localhost:8080/callback")?;
    let auth = client.build_authorize_url(None, &[&scopes::Scopes::AuthUserinfoProfile]);
    let url = auth.redirect_url;
    let csrf_token = auth.csrf_token;
    let scopes = auth.scopes;

    // ... Callback codes here (You can't save states in oauth2 crate for some reasons :( )

    let code = "...";
    let token = client.get_token(code, None).block_on()?;
    Ok(())
// Do request stuff
}

Yes, you can mix and match the slice with anything that implements ToGoogleScope trait.

Credits

Thanks heapunderfl0w for the proc macro idea instead of shitty Jinja2 implementation!

Dependencies

~10–23MB
~318K SLoC