Skip to main content

Crate larust_socialite

Crate larust_socialite 

Source
Expand description

Laravel’s laravel/socialite - OAuth Authorization Code login (“Sign in with GitHub/Google”), narrowed to its real core. Two built-in providers (github, google) plus a generic OAuthProvider shape a third provider can be built from directly (just URLs and a small mapping function - no plugin/registration mechanism to learn).

Owns no database table at all, unlike larust-permissions/ larust-sanctum/larust-cache - turning “an OAuth provider’s user info” into “a real app user” is entirely app-owned logic via SocialiteUser::find_or_create_from_provider, the same “provide the hook, the app owns persistence” shape larust_auth::Authenticatable itself already established (an app might add provider/ provider_user_id columns to its own users table, or a separate pivot table - this crate has no opinion). This crate’s own job is narrower: drive the OAuth protocol exchange and verify the anti-CSRF state parameter via the session, nothing else.

Re-exported through larust_support::socialite (see crates/ larust-support/src/lib.rs) so generated apps depend only on larust-support, never on this crate directly.

A typical route pair (the app wires these itself - nothing here is auto-mounted, the same convention every other shim crate this session follows):

ⓘ
async fn redirect(session: Session) -> Result<impl IntoResponse, AppError> {
    let provider = larust_support::socialite::github()?;
    Ok(Redirect::to(&larust_support::socialite::redirect_url(&session, "github", &provider).await?))
}

async fn callback(
    session: Session,
    Query(params): Query<CallbackParams>,
) -> Result<impl IntoResponse, AppError> {
    let provider = larust_support::socialite::github()?;
    let user: User = larust_support::socialite::user_from_callback(
        &session, "github", &provider, &params.code, &params.state,
    ).await?;
    larust_support::auth::login(&session, &user).await?;
    larust_support::redirect().to("/")
}

§Deliberately out of scope for this version

  • No OpenID Connect ID-token verification - plain OAuth2 Authorization Code flow plus a userinfo GET, matching real Socialite’s own default behavior (it doesn’t verify ID tokens either).
  • No token refresh / long-lived provider-token storage. The provider’s own access token is used once, to fetch userinfo, then discarded - only the resolved app user is persisted (via session login), same as every other login path in this codebase.
  • No extend()-style provider registry. A third provider is a third OAuthProvider value an app constructs directly - no plugin/registration mechanism, matching this crate’s otherwise stateless design.

Structs§

OAuthProvider
A configured OAuth2 provider - the two built-in constructors (github/google) build one from environment variables; a third provider is built the same way by hand.
ProviderUser
The fields this crate actually needs out of a provider’s userinfo response - email/name are Option, not required: a provider can legitimately omit either (GitHub omits email entirely unless the user:email scope was granted), and pretending otherwise would mean silently creating users with an empty email. The app decides how to handle a missing one in its own find_or_create_from_provider.

Traits§

SocialiteUser
The hook an app implements to turn a ProviderUser into a real Authenticatable user - find an existing account (by provider id, by email, whatever the app’s own schema tracks) or create a new one. Mirrors demo/app/Http/Controllers/auth_controller.rs’s own register handler (User::create(...)) - this crate calls the hook, the app owns everything about how a user gets resolved or created.

Functions§

github
GitHub’s Authorization Code OAuth app flow (https://docs.github.com/en/apps/oauth-apps). Reads GITHUB_CLIENT_ ID/GITHUB_CLIENT_SECRET/GITHUB_REDIRECT_URL - Err(AppError:: Config), naming the missing variable, if any are unset, so a misconfigured provider fails clearly here rather than sending a broken authorize URL to the browser.
google
Google’s OAuth2 flow (https://developers.google.com/identity/ protocols/oauth2/web-server) - plain OAuth2 userinfo, not OIDC ID-token verification (see this crate’s own doc comment for why). Reads GOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET/GOOGLE_REDIRECT_URL.
redirect_url
Builds the URL to redirect the browser to for provider, generating a fresh anti-CSRF state value (larust_http::random_hex(32) - the same generator csrf::token uses) and storing it in the session under a provider-scoped key, so a user can plausibly have two concurrent OAuth attempts open in different tabs without one clobbering the other’s state.
user_from_callback
Completes the flow redirect_url started: verifies state against the session (single-use - session.remove, not .get, so a replayed callback with the same state fails the second time), exchanges code for an access token, fetches the provider’s userinfo, and resolves the app’s own user via [SocialiteUser::find_or_create_from_ provider]. Does not log the resolved user into the session itself - the caller does that explicitly (larust_auth::login(&session, &user)), the same shape AuthController::register already uses, rather than this function silently authenticating a session as a side effect.