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, ¶ms.code, ¶ms.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 thirdOAuthProvidervalue an app constructs directly - no plugin/registration mechanism, matching this crate’s otherwise stateless design.
Structs§
- OAuth
Provider - 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. - Provider
User - The fields this crate actually needs out of a provider’s userinfo
response -
email/nameareOption, not required: a provider can legitimately omit either (GitHub omitsemailentirely unless theuser:emailscope 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 ownfind_or_create_from_provider.
Traits§
- Socialite
User - The hook an app implements to turn a
ProviderUserinto a realAuthenticatableuser - find an existing account (by provider id, by email, whatever the app’s own schema tracks) or create a new one. Mirrorsdemo/app/Http/Controllers/auth_controller.rs’s ownregisterhandler (User::create(...)) - this crate calls the hook, the app owns everything about how a user gets resolved or created.
Functions§
- github
- GitHub’s
Authorization CodeOAuth app flow (https://docs.github.com/en/apps/oauth-apps). ReadsGITHUB_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’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). ReadsGOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET/GOOGLE_REDIRECT_URL. - redirect_
url - Builds the URL to redirect the browser to for
provider, generating a fresh anti-CSRFstatevalue (larust_http::random_hex(32)- the same generatorcsrf::tokenuses) 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’sstate. - user_
from_ callback - Completes the flow
redirect_urlstarted: verifiesstateagainst the session (single-use -session.remove, not.get, so a replayed callback with the samestatefails the second time), exchangescodefor 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 shapeAuthController::registeralready uses, rather than this function silently authenticating a session as a side effect.